DBMS Unit-2
DBMS Unit-2
UNIT-II
Relational Model:
The relational model represents how data is stored in Relational Databases. A relational
database consists of a collection of tables each of which is assigned a unique name. Consider
a relation STUDENT with attributes ROLL_NO, NAME, ADDRESS, PHONE, and AGE
shown in the table.
Table STUDENT:
• Relation Schema: A relation schema defines the structure of the relation and
represents the name of the relation with its attributes. e.g. STUDENT (ROLL_NO,
NAME, ADDRESS, PHONE, and AGE) is the relation schema for STUDENT. If a
schema has more than 1 relation it is called Relational Schema.
• Tuple: Each row in the relation is known as a tuple. The above relation contains
4 tuples one of which is shown as:
• Column: The column represents the set of values for a particular attribute.
The column ROLL_NO is extracted from the relation STUDENT.
• NULL Values: The value which is not known or unavailable is called a NULL
value. It is represented by NULL. e.g. PHONE of STUDENT having ROLL_NO 4 is
NULL.
• Relation Key: These are basically the keys that are used to identify the rows
uniquely or also help in identifying tables. These are of the following types:
1. Primary Key
2. Foreign Key
It is essential to understand that a NULL value differs from a zero or an empty string. A
NULL value represents missing or undefined data. Since it is often not possible to
determine which interpretation applies, SQL treats all NULL values as distinct and does not
distinguish between them. Typically, it can have one of three interpretations:
Constraints in DBMS-
Integrity Constraints:
• Integrity constraints are a set of rules. It is used to maintain the quality of information.
• 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.
• They ensure the correctness or consistency of data in the database.
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:
Example:
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:
Relational Algebra:
Relational algebra is a procedural query language that works on relational model. The
purpose of a query language is to retrieve data from database or perform various operations
such as insert, update, and delete on the data.
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Derived Operations:
3. Intersection (∩)
Select Operator is denoted by sigma (σ) and it is used to find the tuples (or rows) in a
relation (or table) which satisfy the given condition.
σ Condition/Predicate(Relation/Table name)
Select Operator (σ) Example
Table: CUSTOMER
Query:
Output:
Project operator is denoted by ∏ symbol and it is used to select desired columns (or
attributes) from a table (or relation).
In this example, we have a table CUSTOMER with three columns, we want to fetch only two
columns of the table, which we can do with the help of Project Operator ∏.
Table: CUSTOMER
Query:
Output:
Customer_Name Customer_City
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi
Union operator is denoted by ∪ symbol and it is used to select all the rows (tuples) from two
tables (relations).
table_name1 ∪ table_name2
Union Operator (∪) Example
Table 1: COURSE
Table 2: STUDENT
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:
Output:
Student Name
Aditya
Carl
Paul
Lucy
Rick
Steve
Note: As you can see there are no duplicate names present in the output even though we had
few common names in both the tables, also in the COURSE table we had the duplicate
name itself.
Intersection operator is denoted by ∩ symbol and it is used to select common rows (tuples)
from two tables (relations).
Lets say we have two relations R1 and R2 both have same columns and we want to select all
those tuples(rows) that are present in both the relations, then in that case we can apply
intersection operation on these two relations R1 ∩ R2.
Note: Only those rows that are present in both the tables will appear in the result set.
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Table 1: COURSE
Table 2: STUDENT
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:
Output:
Student_Name
Aditya
Steve
Paul
Lucy
Set Difference is denoted by – symbol. Lets say we have two relations R1 and R2 and we
want to select all those tuples(rows) that are present in Relation R1 but not present in
Relation R2, this can be done using Set difference R1 – R2.
table_name1 - table_name2
Set Difference (-) Example
∏ Student_Name (STUDENT) - ∏ Student_Name (COURSE)
Output:
Student_Name
Carl
Rick
Cartesian Product is denoted by X symbol. Lets say we have two relations R1 and R2 then
the cartesian product of these two relations (R1 X R2) would combine each tuple of first
relation R1 with the each tuple of second relation R2. I know it sounds confusing but once
we take an example of this, you will be able to understand this.
R1 X R2
Table 1: R
Col_A Col_B
AA 100
BB 200
CC 300
Table 2: S
Col_X Col_Y
----- -----
XX 99
YY 11
ZZ 101
Query:
Let’s find the Cartesian product of table R and S.
R X S
Output:
AA 100 XX 99
AA 100 YY 11
AA 100 ZZ 101
BB 200 XX 99
BB 200 YY 11
BB 200 ZZ 101
CC 300 XX 99
CC 300 YY 11
CC 300 ZZ 101
Note: The number of rows in the output will always be the cross product of number of rows
in each table. In our example table 1 has 3 rows and table 2 has 3 rows so the output has 3×3
= 9 rows.
Rename (ρ)
Lets say we have a table customer, we are fetching customer names and we are renaming the
resulted relation to CUST_NAMES.
Table: CUSTOMER
Query:
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
Steve
Raghu
Chaitanya
Ajeet
Carl
Join Operations:
JOIN operation also allows joining variously related tuples from different relations.
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
• INNER JOIN: Returns records that have matching values in both tables
• LEFT OUTER JOIN: Returns all records from the left table, and the
matched records from the right table
• RIGHT OUTER JOIN: Returns all records from the right table, and the
matched records from the left table
• FULL OUTER JOIN: Returns all records when there is a match in either left or
right table
Relational Calculus:
Relational calculus is a non-procedural query language that tells the system what data to be
retrieved but doesn’t tell how to retrieve it.
Tuple relational calculus is used for selecting those tuples that satisfy the given condition.
Table: Student
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Query to display the last name of those students where age is greater than 30
In the above query you can see two parts separated by | symbol. The second part is where we
define the condition and in the first part we specify the fields which we want to display for
the selected tuples.
Last_Name
Singh
In domain relational calculus the records are filtered based on the domains.
Again we take the same table to understand how DRC works.
Table: Student
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Query to find the first name and age of students where student age is greater than 27
Output:
First_Name Age
Ajeet 30
Chaitanya 31
Carl 28
Database Schema:
A database schema is the design or structure of a database that defines how data is organized
and how different data elements relate to each other.
• It defines how data is logically organized, including tables, fields, and relationships.
• It outlines the relationships between entities, such as primary and foreign keys.
In simple terms, the schema provides the framework that makes it easier to understand,
manage, and use data in a database. It’s created by database designers to ensure the data is
consistent and efficiently organized.
• A physical schema defines how data is stored in the storage system, including the
arrangement of files, indices and other storage structures. It specifies the actual
code and syntax needed to create the database structure. Essentially, it determines
where and how the data is stored in the physical storage medium.
• The database administrator decides the storage locations and organization of data
within the storage blocks. This schema represents the lowest level of abstraction
• A logical database schema defines the logical structure of the data, including
tables, views, relationships, and integrity constraints. It describes how data is
organized in tables and how the attributes of these tables are connected. The logical
schema ensures that the data is stored in an organized manner, while maintaining
data integrity.
• A database can have multiple view schemas, also known as subschemas, each
providing a different perspective of the data. These schemas describe only a part of
the database.
For creating a schema, the statement “CREATE SCHEMA” is used in every database. But
different databases have different meanings for this.
1. MySQL: In MySQL, we use the “CREATE SCHEMA” statement for creating the
database, because, in MySQL CREATE SCHEMA and CREATE DATABASE, both
statements are similar.
Each column in a database table is required to have a name and a data type.
-9,223,372,036,854,775,808 to
BIGINT Stores larger whole numbers
9,223,372,036,854,775,807
Character data types store all alphabetic values and special characters.
00:00:00.0000000 to
Time Stores time only(no date)
23:59:59:9999999
SQL Table:
Table is a collection of data, organized in terms of rows and columns. In DBMS term, table is
known as relation and row as tuple.
Table is the simple form of data storage. A table is also considered as a convenient
representation of relations.
Employee
EMP_NAME ADDRESS SALARY
Ankit Lucknow 15000
Raman Allahabad 18000
Mike New York 20000
In the above table, "Employee" is the table name, "EMP_NAME", "ADDRESS" and
"SALARY" are the column names. The combination of data of multiple columns forms a row
e.g. "Ankit", "Lucknow" and 15000 are the data of one row.
Syntax:
CREATE TABLE table name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer,
date, etc.).
The following example creates a table called "Persons" that contains five columns: Person
ID, Last Name, First Name, Address, and City:
Example:
CREATE TABLE Persons (
Person ID int,
Last Name varchar (255),
First Name varchar (255),
Address varchar (255),
City varchar (255)
);
The Last Name, First Name, Address, and City columns are of type varchar and will hold
characters, and the maximum length for these fields is 255 characters.
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
Example:
ALTER TABLE Customers
ADD Email varchar (255);
To delete a column in a table, use the following syntax (notice that some database systems
don't allow deleting a column):
The following SQL deletes the "Email" column from the "Customers" table:
Example:
ALTER TABLE Customers
DROP COLUMN Email;
DML Operations:
Example:
Example:
The following SQL statement inserts a new record in the "Customers" table:
The selection from the "Customers" table will now look like this:
Example: Customers
UPDATE Customers
SET Customer Name = Raju, City= 'KKD'
WHERE Customer ID = 1;