April 16, 2010 at 5:52 pm
I am new to T-SQL and cannot understand why the following creates a PK with additional alpha numerics after the name in the Keys section:
CREATE TABLE OWNERS (OwnerIDINT PRIMARY KEY IDENTITY)
Using the Object Explorer in MS SQL MS 2008, under Keys for the new table, it shows:
PK__OWNERS__8193859807F6335A
Why is it adding all the numbers (and alpha) and more importantly, how can I create it without it adding them?
TIA,
Dobermann
April 16, 2010 at 7:09 pm
Why is it adding all the numbers (and alpha) and more importantly, how can I create it without it adding them?
Why do you want to create them without all the numbers, etc?
April 16, 2010 at 8:06 pm
defining a constraint name is optional...primary key, foreign key, defaults, check constraints...all of them will be auto named unless you explicitly define it:
here's your table with the named constraint instead:
CREATE TABLE [dbo].[OWNERS] (
[OWNERID] INT IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK__OWNERS__OWNERID] PRIMARY KEY CLUSTERED (OwnerID))
Lowell
April 16, 2010 at 8:59 pm
bitbucket-25253 (4/16/2010)
Why do you want to create them without all the numbers, etc?
Because I like it to be clean and just contain the necessary info in the title. The other stuff is just extra baggage.
Maybe I am also anal-retentive <G>...
Dobermann
April 16, 2010 at 11:29 pm
Lowell (4/16/2010)
defining a constraint name is optional...primary key, foreign key, defaults, check constraints...all of them will be auto named unless you explicitly define it:here's your table with the named constraint instead:
CREATE TABLE [dbo].[OWNERS] (
[OWNERID] INT IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK__OWNERS__OWNERID] PRIMARY KEY CLUSTERED (OwnerID))
Why does yours work and mine does not?
CREATE TABLE OWNERS (
OwnerIDINT NOT NULL PRIMARY KEY IDENTITY CONSTRAINT PK_OWNERS_OwnersID)
I did not use the [] on the names, as they did not need it plus IDENTITY defaults to (1,1), so I omitted that too. PKs are automagically clustered, so I omitted that. If I add a comma after NOT NULL, it does not like it.
So what am I not seeing? I am brain dead on it. I always thought the order did not matter.
Thanks again,
Dobermann
April 17, 2010 at 4:19 am
it's just syntax.
After the datatype, you say constraint/nameOfConstraint/Type of constraint if you are doing it inline:
CREATE TABLE OWNERS2 (OwnerID INT CONSTRAINT [PK__OWNERS__TWO] PRIMARY KEY IDENTITY)
Lowell
April 17, 2010 at 5:12 pm
Thank you, Lowell. I had played around with many combinations but not the right one apparently. I will be making a note of your answer in my "book of notes" for future reference.
I appreciate your help,
Dobermann
April 18, 2010 at 3:28 am
Dobermann (4/16/2010)
PKs are automagically clustered
This is true by default, but a PK will be created as non-clustered if it is added to a table that already has a clustered index.
CREATE TABLE OWNERS
(
AlternateKey INT UNIQUE CLUSTERED NOT NULL,
OwnerID INT PRIMARY KEY IDENTITY,
);
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
April 18, 2010 at 2:27 pm
Paul White NZ (4/18/2010)
Dobermann (4/16/2010)
PKs are automagically clusteredThis is true by default, but a PK will be created as non-clustered if it is added to a table that already has a clustered index.
CREATE TABLE OWNERS
(
AlternateKey INT UNIQUE CLUSTERED NOT NULL,
OwnerID INT PRIMARY KEY IDENTITY,
);
Yes, I agree. Since I was creating a new table though, I knew it had but the one PK and did not have to worry about declaring clustered. I was stuck on the syntax and neither BOL nor Google seemed to come up with an answer - at least under the search criteria that I could think of.
I noticed that while I had learned to put NOT NULL right after field type, you and many others do not. I used to think it was a matter of personal style, but after seeing the problems I had at the beginning of this post and based upon Lowell's post, I am beginning to wonder if I should change my way of ordering.
Dobermann
April 18, 2010 at 6:15 pm
Dobermann (4/18/2010)
Yes, I agree. Since I was creating a new table though, I knew it had but the one PK and did not have to worry about declaring clustered.
Ok. I must admit I often omit CLUSTERED / NONCLUSTERED myself in forum postings, but tend to include it in production code - there's no real reason, except that the default has changed once before (SQL 7 IIRC).
I noticed that while I had learned to put NOT NULL right after field type, you and many others do not. I used to think it was a matter of personal style, but after seeing the problems I had at the beginning of this post and based upon Lowell's post, I am beginning to wonder if I should change my way of ordering.
I am a big fan of always using NULL / NOT NULL - since the default depends on the current setting of ANSI_NULL_DFLT_OFF / ANSI_NULL_DFLT_ON. I do occasionally skip NOT NULL on PRIMARY KEYs and IDENTITY columns - but that is just laziness 😉
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply