NodeJS ORM Sequelize使用INFORMATION_SCHEMA.TABLE_CONSTRAINTS
to show constraints,我遇到了一个问题,即该查询中未显示约束,但确实存在约束,因此我无法更改约束在连续的迁移中。
我不确定这是序列问题还是MySQL问题,但是让我抓狂的原始SQL是:
mysql> use rypedb; SHOW INDEX FROM teacher_information;
Database changed
+---------------------+------------+-----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+--------
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment
+---------------------+------------+-----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+--------
| teacher_information | 0 | PRIMARY | 1 | id | A | 143 | NULL | NULL | | BTREE |
| teacher_information | 1 | teacher_information_teacher_id_fk | 1 | account_id | A | 143 | NULL | NULL | | BTREE |
+---------------------+------------+-----------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+--------
2 rows in set (0.00 sec)
mysql> select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME = 'teacher_information_teacher_id_fk' AND TABLE_SCHEMA = 'rypedb';
Empty set, 2 warnings (0.01 sec)
是否期望INFORMATION_SCHEMA.TABLE_CONSTRAINTS
不具有SHOW INDEX
查询中显示的索引?它确实显示出其他限制,这是我第一次看到此问题。
这是我的create table语句的输出:
| teacher_information | CREATE TABLE `teacher_information` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(10) unsigned NOT NULL,
`description` varchar(1024) DEFAULT NULL,
`image_url` varchar(256) DEFAULT NULL,
`bio_link` varchar(256) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `teacher_information_teacher_id_fk` (`account_id`)
) ENGINE=InnoDB AUTO_INCREMENT=144 DEFAULT CHARSET=utf8 |
答案 0 :(得分:2)
约束与索引不同。
这是一个表的示例,该表包含一个主键,一个辅助唯一键,一个外键,一个CHECK约束以及另一个不是约束的索引。
CREATE TABLE `test`.`bar` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`foo_id` bigint(20) unsigned DEFAULT NULL,
`unique_int` int(11) DEFAULT NULL,
`nonunique_int` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_int` (`unique_int`),
KEY `my_fk` (`foo_id`),
KEY `nonunique_int` (`nonunique_int`),
CONSTRAINT `bar_ibfk_1` FOREIGN KEY (`foo_id`) REFERENCES `foo` (`id`),
CONSTRAINT `my_chk` CHECK ((`nonunique_int` in (1,2,3)))
)
这是INFORMATION_SCHEMA.TABLE_CONSTRAINTS中的内容:
select * from table_constraints where table_schema='test' and table_name='bar';
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE | ENFORCED |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
| def | test | PRIMARY | test | bar | PRIMARY KEY | YES |
| def | test | unique_int | test | bar | UNIQUE | YES |
| def | test | bar_ibfk_1 | test | bar | FOREIGN KEY | YES |
| def | test | my_chk | test | bar | CHECK | YES |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
以下是同一表的INFORMATION_SCHEMA.STATISTICS表中的内容:
select * from statistics where table_schema='test' and table_name='bar';
+---------------+--------------+------------+------------+--------------+---------------+--------------+---------------+-----------+-------------+----------+--------+----------+------------+---------+---------------+------------+------------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | NON_UNIQUE | INDEX_SCHEMA | INDEX_NAME | SEQ_IN_INDEX | COLUMN_NAME | COLLATION | CARDINALITY | SUB_PART | PACKED | NULLABLE | INDEX_TYPE | COMMENT | INDEX_COMMENT | IS_VISIBLE | EXPRESSION |
+---------------+--------------+------------+------------+--------------+---------------+--------------+---------------+-----------+-------------+----------+--------+----------+------------+---------+---------------+------------+------------+
| def | test | bar | 1 | test | my_fk | 1 | foo_id | A | 0 | NULL | NULL | YES | BTREE | | | YES | NULL |
| def | test | bar | 1 | test | nonunique_int | 1 | nonunique_int | A | 0 | NULL | NULL | YES | BTREE | | | YES | NULL |
| def | test | bar | 0 | test | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | YES | NULL |
| def | test | bar | 0 | test | unique_int | 1 | unique_int | A | 0 | NULL | NULL | YES | BTREE | | | YES | NULL |
+---------------+--------------+------------+------------+--------------+---------------+--------------+---------------+-----------+-------------+----------+--------+----------+------------+---------+---------------+------------+------------+
这显示了PRIMARY KEY和UNIQUE KEY和FOREIGN KEY会隐式创建索引,但是: