Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

SQL Constraints

SQL Create Constraints

Constraints can be declared during the table creation process using the CREATE TABLE statement or added later using the ALTER TABLE statement.

Syntax

CREATE TABLE table_name (
    column1 datatype constraint,
    column2 datatype constraint,
    column3 datatype constraint,
    ….
);

SQL Constraints

SQL constraints define regulations for data within a table.

Constraints serve to restrict the variety of data permissible in a table, thereby upholding data accuracy and reliability. Any conflict between a constraint and a data operation results in the termination of the action.

Constraints can be either column-specific or table-wide. Column-level constraints pertain to individual columns, while table-level constraints extend across the entire table.

SQL frequently employs the subsequent limitations:

  • NOT NULL: Enforces non-null values
  • UNIQUE: Ensures uniqueness in a column
  • PRIMARY KEY: Combines NOT NULL and UNIQUE to identify rows uniquely
  • FOREIGN KEY: Maintains relationships between tables
  • CHECK: Validates values against specific conditions
  • DEFAULT: Sets a default value if none specified
  • CREATE INDEX: Enhances data retrieval speed