Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

UID as PK

 
   Database Help (Home) -> General Discussions RSS
Next:  5 Video DVD Pack of World Geography...  
Author Message
JG

External


Since: Dec 01, 2008
Posts: 4



(Msg. 1) Posted: Fri Dec 26, 2008 8:10 pm
Post subject: UID as PK
Archived from groups: comp>databases>ms-sqlserver (more info?)

I notice some of the tables at work use a UID, that's a default created
during insertion, as a non-clustered primary key. I wonder what's the
purpose of that -- it seems like extra work that merely confirms the
UIDs are all different, but so what? I assume the DBA specialist who
created this knew what he was doing, and I'm missing something.

 >> Stay informed about: UID as PK 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 2) Posted: Sat Dec 27, 2008 5:25 am
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

JG (jgeissman@socal.rr.com) writes:
> I notice some of the tables at work use a UID, that's a default created
> during insertion, as a non-clustered primary key. I wonder what's the
> purpose of that -- it seems like extra work that merely confirms the
> UIDs are all different, but so what? I assume the DBA specialist who
> created this knew what he was doing, and I'm missing something.

That is a GUID?, a value of the data type uniqueidentifier?

There are plenty of situations where surrogate keys are called for. The
natural key is too unwieldy to work with, or cannot be relied on being
100% unique. This leads more than one database designer to slap surrogate
keys on all tables, without really trying to find the natural key.

There may also be features in SQL Server or an application framework that
requires a surrogate to work with. One such example is merge replication
which requires all table to have a rowguid column.

Without knowing more about your tables at work, it's impossible to say
whether these columns are really useful or not.


--
Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: UID as PK 
Back to top
Login to vote
JG

External


Since: Dec 01, 2008
Posts: 4



(Msg. 3) Posted: Sat Dec 27, 2008 10:38 pm
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks, Erland. Yes, these are GUIDs, however, because it's several
years old the columns are defined as Char(36) and populated by NewID()
rather than what you suggest.

My more general question is why one needs a unique key. If the data
will truly be unique, then select a key from the columns in the table.
But if the data aren't always going to be unique, why have a PK at all?

You mentioned replication, and these tables get mirrored back and forth
among three servers that each handle some of the app servers, and a
central archive server, so maybe that's why they need a unique key -- I
supose two different working servers could create the same row (in the
critical columns) if their app servers get the same loan to underwrite.
I'm a software developer who does some DB definition and programming,
not a DBA, so I never got education about these matters.

Thanks,
Jim Geissman

In article , esquel.DeleteThis@sommarskog.se
says...
> JG (jgeissman@socal.rr.com) writes:
> > I notice some of the tables at work use a UID, that's a default created
> > during insertion, as a non-clustered primary key. I wonder what's the
> > purpose of that -- it seems like extra work that merely confirms the
> > UIDs are all different, but so what? I assume the DBA specialist who
> > created this knew what he was doing, and I'm missing something.
>
> That is a GUID?, a value of the data type uniqueidentifier?
>
> There are plenty of situations where surrogate keys are called for. The
> natural key is too unwieldy to work with, or cannot be relied on being
> 100% unique. This leads more than one database designer to slap surrogate
> keys on all tables, without really trying to find the natural key.
>
> There may also be features in SQL Server or an application framework that
> requires a surrogate to work with. One such example is merge replication
> which requires all table to have a rowguid column.
>
> Without knowing more about your tables at work, it's impossible to say
> whether these columns are really useful or not.
>
>
>
 >> Stay informed about: UID as PK 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 4) Posted: Sun Dec 28, 2008 7:26 am
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

JG (jgeissman@socal.rr.com) writes:
> Thanks, Erland. Yes, these are GUIDs, however, because it's several
> years old the columns are defined as Char(36) and populated by NewID()
> rather than what you suggest.

That's certainly not efficient. Takes more space, and character comparison
is more expensive, unless the columns has a binary collation.

By the way, these columns has to be very old, if the excuse is that
they were added before SQL Server had the uniqueidentifier data type -
this type was added in SQL 7.

> My more general question is why one needs a unique key. If the data
> will truly be unique, then select a key from the columns in the table.
> But if the data aren't always going to be unique, why have a PK at all?

Most of the time, you do want to have a PK, even if there is no unique
key in the data. Makes it easier if you present a row in a GUI, and then
want to update that same row.

In many cases when there is no 100% unique key in the data, there is
a near-unique key. Maybe because there is a difference in an attribute
that was over-looked, or not deemed worthwhile to record in the database.

> You mentioned replication, and these tables get mirrored back and forth
> among three servers that each handle some of the app servers, and a
> central archive server, so maybe that's why they need a unique key -- I
> supose two different working servers could create the same row (in the
> critical columns) if their app servers get the same loan to underwrite.

Maybe, but the replication functions in SQL Server requirs a real
uniqueidentifier column, so in this case this is something homegrown.

--
Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: UID as PK 
Back to top
Login to vote
DA Morgan

External


Since: Dec 14, 2007
Posts: 757



(Msg. 5) Posted: Sun Dec 28, 2008 1:12 pm
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

JG wrote:
> My more general question is why one needs a unique key.

http://www.psoug.org/files/codd-1970.pdf
--
Daniel A. Morgan
University of Washington
damorgan.DeleteThis@x.washington.edu (replace x with u to respond)
 >> Stay informed about: UID as PK 
Back to top
Login to vote
David Portas

External


Since: Nov 11, 2003
Posts: 854



(Msg. 6) Posted: Sun Dec 28, 2008 2:26 pm
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"JG" wrote in message

>
> My more general question is why one needs a unique key. If the data
> will truly be unique, then select a key from the columns in the table.
> But if the data aren't always going to be unique, why have a PK at all?
>

Because duplicate data (redundancy) causes anomalies, incorrect results and
wastes storage and processor cycles (among other things).

In a truly relational DBMS all data would be represented as relations, which
don't have duplicate tuples and always have keys. SQL compromises this
principle and because of this database designers have to go to a lot more
trouble to get the right results out of the database. Generally speaking the
SQL language makes it hard to work with duplicate rows, even though keys are
optional in SQL tables. For most purposes SQL statements require a key in
order to properly identify individual rows. There are certain exceptions and
workarounds but to take full advantage of the SQL language you usually need
to ensure the keys are being enforced.

This is fundamental stuff so I suggest you might want to read a good book on
the basics, such as:

http://www.amazon.com/Introduction-Database-Systems-C-J-Date/dp/0321197844

--
David Portas
 >> Stay informed about: UID as PK 
Back to top
Login to vote
jgeissman

External


Since: Dec 29, 2008
Posts: 1



(Msg. 7) Posted: Mon Dec 29, 2008 10:48 am
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks everyone. I see the newer tables use uniqueidentifier as the
data type for this key column.

Jim
 >> Stay informed about: UID as PK 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 8) Posted: Fri Jan 02, 2009 11:06 am
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> My more general question is why one needs a unique key.  If the data will truly be unique, then select a key from the columns in the table.   But if the data aren't always going to be unique, why have a PK at all?  <<

Think about it. A VIN is the unique identifier for an automobile. It
is a great key; unique, required by law, has verifiability and is easy
to validate, etc. Now look at car color; it changes. Nobody wants to
drive some of the things we has in the 1960's -- have you seen an
avocado green refrigerator? When I re-paint my 1965 Studebaker Silver
Hawk to candy apple red, it is still the same automobile.

Some attributes are invariant and some are not.
 >> Stay informed about: UID as PK 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 9) Posted: Fri Jan 02, 2009 4:13 pm
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

..>> Anything that is required by law is likely to be a difficult key,
since laws are different in different countries. <<

The VIN is both ISO and **very international**' people lie to buy and
sell automobiles. If you want a really bad international standard
the quote the IIBAN ..
 >> Stay informed about: UID as PK 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 10) Posted: Fri Jan 02, 2009 4:25 pm
Post subject: Re: UID as PK [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

--CELKO-- (jcelko212@earthlink.net) writes:
> Think about it. A VIN is the unique identifier for an automobile. It
> is a great key; unique, required by law,

Anything that is required by law is likely to be a difficult key, since
laws are different in different countries.


--
Erland Sommarskog, SQL Server MVP, esquel.DeleteThis@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: UID as PK 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Can't Auto-Start Agent - I have a SQL Server 2000 that failed to start after a Windows reboot. Agent properties are checked to auto start if stopped. Under the Agent Error Log, I have the following data: Information: SQL Agent stopping because of Windows shutdown. SQL Agent..

Seek method, table-direct, and sql server2005 - From what I've read in the docs, ado.net currently supports opening sql server ce tables in table-direct mode and performing Seek operations on them (using SqlCeDataReader), but not on the full-blown sql server. Is this (will this be) still true with...

pass hash (#) table with different structure to stored pro.. - Dear Techies, I making one stored procedure, which does some operation based on an interface hash (#) table ---- name #mydata. This stored has two section of code (seperated by parameter value 0 and 1) But hash table #mydata (same name) number/name of....

Datatype - I am using Visual Studio .NET 2003 with SQL Server 2000. I am trying to insert the date and time into a SQL database by using hour(now). I am having a hard time trying to figure out which datatype to use in SQL to store this value. I have tried using..

Date Compare in SQL Server Stored Procedure - Hi, i have "req_date" column of "datetime" type in Database table besides other columns. From my Web page, i am calling the Stored Procedure with variable parameter "Search_Date" of Varchar(60) type. the value, i am pa...
   Database Help (Home) -> General Discussions All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]