table constraint and nocheck

Shambhu Rai 1,411 Reputation points
2023-05-23T15:21:03.9366667+00:00

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

User's image

Azure SQL Database
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,705 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,344 questions
Exchange | Exchange Server | Management
SQL Server | Other
{count} votes

Accepted answer
  1. Vahid Ghafarpour 23,385 Reputation points Volunteer Moderator
    2023-05-23T17:05:05.6733333+00:00

    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,


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.