Monday, March 26, 2012

primay key

How do you assign a primary key to a column in SQL 2005? Thanks.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.
>|||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
--|||Sorry. How do you do that in SQL Server Management Studio?
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1161239072.297334.114770@.b28g2000cwb.googlegroups.com...
> 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
> --
>|||On Oct 19, 11:48 am, "00KobeBrian" <a...@.b.com> wrote:
> Sorry. How do you do that in SQL Server Management Studio?
> "David Portas" <REMOVE_BEFORE_REPLYING_dpor...@.acm.org> wrote in messagenews:1161239072.297334.114770@.b28g2000cwb.googlegroups.com...
>
> > 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
> > -- Hide quoted text -- Show quoted text -
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|||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_REPLYING_dportas@.acm.org> wrote in message
> news:1161239072.297334.114770@.b28g2000cwb.googlegroups.com...
>> 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
>> --
>|||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_REPLYING_dportas@.acm.org> wrote in message
> news:1161239072.297334.114770@.b28g2000cwb.googlegroups.com...
>> 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
>> --
>|||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...
>> Sorry. How do you do that in SQL Server Management Studio?
>>
>> "David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
>> news:1161239072.297334.114770@.b28g2000cwb.googlegroups.com...
>> 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 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|||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:
>> 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|||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...
>> 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 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
--|||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_REPLYING_dportas@.acm.org> wrote in message
news:1163119406.397364.220710@.b28g2000cwb.googlegroups.com...
> 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
> --
>|||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_REPLYING_dportas@.acm.org> wrote in message
> news:1163119406.397364.220710@.b28g2000cwb.googlegroups.com...
>> 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
>> --
>

No comments:

Post a Comment