21aim45a-Dbms Module-2
21aim45a-Dbms Module-2
21aim45a-Dbms Module-2
SYSTEMS (DBMS)
MODULE -II
21AIM45A
MODULE –II : SYLLABUS
1.Domain Constraints
2.Key Constraints
3.Referential Integrity Constraints
4.The Entity integrity constraint
Domain Constraints
Domain constraints can be violated if an attribute value is
not appearing in the corresponding domain or it is not of the
appropriate data type.
Domain constraints specify that within each tuple, and
the value of each attribute must be unique. This is
specified as data types which include standard data types
integers, real numbers, characters, Booleans, variable
length strings, etc.
Constraints in DBMS are the restrictions that are applied to
data or operations on the data. This means that constraints
allow only a particular kind of data to be inserted in the
database or only some particular kind of operations to be
performed on the data in the database.
Thus, constraints ensure the correctness of data in a
Database Management System (DBMS).
Consider the following example relations.
Student Student Marks
ID Name (in %) So, we can say that this is a valid table.
This is because the student ID attribute
1 Guneet 90 can have only integers as ID and it does
have only integers as ID. Also, the names
2 Ahan 92 can be strings only and the marks can be
integers or floating values only. So, every
3 Yash 87 attribute for every tuple in this table has
its values within its domain.
4 Lavish 90
5 Ashish 79
Example:
In the given table, CustomerID is a key attribute of
Customer Table. It is most likely to have a single key for one
customer, CustomerID =1 is only for the
CustomerName =” Google”.
Keys
•Keys play an important role in the relational database.
•It is used to uniquely identify any record or row of data from the
table. It is also used to establish and identify relationships between
tables.
Types of keys:
1. Primary key
•It is the first key used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys, as we saw in the PERSON
table. The key which is most suitable from those lists becomes a primary key.
•In the EMPLOYEE table, ID can be the primary key since it is unique for
each employee. In the EMPLOYEE table, we can even select
License_Number and Passport_Number as primary keys since they are also
unique.
•For each entity, the primary key selection is based on requirements and
developers.
2. Candidate key
•A candidate key is an attribute or set of attributes that
can uniquely identify a tuple.
•Except for the primary key, the remaining attributes are
considered a candidate key. The candidate keys are as
strong as the primary key.
For example: In the EMPLOYEE table, id is best
suited for the primary key. The rest of the attributes, like
SSN, Passport_Number, License_Number, etc., are
considered a candidate key.
3. Foreign key
•Foreign keys are the column of the table used to point to the
primary key of another table.
•Every employee works in a specific department in a company,
and employee and department are two different entities. So we
can't store the department's information in the employee table.
That's why we link these two tables through the primary key of
one table.
•We add the primary key of the DEPARTMENT table,
Department_Id, as a new attribute in the EMPLOYEE table.
•In the EMPLOYEE table, Department_Id is the foreign key,
and both the tables are related.
Department
In the given
example, we have 2
relations, Customer
and Billing.
Tuple for
CustomerID =1 is
referenced twice in
the relation Billing.
So we know
CustomerName=Go
ogle has billing
amount $300
EXAMPLE 2:
The Entity integrity constraint
The entity integrity constraint states that no primary key value can be NULL. This is because
the primary key value is used to identify individual tuples in a relation. Having NULL values for the primary
key implies that we cannot identify some tuples. For example, if two or more tuples had NULL for their
primary keys, we may not be able to distinguish them if we try to reference them from other relations.
Key constraints and entity integrity constraints are specified on individual relations.
The referential integrity constraint:
The referential integrity constraint is specified between two relations and is used to
maintain the consistency among tuples in the two relations.
Informally, the referential integrity constraint states that a tuple in one relation that
refers to another relation must refer to an existing tuple in that relation.
Relational Database Schemas
A relational database usually contains many relations,
with tuples in relations that are related in various ways. In this
section, we define a relational database and a relational
database schema.
Data Manipulation
Language
•DML commands are
used to modify the
database. It is
responsible for all form
of changes in the
database.
•The command of DML
is not auto-committed
that means it can't
permanently save all the
changes in the database.
They can be rollback.
DCL - Data Control Language
XML data type This data type is used to store XML data.
ALTER TABLE
changes the
structure of a table.
Here is how you
would add a 2. To modify existing column in the
column to a table:
database:
ALTER TABLE table_name MO
DIFY
(column_definitions....);
Example
Syntax
CREATE TABLE table_name
(
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL Constraints
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 .
..;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Examples:
Let us see each of the SET operators in more detail with the help of
examples. Consider we have the following tables with the given data.
Table 1: t_employees Table 2: t2_employees
I Name Department Salar Year_
ID Name Department Salary Year_ D y of_Ex
of_Ex perien
perien ce
ce 1 Prashant Wagh R&D 49000 1
1 Aakash Singh Development 72000 2 2 Abhishek Pawar Production 45000 1
2 Abhishek Pawar Production 45000 1
3 Gautam Jain Development 56000 4
3 Pranav Deshmukh HR 59900 3
4 Shubham Accounts 57000 2
4 Shubham Mahale Accounts 57000 2
Mahale
5 Sunil Kulkarni Development 87000 3 5 Rahul Thakur Production 76000 4
6 Bhushan Wagh R&D 75000 2 6 Bhushan Wagh R&D 75000 2
7 Paras Jaiswal Marketing 32000 1 7 Anand Singh Marketing 28000 1
1. UNION:
•UNION will be used to combine the result of two select
statements.
•Duplicate rows will be eliminated from the results obtained
after performing the UNION operation.
Example 1:
Write a query to perform union between the table t_employees
and the table t2_employees.
Min():