日期:2014-05-17  浏览次数:20418 次

求助-- 主键 外键 约束 声明方式的问题
主键 外键 约束 有两种声明方式如下:
SQL code
 
--方式一
create table test
(
column1 int identity(1,1) primary key,
column2 bit default(0),
column3 char(2) not null check(column3 in('男','女')),
column4 tinyint check(column4>10),
column5 int not null  references foreigntable(column5),
)

--方式二
create table test
(
column1 int identity(1,1),
column2 bit default(0),
column3 char(2) not null,
column4 tinyint ,
column5 int not null,
constraint PK_test primary key(column1),
constraint FK_test_foreigntable foreign key(column5) references foreigntable(column5),
constraint column3_Check check(column3 in('男','女')),
constraint column4_Check check(column4>10),
)
--大家帮忙看一下,那种方式好,我觉得方式二好,但是我不知道原因








------解决方案--------------------
SQL code

各有各的好处,1,简单方便。
2,方便管理,比如你现在要删除一个约束的话,1这种方式你必须要去找他自动生成的很长一串的constraint Name,二这种方式的话就很容易找到了;

------解决方案--------------------
第一个不推荐,不好管理。删除修改什么的都不方便,而且如果通过脚本生成数据库的话,在不同的机器上生成的默认约束名字还不一样。
------解决方案--------------------
第一种是比较简单的方式,不命名的方式,不用指定外键约束的名称,而是有系统自动命名,而第二种方式是命名方式,需要显示的指定外键约束的名称,2中方式没有什么本质的区别