Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234

T-SQL Expand / Collapse
Author
Message
Posted Tuesday, February 10, 2009 12:53 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:24 PM
Points: 23,067, Visits: 31,595
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.



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)
Post #654132
Posted Tuesday, February 10, 2009 12:57 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, June 19, 2012 10:11 AM
Points: 2,689, Visits: 36
Fully agree

Leslaw
Post #654134
Posted Tuesday, February 10, 2009 3:08 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 5:40 PM
Points: 209, 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)



Post #654277
Posted Tuesday, February 10, 2009 11:31 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, February 6, 2014 4:15 AM
Points: 1,242, Visits: 1,546
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

Post #654473
Posted Wednesday, February 11, 2009 10:21 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:24 PM
Points: 4,011, Visits: 6,082
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
Post #655062
Posted Thursday, June 25, 2009 8:19 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 1:33 PM
Points: 8,571, Visits: 9,076
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
Post #741851
Posted Thursday, June 25, 2009 8:35 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 1:33 PM
Points: 8,571, Visits: 9,076
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
Post #741863
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse