table constraint and nocheck

Shambhu Rai 1,406 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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,815 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,460 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,247 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,368 questions
{count} votes

Accepted answer
  1. Vahid Ghafarpour 18,370 Reputation points
    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