18BCS42C U3
18BCS42C U3
18BCS42C U3
Sc Computer Science
18BCS42C – DATABASE MANAGEMENT SYSTEM
UNIT-III
RELATIONAL MODEL: Introduction to Relational Data Model – Basic
concepts – Enforcing data Integrity constraints – Relational Algebra Operations –
Extended Relational Algebra Operations.
RELATIONAL DATABASE MANIPULATION: Introduction to Structured
Query Language (SQL) – SQL Commands for defining Database, Constructing
database, Manipulations on database – Basic data retrieval operations – Advanced
Queries in SQL – Aggregation –Updates in SQL.
Relational Model
Introduction to Relational Data Model
Relational data model is the primary data model, which is used widely
around the world for data storage and processing.
This model is simple and it has all the properties and capabilities required to
process data with storage efficiency.
Concepts
Tables − In relational data model, relations are saved in the format of
Tables. This format stores the relation among entities. A table has rows and
columns, where rows represent records and columns represent the
attributes.
Tuple − A single row of a table, which contains a single record for that
relation is called a tuple.
Relation instance − A finite set of tuples in the relational database system
represents relation instance. Relation instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table
name), attributes, and their names.
Relation key − Each row has one or more attributes, known as relation key,
which can identify the row in the relation (table) uniquely.
Attribute domain − Every attribute has some pre-defined value scope,
known as attribute domain.
Constraints
Every relation has some conditions that must hold for it to be a valid
relation.
These conditions are called Relational Integrity Constraints.
There are three main integrity constraints −
Key constraints
Domain constraints
Referential integrity constraints
Key Constraints
There must be at least one minimal subset of attributes in the relation,
which can identify a tuple uniquely.
This minimal subset of attributes is called key for that relation.
If there are more than one such minimal subsets, these are called candidate
keys.
Key constraints force that –
o in a relation with a key attribute, no two tuples can have identical
values for key attributes.
o a key attribute can not have NULL values.
o Key constraints are also referred to as Entity Constraints.
Domain Constraints
Attributes have specific values in real-world scenario.
For example, age can only be a positive integer.
The same constraints have been tried to employ on the attributes of a
relation.
Every attribute is bound to have a specific range of values.
For example, age cannot be less than zero and telephone numbers cannot
contain a digit outside 0-9.
Referential integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys.
A foreign key is a key attribute of a relation that can be referred in other
relation.
Referential integrity constraint states that if a relation refers to a key
attribute of a different or same relation, then that key element must exist.
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.
Thus, integrity constraint is used to guard against accidental damage to the
database.
Types of Integrity Constraint
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:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set
uniquely.
o An entity set can have multiple keys, but out of which one key will be the
primary key.
o A primary key can contain a unique and null value in the relational table.
Example:
RELATIONAL ALGEBRA
Relational algebra is a procedural query language.
It gives a step by step process to obtain the result of the query.
It uses operators to perform queries.
Types of Relational operation
1. Select Operation:
The select operation selects tuples that satisfy a given predicate.
It is denoted by sigma (σ).
Notation: σ p(r)
Where:
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like:
AND OR and NOT. These relational can use as relational operators like =,
≠, ≥, <, >, ≤.
For example: LOAN Relation
BRANCH_NAME LOAN_NO AMOUNT
Downtown L-17 1000
Redwood L-23 2000
Perryride L-15 1500
Downtown L-14 1500
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300
Input:
σ BRANCH_NAME="perryride" (LOAN)
Output:
BRANCH_NAME LOAN_NO AMOUNT
Perryride L-15 1500
Perryride L-16 1300
2. Project Operation:
This operation shows the list of those attributes that we wish to appear in the
result.
Rest of the attributes will be eliminated from the table.
It is denoted by ∏.
Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Example: CUSTOMER RELATION
NAME STREET CITY
Jones Main Harrison
Smith North Rye
Hays Main Harrison
Curry North Rye
Johnson Alma Brooklyn
Brooks Senator Brooklyn
Input:
∏ NAME, CITY (CUSTOMER)
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
Suppose there are two tuples R and S. The union operation contains all the
tuples that are either in R or S or both in R & S.
It eliminates the duplicate tuples. It is denoted by ∪.
Notation: R ∪ S
A union operation must hold the following condition:
o R and S must have the attribute of the same number.
o Duplicate tuples are eliminated automatically.
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME ACCOUNT_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Johnson
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.
It is denoted by intersection ∩.
Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in R but not in S.
It is denoted by intersection minus (-).
Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Jackson
Hayes
Williams
Curry
6. Cartesian product
The Cartesian product is used to combine each row in one table with each
row in the other table. It is also known as a cross product.
It is denoted by X.
Notation: E X D
Example:
EMPLOYEE
EMP_ID EMP_NAME EMP_DEPT
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
EMPLOYEE X DEPARTMENT
Output:
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry B C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation.
It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to
STUDENT1.
ρ(STUDENT1, STUDENT)
SQL Commands
o SQL commands are instructions.
o It is used to communicate with the database.
o It is also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop
the table, modify the table, set permission for users.
a. CREATE:
It is used to create a new table in the database.
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPE
S[,....]);
Example:
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VA
RCHAR2(100), DOB DATE);
b. DROP:
It is used to delete both the structure and record stored in the table.
Syntax
DROP TABLE ;
Example
DROP TABLE EMPLOYEE;
c. ALTER:
It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new
attribute.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-
definition;
To modify existing column in the table:
ALTER TABLE MODIFY(COLUMN DEFINITION....);
EXAMPLE
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20)
);
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(2
0));
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;
Example:
TRUNCATE TABLE EMPLOYEE;
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_name
N = valueN] [WHERE CONDITION]
For example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
c. DELETE:
It is used to remove one or more row from a table.
Syntax:
DELETE FROM table_name [WHERE condition];
For example:
DELETE FROM javatpoint
WHERE Author="Sonoo";
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;
c. SAVEPOINT:
It is used to roll the transaction back to a certain point without rolling
back the entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language(DQL)
DQL is used to fetch the data from the database.
It uses only one command:
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
WHERE age > 20;
AGGREGATION
In aggregation, the relation between two entities is treated as a single entity.
In aggregation, relationship with its corresponding entities is aggregated
into a higher level entity.
DBMS Aggregration
Aggregation is a process in which a single entity alone is not able to make
sense in a relationship so the relationship of two entities acts as one entity.
Aggregration Example
In real world, we know that a manager not only manages the employee
working under them but he has to manage the project as well.
In such scenario if entity “Manager” makes a “manages” relationship with
either “Employee” or “Project” entity alone then it will not make any sense
because he has to manage both.
In these cases the relationship of two entities acts as one entity.
In our example, the relationship “Works-On” between “Employee” &
“Project” acts as one entity that has a relationship “Manages” with the entity
“Manager”.
Aggregation in DBMS
Aggregation refers to the process by which entities are combined to form a
single meaningful entity.
The specific entities are combined because they do not make sense on their
own.
To establish a single entity, aggregation creates a relationship that combines
these entities.
The resulting entity makes sense because it enables the system to function
well.
When using data in the form of numerical values, the following operations
can be used to perform DBMS aggregation:
Average (AVG): This function provides the mean or average of the
data values.
Sum: This provides a total value after the data values have been
added.
Count: This provides the number of records.
Maximum (Max): This function provides the maximum value of a
given set of data.
Minimum (Min): This provides the minimum value of a given set of
data.
Standard deviation (std dev): This provides the dispersion or
variation of the sets of data.
The following are the main types of relationships in an ER model:
One-to-one: Here, the trivial entity forms a relationship with only one other
entity. For example, one employee can work in only one department of an
organization.
One-to-many: In this relationship, one entity forms a relationship with
multiple entities. For example, an employee can work in multiple
departments within the same organization.
Many-to-one: Here, multiple entities in a certain entity set can form a
relationship with only one entity. For example, many employees can work in
only one department.
Many-to-many: In this category, multiple entities from a certain entity set,
that can form a relationship with many entities from another entity set. For
example, many employees can work in multiple departments within the
same organization.
The following diagram shows a simple ER model that can be used to explain the
process flow for aggregation in DBMS.
UPDATE Table
The following SQL statement updates the first customer (CustomerID = 1)
with a new contact person and a new city.
Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The selection from the "Customers" table will now look like this:
Customer Customer Contact Address City Postal Country
ID Name Name Code
1 Alfreds Alfred Obere Frankfurt 12209 Germany
Futterkiste Schmidt Str.57
2 Ana Ana Trujillo Avda. México 05021 Mexico
Trujillo de la D.F.
Empare Constitu
dados y ción
helados 2222
3 Antonio Antonio Matader México 05023 Mexico
Moreno Moreno os 2312 D.F.
Taquería
4 Around the Thomas 120 London WA1 UK
Horn Hardy Hanover 1DP
Sq.
5 Berglunds Christina Berguvs Luleå S-958 Sweden
snabbköp Berglund vägen 8 22