表Country(ID,Code,Name)国家
表City(ID,Code,Name)城市
表Persion(ID,Code,Name,Country,City)人员三张表都是NOT NUL的主键都是ID(自增),但是三张表的Code都显式建立了Unique Index索引要求Persion.Country这个字段必须来自Country表的数据
要求Persion.City这个字段要么等于空串,要么必须匹配City.Code请问SQL 的CHECK如何编写
表City(ID,Code,Name)城市
表Persion(ID,Code,Name,Country,City)人员三张表都是NOT NUL的主键都是ID(自增),但是三张表的Code都显式建立了Unique Index索引要求Persion.Country这个字段必须来自Country表的数据
要求Persion.City这个字段要么等于空串,要么必须匹配City.Code请问SQL 的CHECK如何编写
解决方案 »
- 那位能帮我把这个oracle语法的触发器转成ms-sql语法的?
- 一个关于恢复的问题
- sql server 是否会自动中断长期处于不活动状态下的客户端的连接?
- sql server2000数据库执行create table时连接中断
- 高分求删除重复行后的总行数?
- SQL 2005 附加数据库错误 602
- 存储过程中的参数循环
- 求教set @name = 'table1' , select * from @name是否可以?
- =========入门问题:如何组织数据?========
- 关于数据大分表,分库存储的问题
- SQL Server 2005安装过程中出现错误:证书链是由不受信任的颁发机构颁发的。
- 存放空间大一点,会不会影响数据库查询的性能?
create unique index ix_country_code on country(code);create table city (id int identity, code char(8) not null, name varchar(40) not null);
create unique index ix_city_code on city(code);create table person
(id int identity, code char(12) not null, name varchar(40) not null,
country_code char(4) references country(code),
city_code char(8) references city(code));
create unique index ix_person_code on person(code);
但是对于city的约束失败,
因为实际情况上,persion.city可以不填,如果填写了就必须匹配city.code就是说,persion.city要么是空,要么就必须是city表里面的
难道非要举个例子,create table country (id int identity, code char(4) not null, name varchar(40) not null);
create unique index ix_country_code on country(code);
go
insert into country(code, name) values('0001','China');
gocreate table city (id int identity, code char(8) not null, name varchar(40) not null);
create unique index ix_city_code on city(code);
go
insert into city (code,name) values('00010001','Beijin');
gocreate table person
(id int identity, code char(12) not null, name varchar(40) not null,
country_code char(4) references country(code),
city_code char(8) references city(code));
create unique index ix_person_code on person(code);
go
insert into person (code, name,country_code,city_code)
values ('000100010001','Hu','0001',null);
goselect * from person;
/*
1 000100010001 Hu 0001 null
*/
update person set city_code='00010001' where id=1;select * from person;
/*
1 000100010001 Hu 0001 00010001
*/
drop table person,country,city;
go
values ('000100010001','Hu','0001',null);执行到此句时肯定是不行的INSERT 语句与 COLUMN FOREIGN KEY 约束 'FK__person__country___79A81403' 冲突。该冲突发生于数据库 'tempdb',表 'country', column 'code'。
语句已终止。因为三个表的所有字段都不能出现null,所以无法用外键约束,看来是没法解决了
不过还要是感谢楼上的
但是在程序处理null很麻烦
等会结贴
如果是null的话是可以的,但是如果是空串''就不行
但是依然感谢
结贴,给分