Home > Archive > MS SQL Server > November 2006 > primay key









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author primay key
00KobeBrian

2006-10-24, 6:38 pm

How do you assign a primary key to a column in SQL 2005? Thanks.


Uri Dimant

2006-10-24, 6:38 pm

Hi

CREATE TABLE dbo.Test (c INT NOT NULL PRIMARY KEY)



"00KobeBrian" <a@b.com> wrote in message
news:eVQgpKz8GHA.2248@TK2MSFTNGP04.phx.gbl...
> How do you assign a primary key to a column in SQL 2005? Thanks.
>



David Portas

2006-10-24, 6:38 pm

00KobeBrian wrote:
> How do you assign a primary key to a column in SQL 2005? Thanks.


ALTER TABLE tbl ADD CONSTRAINT tbl_pk PRIMARY KEY (col);

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

00KobeBrian

2006-10-24, 6:38 pm

Sorry. How do you do that in SQL Server Management Studio?


"David Portas" < REMOVE_BEFORE_REPLYI
NG_dportas@acm.org> wrote in message
news:1161239072.297334.114770@b28g2000cwb.googlegroups.com...
> 00KobeBrian wrote:
>
> ALTER TABLE tbl ADD CONSTRAINT tbl_pk PRIMARY KEY (col);
>
> --
> David Portas, SQL Server MVP
>
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
>
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>



amish

2006-10-24, 6:38 pm



On Oct 19, 11:48 am, "00KobeBrian" <a...@b.com> wrote:[color=darkred
]
> Sorry. How do you do that in SQL Server Management Studio?
>
> "David Portas" < REMOVE_BEFORE_REPLYI
NG_dpor...@acm.org> wrote in messagenews:11612390
72.297334.114770@b28g2000cwb.googlegroups.com...
>
>
>
>
>
>
>

right click on table - select modify table.
select column you want to set as primary key and right click and select
option to set primary key

Regards
Amish Shah
http://shahamishm.tripod.com

00KobeBrian

2006-11-09, 12:12 am

Can anyone please help?


"00KobeBrian" <a@b.com> wrote in message
news:%23w0gVq08GHA.788@TK2MSFTNGP05.phx.gbl...
> Sorry. How do you do that in SQL Server Management Studio?
>
>
> "David Portas" < REMOVE_BEFORE_REPLYI
NG_dportas@acm.org> wrote in message
> news:1161239072.297334.114770@b28g2000cwb.googlegroups.com...
>
>



00KobeBrian

2006-11-09, 5:16 am

I mean like SQL 2000. All I do is open up the Enterprise Manager and click
the key icon in the top to the column of a table and then the primary index
get assigned. How can I do it similiarly in SQL 2005? There got to be a way,
right? Thanks.


"00KobeBrian" <a@b.com> wrote in message
news:%23w0gVq08GHA.788@TK2MSFTNGP05.phx.gbl...
> Sorry. How do you do that in SQL Server Management Studio?
>
>
> "David Portas" < REMOVE_BEFORE_REPLYI
NG_dportas@acm.org> wrote in message
> news:1161239072.297334.114770@b28g2000cwb.googlegroups.com...
>
>



Dinesh.T.K

2006-11-09, 5:16 am

Right click mouse on the column...

"00KobeBrian" <a@b.com> wrote in message
news:e4leM28AHHA.3316@TK2MSFTNGP02.phx.gbl...
>I mean like SQL 2000. All I do is open up the Enterprise Manager and click
>the key icon in the top to the column of a table and then the primary index
>get assigned. How can I do it similiarly in SQL 2005? There got to be a
>way, right? Thanks.
>
>
> "00KobeBrian" <a@b.com> wrote in message
> news:%23w0gVq08GHA.788@TK2MSFTNGP05.phx.gbl...
>
>



Arto Viitanen

2006-11-09, 5:16 am

00KobeBrian wrote:
> I mean like SQL 2000. All I do is open up the Enterprise Manager and click
> the key icon in the top to the column of a table and then the primary index
> get assigned. How can I do it similiarly in SQL 2005? There got to be a way,
> right? Thanks.
>


On Server Management Studio, create the table and right click on the
left one column. Select "Set Primary Key" from the popup menu.

--
Arto Viitanen, CSC Ltd.
Espoo, Finland
00KobeBrian

2006-11-09, 5:16 am

Hi Guys,

I cannot find "Set Primary Key" from the popup menu. It only has
new column, modify, rename, delete, refresh and properties. In properties
page, the primary key field is grey out. I cannot change from no to yes.
Please help. Thanks.

"Arto Viitanen" <arto.viitanen@csc.fi> wrote in message
news:4552da27$0$8957
$ba624cd0@newsread.funet.fi...
> 00KobeBrian wrote:
>
> On Server Management Studio, create the table and right click on the
> left one column. Select "Set Primary Key" from the popup menu.
>
> --
> Arto Viitanen, CSC Ltd.
> Espoo, Finland



00KobeBrian

2006-11-09, 7:14 pm

Can anyone please help? Thanks.

"00KobeBrian" <a@b.com> wrote in message
news:ubG6kP9AHHA.996@TK2MSFTNGP02.phx.gbl...
> Hi Guys,
>
> I cannot find "Set Primary Key" from the popup menu. It only
> has new column, modify, rename, delete, refresh and properties. In
> properties page, the primary key field is grey out. I cannot change from
> no to yes. Please help. Thanks.
>
> "Arto Viitanen" <arto.viitanen@csc.fi> wrote in message
> news:4552da27$0$8957
$ba624cd0@newsread.funet.fi...
>
>



David Portas

2006-11-09, 7:14 pm

00KobeBrian wrote:
> Can anyone please help? Thanks.
>


The best answer is the one already given: use ALTER TABLE ADD
CONSTRAINT. That way if it doesn't work you'll get an error message
that will explain why it won't work. I recommend you do not use the
Enterprise Manager / Management Studio interface to modify a production
system. The GUI can be unreliable in some respects and doesn't give you
as much control as using the SQL syntax.

As for the reason why you can't create a PK using the GUI - there could
be several possibilities. Maybe the table already has a PK (you can't
create another) or maybe the column(s) is/are nullable or of a datatype
that isn't valid for a Primary Key.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

00KobeBrian

2006-11-10, 12:12 am

Thanks for the sharing. I understand it is better to issue a command. On
the other hand, I just found out that set primary key icon is grey out. I
don't know if there is any problem with my database or not and want to find
out. Do you have any idea? Thanks.


"David Portas" < REMOVE_BEFORE_REPLYI
NG_dportas@acm.org> wrote in message
news:1163119406.397364.220710@b28g2000cwb.googlegroups.com...
> 00KobeBrian wrote:
>
> The best answer is the one already given: use ALTER TABLE ADD
> CONSTRAINT. That way if it doesn't work you'll get an error message
> that will explain why it won't work. I recommend you do not use the
> Enterprise Manager / Management Studio interface to modify a production
> system. The GUI can be unreliable in some respects and doesn't give you
> as much control as using the SQL syntax.
>
> As for the reason why you can't create a PK using the GUI - there could
> be several possibilities. Maybe the table already has a PK (you can't
> create another) or maybe the column(s) is/are nullable or of a datatype
> that isn't valid for a Primary Key.
>
> --
> David Portas, SQL Server MVP
>
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
>
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>



Dinesh.T.K

2006-11-10, 12:12 am

If its greyed out, then another column is already chosen as a PK.

Its a bit confusing to explain the GUI issues, may be a screenshot will
help.

"00KobeBrian" <a@b.com> wrote in message
news:u3WPMpGBHHA.3560@TK2MSFTNGP03.phx.gbl...
> Thanks for the sharing. I understand it is better to issue a command. On
> the other hand, I just found out that set primary key icon is grey out. I
> don't know if there is any problem with my database or not and want to
> find out. Do you have any idea? Thanks.
>
>
> "David Portas" < REMOVE_BEFORE_REPLYI
NG_dportas@acm.org> wrote in message
> news:1163119406.397364.220710@b28g2000cwb.googlegroups.com...
>
>



Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com