What is constraint
SQL constraints are used to specify rules for data in a table.
The following are the constraints.
NOT NULL - Ensures that a column cannot have a NULL value.
UNIQUE - Ensures that all values in a column are different.
PRIMARY KEY - The PRIMARY KEY constraint uniquely identifies each record in a database table, (or) A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table.
FOREIGN KEY - A FOREIGN KEY is a key used to link two tables together. Uniquely identifies a row/record in another table.
CHECK - The CHECK constraint is used to limit the value range that can be placed in a column. Ensures that all values in a column satisfies a specific condition.
Example:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
DEFAULT - Sets a default value for a column when no value is specified.
INDEX - Used to create and retrieve data from the database very quickly.
SQL constraints are used to specify rules for data in a table.
The following are the constraints.
NOT NULL - Ensures that a column cannot have a NULL value.
UNIQUE - Ensures that all values in a column are different.
PRIMARY KEY - The PRIMARY KEY constraint uniquely identifies each record in a database table, (or) A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table.
FOREIGN KEY - A FOREIGN KEY is a key used to link two tables together. Uniquely identifies a row/record in another table.
CHECK - The CHECK constraint is used to limit the value range that can be placed in a column. Ensures that all values in a column satisfies a specific condition.
Example:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
DEFAULT - Sets a default value for a column when no value is specified.
INDEX - Used to create and retrieve data from the database very quickly.
0 comments:
Post a Comment