Integrity Constraints | Database Management System (DBMS) - Computer Science Engineering (CSE) PDF Download

SQL Integrity Constraints

Integrity Constraints are used to apply business rules for the database tables.

The constraints available in SQL are  Foreign Key, Not Null, Unique, Check.

Constraints can be defined in two ways 
1) The constraints can be specified immediately after the column definition. This is called column-level definition. 
2) The constraints can be specified after all the columns are defined. This is called table-level definition.

1) SQL Peimary

This constraint defines a column or combination of columns which uniquely identifies each row in the table.

Syntax to define a Primary key at column level :

column name datatype [CONSTRAINT constraint_name] PRIMARY KEY

Syntax to define a Primary key at table level :

[CONSTRAINT constraint_name] PRIMARY KEY (column_name1,column_name2,..)

  • column_name1, column_name2 are the names of the columns which define the primary Key.
  • The syntax within the bracket i.e. [CONSTRAINT constraint_name] is optional.

For Example: To create an employee table with Primary Key constraint, the query would be like.

Primary Key at column level :

CREATE TABLE employee 
 ( id number(5) PRIMARY KEY, 
 name char(20), 
 dept char(10), 
 age number(2), 
 salary number(10), 
 location char(10) 
 );

or

CREATE TABLE employee
( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY, 
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);

Primary Key at column level :

CREATE TABLE employee 
( id number(5), 
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
CONSTRAINT emp_id_pk PRIMARY KEY (id)
);

Primary Key at table level :

CREATE TABLE employee 
 ( id number(5), NOT NULL,
 name char(20),
 dept char(10),
 age number(2),
 salary number(10),
 location char(10),
 ALTER TABLE employee ADD CONSTRAINT PK_EMPLOYEE_ID PRIMARY KEY (id)
 );

1) SQL Foreign key Refernital Inyegrity :

This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes a relationship between two columns in the same table or between different tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be defined as Foreign key.

Syntax to define a Foreign key at column level :

[CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name)

Syntax to define a Foreign key at table level :

[CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES referenced_table_name(column_name);

For Example:

1) Lets use the "product" table and "order_items". 

Foreign Key at column level :

CREATE TABLE product 
 ( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY, 
 product_name char(20),
 supplier_name char(20),
 unit_price number(10)
 );

CREATE TABLE order_items
 ( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY,
 product_id number(5) CONSTRAINT pd_id_fk REFERENCES, product(product_id),
 product_name char(20),
 supplier_name char(20),
 unit_price number(10)
 );

Foreign Key at table level :

CREATE TABLE order_items
( order_id number(5) ,
product_id number(5),
product_name char(20),
supplier_name char(20),
unit_price number(10)
CONSTRAINT od_id_pk PRIMARY KEY(order_id),
CONSTRAINT pd_id_fk FOREIGN KEY(product_id) REFERENCES product(product_id)
);

2) If the employee table has a 'mgr_id' i.e, manager id as a foreign key which references primary key 'id' within the same table, the query would be like,

CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
mgr_id number(5) REFERENCES employee(id),
salary number(10),
location char(10) 
);

3) SQL Not Null Constraint :

This constraint ensures all rows in the table contain a definite value for the column which is specified as not null. Which means a null value is not allowed.

Syntax to define a Not Null constraint :

[CONSTRAINT constraint name] NOT NULL

For Example: To create a employee table with Null value, the query would be like

CREATE TABLE employee
( id number(5),
name char(20) CONSTRAINT nm_nn NOT NULL,
dept char(10),
age number(2),
salary number(10),
location char(10) 
);

4) SQL Unique Key :

This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated.

Syntax to define a Unique key at column level :

[CONSTRAINT constraint_name] UNIQUE

Syntax to define a Unique key at table level :

[CONSTRAINT constraint_name] UNIQUE(column_name)

For Example : To create an employee table with Unique key, the query would be like,

Unique Key at column level :

CREATE TABLE employee
 ( id number(5) PRIMARY KEY,
 name char(20),
 dept char(10),
 age number(2),
 salary number(10),
 location char(10) UNIQUE 
 );

or

CREATE TABLE employee
( id number(5) PRIMARY KEY,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10) CONSTRAINT loc_un UNIQUE 
);

Unique Key at table level :

CREATE TABLE employee
 ( id number(5) PRIMARY KEY,
 name char(20),
 dept char(10),
 age number(2),
 salary number(10),
 location char(10),
 CONSTRAINT loc_un UNIQUE(location) 
 );

5) SQL Check Constraint

This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint can be applied for a single column or a group of columns.

Syntax to define a Check constraint :

[CONSTRAINT constraint_name] CHECK (condition)

For Example : In the employee table to select the gender of a person, the query would be like

Check Constraint at column level : 

CREATE TABLE employee 
 ( id number(5) PRIMARY KEY, 
 name char(20), 
 dept char(10), 
 age number(2), 
 gender char(1) CHECK (gender in ('M','F')), 
 salary number(10), 
 location char(10) 
 );

Check Constraint at table level : 

CREATE TABLE employee 
( id number(5) PRIMARY KEY, 
name char(20), 
dept char(10), 
age number(2), 
gender char(1), 
salary number(10), 
location char(10), 
CONSTRAINT gender_ck CHECK (gender in ('M','F')) 
);

The document Integrity Constraints | Database Management System (DBMS) - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Database Management System (DBMS).
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
62 videos|66 docs|35 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Integrity Constraints - Database Management System (DBMS) - Computer Science Engineering (CSE)

1. What are integrity constraints in computer science engineering?
Ans. Integrity constraints in computer science engineering are rules or conditions that are defined to ensure the accuracy, consistency, and validity of data in a database. These constraints are used to enforce data integrity and prevent any inconsistencies or errors in the data.
2. What is the role of integrity constraints in database design?
Ans. Integrity constraints play a crucial role in database design as they help maintain the accuracy and consistency of the data stored in the database. They ensure that data meets certain conditions or rules, preventing incorrect or incomplete information from being entered into the database.
3. What are the types of integrity constraints commonly used in databases?
Ans. There are several types of integrity constraints commonly used in databases, including: 1. Primary Key Constraint: Ensures that each record in a table has a unique identifier called a primary key. 2. Foreign Key Constraint: Maintains the referential integrity between two tables by ensuring that values in one table match the values in another table's primary key. 3. Unique Constraint: Ensures that a column or a combination of columns in a table has unique values, preventing duplicates. 4. Check Constraint: Defines a condition that must be satisfied for data to be entered or modified in a table. 5. Not Null Constraint: Specifies that a column cannot contain null values.
4. How do integrity constraints help maintain data integrity in databases?
Ans. Integrity constraints help maintain data integrity in databases by enforcing rules and conditions on the data. They prevent the insertion or modification of data that does not meet the defined constraints, ensuring that the data remains accurate and consistent. By enforcing data integrity, integrity constraints minimize the chances of data corruption, data loss, or inconsistencies in the database.
5. Can integrity constraints be modified or removed after they are defined in a database?
Ans. Yes, integrity constraints can be modified or removed after they are defined in a database. However, caution should be exercised when making changes to integrity constraints, as it can impact the existing data and the overall integrity of the database. It is recommended to carefully evaluate the consequences of modifying or removing integrity constraints before making any changes.
62 videos|66 docs|35 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Integrity Constraints | Database Management System (DBMS) - Computer Science Engineering (CSE)

,

Integrity Constraints | Database Management System (DBMS) - Computer Science Engineering (CSE)

,

study material

,

Free

,

Integrity Constraints | Database Management System (DBMS) - Computer Science Engineering (CSE)

,

Extra Questions

,

pdf

,

Sample Paper

,

Summary

,

ppt

,

Objective type Questions

,

video lectures

,

Important questions

,

Semester Notes

,

Viva Questions

,

Previous Year Questions with Solutions

,

Exam

,

past year papers

,

practice quizzes

,

shortcuts and tricks

,

mock tests for examination

,

MCQs

;