| |
|
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.
|
 |

10-24-08, 10:33
|
|
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. 
|
|

10-24-08, 10:40
|
|
vaguely human
|
|
Join Date: Jun 2007
Location: London
Posts: 2,519
|
|
Code:
create table MyDataHistory(
data_value float,
time_captured datetime
)
|
|

10-24-08, 10:46
|
|
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 )
;
|
|

10-24-08, 10:49
|
|
www.gvee.co.uk
|
|
Join Date: Jan 2007
Location: UK
Posts: 10,156
|
|
|
|

10-24-08, 10:52
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
lmso??? laughing your socks off?
|
|

10-24-08, 11:33
|
|
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.
|
|

10-24-08, 11:35
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
i would love that approach, if it was warranted
|
|

10-24-08, 11:45
|
|
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 
|
|

10-29-08, 02:53
|
|
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`);
|
|

10-29-08, 06:56
|
|
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.
|
|

10-29-08, 08:46
|
|
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`);
|
|

10-29-08, 10:07
|
|
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.
|
|

10-29-08, 10:17
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
spot on with everything so far. mike

|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|