51

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
  • 16,667
  • 6
  • 46
  • 63
Alexander
  • 511
  • 1
  • 5
  • 4

7 Answers7

64
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
  • 26,839
  • 13
  • 60
  • 97
RRM
  • 2,385
  • 26
  • 40
35

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,115
  • 3
  • 18
  • 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,308
  • 10
  • 48
  • 76
12

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
  • 26,760
  • 8
  • 50
  • 69
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,794
  • 2
  • 31
  • 41
  • 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