DMS Unit 2
DMS Unit 2
Attribute: It contains the name of a column in a particular table. Each attribute Ai must
have a domain, dom(Ai)
Relational schema: A relational schema contains the name of the relation and name of
all columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify
the row in the relation uniquely.
Vidya Lunge 1
DMS 22319 unit 2
● In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are
the attributes.
Properties of Relations
Vidya Lunge 2
DMS 22319 unit 2
Vidya Lunge 3
DMS 22319 unit 2
Vidya Lunge 4
DMS 22319 unit 2
● A single robust language should be able to define integrity constraints, views, data
manipulations, transactions and authorizations.
● If the database allows access to the aforementioned (denoting a thing or person
previously mentioned)ones, it is violating this rule.
Rule 6 − Rule of Updating Views
● Views should reflect the updates of their respective base tables and vice versa.
● A view is a logical table which shows restricted data.
● Views generally make the data readable but not modifiable.
● Views help in data abstraction.
Rule 7 − Rule of Set level insertion, update and deletion
● A single operation should be sufficient to retrieve, insert, update and delete the
data.
Rule 8 − Rule of Physical Data Independence
● End user operations are logically separated from physical storage and respective
access methods.
Rule 9 − Rule of Logical Data Independence
● End users can change the database schema without having to recreate it or recreate
the applications built upon it.
Rule 10 − Rule of Integrity Independence
● Integrity constraints should be available and stored as metadata in data dictionary
and not in the application programs.
Rule 11 − Rule of Distribution Independence
● The Data Manipulation Language of the relational system should not be
concerned about the physical data storage and no alterations should be required if
the physical data is centralized or distributed.
Rule 12 − Rule of Non Subversion
● Any row should obey the security and integrity constraints imposed.
● No special privileges are applicable.
● Almost all full scale DBMSs are RDMSs.
Vidya Lunge 5
DMS 22319 unit 2
● It is used to uniquely identify any record or row of data from the table.
For example:
In PERSON table, passport_number, license_number, SSN are keys since they are
unique for each person.
Types of key:
Vidya Lunge 6
DMS 22319 unit 2
1. Candidate key
● The remaining attributes except for primary key are considered as a candidate key.
The candidate keys are as strong as the primary key.
● The minimal set of attributes that can uniquely identify a tuple is known as a
candidate key.
● The value of the Candidate Key is unique and non-null for every tuple.
● There can be more than one candidate key in a relation.
Vidya Lunge 7
DMS 22319 unit 2
For example: In the EMPLOYEE table, id is best suited for the primary key. Rest of the
attributes like SSN, Passport_Number, and License_Number, etc. are considered as a
candidate key.
2 .Primary key
● It is the first key which is used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys as we saw in PERSON table. The
key which is most suitable from those lists become a primary key.
● In the EMPLOYEE table, ID can be primary key since it is unique for each
employee. In the EMPLOYEE table, we can even select License_Number and
Passport_Number as primary key since they are also unique.
● For each entity, selection of the primary key is based on requirement and
developers.
● There can be more than one candidate key in relation out of which one can be
chosen as the primary key.
Vidya Lunge 8
DMS 22319 unit 2
Eg:
Vidya Lunge 9
DMS 22319 unit 2
5 max
3. Super Key
Vidya Lunge 10
DMS 22319 unit 2
4. Foreign key
● Foreign keys are the column of the table which is used to point to the primary key
of another table.
Vidya Lunge 11
DMS 22319 unit 2
● Now in the EMPLOYEE table, Department_Id is the foreign key, and both the
tables are related.
Vidya Lunge 12
DMS 22319 unit 2
Eg:
5.Composite Key:
A composite key is the DBMS key having two or more attributes that together can
uniquely identify a tuple in a table. Such a key is also known as Compound Key, where
each attribute creating a key is a foreign key in its own right.
Vidya Lunge 13
DMS 22319 unit 2
Ex:2
Foreign Key:
Vidya Lunge 14
DMS 22319 unit 2
Primary Key of any given relation, Foreign Key can be NULL as well as may
contain duplicate tuples i.e. it need not follow uniqueness constraint.
For Example, STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in
STUDENT relation.
It may be worth noting that unlike,
For Example,:
STUD_NO in STUDENT_COURSE relation is not unique.
It has been repeated for the first and third tuples.
However, the STUD_NO in STUDENT relation is a primary key and it needs to be
always unique and it cannot be null.
Eg:
Normalization
Vidya Lunge 15
DMS 22319 unit 2
● Normalization divides the larger table into the smaller table and links them using
relationship.
● The normal form is used to reduce redundancy from the database table.
Example of normalizatiion:
Table 1:Student_Data
SID Sname Age
1 Pinky 20
2 Dinky 25
1 Pinky 20
Vidya Lunge 16
DMS 22319 unit 2
Table2:Stud_course_faculty_Data
In this table:
SID is primary key,
no 2 rows are same ,but there is column level redundancy
check row 1,3,4:it is repeated
Vidya Lunge 17
DMS 22319 unit 2
not allowed
Deletion anomaly:
Delete SID 2 it ,deletes all details of DSU and recovery will not be possible.
Updation anomaly:
If Zinky have changed name to booby it will change but if salary of F1 is changed to
50000Rs ,it will requires changes at multiple places though F1 is only one person.
So to avoid all this anomaly we decompose the table into 3 each having primary key.
Student_Details:
SID Sname
Vidya Lunge 18
DMS 22319 unit 2
1 Pinky
2 Binky
3 Minky
4 Zinky
5 Yinky
6 Zinky
Course_Details:
CID Cname
C1 DBMS
C2 DSU
C6 Western music
C7 Indian mytho
Faculty_Details:
Vidya Lunge 19
DMS 22319 unit 2
Vidya Lunge 20
DMS 22319 unit 2
Example2:
Vidya Lunge 21
DMS 22319 unit 2
EMPLOYEE table:
7272826385,
14 John 9064738238 UP
7390372389,
12 Sam 8589830302 Punjab
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
14 John 9064738238 UP
Example 2 –
Vidya Lunge 22
DMS 22319 unit 2
ID Name Courses
--------------------------------
1 Amit c1, c2
2 Eva c3
3 Monty c2, c3,c1
ID Name Course
----------------------------
1 Amit c1
1 Amit c2
2 Eva c3
3 Monty c2
3 Monty c3
3 Monty c1
*************************************************
Functional dependencies
Functional dependencies are the constraints that are derived from the meaning and
interrelationship of the data.
Definition −
Let R is a relation schema with n attributes A1, A2,…..An.
A functional dependency, denoted by X->Y,
between two sets of attributes X and Y that are subsets of R species a constraint on the
possible tuples that can form a relation state r of R.
Y depends on X or Y is determined by X or Y is functional dependent on X. Similarly X
determines Y.
● If X is the candidate key of R then X->Y for any subset of attributes Y of R.
Vidya Lunge 23
DMS 22319 unit 2
Example 1
Given below is an example of functional dependency in database management system
(DBMS) −
1. SSN->ENAME read as
SSN functionally defines ENAME
Or
SSN determines ENAME
2. PNUMBER->{PNAME,PLOCATION}
(PNUMBER determines PNAME and PLOCATION)
3. {SSN,PNUMBER}->HOURS
(SSN and PNUMBER combinedly determines HOURS)
Output
You will get the following result −
Example 2
Consider another example: BOOK table.
Vidya Lunge 24
DMS 22319 unit 2
Here,
● Bookid->Bname {bookid determines Bname}
● Bname ->author {bookname does not determine author, because same bname has
different authors}
● Author->price {Author determines price}
Note − There is no algorithm to identify functional dependency. We have to use our
commonsense and judgment to identify functional dependency.
Vidya Lunge 25
DMS 22319 unit 2
StudentID StudentName
S01 Katie
S02 Ollie
Vidya Lunge 26
DMS 22319 unit 2
S03
<ProjectInfo>
ProjectNo ProjectName
Partial Dependency –
If the proper subset of candidate key determines non-prime attribute, it is called partial
dependency.
Example 1 – Consider table-3 as following below.
STUD_NO COURSE_NO COURSE_FEE
1 C1 1000
2 C2 1500
1 C4 2000
4 C3 1000
4 C1 1000
2 C5 2000
{Note that, there are many courses having the same course fee. }
Here,
COURSE_FEE cannot alone decide the value of COURSE_NO or STUD_NO;
COURSE_FEE together with STUD_NO cannot decide the value of COURSE_NO;
Vidya Lunge 27
DMS 22319 unit 2
Hence,
COURSE_FEE would be a non-prime attribute, as it does not belong to the one only
candidate key {STUD_NO, COURSE_NO} ;
But,
COURSE_NO -> COURSE_FEE ,
i.e., COURSE_FEE is dependent on COURSE_NO, which is a proper subset of the
candidate key.
Non-prime attribute COURSE_FEE is dependent on a proper subset of the candidate
key, which is a partial dependency and so this relation is not in 2NF.
NOTE: 2NF tries to reduce the redundant data getting stored in memory. For instance, if
there are 100 students taking C1 course, we dont need to store its Fee as 1000 for all the
100 records, instead once we can store it in the second table as the course fee for C1 is
1000.
Vidya Lunge 28
DMS 22319 unit 2
A relation is in third normal form if it holds atleast one of the following conditions for
every non-trivial function dependency:
X → Y.
1. X is a super key.
Transitive dependency – :
● If A->B ….1)
● and B->C…….2)
● are two FDs
● then A->C is called transitive dependency.
Vidya Lunge 29
DMS 22319 unit 2
Here,
EMP_STATE & EMP_CITY dependent on EMP_ZIP
And
EMP_ZIP dependent on EMP_ID.
Vidya Lunge 30
DMS 22319 unit 2
Vidya Lunge 31
DMS 22319 unit 2
○ 4.
Normal Description
Form
Vidya Lunge 32
DMS 22319 unit 2
4NF A relation will be in 4NF if it is in Boyce Codd normal form and has
no multi-valued dependency.
Vidya Lunge 33
DMS 22319 unit 2
● For BCNF, the table should be in 3NF, and for every FD, LHS is super key.
Vidya Lunge 34
DMS 22319 unit 2
Example: Let's assume there is a company where employees work in more than one
department.
EMPLOYEE table:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
Vidya Lunge 35
DMS 22319 unit 2
EMP_ID EMP_COUNTRY
264 India
364 UK
EMP_DEPT table:
EMP_DEPT_MAPPING table:
Vidya Lunge 36
DMS 22319 unit 2
D394 283
D394 300
D283 232
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
● A relation will be in 4NF if it is in Boyce Codd normal form (BCNF) and has no
multi-valued dependency.
● For a dependency A → B,
Vidya Lunge 37
DMS 22319 unit 2
1 burger 40
2 dosa 55
Example
STUDENT
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
Vidya Lunge 38
DMS 22319 unit 2
22 computer dancing
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two
independent entity. Hence, there is no relationship between COURSE and HOBBY.
In the STUDENT relation, a student with STU_ID, 21 contains two courses, Computer
and Math and two hobbies, Dancing and Singing.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE
STU_ID COURSE
21 Computer
21 Math
21 Chemistry
34 Chemistry
74 Biology
59 Physics
Vidya Lunge 39
DMS 22319 unit 2
STUDENT_HOBBY
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
● A relation is in 5NF if it is in 4NF and not contains any join dependency and
joining should be lossless.
● 5NF is satisfied when all the tables are broken into as many tables as possible in
order to avoid redundancy.
Example:tab1
Vidya Lunge 40
DMS 22319 unit 2
Candidate key=(subject+lecturer+semester)
● John takes both Computer and Math class for Semester 1 but he doesn't take Math
class for Semester 2.
● In this case, combination of all these fields required to identify a valid data.
● Suppose we add a new Semester as Semester 3 but do not know about the subject
and who will be taking that subject so we leave Lecturer and Subject as NULL.
● But all three columns together acts as a primary key, so we can't leave other two
columns blank.
● So to make the above table into 5NF, we can decompose it into three relations P1,
P2 & P3:
Vidya Lunge 41
DMS 22319 unit 2
P1
SEMESTER SUBJECT
Semester 1 Computer
Math
Semester 1
Semester 1 Chemistry
Semester 2 Math
P2
SUBJECT LECTURER
Vidya Lunge 42
DMS 22319 unit 2
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen
P3
SEMESTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Vidya Lunge 43
DMS 22319 unit 2
Semester 2 Akash
Semester 1 Praveen
=======================================================
Structured Query Language.(SQL)
● It is used for storing and managing data in relational database management system
(RDMS).
● It enables a user to create, read, update and delete relational databases and tables.
● All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use
SQL as their standard database language.
● SQL allows users to query the database in a number of ways, using English-like
statements.
Rules:
Vidya Lunge 44
DMS 22319 unit 2
● Using the SQL statements, you can perform most of the actions in a database.
SQL process:
● When an SQL command is executing for any RDBMS, then the system figure out
the best way to carry out the request and the SQL engine determines that how to
interpret the task.
● All the non-SQL queries are handled by the classic query engine, but SQL query
engine won't handle logical files.
Vidya Lunge 45
DMS 22319 unit 2
Characteristics of SQL
● SQL is used to define the data in the database and manipulate it when needed.
Advantages of SQL
Vidya Lunge 46
DMS 22319 unit 2
High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of
records from a database.
No coding needed
In the standard SQL, it is very easy to manage the database system. It doesn't require a
substantial amount of code to manage the database system.
Long established are used by the SQL databases that are being used by ISO and ANSI.M
Portability
SQL can be used in laptop, PCs, server and even some mobile phones.
Interactive language
Using the SQL language, the users can make different views of the database structure.
SQL Datatype
● SQL Datatype is used to define the values that a column can contain.
● Every column is required to have a name and data type in the database table.
Vidya Lunge 47
DMS 22319 unit 2
Datatype of SQL:
1. Binary Datatypes
There are Three types of binary Datatypes which are given below:
Data Description
Type
Vidya Lunge 48
DMS 22319 unit 2
Vidya Lunge 49
DMS 22319 unit 2
Data Description
type
Vidya Lunge 50
DMS 22319 unit 2
Datatype Description
timestamp It stores the year, month, day, hour, minute, and the second value.
SQL Commands
● SQL commands are instructions.
● It is used to communicate with the database.
● It is also used to perform specific tasks, functions, and queries of data.
SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
Types of SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
DBMS SQL command
1. Data Definition Language (DDL)
Vidya Lunge 51
DMS 22319 unit 2
● DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
● All the command of DDL are auto-committed that means it permanently save all
the changes in the database.
● Here are some commands that come under DDL:
CREATE
ALTER
DROP
TRUNCATE
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example:
CREATE TABLE EMPLOYEE
(
Name VARCHAR2(20),
Email VARCHAR2(100),
DOB DATE
);
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
DROP TABLE table_name;
Example
DROP TABLE EMPLOYEE;
Vidya Lunge 52
DMS 22319 unit 2
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;
EXAMPLE
ALTER TABLE STU_DETAILS
ADD
(
ADDRESS VARCHAR2(20)
);
d. TRUNCATE:
It is used to delete all the rows from the table and free the space containing the
table.
Syntax:
TRUNCATE TABLE table_name;
Vidya Lunge 53
DMS 22319 unit 2
Example:
TRUNCATE TABLE EMPLOYEE;
INSERT
UPDATE
DELETE
Syntax:
INSERT INTO TABLE_NAME
(
col1, col2, col3,.... col N)
VALUES
(value1, value2, value3, .... valueN)
);
Details:
Vidya Lunge 54
DMS 22319 unit 2
Or
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3, .... valueN);
For example:
INSERT INTO javatpoint
(Author, Subject)
VALUES
("Sonoo", "DBMS");
b. UPDATE:
This command is used to update or modify the value of a column in the table.
Syntax:
UPDATE table_name
SET
[column_name1= value1,...column_nameN = valueN]
[WHERE CONDITION]
For example:
UPDATE students
SET
User_Name = 'Sonoo'
Vidya Lunge 55
DMS 22319 unit 2
Update p3
Set
Program code=’CO3I’;
Update CO_dept
Set
Program code=’CO3I
Where
Program code=’CO2I’’;
Syntax:
DELETE FROM table_name [WHERE condition];
Or
DELETE FROM table_name;
DESC tablename;
example:
DELETE FROM javatpoint
WHERE
Author="Sonoo";
Vidya Lunge 56
DMS 22319 unit 2
Grant
Revoke
Example
GRANT SELECT, UPDATE ON MY_TABLE
TO
USER1, USER2;
Example
REVOKE SELECT, UPDATE ON MY_TABLE
FROM
USER1, USER2;
Vidya Lunge 57
DMS 22319 unit 2
● COMMIT
● ROLLBACK
● SAVEPOINT
a. Commit:
Commit command is used to save all the transactions to the database.
Syntax:
COMMIT;
Example:
DELETE FROM CUSTOMERS
WHERE
AGE = 25;
COMMIT;
b. Rollback:
Rollback command is used to undo transactions that have not already been saved to
the database.
Syntax:
ROLLBACK;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;
Vidya Lunge 58
DMS 22319 unit 2
c. SAVEPOINT:
It is used to roll the transaction back to a certain point without rolling back the
entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
SELECT
a. SELECT: This is the same as the projection operation of relational algebra. It is used
to select the attribute based on the condition described by WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
For example:
SELECT emp_name
FROM employee ;
SELECT emp_name
FROM employee
WHERE age > 20;
Vidya Lunge 59
DMS 22319 unit 2
Syntax
);
SQL Constraints:
● SQL constraints are used to specify rules for the data in a table.
● Constraints 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 table.
● If there is any violation between the constraint and the data action, the action is
aborted.
● Constraints can be column level or table level.
● Column level constraints apply to a column
● Table level constraints apply to the whole table.
● P10(rollno,name,age,%of1year)
Vidya Lunge 60
DMS 22319 unit 2
The following SQL ensures that the "ID", "LastName", and "FirstName" columns will
NOT accept NULL values when the "Persons" table is created:
Example:
CREATE TABLE Persons
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
● The UNIQUE constraint ensures that all values in a column are different.
● Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
● A PRIMARY KEY constraint automatically has a UNIQUE constraint.
● However, you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
Vidya Lunge 61
DMS 22319 unit 2
The following SQL creates a UNIQUE constraint on the "ID" column when the
"Persons" table is created:
To create a UNIQUE constraint on the "ID" column when the table is already created,
use the following SQL:
EX:
ALTER TABLE Persons
Vidya Lunge 62
DMS 22319 unit 2
Ex:
ALTER TABLE Persons
ADD
CONSTRAINT UC_Person UNIQUE (ID,LastName);
3)PRIMARY KEY -
● A combination of a NOT NULL and UNIQUE.
● Uniquely identifies each row in a table.
● SQL PRIMARY KEY Constraint
● 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; and in the table, this primary key can
consist of single or multiple columns (fields).
● class_CO((enroll,roll,name ,location,mobile)
● P1:(enroll)
● P2:(enroll,roll)
Vidya Lunge 63
DMS 22319 unit 2
● p3:(enroll,roll,name)
●
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons"
table is created:
MySQL:
);
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
Vidya Lunge 64
DMS 22319 unit 2
Note: In the example above there is only ONE PRIMARY KEY (PK_Person).
However, the VALUE of the primary key is made up of TWO COLUMNS (ID +
LastName).
4)Foreign KEY - Prevents actions that would destroy links between tables.
Dept(parent)
Dept no dept name(primary key) dept location
1 co 2floor
2 ej 3floor
3 me
Society(child)
Vidya Lunge 65
DMS 22319 unit 2
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
A 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.
Persons Table
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20
Orders Table
1 77895 3
2 44678 3
3 22456 2
Vidya Lunge 66
DMS 22319 unit 2
4 24562 1
Notice that the "PersonID" column in the "Orders" table points to the "PersonID"
column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons"
table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint prevents invalid data from being inserted into the
foreign key column, because it has to be one of the values contained in the parent table
The following SQL creates a FOREIGN KEY on the "PersonID" column when the
"Orders" table is created:
MySQL:
Vidya Lunge 67
DMS 22319 unit 2
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders"
table is already created, use the following SQL:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
Vidya Lunge 68
DMS 22319 unit 2
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.
The following SQL creates a CHECK constraint on the "Age" column when the
"Persons" table is created. The CHECK constraint ensures that the age of a person must
be 18, or older:
Vidya Lunge 69
DMS 22319 unit 2
MySQL:
Vidya Lunge 70
DMS 22319 unit 2
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);
To create a CHECK constraint on the "Age" column when the table is already created,
use the following SQL:
MySQL:
Vidya Lunge 71
DMS 22319 unit 2
=================================================
Integrity Constraints
● Integrity constraints ensure that the data insertion, updating, and other processes
have to be performed in such a way that data integrity is not affected.
● UID=DML
Vidya Lunge 72
DMS 22319 unit 2
1. Domain constraints
● Domain constraints can be defined as the definition of a valid set of values for an
attribute.
● The data type of domain includes string, character, integer, time, date, currency,
etc. The value of the attribute must be available in the corresponding domain.
Example:
● The entity integrity constraint states that primary key value can't be null.
● This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those rows.
● A table can contain a null value other than the primary key field.
Example:
Vidya Lunge 73
DMS 22319 unit 2
Example:
Vidya Lunge 74
DMS 22319 unit 2
4. Key constraints
● Keys are the entity set that is used to identify an entity within its entity set
uniquely.
● An entity set can have multiple keys, but out of which one key will be the primary
key. A primary key can contain a unique and null value in the relational table.
Example:
Vidya Lunge 75
DMS 22319 unit 2
Types of Operator
Following are the various arithmetic operators performed on the SQL data:
Vidya Lunge 76
DMS 22319 unit 2
● The Addition Operator in SQL performs the addition on the numerical data of
the database table.
● In SQL, we can easily add the numerical values of two columns of the same table
by specifying both the column names as the first and second operand.
● We can also add the numbers to the existing numbers of the specific column.
This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.
● Suppose, we want to add 20,000 to the salary of each employee specified in the
table. Then, we have to write the following query in the SQL:
Vidya Lunge 77
DMS 22319 unit 2
o/p:
Emp_New_Salary
45000
50000
In this query, we have performed the SQL addition operation on the single column of the
given table.
● Suppose, we want to add the Salary and monthly bonus columns of the above
table, then we have to write the following query in SQL:
1. SELECT Emp_Salary + Emp_Monthlybonus as Emp_Total_Salary FROM
Employee_details;
In this query, we have added two columns with each other of the above table.
The Subtraction Operator in SQL performs the subtraction on the numerical data of the
database table. In SQL, we can easily subtract the numerical values of two columns of
the same table by specifying both the column names as the first and second operand. We
can also subtract the number from the existing number of the specific table column.
Vidya Lunge 78
DMS 22319 unit 2
This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.
● Suppose we want to subtract 5,000 from the salary of each employee given in the
Employee_details table. Then, we have to write the following query in the SQL:
In this query, we have performed the SQL subtraction operation on the single column of
the given table.
● If we want to subtract the penalty from the salary of each employee, then we have
to write the following query in SQL:
1. SELECT Emp_Salary - Tax as Emp_Total_Salary
2. FROM
3. Employee_details;
The Multiplication Operator in SQL performs the Multiplication on the numerical data
of the database table.
In SQL, we can easily multiply the numerical values of two columns of the same table
by specifying both the column names as the first and second operand.
Vidya Lunge 79
DMS 22319 unit 2
This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_Monthlybonus.
In this query, we have performed the SQL multiplication operation on the single column
of the given table.
Vidya Lunge 80
DMS 22319 unit 2
In this query, we have multiplied the values of two columns by using the WHERE
clause.
The Division Operator in SQL divides the operand on the left side by the operand on the
right side.
In SQL, we can also divide the numerical values of one column by another column of
the same table by specifying both column names as the first and second operand.
We can also perform the division operation on the stored numbers in the column of the
SQL table.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Vidya Lunge 81
DMS 22319 unit 2
In this query, we have performed the SQL division operation on the single column of the
given table.
The Modulus Operator in SQL provides the remainder when the operand on the left side
is divided by the operand on the right side.
This example consists of a Division table, which has three columns Number,
First_operand, and Second_operand.
1 56 4
2 32 8
3 89 9
4 18 10
5 10 5
Vidya Lunge 82
DMS 22319 unit 2
The Comparison Operators in SQL compare two different data of SQL table and check
whether they are the same, greater, and lesser.
The SQL comparison operators are used with the WHERE clause in the SQL queries
Following are the various comparison operators which are performed on the data
stored in the SQL database tables:
This operator is highly used in SQL queries. The Equal Operator in SQL shows only
data that matches the specified value in the query.
This operator returns TRUE records from the database table if the value of both
operands specified in the query is matched.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Vidya Lunge 83
DMS 22319 unit 2
● Suppose, we want to access all the records of those employees from the
Employee_details table whose salary is 30000. Then, we have to write the
following query in the SQL database:
1.
2. SELECT * FROM Employee_details
3. WHERE Emp_Salary = 30000;
In this example, we used the SQL equal operator with WHERE clause for getting the
records of those employees whose salary is 30000.
The Equal Not Operator in SQL shows only those data that do not match the query's
specified value.
This operator returns those records or rows from the database views and tables if the
value of both operands specified in the query is not matched with each other.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Vidya Lunge 84
DMS 22319 unit 2
● Suppose, we want to access all the records of those employees from the
Employee_details table whose salary is not 45000. Then, we have to write the
following query in the SQL database:
1. SELECT * FROM Employee_details
2. WHERE Emp_Salary != 45000;
In this example, we used the SQL equal not operator with WHERE clause for getting the
records of those employees whose salary is not 45000.
The Greater Than Operator in SQL shows only those data which are greater than the
value of the right-hand operand.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Vidya Lunge 85
DMS 22319 unit 2
● Suppose, we want to access all the records of those employees from the
Employee_details table whose employee id is greater than 202. Then, we have to
write the following query in the SQL database:
1. SELECT * FROM Employee_details
2. WHERE Emp_Id > 202;
Here, SQL greater than operator displays the records of those employees from the above
table whose Employee Id is greater than 202.
The Greater Than Equals to Operator in SQL shows those data from the table which
are greater than and equal to the value of the right-hand operand.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Vidya Lunge 86
DMS 22319 unit 2
● Suppose, we want to access all the records of those employees from the
Employee_details table whose employee id is greater than and equals to 202. For
this, we have to write the following query in the SQL database:
1. SELECT * FROM Employee_details
2. WHERE Emp_Id >= 202;
Here,'SQL greater than equals to operator' with WHERE clause displays the rows of
those employees from the table whose Employee Id is greater than and equals to 202.
The Less Than Operator in SQL shows only those data from the database tables which
are less than the value of the right-side operand.
This comparison operator checks that the left side operand is lesser than the right side
operand. If the condition becomes true, then this operator in SQL displays the data
which is less than the value of the right-side operand.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Vidya Lunge 87
DMS 22319 unit 2
● Suppose, we want to access all the records of those employees from the
Employee_details table whose employee id is less than 204. For this, we have to
write the following query in the SQL database:
1. SELECT * FROM Employee_details
2. WHERE Emp_Id < 204;
Here,SQL less than operator with WHERE clause displays the records of those
employees from the above table whose Employee Id is less than 204.
The Less Than Equals to Operator in SQL shows those data from the table which are
lesser and equal to the value of the right-side operand.
This comparison operator checks that the left side operand is lesser and equal to the right
side operand.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, and Emp_Salary.
Vidya Lunge 88
DMS 22319 unit 2
● Suppose, we want to access all the records of those employees from the
Employee_details table whose employee id is less and equals 203. For this, we
have to write the following query in the SQL database:
1. SELECT * FROM Employee_details
2. WHERE Emp_Id <= 203;
Here, SQL less than equals to the operator with WHERE clause displays the rows of
those employees from the table whose Employee Id is less than and equals 202.
The Logical Operators in SQL perform the Boolean operations, which give two results
True and False. These operators provide True value if both operands match the logical
condition.
Following are the various logical operators which are performed on the data stored
in the SQL database tables:
The ALL operator in SQL compares the specified value to all the values of a column
from the sub-query in the SQL database.
Vidya Lunge 89
DMS 22319 unit 2
1. SELECT,
2. HAVING, and
3. WHERE.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Vidya Lunge 90
DMS 22319 unit 2
Here, we used the SQL ALL operator with greater than the operator.
The AND operator in SQL would show the record from the database table if all the
conditions separated by the AND operator evaluated to True. It is also known as the
conjunctive operator and is used with the WHERE clause.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
● Suppose, we want to access all the records of those employees from the
Employee_details table whose salary is 25000 and the city is Delhi. For this, we
have to write the following query in SQL:
Vidya Lunge 91
DMS 22319 unit 2
● Here,SQL AND operator with WHERE clause shows the record of employees
whose salary is 25000 and the city is Delhi.
SQL OR Operator
The OR operator in SQL shows the record from the table if any of the conditions
separated by the OR operator evaluates to True. It is also known as the conjunctive
operator and is used with the WHERE clause.
Syntax of OR operator:
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Vidya Lunge 92
DMS 22319 unit 2
Here, SQL OR operator with WHERE clause shows the record of employees whose
salary is 25000 or the city is Delhi.
The BETWEEN operator in SQL shows the record within the range mentioned in the
SQL query. This operator operates on the numbers, characters, and date/time operands.
If there is no value in the given range, then this operator shows NULL value.
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Vidya Lunge 93
DMS 22319 unit 2
● Suppose, we want to access all the information of those employees from the
Employee_details table who is having salaries between 20000 and 40000. For
this, we have to write the following query in SQL:
1. SELECT * FROM Employee_details WHERE Emp_Salary BETWEEN 30000
AND 45000;
Here, we used the SQL BETWEEN operator with the Emp_Salary field.
SQL IN Operator
The IN operator in SQL allows database users to specify two or more values in a
WHERE clause. This logical operator minimizes the requirement of multiple OR
conditions.
This operator makes the query easier to learn and understand. This operator returns those
rows whose values match with any value of the given list.
Syntax of IN operator:
This example consists of an Employee_details table, which has three columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Vidya Lunge 94
DMS 22319 unit 2
● Suppose, we want to show all the information of those employees from the
Employee_details table whose Employee Id is 202, 204, and 205. For this, we
have to write the following query in SQL:
1. SELECT * FROM Employee_details WHERE Emp_Id IN (202, 204, 205);
● Suppose, we want to show all the information of those employees from the
Employee_details table whose Employee Id is not equal to 202 and 205. For this,
we have to write the following query in SQL:
1. SELECT * FROM Employee_details WHERE Emp_Id NOT IN (202,205);
2.
Here, we used the SQL NOT IN operator with the Emp_Id column.
The NOT operator in SQL shows the record from the table if the condition evaluates to
false. It is always used with the WHERE clause.
Vidya Lunge 95
DMS 22319 unit 2
This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
● Suppose, we want to show all the information of those employees from the
Employee_details table whose Cityis not Delhi. For this, we have to write the
following query in SQL:
1. SELECT * FROM Employee_details WHERE NOT Emp_City = 'Delhi' ;
In this example, we used the SQL NOT operator with the Emp_City column.
● Suppose, we want to show all the information of those employees from the
Employee_details table whose Cityis not Delhi and Chandigarh. For this, we
have to write the following query in SQL:
1. SELECT * FROM Employee_details WHERE NOT Emp_City = 'Delhi' AND
NOT Emp_City = 'Chandigarh';
In this example, we used the SQL NOT operator with the Emp_City column.
Vidya Lunge 96
DMS 22319 unit 2
The ANY operator in SQL shows the records when any of the values returned by the
sub-query meet the condition.
The ANY logical operator must match at least one record in the inner query and must be
preceded by any SQL comparison operator.
The LIKE operator in SQL shows those records from the table which match with the
given pattern specified in the sub-query.
The percentage (%) sign is a wildcard which is used in conjunction with this logical
operator.
This operator is used in the WHERE clause with the following three statements:
1. SELECT statement
2. UPDATE statement
3. DELETE statement
This example consists of an Employee_details table, which has four columns Emp_Id,
Emp_Name, Emp_Salary, and Emp_City.
Vidya Lunge 97
DMS 22319 unit 2
In this example, we used the SQL LIKE operator with Emp_Name column because we
want to access the record of those employees whose name starts with s.
++++++++++++++++++++++++++++++++++++++++++++
Vidya Lunge 98