如何在SQL Server 2005的现有表中添加主键(查询)

问题描述:

你好

我已经在sql编辑器上执行了以下查询

Hello

i have execute the following query on sql editor

create database Employee
use Employee
create table Emp
(
id integer,
E_Name nvarchar (20),
e_Add nvarchar(50)
)
select * from Emp
insert into Emp values('001','Reshma','BBSr')



所以我想在现有表Emp
上添加一个主键 我在sql编辑器上写了以下查询



so i want to add a primary key on existing table Emp
i have write the following query on sql editor

alter table Emp add constraint pk_Emp primary key(id)


但执行上述查询后

将出现以下错误



but after execute the of the above query

the following error will be raised


Msg 8111, Level 16, State 1, Line 1
Cannot define PRIMARY KEY constraint on nullable column in table 'Emp'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.



因此,请尽可能向我建议ASAP



So how it can possible please suggest me ASAP

要解决此问题,需要将应声明PRIMARY KEY约束的所有列显式创建为NOT NULL .试试看,让我们知道.
To fix this this case all columns over which the PRIMARY KEY constraint should be declared needs to be explicitly created as NOT NULL. Give a try and let us know.


是的,我已经完成了,谢谢你给我回复
yes i have done Thank u for sending me a response