Show constraints on tables command
我有桌子,我已经尝试设置PK FK关系,但我想验证这一点。 如何显示PK / FK限制? 我看到了这个手册页,但它没有显示示例,我的谷歌搜索也没有结果。 我的数据库是
我用
这将显示以当前形式重新标记
只需查询INFORMATION_SCHEMA:
1 2 3 4 5 6 7 8 9 10 |
验证答案的主要问题是您必须解析输出以获取信息。这是一个允许您以更有用的方式获取它们的查询:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | SELECT cols.TABLE_NAME, cols.COLUMN_NAME, cols.ORDINAL_POSITION, cols.COLUMN_DEFAULT, cols.IS_NULLABLE, cols.DATA_TYPE, cols.CHARACTER_MAXIMUM_LENGTH, cols.CHARACTER_OCTET_LENGTH, cols.NUMERIC_PRECISION, cols.NUMERIC_SCALE, cols.COLUMN_TYPE, cols.COLUMN_KEY, cols.EXTRA, cols.COLUMN_COMMENT, refs.REFERENCED_TABLE_NAME, refs.REFERENCED_COLUMN_NAME, cRefs.UPDATE_RULE, cRefs.DELETE_RULE, links.TABLE_NAME, links.COLUMN_NAME, cLinks.UPDATE_RULE, cLinks.DELETE_RULE FROM INFORMATION_SCHEMA.`COLUMNS` as cols LEFT JOIN INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` AS refs ON refs.TABLE_SCHEMA=cols.TABLE_SCHEMA AND refs.REFERENCED_TABLE_SCHEMA=cols.TABLE_SCHEMA AND refs.TABLE_NAME=cols.TABLE_NAME AND refs.COLUMN_NAME=cols.COLUMN_NAME LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS cRefs ON cRefs.CONSTRAINT_SCHEMA=cols.TABLE_SCHEMA AND cRefs.CONSTRAINT_NAME=refs.CONSTRAINT_NAME LEFT JOIN INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` AS links ON links.TABLE_SCHEMA=cols.TABLE_SCHEMA AND links.REFERENCED_TABLE_SCHEMA=cols.TABLE_SCHEMA AND links.REFERENCED_TABLE_NAME=cols.TABLE_NAME AND links.REFERENCED_COLUMN_NAME=cols.COLUMN_NAME LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS cLinks ON cLinks.CONSTRAINT_SCHEMA=cols.TABLE_SCHEMA AND cLinks.CONSTRAINT_NAME=links.CONSTRAINT_NAME WHERE cols.TABLE_SCHEMA=DATABASE() AND cols.TABLE_NAME="table" |
afaik要求
尝试做:
外键约束列在输出的"注释"列中。
你可以用这个:
1 2 3 4 5 6 7 8 |
或者为了更好的格式化输出使用此:
1 2 3 4 5 6 7 8 9 |
类似于@ Resh32,但不需要使用
1 2 3 4 5 6 7 8 9 |
有用,例如使用ORM。
还有一个oracle制作的工具叫做mysqlshow
如果使用
1 2 3 4 5 6 7 |
例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ╰─? mysqlshow -h 127.0.0.1 -u root -p --keys database tokens Database: database Table: tokens +-----------------+------------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +-----------------+------------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ | id | int(10) unsigned | | NO | PRI | | auto_increment | select,insert,update,references | | | token | text | utf8mb4_unicode_ci | NO | | | | select,insert,update,references | | | user_id | int(10) unsigned | | NO | MUL | | | select,insert,update,references | | | expires_in | datetime | | YES | | | | select,insert,update,references | | | created_at | timestamp | | YES | | | | select,insert,update,references | | | updated_at | timestamp | | YES | | | | select,insert,update,references | | +-----------------+------------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ +--------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +--------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | tokens | 0 | PRIMARY | 1 | id | A | 2 | | | | BTREE | | | | tokens | 1 | tokens_user_id_foreign | 1 | user_id | A | 2 | | | | BTREE | | | +--------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ |