0% found this document useful (0 votes)
4 views21 pages

SP 23 24 Lecture10 IDB

The document provides an overview of data and edit constraints in databases, explaining their importance in ensuring data accuracy and reliability. It details various types of constraints such as NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK, along with their syntax for implementation. Additionally, it covers how to add, drop, disable, and enable constraints after table creation.

Uploaded by

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

SP 23 24 Lecture10 IDB

The document provides an overview of data and edit constraints in databases, explaining their importance in ensuring data accuracy and reliability. It details various types of constraints such as NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK, along with their syntax for implementation. Additionally, it covers how to add, drop, disable, and enable constraints after table creation.

Uploaded by

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

1

Introduction to Database
Lecture 11:
Data Constraints & Edit Constraints
Learning Objectives
2

To know about:
Data Constraints
Edit Constraints
Data Constraints
3

Constraints are the rules enforced on


data columns on table.
Used to limit the type of data that can
go into a table
Ensures the accuracy and reliability of
the data in the database
 Could be column level or table level
Column level constraints are applied
one by one column
Table level constraints are applied to
the whole table.
Data Constraints
4

Following are commonly used


constraints available in SQL:
NOT NULL Constraint: Ensures that a
column cannot have NULL value.
DEFAULT Constraint: Provides a
default value for a column when none
is specified.
UNIQUE Constraint: Ensures that all
values in a column are different.
PRIMARY Key Uniquely identified each
rows/records in a database table.
Data Constraints
5

FOREIGN Key: Uniquely identified a


rows/records in any another database
table.
CHECK Constraint: The CHECK
constraint ensures that all values in a
column satisfy certain conditions.
Constraints can be specified when a
table is created with the CREATE
TABLE statement
OR
You can use ALTER TABLE statement to
create constraints even after the table is
NOT NULL Constraints
6

The NOT NULL constraint enforces a


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

Syntax:
<col><datatype>(size)
not null
DEFAULT Constraints
7

Column level syntax:


<col_name> <data type> (<size>)
DEFAULT <default_value>
** default constraint can not have a name
UNIQUE Constraints
8

The UNIQUE constraint uniquely


identifies each record in a database
table.

Syntax:
<col><datatype>(size) unique;
PRIMARY KEY Constraints
9

The PRIMARY KEY constraint uniquely


identifies each record in a database
table.
Primary keys must contain unique
values. A
primary key column cannot contain
NULL
values. Each table should have a
primary key,
and each table can have only ONE
primary key.
Syntax for Primary Key Constraints
10

Syntax:
1. Column level: <colname><datatype>(size)
primary key
Or,
<colname><datatype>(size) CONSTRAINT
<constraint name> PRIMARY KEY

2. Table level: CONSTRAINT <constraint


name> PRIMARY KEY (<colname>)
FOREIGN KEY Constraints
11

PRIMARY KEY table must be created first.


Foreign key column Size should be same as
primary key column.
Syntax:
1. Column level:
<col name> <datatype> (<size>)
CONSTRAINT <constraint_name>
REFERENCES
<parent_table_name>(<parent_table_column_
name>)
Syntax for Foreign Key Constraints
12

Table level:
CONSTRAINT <constraint_name>
FOREIGN KEY (<col_name>)
REFERENCES
<parent_table_name>(<parent_table_column_
name>)
CHECK Constraints
13

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 single
column it allows only certain values for this
column.
Syntax:
CONSTRAINT <constraint_name>
CHECK (<col name> <logical
expression>)
Or,
<colname><datatype>(size)
check(<logical expression>)
Edit Constraints
14

Constraints are the rules enforced on


data columns on table. These 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 database.
Previously, we have learnt to add
constraints on the time of creating the
table. You can also add or change
constraint after table have been
created.
Edit Constraints
15

 For default constraint:


 ALTER TABLE <table name>
add <colname> <datatype>(<size>) DEFAULT
<default_value>
 For primary key constraint:
 ALTER TABLE <table name>
ADD CONSTRAINT <constraint name>
PRIMARY KEY (<column name>)
 For Foreign key constraint:
 ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
Foreign key (<col_name>) REFERENCES
<parent_table_name>(<parent_table_column_name>)
Edit Constraints
16

For check constraint:


ALTER TABLE <table_name>
ADD CONSTRAINT <constraintname>
CHECK ( <colname> <expression>)
Drop Constraints
17

Syntax:
alter table <table> drop constraint
<constraint_name>;
Disable Constraints
18

Syntax:
alter table <table> disable constraint
<constraint_name>;
Enable Constraints
19

Syntax:
alter table <table>
enable constraint <constraint_name>;
Viewing Colums Associated with
Constraints
20

Syntax:
select constraint_name, column_name
from user_cons_columns where
table_name = ‘<table>’;
21

THANK YOU

You might also like