0% found this document useful (0 votes)
23 views16 pages

Lecture10 IDB

The document outlines data constraints in databases, explaining their purpose in ensuring data accuracy and reliability. It details various SQL constraints such as NOT NULL, UNIQUE, PRIMARY KEY, and CHECK, along with their syntax for implementation. Additionally, it covers how to add, drop, disable, and enable constraints after table creation, and provides references for further reading.

Uploaded by

r30563080
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)
23 views16 pages

Lecture10 IDB

The document outlines data constraints in databases, explaining their purpose in ensuring data accuracy and reliability. It details various SQL constraints such as NOT NULL, UNIQUE, PRIMARY KEY, and CHECK, along with their syntax for implementation. Additionally, it covers how to add, drop, disable, and enable constraints after table creation, and provides references for further reading.

Uploaded by

r30563080
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

Constraints

Course Code: CSC 2108 Course Title: Introduction To Database

Department of Computer Science


Faculty of Science and Technology

Lecture No: 10 Week No: 05 Semester: Summer


2019-2020
Lecturer: Kawser Irom Rushee & [email protected]
Lecture Outline

• Data Constraints
• Edit Constraints
Data Constraints

•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 only to one column
•Table level constraints are applied to the whole table.
Data Constraints

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.
•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
created.
NOT NULL Constraints

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
UNIQUE Constraints

The UNIQUE constraint uniquely identifies each record in a database table.

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

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:
<col><datatype>(size)primary key
Or,
constraint <constraint_name> primary key(<col1>, <col2>)
CHECK Constraints

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:
<col><datatype>(size) check(<logical expression>)
Or,
constraint <constraint_name> check <col> (<logical expression>)
Edit Constraints
•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.
Add Constraints

Syntax:
alter table <table>add constraint <constraint_name> <constraint type> (<col>);

**for foreign key:


alter table <table>add constraint<constraint_name> foreign key(<current table
col>) references <reference table name>(<reference col>);
Drop Constraints

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

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

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

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

1. Modern Database Management (Sixth Edition) by Fred R. McFadden, Jeffrey A.


Hoffer, Mary B. Prescott
2. Database System Concepts (Fifth Edition) by Henry F. Korth, S. Sudarshan, A.
Silberschatz
3. Oracle-database-10g-sql-fundamentals-1-student-guide-volume-1
4. SQL and Relational Theory: How to Write Accurate SQL Code by C.J. Date
5. Database Systems: A Practical Approach to Design, Implementation and
Management (4th Edition) by Thomas M. Connolly, Carolyn E. Begg
6. Fundamentals of Database Systems, 5th Edition by RamezElmasri, Shamkant B.
Navathe
7. Database Design and Relational Theory: Normal Forms and All That Jazz by C. J. Date
8. An Introduction to Database Systems 8th Edition, by C.J. Date
References

1. https://www.db-book.com/db6/slide-dir/index.html
2. https://docs.oracle.com/en/database/oracle/oracle-database/20/sqlrf/SQL-Sta
ndards.html#GUID-BCCCFF75-D2A4-43AD-8CAF-C3C97D92AC63
3. https://www.slideshare.net/HaaMeemMohiyuddin1/data-knowledge-and-infor
mation
4. https://www.slideshare.net/tabinhasan/from-data-to-wisdom
5. https://www.slideshare.net/thinnaphat.bo/

You might also like