Jump to content

[HELP] Enforcing Referential Integrity


Solar

Recommended Posts

Hey PHPFreaks!

I've ran into a problem and hoping you could help me out. I did not design these tables and I am having problems enforcing referential integrity.
Is a constraint required for this task?

Here are the two tables

ClientsTable (ClientID, ClientName)

VALUES("11111TOR","JOE");
VALUES("22222OTT","BOB");
VALUES("33333NEW","PAM");

 

TimezoneTable (Location, Timezone)
VALUES("OTT", "EST")
VALUES("TOR", "EST")
VALUES("CAL", "MDI")
VALUES("NEW","EST")

There is a one to many relationship between TimezoneTable and ClientsTable. Due to the data, I cannot enforce referential integrity.

What is a solution in joining these together? Could I do so by creating a new table? How could I go from there?

Thanks in advanced,
Solar

Link to comment
https://forums.phpfreaks.com/topic/277079-help-enforcing-referential-integrity/
Share on other sites

Sorry I didn't get back earlier - sleep time

 

You could split the id field into 2 fields. If the numeric bit is not unique on its own (would be better if it were) then you would define a compound primary key using both fields. You could then define referential integrity for the location part.

 

if it the combination of location/id that is unique then you could define the the table like this, ensuring clientid is unique within a location

location CHAR(3) NOT NULL,
clientid INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(location, clientid),
KEY `loc` (`location`),
CONSTRAINT `loc` FOREIGN KEY (`location`) REFERENCES `timezone` (`location`) 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.