0% found this document useful (0 votes)
21 views

SQL Constraints: SQL Create Table + Constraint Syntax

This document discusses SQL constraints which are used to specify rules for data in tables. Constraints can be specified when a table is created or altered and include NOT NULL, which enforces columns to not accept null values, and UNIQUE, which uniquely identifies records in the table. The UNIQUE and PRIMARY KEY constraints both guarantee uniqueness but there can be multiple UNIQUE constraints and only one PRIMARY KEY per table.

Uploaded by

Mamta Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

SQL Constraints: SQL Create Table + Constraint Syntax

This document discusses SQL constraints which are used to specify rules for data in tables. Constraints can be specified when a table is created or altered and include NOT NULL, which enforces columns to not accept null values, and UNIQUE, which uniquely identifies records in the table. The UNIQUE and PRIMARY KEY constraints both guarantee uniqueness but there can be multiple UNIQUE constraints and only one PRIMARY KEY per table.

Uploaded by

Mamta Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

SQL Constraints

SQL constraints are used to specify rules for the data in a table.

If there is any violation between the constraint and the data action, the
action is aborted by the constraint.

Constraints can be specified when the table is created (inside the CREATE
TABLE statement) or after the table is created (inside the ALTER TABLE
statement).

SQL CREATE TABLE + CONSTRAINT Syntax


CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);

NOT NULL Constraint


The NOT NULL constraint enforces a column to NOT accept NULL values.

The NOT NULL constraint enforces a field to always contain a value. This
means that you cannot insert a new record, or update a record without
adding a value to this field.

UNIQUE Constraint
The UNIQUE constraint uniquely identifies each record in a database table.

The UNIQUE and PRIMARY KEY constraints both provide a guarantee for
uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on


it.

Note that you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.

You might also like