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

SQL Constraints Unit III (1)

SQL constraints are rules that limit the type of data that can be entered into a table, ensuring data accuracy and reliability. Common constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT, each serving specific purposes to maintain data integrity. Examples illustrate how to implement these constraints in SQL table creation and alteration.

Uploaded by

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

SQL Constraints Unit III (1)

SQL constraints are rules that limit the type of data that can be entered into a table, ensuring data accuracy and reliability. Common constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT, each serving specific purposes to maintain data integrity. Examples illustrate how to implement these constraints in SQL table creation and alteration.

Uploaded by

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

SQL Constraints

KSN PRASAD
Lecturer in Computer Science
Govt. Degree College
Ravulapalem
SQL Constraints

• 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.
The following constraints are commonly used in SQL:

1. NOT NULL - Ensures that a column cannot have a NULL value


2. UNIQUE - Ensures that all values in a column are different

3. PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely


identifies each row in a table

4. FOREIGN KEY - Uniquely identifies a row/record in another table

5. CHECK - Ensures that all values in a column satisfies a specific


condition

6. DEFAULT - Sets a default value for a column when no value is


specified
1.SQL NOT NULL Constraint

• The NOT NULL constraint enforces a column to


NOT accept NULL values.
• This enforces a field always contain at least a
single value
• Example
• >CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
2.SQL UNIQUE Constraint

• The UNIQUE constraint ensures that all values in a


column are different.
• Example:
• >CREATE TABLE Persons ( ID NUMBER UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255), Age int);
SQL UNIQUE Constraint on ALTER TABLE

To create a UNIQUE constraint on the "ID"


column when the table is already created,
Example
>ALTER TABLE Persons
ADD UNIQUE (ID);
3.SQL PRIMARY KEY Constraint

• The PRIMARY KEY constraint uniquely identifies


each record in a database table.
• Primary keys must contain UNIQUE values, and
cannot contain NULL values.
• Example
>CREATE TABLE Persons (ID NUMBER PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int);
SQL PRIMARY KEY on ALTER TABLE
To create a PRIMARY KEY constraint on the "ID"
column when the table is already created, use the
following

Example
>ALTER TABLE Persons ADD PRIMARY KEY (ID);
4.SQL FOREIGN KEY Constraint
• A FOREIGN KEY is a key used to link two tables together.
• A FOREIGN KEY is a field in one table(Child table) that
refers to the PRIMARY KEY in another table(Parent table).

• The FOREIGN KEY constraint is used to prevent actions


that would destroy links between tables.

• The FOREIGN KEY constraint also prevents invalid data


from being inserted into the foreign key column,
because it has to be one of the values contained in the
table it points to.
Example
Creating parent table:
>CREATE TABLE DEPT(DEPTNO NUMBER PRIMARY
KEY, DNAME VARCHAR2(10), LOC VARCHAR2(10);

Creating child table

>CREATE TABLE EMP(EMPNO NUMBER PRIMARY KEY,


ENAME VARCHAR2(10), DEPTNO NUMBER
REFERENCES DEPT(DEPTNO);
5.SQL CHECK Constraint

1.The CHECK constraint is used to limit the value


range that can be placed in a column.

2. If you define a CHECK constraint on a single


column it allows only certain values for this
column.
EXAMPLE

>CREATE TABLE TEST(EMPNO NUMBER,


SAL NUMBER CHECK(SAL>5000));
>CREATE TABLE TEST(EMPNO NUMBER ,
AGE NUMBER CHECK(AGE>25) ,DOB DATE);
SQL DEFAULT Constraint

• The DEFAULT constraint is used to provide a


default value for a column.
• The default value will be added to all new
records IF no other value is specified.
EXAMPLE

>CREATE TABLE Persons (ID NUMBER NOT NULL,


City varchar(255) DEFAULT ‘MEXICO');
EMP TABLE
DEPT TABLE
DEPT NO DNAME LOC

10 ACCOUNTING NEW YORK


20 RESEARCH DALLAS
30 SALES CHICAGO
THANK YOU

You might also like