If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Table setup to trend data captured periodically

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-24-08, 10:33
totus totus is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Table setup to trend data captured periodically

Can anyone give me a direction to go in?

Thank you gurus! for any love at all.
Reply With Quote
  #2 (permalink)  
Old 10-24-08, 10:40
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Code:
create table MyDataHistory(
       data_value        float,
       time_captured     datetime
)
Reply With Quote
  #3 (permalink)  
Old 10-24-08, 10:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
ALTER TABLE MyDataHistory
ADD CONSTRAINT every_table_needs_a_PK_mike PRIMARY KEY ( time_captured )
;
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 10-24-08, 10:49
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
rotflmso !
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 10-24-08, 10:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
lmso??? laughing your socks off?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 10-24-08, 11:33
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Totally agree with you - all tables need a PK - I just wanted to wait until the spec for the table widened up a bit before guessing what the key might be. We could for instance want to store lot's of different data values in this table and differentiate between them using a type field. I'm guessing you wouldn't like this approach but it would mean using a different PK to just the time.
Reply With Quote
  #7 (permalink)  
Old 10-24-08, 11:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
i would love that approach, if it was warranted
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 10-24-08, 11:45
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by r937
lmso??? laughing your socks off?
Got it in one
__________________
George
Twitter | Blog
Reply With Quote
  #9 (permalink)  
Old 10-29-08, 02:53
totus totus is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Like so...

Code:
CREATE TABLE `SocialSites`
(
`SiteID` INTEGER unsigned  NOT NULL AUTO_INCREMENT ,
`SiteThumbnail` VARCHAR(255),
`SiteName` VARCHAR(150) UNIQUE ,
`SiteURL` VARCHAR(255) NOT NULL,
`AlexaRankID` INTEGER UNIQUE ,
PRIMARY KEY (`SiteID`)
);

CREATE TABLE `AlexaTrend`
(
`AlexaRankID` INTEGER,
`AlexaRank` BIGINT,
`DateCaptured` DATETIME
);

ALTER TABLE `AlexaTrend` ADD FOREIGN KEY (`AlexaRankID`) REFERENCES `SocialSites`(`AlexaRankID`);
Reply With Quote
  #10 (permalink)  
Old 10-29-08, 06:56
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
You missed the PK on the data history table. This should probably be put in with the create table statement but I'll keep with Rudy's syntax
Code:
ALTER TABLE AlexaTrend
ADD CONSTRAINT look_whos_laughing_rudy PRIMARY KEY ( AlexaRankID, DateCaptured );
Mike

PS the fields don't need the quotes around them in your code, they are only necessary if your field names have spaces in them.
Reply With Quote
  #11 (permalink)  
Old 10-29-08, 08:46
totus totus is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Quote:
Originally Posted by mike_bike_kite
You missed the PK on the data history table. This should probably be put in with the create table statement but I'll keep with Rudy's syntax
Code:
ALTER TABLE AlexaTrend
ADD CONSTRAINT look_whos_laughing_rudy PRIMARY KEY ( AlexaRankID, DateCaptured );
Mike

PS the fields don't need the quotes around them in your code, they are only necessary if your field names have spaces in them.
Thanks Mike. Below is my revision. Are you saying that a Primary is required for both AlexaRankID and DateCaptured? When creating the Foreign Key link to the table I get this...

Code:
...
....
....
`PingTimer` INTEGER,
`DLSpeed` INT,
`AlexaRankID` INTEGER,
`AlexaDateCaptured` DATETIME,
PRIMARY KEY (`SiteID`)
);

CREATE TABLE `AlexaTrend`
(
`AlexaRankID` INTEGER,
`AlexaRank` BIGINT,
`AlexaDateCaptured` DATETIME,
PRIMARY KEY (`AlexaRankID`,`AlexaDateCaptured`)
);

ALTER TABLE `SocialSites` ADD FOREIGN KEY (`AlexaRankID,AlexaDateCaptured`) REFERENCES `AlexaTrend`(`AlexaRankID,AlexaDateCaptured`);
Reply With Quote
  #12 (permalink)  
Old 10-29-08, 10:07
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I'm a bit confused by what you're doing now but I'd say:
  • The FK should go from AlexaTrend to SocialSites and not the other way round.
  • The field AlexaDateCaptured shouldn't be in the table SocialSites.
  • If the PK on SocialSites is SiteID then I believe this field should be used in the table AlexaTrend rather than AlexaRankID.
  • I'd still get rid of those quotes you have everywhere as they make things more complex to read and occasionally cause mistakes ie FOREIGN KEY (`AlexaRankID,AlexaDateCaptured`).
Rudy is the FK king on this forum so I'll bow to him on the accuracy of any of the above.
Reply With Quote
  #13 (permalink)  
Old 10-29-08, 10:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
spot on with everything so far. mike

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On