我正在使用Teradata 16.20.05.01运行以下脚本:
create table t1(v int not null);
create table t2(w int null);
alter table t1 add constraint pk primary key (v);
alter table t2 add constraint t2_fk foreign key (w) references t1 (v);
Run Code Online (Sandbox Code Playgroud)
添加外键后,我突然在架构中得到一个多余的表:
select TableName, RequestText
from "DBC".Tables
where DatabaseName = 'test'
and (TableName like 't1%' or TableName like 't2%')
Run Code Online (Sandbox Code Playgroud)
输出:
TableName |RequestText |
----------|----------------------------------------------------------------------|
t1 |alter table t1 add constraint pk primary key (v) |
t2 |create table t2(w int null) |
T2_0 |alter table t2 add constraint t2_fk foreign key (w) references t1 (v) |
Run Code Online (Sandbox Code Playgroud)
重新创建该外键时,这尤其令人讨厌:
alter table t2 drop constraint t2_fk;
alter table t2 add constraint t2_fk foreign key (w) references t1 (v);
Run Code Online (Sandbox Code Playgroud)
由于以下原因,这是不可能的:
SQL错误[5303] [HY000]:[Teradata数据库] [TeraJDBC 15.00.00.33] [错误5303] [SQLState HY000]错误表'TEST.t2_0'已经存在。
使用内联约束定义时,不会出现此问题
create table t1(v int not null, constraint pk primary key (v));
create table t2(w int null, constraint t2_fk foreign key (w) references t1 (v));
Run Code Online (Sandbox Code Playgroud)
这是一个已知的问题?有可靠的解决方法吗?
这是有记录的行为,当您将外键添加到现有表时,会创建一个错误表,并将所有违反约束的行都复制到该表中。并且不会在ALTER之后自动删除。
解决方法很简单:不要使用标准外键,几乎找不到使用它的站点。切换到批处理FK,即REFERENCES WITH CHECK OPTION在请求级别上应用检查(而不是逐行),或切换到软/虚拟FK REFERENCES WITH NO CHECK OPTION,后者仅定义约束而不执行约束(您必须检查自己的PK / FK违规情况仍然加载脚本)。
| 归档时间: |
|
| 查看次数: |
167 次 |
| 最近记录: |