How to add primary key to existing table with data

All topics about programming / development in T-SQL or other languages for SQL Server.
Post Reply
steelie
Posts: 2
Joined: Wed Jul 03, 2019 4:01 pm

Wed Jul 03, 2019 4:03 pm

Hi, how do i add a primary key too a table that already contains data?

My table is:

Code: Select all

dbo.Staff_Members (
StaffID INT NOT NULL,
StaffUsername VARCHAR(MAX) NOT NULL
)
Tilly
Posts: 8
Joined: Wed Jun 26, 2019 4:49 pm

Wed Jul 03, 2019 4:07 pm

Hi, is the StaffID Unique? if so you can use (UserID is already NOT NULL which is required):

Code: Select all

ALTER TABLE dbo.Staff_Members ADD PRIMARY KEY (UserID)
What does the data in the table look like?

Thanks
steelie
Posts: 2
Joined: Wed Jul 03, 2019 4:01 pm

Wed Jul 03, 2019 4:08 pm

Ah that worked, thank you very much!
Post Reply

Social Media