Solved

How do I connect the primary key or unique key in the best way?

Posted on 2010-09-04
3
268 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:kenuk110
  • 2
3 Comments
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 33602986
1. You should use countryCode, cityCode, storeCode as PRIMARY KEY by itself without PK field, since you state that it will be unique value. However, you still can keep PK if you want, but just make it as IDENTITY (AKA autonumber)

script example
CREATE TABLE [Country] (
pk bigint IDENTITY (1,1) , -- this line, keep or remove is depend on your need
countryCode nvarchar(3) PRIMARY KEY,
countryName nvarchar(150)
)

2. in order to validate that, City table should enter only valid countryCode, Store table should enter valid cityCode.
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]
ADD CONSTRAINT FK_City_Country FOREIGN KEY ( countryCode )
REFERENCES [Country] ( countryCode )
cityCode must be exists for data enter into Store table
ALTER TABLE [Store]
ADD CONSTRAINT FK_Store_City FOREIGN KEY ( cityCode )
REFERENCES [City] ( cityCode )
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33605188
Also in your Store table you do not need a field for cityName since you alredy have the cityCode

You can get it by joining Store to City
select cityName
from city
inner join store on (store.cityCode = city.CityCode)
0
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 250 total points
ID: 33614628
Addition to ewangoya suggestion

Unless you want to have countryCode and cityName on Store table on purpose.
You can normalize table by remove countryCode and cityName.
and optain it by join the table.

SELECT
... , cityName, countryCode
FROM Store
INNER JOIN City ON Store.cityCode = City.cityCode
or

SELECT
..., cityName,
country.countryCode, country.countryName
FROM Store
INNER JOIN City ON Store.cityCode = City.cityCode
INNER JOIN Country ON City.countyCode = Country.countyCode

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question