Azure SQL Database
An Azure relational database service.
3,814 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi Expert,
how to find constraints, pk and fk on the table level using the query and Is it possible to add constraint when table is loaded with data
Expected output
Hi Shambhu,
To retrieve all constraints on a specific table:
SELECT name AS constraint_name, type_desc AS constraint_type
FROM sys.objects
WHERE parent_object_id = OBJECT_ID('your_table_name')
AND type IN ('F', 'PK', 'UQ', 'D');
And to retrieve all foreign key constraints on a specific table:
SELECT name AS constraint_name, referenced_object_name AS referenced_table
FROM sys.foreign_keys
WHERE parent_object_id = OBJECT_ID('your_table_name');
Also you can use following query to find all tables:
ALTER TABLE your_table_name
ADD CONSTRAINT fk_constraint_name
FOREIGN KEY (column_name)
REFERENCES referenced_table_name (referenced_column_name);
I hope they can help you,