473,470 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

identity column

Sam
Hi,
I would like a piece of advice.
I have 3 foreign keys in a table used as primary keys for this table.
Is it useful in that case to have just one identity column that would
be used as the unique primary key, thus no need to have 3 primary keys
?
Advantages vs Drabacks ?

Regards

Jul 23 '05 #1
7 1799
Sam (sa**************@voila.fr) writes:
I would like a piece of advice.
I have 3 foreign keys in a table used as primary keys for this table.
Is it useful in that case to have just one identity column that would
be used as the unique primary key, thus no need to have 3 primary keys
?
Advantages vs Drabacks ?


Sometimes this makes things easier. Sometimes it makes things more
difficult.

The advantage with the IDENTITY column are more directly apparent with
less typing and all that. The disadvantages are more subtle but once you
run into them, you often find that they are more serious.

Thus, unless there is a special reason for it, do not use a surrogate key.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Sam, many developers seem to follow the dogma that every table must
have a unique numeric primary key (PK). I do not. The question comes
down to this: will the application or end users ever use the generated
PK? If no, then why add it? In this case there is no point.

On the other hand you need to consider if this table will have child
rows and how they will relate back to the table in question. You also
have to consider if any of the PK columns are subject to value changes
and if so how such changes will be handled.

So in my opinion the choice on how to construct the PK comes down to
how will the data be accessed and updated? Does the chosen PK meet all
the application requirements?

HTH -- Mark D Powell --

Jul 23 '05 #3
Sam
Maybe it's clearer with my tables. Here are the ones concerned with
only the relevant fields:

User(UserId)
Web(WebId)
Query(QueryId)
UserQueries(UserId, WebId, QueryId)

UserQueries is the trouble maker. Right now (UserId, WebId, QueryId)
are FK and PK at the same timefor this table. They must be FK anyway,
but wouldn't it be better to have something like:

UserQueries(UserQueriesId, UserId, WebId, QueryId)

with userQueriesId being Identity type ?

Jul 23 '05 #4
I guess you mean that your table has a composite primary key made up of
three columns, each of which is a foreign key on another table? If
that's the correct primary key according to your data model, and you
don't have any serious performance issues, then it's probably best not
to change anything.

You might use IDENTITY as an artificial key either because there is no
natural key for the table, or because the natural key is so wide
physically that it creates real performance issues. Even if you did
decide to use IDENTITY instead of the natural key, you would still keep
a UNIQUE constraint on the natural key to make sure you don't get any
duplicates. And then you would be improving performance at the cost of
more maintenance, so it's probably better not to do it unless you
really have to.

Simon

Jul 23 '05 #5
Sam (sa**************@voila.fr) writes:
Maybe it's clearer with my tables. Here are the ones concerned with
only the relevant fields:

User(UserId)
Web(WebId)
Query(QueryId)
UserQueries(UserId, WebId, QueryId)

UserQueries is the trouble maker. Right now (UserId, WebId, QueryId)
are FK and PK at the same timefor this table. They must be FK anyway,
but wouldn't it be better to have something like:

UserQueries(UserQueriesId, UserId, WebId, QueryId)

with userQueriesId being Identity type ?


It could be, but I say that the odds are poor. Unless you really some
use which mandates a surrogate key, there is no reason to have any
UserQueriesId.

If you add an IDENTITY column you should still create a UNIQUE constraint
for (UserId, WebId, QueryId).
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6
Simon Hayes (sq*@hayes.ch) writes:
You might use IDENTITY as an artificial key either because there is no
natural key for the table, or because the natural key is so wide
physically that it creates real performance issues.


Just to add to this: Adding the IDENTITY column could in fact
decrease your performance as well, as the table would be larger. If
there is a child table, that could use this identity column as its
FK, then that table could indeed be smaller. Then again, you could then
find that more operations on the child would require joining to the
parent table, in which case you are again losing in the IDENTITY column.


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7
Some real DDL will help. Never use an IDENTITY as a key, sicne by
definition it is not an attribute of the entity being modeled.
Basically it says that you don't know RM and are going back to using a
physical locator, like pointer chains, to navigate yoru data. You have
to have the relational keys anyway; if you don't, you will get
redundant duplicates. Do you really mean to destroy all your data
integrity? How do you know that this "magivalk one size fits all"
number is actually in synch everywhere it is used? You don't. So all
that IDENTITY does is make the physical storage used bigger and
therefore slower. Your code will not port. Etc.

CREATE TABLE Users
(user_id INTEGER NOT NULL PRIMARY KEY,
...);

CREATE TABLE Web
(web_id INTEGER NOT NULL PRIMARY KEY,
..);

CREATE TABLE Queries
(query_id INTEGER NOT NULL PRIMARY KEY,
..) ;

Now throw in some DRI actions and let the schema take care of itself
for you

UserQueries
(user_id INTEGER NOT NULL
REFERENCES Users(user_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
web_id INTEGER NOT NULL
REFERENCES Web(web_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
query_id INTEGER NOT NULL,
REFERENCES Queries(query_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
PRIMARY KEY (user_id, web_id, query_id));

You might want to read a book basic relational theory and look up what
Dr. Codd said about system generatee surrogates. The fact that
IDENTITY is not a key is a matter of definition, not opinion.

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: grzes | last post by:
MS SQL Server 2000. My case is: I have the table T with primary key calling __recid int without identity property. This table includes a lot of records (about 1000000). I need to convert __recid's...
4
by: UDBDBA | last post by:
Hi: we have column with GENERATED ALWAYS AS DEFAULT. So, we can insert into this column manually and also let db2 generate a value for this column. Given a scenario, how can i find the NEXTVAL...
3
by: mal_k100 | last post by:
Hi All, 1. Created table in SQL Server 2K with Primary key as int Identity 2. Link to table in MS Access 2K 3. Using form in MSAccess to update the linked table I want SQL server to...
2
by: WhiteEagl | last post by:
Hello, I would need some help with this identity column problem in SQLServer. I have a database with two tables. The Parent table has an Identity column. Parent (ParentID IDENTITY, Name)...
2
by: .Net Newbie | last post by:
Hello, I am somewhat new to .Net and currently working on an intranet site using C# going against SQL Server 2k. I am accepting personal information on a single webform and trying to insert the...
1
by: smauldin | last post by:
Creating a table with an identity column works fine create table test(a integer, id integer generated always as identity ) When I attempt to add an identity column it fails. create table...
4
by: shorti | last post by:
Can anyone explain in greater (and more comprehensive) detail what the RESTART option does in the ALTER TABLE table ALTER COLUMN statement. This is the description in Info Center: RESTART or...
5
by: Veeru71 | last post by:
Given a table with an identity column (GENERATED BY DEFAULT AS IDENTITY), is there any way to get the last generated value by DB2 for the identity column? I can't use identity_val_local() as...
3
by: Rob | last post by:
Hi all, I have a bit of a complicated question, hope we have an SQL guru out there that can help us solve this killer problem. Due to the size of SQL Database we have (largest in the US), we...
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.