呵呵,给你做了一个实验:if object_id('tb') is not null
drop table tb
gocreate table tb(id int primary key,vid int not null,vname varchar(20))insert into tb
values(1,2,'aa')
go
--先删除约束,通过下面的图来取得约束名称。
alter table tb
drop constraint PK__tb__3213E83F75AD65ED
go--如果要加入到primary key中的字段有运行null的字段,那么需要改为not null
/*
alter table tb
alter column vid int not null
*/--名称为pk_tb_xxxxx
alter table tb
add constraint pk_tb_xxxxx primary key clustered
(
id,
vid
)
drop table tb
gocreate table tb(id int primary key,vid int not null,vname varchar(20))insert into tb
values(1,2,'aa')
go
--先删除约束,通过下面的图来取得约束名称。
alter table tb
drop constraint PK__tb__3213E83F75AD65ED
go--如果要加入到primary key中的字段有运行null的字段,那么需要改为not null
/*
alter table tb
alter column vid int not null
*/--名称为pk_tb_xxxxx
alter table tb
add constraint pk_tb_xxxxx primary key clustered
(
id,
vid
)
改一下,去掉drop table:create table tb(id int primary key,vid int not null,vname varchar(20))insert into tb
values(1,2,'aa')
go
--先删除约束,通过下面的图来取得约束名称。
alter table tb
drop constraint PK__tb__3213E83F75AD65ED
go--如果要加入到primary key中的字段有运行null的字段,那么需要改为not null
/*
alter table tb
alter column vid int not null
*/--名称为pk_tb_xxxxx
alter table tb
add constraint pk_tb_xxxxx primary key clustered
(
id,
vid
)
Cannot define PRIMARY KEY constraint on nullable column in table 'Song'.
Msg 1750, Level 16, State 0, Line 11
Could not create constraint. See previous errors.
主键中不能包含null,比如你要把xx列放到主键中,那么必须这样:alter table song
alter column xx 数据类型 not null
我是做作业,各种都试验下,我估计 比如你经常select 某两个列,但是只有一个在聚集索引里,就把他们加进去!
我是做作业,各种都试验下,我估计 比如你经常select 某两个列,但是只有一个在聚集索引里,就把他们加进去!建聚集索引的一般要求是:1.最好是唯一的。2.占用字节尽量短。3.经常用来查询的。4.很少修改的。其实要同时满足上面这些条件是很难的,所以,我觉得一般建聚集索引,主要还是考虑3和4,也就是经常用来查询,很少修改的。另外,需要特别注意的是,主键和聚集索引,是不同的两个概念,如果你需要实现不重复唯一,那么就建个主键,主键是一个约束,是逻辑概念,而索引是一个物理的概念,也就是建完索引,在你的硬盘上会占用具体的空间的。