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

Constraints

assignment

Uploaded by

zeel.patel1441
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Constraints

assignment

Uploaded by

zeel.patel1441
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Create 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.

SQL Constraints

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.

Constraints can be column level or table level. Column level constraints apply to a column, and table level
constraints apply to the whole table.

The following constraints are commonly used in SQL:

 NOT NULL - Ensures that a column cannot have a NULL value


 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
 FOREIGN KEY - Prevents actions that would destroy links between tables
 CHECK - Ensures that the values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column if no value is specified
 CREATE INDEX - Used to create and retrieve data from the database very quickly

SQL NOT NULL Constraint

By default, a column can hold NULL values.

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

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

CREATE TABLE Persons ( ALTER TABLE Persons


ID int NOT NULL, MODIFY COLUMN Age int NOT NULL;
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
SQL UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different.

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

A PRIMARY KEY constraint automatically has a UNIQUE constraint.

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

CREATE TABLE Persons (


ID int NOT NULL, ALTER TABLE Persons
LastName varchar(255) NOT NULL, DROP INDEX UC_Person;
FirstName varchar(255),
Age int,
UNIQUE (ID)
);

SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each record in a table.

Primary keys must contain UNIQUE values, and cannot contain NULL values.

A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple
columns (fields).

CREATE TABLE Persons ( ALTER TABLE Persons


ID int NOT NULL, ADD PRIMARY KEY (ID);
LastName varchar(255) NOT NULL,
FirstName varchar(255), ALTER TABLE Persons
Age int, DROP PRIMARY KEY;
PRIMARY KEY (ID)
);
SQL CHECK Constraint

The CHECK constraint is used to limit the value range that can be placed in a column.

If you define a CHECK constraint on a column it will allow only certain values for this column.

If you define a CHECK constraint on a table it can limit the values in certain columns based on values in
other columns in the row.

CREATE TABLE Persons ( ALTER TABLE Persons


ID int NOT NULL, ADD CHECK (Age>=18);
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);

SQL FOREIGN KEY Constraint

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.

A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another
table.

The table with the foreign key is called the child table, and the table with the primary key is called the
referenced or parent table.

Look at the following two tables:


CREATE TABLE Orders ( ALTER TABLE Orders
OrderID int NOT NULL, ADD FOREIGN KEY (PersonID)
OrderNumber int NOT NULL, REFERENCES Persons(PersonID);
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

SQL DEFAULT Constraint

The DEFAULT constraint is used to set a default value for a column.

The default value will be added to all new records, if no other value is specified.

CREATE TABLE Persons ( ALTER TABLE Persons


ID int NOT NULL, ALTER City SET DEFAULT 'Sandnes';
LastName varchar(255) NOT NULL,
FirstName varchar(255), ALTER TABLE Persons
Age int, ALTER City DROP DEFAULT;
City varchar(255) DEFAULT 'Sandnes'
);
ORACLE COMPITABLE PL/SQL:

ALTER TABLE Persons


MODIFY Age int NOT NULL;

CREATE TABLE Persons ( ALTER TABLE Persons


ID int NOT NULL, ADD UNIQUE (ID);
LastName varchar(255) NOT NULL,
FirstName varchar(255), ALTER TABLE Persons
Age int, DROP CONSTRAINT UC_Person;
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);

CREATE TABLE Persons ( ALTER TABLE Persons


ID int NOT NULL PRIMARY KEY, ADD PRIMARY KEY (ID);
LastName varchar(255) NOT NULL,
FirstName varchar(255), ALTER TABLE Persons
Age int DROP CONSTRAINT PK_Person;
);

CREATE TABLE Persons ( ALTER TABLE Persons


ID int NOT NULL, ADD CHECK (Age>=18);
LastName varchar(255) NOT NULL,
FirstName varchar(255), ALTER TABLE Persons
Age int CHECK (Age>=18) DROP CONSTRAINT CHK_PersonAge;
);

ALTER TABLE Persons


MODIFY City DEFAULT 'Sandnes';

ALTER TABLE Persons


ALTER COLUMN City DROP DEFAULT;

You might also like