SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL


T-SQL

Author
Message
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39358 Visits: 38543
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
leslaw
leslaw
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2697 Visits: 36
Fully agree

Leslaw
Tim Wilson-Brown
Tim Wilson-Brown
SSC Veteran
SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)SSC Veteran (275 reputation)

Group: General Forum Members
Points: 275 Visits: 302
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)
Danny Ocean
Danny Ocean
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1618 Visits: 1549
Hi,

Thanks a lot to all for your great response. Here, i learnt a lot.

Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com
The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5400 Visits: 6900
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
TomThomson
TomThomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14304 Visits: 12197
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

TomThomson
TomThomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14304 Visits: 12197
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! BigGrin


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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search