Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show constraints on tables command

I have tables that I've tried setting PK FK relationships on but I want to verify this. How can I show the PK/FK restraints? I saw this manual page, but it does not show examples and my google search was fruitless also. My database is credentialing1 and my constrained tables are practices and cred_insurances.

like image 677
Captain Claptrap Avatar asked Oct 23 '10 13:10

Captain Claptrap


People also ask

How do you display constraints in a table?

select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME from information_schema. KEY_COLUMN_USAGE where TABLE_NAME = 'yourTableName'; To display all constraints on a table, implement the above syntax.

How do I view constraints on a table in SQL Server?

Using SQL Server Management StudioIn the Object Explorer, right-click the table containing the check constraint and select Design. On the Table Designer menu, click Check Constraints.... In the Check Constraints dialog box, under Selected Check Constraint, select the constraint you wish to edit.

How do I view constraints on a SQL Developer table?

First method is with table Constraints tab (select table and select Constraints tab). Tab lists table constraints - primary, unique and foreign keys and check constraints - all in one grid. Foreign keys are the ones with 'Foreign_Key' value in CONSTRAINT_TYPE column.


2 Answers

I use

SHOW CREATE TABLE mytable; 

This shows you the SQL statement necessary to receate mytable in its current form. You can see all the columns and their types (like DESC) but it also shows you constraint information (and table type, charset, etc.).

like image 69
Adrian Smith Avatar answered Sep 20 '22 11:09

Adrian Smith


Simply query the INFORMATION_SCHEMA:

USE INFORMATION_SCHEMA; SELECT TABLE_NAME,        COLUMN_NAME,        CONSTRAINT_NAME,        REFERENCED_TABLE_NAME,        REFERENCED_COLUMN_NAME FROM KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = "<your_database_name>"        AND TABLE_NAME = "<your_table_name>"        AND REFERENCED_COLUMN_NAME IS NOT NULL; 
like image 37
Resh32 Avatar answered Sep 19 '22 11:09

Resh32