Constraint Naming Conventions
Constraint Naming Conventions is a SQL statement in the Constraints category. Following consistent naming conventions for constraints improves database maintainability and debugging. The syntax is -- PK_table, FK_table_col, UQ_table_col, CK_table_col, DF_table_col. It returns constraint naming. A typical example: -- Recommended naming: -- Primary Key: PK_TableName -- CONSTRAINT PK_Employees PRIMARY KEY (id) -- Foreign Key: FK_ChildTable_ParentTable_Column -- CONSTRAINT FK_Orders_Customers_CustomerId FOREIGN KEY (customer_id) REFERENCES Customers(id) -- Unique: UQ_TableName_Column -- CONSTRAINT UQ_Users_Email UNIQUE (email) -- Check: CK_TableName_Description -- CONSTRAINT CK_Products_Price CHECK (price >= 0) -- Default: DF_TableName_Column -- CONSTRAINT DF_Employees_CreatedAt DEFAULT GETDATE() FOR created_at A close relative is PRIMARY KEY, which a column or set of columns that uniquely identifies each row. Only one PK per table. Automatically creates a unique index. A close relative is FOREIGN KEY, which ensures referential integrity: values in this column must exist in the referenced parent table. A close relative is UNIQUE, which ensures all values in a column (or combination) are unique across the table. Allows NULLs (multiple NULLs allowed).