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
.
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.
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.
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.
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.).
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With