List of Constraints from MySQL Database

前端 未结 4 775
猫巷女王i
猫巷女王i 2020-12-02 05:37

How do I get a list of all constraints from a particular database?

4条回答
  •  臣服心动
    2020-12-02 06:15

    Use the information_schema.table_constraints table to get the names of the constraints defined on each table:

    select *
    from information_schema.table_constraints
    where constraint_schema = 'YOUR_DB'
    

    Use the information_schema.key_column_usage table to get the fields in each one of those constraints:

    select *
    from information_schema.key_column_usage
    where constraint_schema = 'YOUR_DB'
    

    If instead you are talking about foreign key constraints, use information_schema.referential_constraints:

    select *
    from information_schema.referential_constraints
    where constraint_schema = 'YOUR_DB'
    

提交回复
热议问题