|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 12:11 AM
Points: 21,602,
Visits: 27,431
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, June 19, 2012 10:11 AM
Points: 2,689,
Visits: 36
|
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Sunday, August 19, 2012 6:40 PM
Points: 202,
Visits: 301
|
|
leslaw (2/10/2009) All tables should have primary key, so it should also be created.
Leslaw
A useful general rule, but not always possible to implement:
* When one of the columns in the candidate key is nullable * When there is no candidate key (no unique combination of columns)
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 11:22 PM
Points: 1,080,
Visits: 1,145
|
|
Hi,
Thanks a lot to all for your great response. Here, i learnt a lot.
Keep Learning - Keep Growing !!! http://growwithsql.blogspot.in
Thanks Vinay Kumar
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 2:02 PM
Points: 3,788,
Visits: 5,543
|
|
A useful general rule, but not always possible to implement:
* When one of the columns in the candidate key is nullable * When there is no candidate key (no unique combination of columns)
It may sound like I'm talking out of both sides of my mouth now, but if the table in question doesn't have a VERY short half-life, those are two reasons to simply create an identity key, either as an identity integer or a GUID.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 5:48 PM
Points: 7,088,
Visits: 7,143
|
|
Bob Hovious (2/11/2009)
A useful general rule, but not always possible to implement:
* When one of the columns in the candidate key is nullable * When there is no candidate key (no unique combination of columns)
It may sound like I'm talking out of both sides of my mouth now, but if the table in question doesn't have a VERY short half-life, those are two reasons to simply create an identity key, either as an identity integer or a GUID.
It seems to me that the only thing a primary key constraint gives me that I don't get from a unique key is support for replication. A unique key can ibe the traget of a foreign key constraint, it can include nullable columns, it can be supported by a clustered or a non-clustered index. So if I don't want replication, why should I create an extra column which is in prcatise meaningless to act as primary key?
Here's some code that does nothing useful except sho that unique keys work quite happily:
use tempdb
create table target ( a int unique clustered ) insert target select 1 union all select null
create table pointer ( a int references target(a) on delete cascade, b int, c int, constraint uq_pointer unique clustered (a,b,c) ) insert pointer select 1,2,3 union all select 1,2,null union all select 1,null,3 union all select null,2,3 union all select null,null,3 union all select null,2,null union all select 1,null,null union all select null,null,null
select * from pointer -- returns 8 rows
insert pointer select null,null,null -- this statement fails demonstrating that a -- unique constraint requires only absence of equality, -- not presence of inequality
drop table pointer, target
I don't understand why you would want the table to have a very short half-life either. As far as I can see, it's perfectly OK if the table has a very long half-life - in fact it may as well last for ever. If its rows have a very short half life that might be interesting - I have seen some cases where a table is not worth replicating for standby since the time required to reach a decision to cut over to the standby system and do it is high enough that the replicated data in that table would be worthless - the table itself would still be needed because new items would appear in it and be dealt with (quickly).
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 5:48 PM
Points: 7,088,
Visits: 7,143
|
|
Hugo Kornelis (2/10/2009)
Either the author of the question wrote this question to educate people about the existance of the IDENTITY() function, in which case SELECT INTO would be the expected answer. Or the author expected the readers to know about this and wrote the question to warn about some less published and less well-known side effects, in which case SELECT INTO would obviously have been wrong and CREATE TABLE ... INSERT SELECT would have been right.
Since there is no way to read the mind of the author of the question, I was faced with a 50/50 chance of getting my answer right. I took my chance ... and lost.
And then I took the opportunity to evangelize about those side effects anyway! :D
Like Hugo I quickly decided that two answers were just plain wrong, and two would work (or work sort of). One of the two that would work (sort of) would only work in case where it was fair to assume that nothing mattered but the values in the table - defaults and other constraints, indexes, and triggers were irrelevant, because "a copy of the table" actually meant "a copy of the data in the table" and not what it said; the other one would work in all circumstances.
Given some of the questions and answers I have seen here I reached the opposite conclusion to Hugo - the answer claimed as correct would probably be the sloppy one which would work only in special circumstances, not the one which would always work; so I "won" by choosing the answer which I knew to be wrong.
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|