57

I'm learning SQL and what bothers me, is that I seem unable to find ALL constraints on a table. I created the table with

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));

and added a constraint with

alter table t2
add constraint c2 check (b<20);

I then tried to see ALL (four) constraints with

show table status
from tenn #-->the name of my database
like 't2';

and then

show create table t2;

and then

select *
from information_schema.key_column_usage
where table_name='t2';

and finally

select *
from information_schema.table_constraints
where table_name='t2';

But none of these shows all four constraints. Could anyone tell me how to see all of them?

Thanks a lot!

unholysampler
  • 17,141
  • 7
  • 47
  • 64
Alexander
  • 571
  • 1
  • 5
  • 4

8 Answers8

66
select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME
from information_schema.KEY_COLUMN_USAGE
where TABLE_NAME = 'table to be checked';
bummi
  • 27,123
  • 14
  • 62
  • 101
RRM
  • 2,495
  • 29
  • 46
39

The simplest way to see the explanation of a current table and its constraints is to use:

SHOW CREATE TABLE mytable;

This will show you exactly what SQL would be entered to define the table structure in its current form.

John Foley
  • 4,373
  • 3
  • 21
  • 23
14

You can use this:

select
    table_name,column_name,referenced_table_name,referenced_column_name
from
    information_schema.key_column_usage
where
    referenced_table_name is not null
    and table_schema = 'my_database' 
    and table_name = 'my_table'

Or for better formatted output use this:

select
    concat(table_name, '.', column_name) as 'foreign key',  
    concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
    information_schema.key_column_usage
where
    referenced_table_name is not null
    and table_schema = 'my_database' 
    and table_name = 'my_table'
Abhishek Gupta
  • 6,465
  • 10
  • 50
  • 82
13

You could get it from information_schema.table_constraints like this :

SELECT * 
FROM   information_schema.table_constraints
WHERE  table_schema = schema()
AND    table_name = 'table_name';
blackbishop
  • 30,945
  • 11
  • 55
  • 76
3

The foreign key constraints are listed in the Comment column of the output from the following command:

 SHOW TABLE STATUS FROM db_name LIKE 'tbl_name';
sreimer
  • 4,913
  • 2
  • 33
  • 43
  • This has not worked for me (mysql 5.5.35-0ubuntu0.12.04.2): the comments field is blank, whereas R R Madhav's solution shows constraints returned as columns. – msanford Jul 11 '14 at 20:24
1

Unfortunately MySQL does not support SQL check constraints. When you define them in your query they are just ignored.

0

Export the database table in SQL.

If you have phpmyadmin, you can do so by visiting the "Export" tab. If you choose the "Custom" export method, be sure to select either "structure" or "structure and data" under the "Format-specific options" section.

Sample .sql export snippet:

--
-- Table structure for table `customers`
--    

CREATE TABLE `customers` (
  `username` varchar(50) NOT NULL,
  `fullname` varchar(100) NOT NULL,
  `postalcode` varchar(50) NOT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
...
hawk8
  • 11
  • 1
0

Unfortunately, all of the answers here (except for SHOW CREATE TABLE, which shows many details of the table) do not return the CHECK constraint. The following query will return the CHECK Constraints on a table:

mysql> ALTER TABLE Vehicle ADD CHECK (Source <> 'apple sauce');

mysql> SELECT tc.constraint_schema, tc.constraint_name, tc.table_name, tc.constraint_type, tc.enforced, cc.check_clause
FROM information_schema.TABLE_CONSTRAINTS tc
   LEFT JOIN information_schema.CHECK_CONSTRAINTS cc
   ON tc.CONSTRAINT_SCHEMA=cc.CONSTRAINT_SCHEMA
   AND tc.CONSTRAINT_NAME=cc.CONSTRAINT_NAME
WHERE
    tc.TABLE_NAME='Vehicle'
    AND tc.CONSTRAINT_TYPE='CHECK'\G;

*************************** 1. row ***************************
CONSTRAINT_SCHEMA: vehicle_db_development
  CONSTRAINT_NAME: vehicle_chk_1
       TABLE_NAME: Vehicle
  CONSTRAINT_TYPE: CHECK
         ENFORCED: YES
     CHECK_CLAUSE: (`Source` <> _utf8mb4\'apple sauce\')
1 row in set (0.01 sec)
Daniel Viglione
  • 8,014
  • 9
  • 67
  • 101