Solved

Sql server Identity usage

Posted on 2014-03-31
1
173 Views
Last Modified: 2014-04-02
Can I make an Identity column in a table a primary key plus make its associated tables have a primary key/Foreign that is an identity also?
If so, can I cascade delete or cascade update on an identity column?
0
Comment
Question by:metro156
1 Comment
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39968404
In short: Yes for all (except for #2 - see below).

In details:
1. You can make the identity column a primary key. Make sure that it has a property to not allow NULLS

2. You create a columns in associated tables with their own primary keys. The primary key in the associated table can be an identity column. However, I do not believe you should make it a foreign key to your main table, because in this case the associated table will be in a one-to-one relationship to your main table. Imagine this simple schema:

Table Patient:
PatientID INT NOT NULL IDENTITY(1,1) PRIMARY KEY
Name Varchar(100)
Address Varchar(100)

Table PatientPhones
PhoneID INT NOT NULL IDENTITY(1,1) PRIMARY KEY
PatientID INT
Phone Varchar(20)
Type CHAR(1)

If a patient has many phones (Home, Mobile, Work, etc) you can just add more records to the PatientPhones table. The PhoneID key in this table will ensure uniqueness. The foreign key should only include the PatientID. You can make it cascading.

3. You can make the column cascading. However, please note that if you want to update an IDENTITY column in your main table you need to jump a few hoops. In short, you need to use set identity_insert Patient ON statement. But it is a question out of scope of your questions. Please let me know if you want to explore this
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now