0% found this document useful (0 votes)
28 views4 pages

Constraints

Constraints are business rules enforced on data stored in database tables. The main types of constraints are primary keys, foreign keys, check constraints, unique constraints, and not null constraints. Primary keys uniquely identify each record in a table and cannot be null. Foreign keys reference a primary key in another table. Check constraints specify conditions that rows must satisfy. Unique constraints require that values be unique across columns. Not null constraints make columns mandatory. Constraints can be disabled and enabled on individual tables.

Uploaded by

saketh bhargava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Constraints

Constraints are business rules enforced on data stored in database tables. The main types of constraints are primary keys, foreign keys, check constraints, unique constraints, and not null constraints. Primary keys uniquely identify each record in a table and cannot be null. Foreign keys reference a primary key in another table. Check constraints specify conditions that rows must satisfy. Unique constraints require that values be unique across columns. Not null constraints make columns mandatory. Constraints can be disabled and enabled on individual tables.

Uploaded by

saketh bhargava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4.

CONSTRAINTS

Constraints are the business Rules which are enforced on the data being stored in a table are
called Constraints
TYPES OF CONSTRAINTS:
1) Primary key
2) Foreign key/references
3) Check
4) Unique
5) Not null
6) Null
7) Default

1) PRIMARY KEY:

A field which is used to identify a record uniquely. A column or


combination of columns can be created as primary key, which can be used as a reference
from other tables. A table contains primary key is known as Master Table.
It must uniquely identify each record in a table.
It must contain unique values.
It cannot be a null field.
It cannot be multi port field.

It should contain a minimum no. of fields necessary to be called unique.


Syntax:
CREATE TABLE Table_Name(column_name data_type(size) PRIMARY KEY,
….);
Example:
create table Employee(Id number(5) constraint Id_pkey primary key, Name
varchar2(10), Dept char(5), Age number(3), Salary number(7));

OUTPUT:

15
2) FOREIGN KEY:
It is a table level constraint. We cannot add this at column level. To reference any primary key
column from other table this constraint can be used. The table in which the foreign key is
defined is called a detail table. The table that defines the primary key and is referenced by the
foreign key is called the master table.
Syntax: CREATE TABLE Table_Name(column_name data_type(size)

FOREIGN KEY(column_name) REFERENCES table_name);

create table Dept(Dept_Id number(5), Dept char(5), EmpId number(5), constraint


fkey foreign key(EmpId) references Employee(Id));

OUTPUT:

3) CHECK CONSTRAINT:
Specifies a condition that each row in the table must satisfy. To satisfy theconstraint, each row
in the table must make the condition either TRUE or unknown (due to null).
Syntax:
CREATE TABLE Table_Name(column_name data_type(size) CHECK(logical
expression), ….);

Example:
create table Employee(Id number(5), Name varchar2(10), Dept char(5), Salary
number(7) constraint sal_ck Check(Salary>5000 and Salary<10000));
16
OUTPUT:

4) UNIQUE CONSTRAINT:

The purpose of a unique key is to ensure that information in the column(s) is unique i.e. a value
entered in column(s) defined in the unique constraint must not be repeated

1) across the column(s). A table may have many unique keys.

create table Employee(Id number(5) constraint uk Unique, Name varchar2(10),


Dept char(5), Salary number(7));

OUTPUT:

17
5.NOT NULL CONSTRAINT:
When a column is defined as NOTNULL, then that column becomes a
mandatory column. It implies that a value must be entered into the column if the record is to

1) be accepted for storage in the table.

create table Employee(Id number(5), Name varchar2(10), Dept char(5), Salary


number(7) constraint N_Null NOT NULL);

OUTPUT:

DISABLE SINGLE CONSTRAINT:

Alter Table Dept disable constraint fkey;


OUTPUT:

Alter Table Employee disable constraint pkey;

OUTPUT:

ENABLE SINGLE CONSTRAINTS:

Alter Table Employee enable constraint pkey;

OUTPUT:

Alter Table Dept enable constraint fkey;

OUTPUT:

18

You might also like