?
Solved

Sql server Identity usage

Posted on 2014-03-31
1
Medium Priority
?
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

Accepted Solution

by:
chaau earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

762 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