April 21, 2010 at 5:53 pm
Hi All,
I've been reading some sql tutorials and doing some exercise and found that a Primary Key can be added without the use of the Constraint, and I was wondering why should i use the constraint?
EX:
CREATE TABLE Test(
Testid INT Primary Key
,blabla VARCHAR(10)
);
OR
CREATE TABLE Test(
Testid INT
CONSTRAINT PKTestTestId
Primary Key
,blabla VARCHAR(10)
)
April 21, 2010 at 8:05 pm
Shot in the dark, but I feel lucky. :hehe:
ANSI standard?
---------------------------------------------------------
How best to post your question[/url]
How to post performance problems[/url]
Tally Table:What it is and how it replaces a loop[/url]
"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
April 22, 2010 at 6:34 am
Since it's an optional key word, I suspect as well that it's to support the ANSI standard. It may also be to keep the language consistent because you have to use the CONSTRAINT key word when adding a primary key using the ALTER TABLE syntax.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
April 23, 2010 at 5:28 am
ramirez.sebastian (4/21/2010)...I was wondering why should i use the constraint?
"constraint" syntax allows you to specify the name of the underlying index so to comply with whatever naming convention you have in place.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.April 23, 2010 at 8:04 am
thank you all, for your answers. Been able to identify the index sounds like a good enough reason for me to use the constraint. If there are any other advantages I will be glad to hear them.
Thanks again 🙂
April 23, 2010 at 7:11 pm
ramirez.sebastian (4/23/2010)
thank you all, for your answers. Been able to identify the index sounds like a good enough reason for me to use the constraint. If there are any other advantages I will be glad to hear them.Thanks again 🙂
If the table is a Temp Table, there's actually a DISADVANTAGE to using CONSTRAINT to name the PK... a constraint is one of those objects whose name must be unique in the entire database. If you name a PK on a Temp Table using CONSTRAINT, then whatever procedure you're running that creates the Temp Table can only run one at a time.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 26, 2010 at 1:18 pm
Jeff Moden (4/23/2010)
If the table is a Temp Table, there's actually a DISADVANTAGE to using CONSTRAINT to name the PK... a constraint is one of those objects whose name must be unique in the entire database.
Good catch on that specific case.
On the other hand, when working with "permanent" objects I would keep naming PK by resorting to CONSTRAINT keyword.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.May 6, 2010 at 6:14 pm
PaulB-TheOneAndOnly (4/26/2010)
Jeff Moden (4/23/2010)
If the table is a Temp Table, there's actually a DISADVANTAGE to using CONSTRAINT to name the PK... a constraint is one of those objects whose name must be unique in the entire database.Good catch on that specific case.
On the other hand, when working with "permanent" objects I would keep naming PK by resorting to CONSTRAINT keyword.
Sorry for the late reply. I absolutely agree on the proper naming of PK's/FK's on permanent tables.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply