如何在SQL Server 2005的现有表中添加主键(查询) [英] how to add primary key in exsiting table in sql server 2005(query)

查看:79
本文介绍了如何在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


这篇关于如何在SQL Server 2005的现有表中添加主键(查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆