February 10, 2009 at 12:53 pm
leslaw (2/10/2009)
All tables should have primary key, so it should also be created.Leslaw
Not part of the question, but if needed it is probably better to add the primary key after creating the table and loading the data.
February 10, 2009 at 12:57 pm
Fully agree
Leslaw
February 10, 2009 at 3:08 pm
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)
February 10, 2009 at 11:31 pm
Hi,
Thanks a lot to all for your great response. Here, i learnt a lot.
Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
February 11, 2009 at 10:21 am
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? Everybody look what's going down. -- Stephen Stills
June 25, 2009 at 8:19 am
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
June 25, 2009 at 8:35 am
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! 😀
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
Viewing 7 posts - 31 through 36 (of 36 total)
You must be logged in to reply to this topic. Login to reply