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.
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.
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.
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].
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);
If you are trying to use a varchar or nvarchar field as primary key, you have to specify a length, and not use MAX.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With