我正在学习 SQL,但困扰我的是,我似乎无法在表上找到所有约束。我用
创建了表格create table t2
(a integer not null primary key,
b integer not null, constraint c1 check(b>0),
constraint fk1 foreign key(a) references t1(a));
并添加了一个约束
alter table t2
add constraint c2 check (b<20);
然后我尝试查看所有(四个)约束
show table status
from tenn #-->the name of my database
like 't2';
然后
show create table t2;
然后
select *
from information_schema.key_column_usage
where table_name='t2';
最后
select *
from information_schema.table_constraints
where table_name='t2';
但这些都没有显示所有四个约束。谁能告诉我如何查看所有这些?
非常感谢!
最佳答案
select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME
from information_schema.KEY_COLUMN_USAGE
where TABLE_NAME = 'table to be checked';
关于MySQL:如何查看表上的所有约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5094948/