Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set primary key in SQL Server Management Studio

I created a table in SQL Server Management Studio. I right-clicked the column I want it to be primary key. But I can't set it. It's disabled.

like image 588
Jenny Avatar asked Mar 22 '12 20:03

Jenny


People also ask

How do you set a primary key variable in SQL?

Create Primary Key - Using ALTER TABLE statement. You can create a primary key in SQL Server (Transact-SQL) with the ALTER TABLE statement. However, you can only use the ALTER TABLE statement to create a primary key on column(s) that are already defined as NOT NULL.

How do I add a primary key to an existing table?

Because a table can have only one primary key, you cannot add a primary key to a table that already has a primary key defined. To change the primary key of a table, delete the existing key using a DROP clause in an ALTER TABLE statement and add the new primary key.

How enable and disable primary key in SQL Server?

The syntax to disable a primary key using the ALTER INDEX statement in MS SQL Server is: ALTER INDEX constraint_name ON table_name DISABLE; Let's look at an example of how to disable a primary using the ALTER INDEX statement in SQL Server: ALTER INDEX idx_tblt_emp_id_pk ON [DataAnalytics].


2 Answers

Primary Key column(s) must be unique and non-nullable

http://msdn.microsoft.com/en-us/library/aa933092%28v=sql.80%29.aspx

Try to add the PK constraint by running SQL Query and see if it gives any errors.

ALTER TABLE Customer ADD PRIMARY KEY (CustomerId);
like image 130
surfen Avatar answered Nov 15 '22 06:11

surfen


If you are trying to use a varchar or nvarchar field as primary key, you have to specify a length, and not use MAX.

like image 20
Brian Bowman Avatar answered Nov 15 '22 06:11

Brian Bowman