请问如何给已有表添加一个新的外键列呢? 请问如何给已有表添加一个新的外键列呢?用语句怎么写呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 alter table tb add sid int not nullgoalter table tb add constraint fk_name_1 foreign key([sid])references [dbo].[newtable] ([id])go alter table tb add constraint 外键约束名 foreign key(外键列名) references 外键引用表 (主键列) alter table tb add sid int not nullgoSQL codealter table tb add constraint 外键约束名 foreign key(外键列名) references 外键引用表 (主键列) 先add这个列,然后再添加外键约束,楼上的很对! ---创建主键约束,唯一约束,检查约束create table test( id int, testname varchar(50), sex bit, class varchar(50), score float default(0) constraint pk_test primary key/*这里可以指定是创建聚集或非聚集索引clustered|nonclustered可选项*/(id) constraint ix_test unique(testname) constraint ck_test check/*这里可以使用 not for replication可选项,用于指定当从其他表中复制数据时,不检查约束条件*/(score>=0)) goalter table tbadd constraint pk_tb primary key nonclustered (id)goalter table tbadd constraint ck_sex check(sex='男' or sex='女')go---创建和使用默认约束alter table tbadd constraint de_test default'test' for test ---在表tb的列test的默认约束为'test'go--创建和使用外键约束alter table tbadd constraint fk_tb_tb1 foreign key(id)references tb1(id)go 100继续求解。关于sql server 2008安装的问题 怎样将多个人一起统计? 求一BOM结构视图,高手请进。 一个简单问题sql语句 有关SQL Server 2005 新建表无法添加数据的问题 请问下安装组件的时候会停止已有的SQL服务么? 全文搜索服务启动了,为什么不能为某数据库启动全文搜索 关于用VC6.0编写数据库的问题 一个高难度的报表,请教高手指导! 请各位大虾帮忙~~看 请问用SQL命令怎么判断是否已经有了指定的用户和登录? 帮忙写一个排名的SQL语句
goalter table tb add constraint fk_name_1 foreign key([sid])
references [dbo].[newtable] ([id])
go
goSQL codealter table tb add constraint 外键约束名 foreign key(外键列名) references 外键引用表 (主键列)
create table test
(
id int,
testname varchar(50),
sex bit,
class varchar(50),
score float default(0)
constraint pk_test primary key/*这里可以指定是创建聚集或非聚集索引clustered|nonclustered可选项*/(id)
constraint ix_test unique(testname)
constraint ck_test check/*这里可以使用 not for replication可选项,用于指定当从其他表中复制数据时,不检查约束条件*/(score>=0)
)
go
alter table tb
add constraint pk_tb primary key nonclustered (id)
goalter table tb
add constraint ck_sex check(sex='男' or sex='女')
go---创建和使用默认约束
alter table tb
add constraint de_test default'test' for test ---在表tb的列test的默认约束为'test'
go--创建和使用外键约束
alter table tb
add constraint fk_tb_tb1 foreign key(id)
references tb1(id)
go