Unit - II
Structured Query
Language
SQL Constraints
https://www.tutorialrepublic.com/sql-tutorial/sql-constraints.php
https://www.edureka.co/blog/sql-constraints/
https://www.w3schools.com/sql/sql_constraints.asp
Recap
SQL> desc student;
Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO VARCHAR2(20)
NAME VARCHAR2(20)
DOB DATE
MARK NUMBER(3)
What is Constraint?
Constraints are the rules that we can apply on the type
of data in a table
A constraint is simply a restriction placed on one or more
columns of a table to limit the type of values that can be
stored in that column.
Constraints provide a standard mechanism to maintain
the accuracy and integrity of the data inside a database
table.
What is Constraint?
There are two types of constraints:
Column-level constraints
These constraints are applied to a single column
Table-level constraints
These constraints are the application to the complete
table
Different types of constraints
NOT NULL
PRIMARY KEY
UNIQUE
DEFAULT
CHECK
FOREIGN KEY
NOT NULL
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.
UNIQUE
The UNIQUE constraint ensures that all values in a
column are different
Many UNIQUE constraints per table
PRIMARY KEY
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
Primary key can consist of single or multiple columns
(fields).
DEFAULT
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.
CHECK
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.
NewStudent Table with constraints
SQL> create table newstudent(
2 regno number(10) primary key,
3 name varchar(20) unique,
4 dob date not null,
5 mark number(3) default 0 check(mark > 0 and mark < 100));
Table created.
NewStudent Table with constraints
SQL> desc newstudent;
Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO NOT NULL NUMBER(10)
NAME VARCHAR2(20)
DOB NOT NULL DATE
MARK NUMBER(3)
USER_Constraints
NewStudent Table Constraints
SQL> select constraint_name,constraint_type from
user_constraints where table_name='NEWSTUDENT';
CONSTRAINT_NAME C
------------------------------ -
SYS_C004048 C
SYS_C004049 C
SYS_C004050 P
SYS_C004051 U
FOREIGN KEY
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.
Example
SQL> create table hostel(
2 regno number(10),
3 name varchar(20),
4 foreign key(regno) references newstudent(regno));
Table created.
Example
SQL> select constraint_name,constraint_type from user_constraints
where table_name='hostel';
no rows selected
SQL> select constraint_name,constraint_type from user_constraints
where table_name='HOSTEL';
CONSTRAINT_NAME C
------------------------------ -
SYS_C004052 R
SQL Constraints
What?
Need
Types
NOT NULL
PRIMARY KEY
UNIQUE
DEFAULT
CHECK
FOREIGN KEY