Hi,
I've searched google etc for an understanding on Primary Keys and Unique Keys but I thought I'd ask here too.
I have a few tables that I want to make sure that ONLY the values in those tables are entered in to a master one.
So, I have:
Country
pk - bigint, indentity auto incerment (1)
countryCode - nvarchar(3)
countryName - nvarchar(150)
City
pk - bigint, indentity auto incerment (1)
countryCode - nvarchar(3)
cityCode - nvarchar(3)
cityName - nvarchar(150)
Store
pk - bigint, indentity auto incerment (1)
countryCode - nvarchar(3)
cityCode - nvarchar(3)
cityName - nvarchar(150)
storeCode - nvarchar(3)
storeName - nvarchar(150)
Now, I have them set up with the PK in each then I use a UNIQUE key between them to only get the values from the other table, so store gets City and City gets Country.
Is this right?
It seems that going down the line a few level that the end table of the unique string could be really long!
Any help or guidance would be much appreciated.
Regards,
Ken
script example
CREATE TABLE [Country] (
countryCode nvarchar(3) PRIMARY KEY,
countryName nvarchar(150)
)
This can be done either by your application front end. Or you may setup FOREIGN KEY to perform control over the table
script example
countryCode must be exists for data enter into City table
ALTER TABLE [City]
REFERENCES [Country] ( countryCode )
ALTER TABLE [Store]
REFERENCES [City] ( cityCode )