SQL Constraints
SQL Constraints
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the
table is created with the ALTER TABLE statement.
Syntax:-
CREATE TABLE table_name
(
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and
reliability of the data in the table. If there is any violation between the constraint and the data action,
the action is aborted.
#-------Multiple Column
ALTER TABLE Stud#1
ADD CONSTRAINT PK_Stud#1 PRIMARY KEY (Name, Address);
CREATE INDEX Syntax:- Creates an index on a table. Duplicate values are allowed:
CREATE INDEX index_name ON table_name (column1, column2, ...);
CREATE UNIQUE INDEX Syntax:- Creates a unique index on a table. Duplicate values are not
allowed:
CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ...);
CREATE INDEX Example
#-------For a single Column
Create Index Idx_Sname on Student#56 (Sname);