Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all constraint on table?

I want to store tables metadata into other table.So how to get all constraint and its type like whether it primary , unique or foreign key in MS SQL server.

table_name constraint_name constraint_type

user pk_user_id PRIMARY_KEY

user_role fk_role FOREIGN_KEY

user unique_email UNIQUE_KEY

like image 985
Shankar Modi Avatar asked Jan 02 '15 11:01

Shankar Modi


People also ask

How do I see all constraints on a table in SQL?

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 you find the constraints in a table?

In Oracle, use the view user_constraints to display the names of the constraints in the database. The column constraint_name contains the name of the constraint, constraint_type indicates the type of constraint, and table_name contains the name of the table to which the constraint belongs.

How do I view constraints on a table in SSMS?

Click on the plus symbol beside the table name. Folders for the columns, indexes and constraints etc will appear. Click on the plus beside the Constraints folder and any constraints on the table will be displayed. If you wish to view details of the constraint.


1 Answers

USE INFORMATION_SCHEMA.TABLE_CONSTRAINTS

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='tablename'
like image 193
knkarthick24 Avatar answered Sep 30 '22 04:09

knkarthick24