0% found this document useful (0 votes)
23 views

Module 2

php

Uploaded by

Yathish Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Module 2

php

Uploaded by

Yathish Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Database

Management System
Module 2

Department of
Master of Computer Applications

www.cambridge.edu.in
Relational Model Concepts
• After designing the conceptual model of the Database using ER Diagram, we
need to convert the conceptual model into a relational model which can be
implemented using any RDBMS language like Oracle SQL, MySQL, etc. So
we will see what the Relational Model is.
• The relational model uses a collection of tables to represent both data and the
relationships among those data.
• Each table has multiple columns, and each column has a unique name. Tables
are also known as relations.
• The relational model is an example of a record-based 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.

Department of Master of Computer


Applications www.cambridge
Relational Model Concepts
Consider a relation STUDENT with attributes ROLL_NO, NAME, ADDRESS,
PHONE, and AGE shown in the table.

Department of Master of Computer


Applications www.cambridge
Important Terminologies

Attribute: Attributes are the properties that define an entity. e.g.; ROLL_NO,
NAME, ADDRESS
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.
Relation Instance: The set of tuples of a relation at a particular instance of time
is called a relation.
Degree: The number of attributes in the relation is known as the degree of the
relation. The STUDENT relation defined above has degree 5.
Cardinality: The number of tuples in a relation is known as cardinality. The
STUDENT relation defined above has cardinality 4.on instance.
Department of Master of Computer
Applications www.cambridge
Relational Model Constraints
In modeling the design of the relational database we can put some restrictions
like what values are allowed to be inserted in the relation, and what kind of
modifications and deletions are allowed in the relation.

In models like Entity-Relationship models, we did not have such features.


Database Constraints can be categorized into 3 main categories:
• Constraints that are applied in the data model are called Implicit Constraints.
• Constraints that are directly applied in the schemas of the data model, by
specifying them in the DDL(Data Definition Language). These are called
Schema-Based Constraints or Explicit Constraints.
• Constraints that cannot be directly applied in the schemas of the data model.
We call these Application-based or Semantic Constraints.
Department of Master of Computer
Applications www.cambridge
Relational Constraints
• These are the restrictions or sets of rules imposed on the database contents. It
validates the quality of the database.
• It validates the various operations like data insertion, updation, and other
processes that have to be performed without affecting the integrity of the data.
• It protects us against threats/damages to the database. Mainly Constraints on
the relational database are of 4 types:

1. Domain constraints
2. Key constraints or Uniqueness Constraints
3. Entity Integrity constraints
4. Referential integrity constraints

Department of Master of Computer


Applications www.cambridge
Relational Constraints

Department of Master of Computer


Applications www.cambridge
Relational Constraints
1. Domain Constraints
• Every domain must contain atomic values(smallest indivisible units) which
means composite and multi-valued attributes are not allowed.
• We perform a datatype check here, which means when we assign a data type
to a column we limit the values that it can contain. Eg. If we assign the
datatype of attribute age as int, we can’t give it values other than int datatype.
• Phone is a multi-values attribute, so it is violating domain constraint.

ID Name Phone

01 Vikash 123456789
234456678
Department of Master of Computer
Applications www.cambridge
Relational Constraints
2. Key Constraints or Uniqueness Constraints
• These are called uniqueness constraints since it ensures that every tuple in the
relation should be unique.

ID Name Phone

01 Vikash 6000000009

02 Amit 9797998798

03 Rahul 9789776768
Department of Master of Computer
Applications www.cambridge
Relational Constraints
3. Entity Integrity Constraints
• Entity Integrity constraints say that no primary key can take a NULL value,
since using the primary key we identify each tuple uniquely in a relation.

ID Name Phone

01 Vikash 6000000009

02 Amit 9797998798

NULL Rahul 9789776768


Department of Master of Computer
Applications www.cambridge
Relational Constraints
4. Referential Integrity Constraints

• The Referential integrity constraint is specified between two relations or


tables and used to maintain the consistency among the tuples in two relations.
• The Referential integrity constraint is specified between two relations or
tables and used to maintain the consistency among the tuples in two relations.
• This constraint is enforced through a foreign key, when an attribute in the
foreign key of relation R1 has the same domain(s) as the primary key of
relation R2, then the foreign key of R1 is said to reference or refer to the
primary key of relation R2.

Department of Master of Computer


Applications www.cambridge
Relational Constraints
• In the above tables, the DNO of Table 1 is the foreign key, and DNO in Table
2 is the primary key. DNO = 22 in the foreign key of Table 1 is not allowed
because DNO = 22 is not defined in the primary key of table 2. Therefore,
Referential integrity constraints are violated here.

ID Name DNO DNO Place


60 Jaipur
01 Vikash 60
13 Mumbai
02 Amit 22
95 Delhi
03 Rahul 95

Department of Master of Computer


Applications www.cambridge
Advantages of Relational Database Model
• It is simpler than the hierarchical model and network model.
• It is easy and simple to understand.
• Its structure can be changed anytime upon requirement.
• Data Integrity: The relational database model enforces data integrity through various constraints
such as primary keys, foreign keys, and unique constraints. This ensures that the data in the
database is accurate, consistent, and valid.
• Flexibility: The relational database model is highly flexible and can handle a wide range of data
types and structures. It also allows for easy modification and updating of the data without
affecting other parts of the database.
• Scalability: The relational database model can scale to handle large amounts of data by adding
more tables, indexes, or partitions to the database. This allows for better performance and faster
query response times.
• Security: The relational database model provides robust security features to protect the data in the
database. These include user authentication, authorization, and encryption of sensitive data.
• Data consistency: The relational database model ensures that the data in the database is consistent
across all tables. This means that if a change is made to one table, the corresponding changes will
Department
be of
madeMaster of Computer
to all related tables.
Applications www.cambridge
• Few database relations Disadvantages
have certainoflimits which can’t
the Relational Modelbe expanded further.

• It can be complex and it becomes hard to use.


• Complexity: The relational model can be complex and difficult to understand,
particularly for users who are not familiar with SQL and database design
principles. This can make it challenging to set up and maintain a relational
database.
• Performance: The relational model can suffer from performance issues when
dealing with large data sets or complex queries. In particular, joins between
tables can be slow, and indexing strategies can be difficult to optimize.
• Cost: Relational databases can be expensive to license and maintain, particularly
for large-scale deployments. Additionally, relational databases often require
dedicated hardware and specialized software to run, which can add to the cost.
• Data redundancy: In some cases, the relational model can lead to data
redundancy,
Department of Masterwhere the same data is stored in multiple tables.
of Computer
Applications www.cambridge
Relational database schemas

• Relation schema defines the design and structure of the relation or table in the
database.
• It is the way of representation of relation states in such a way that every
relation database state fulfills the integrity constraints set (Like Primary key,
Foreign Key, Not null, Unique constraints) on a relational schema.

Department of Master of Computer


Applications www.cambridge
• Relation Name: Name Components
of the tableofthat is stored
a Relation in the database. It should be
Schema
unique and related to the data that is stored in the table. For example- The
name of the table can be Employee store the data of the employee.
• Attributes Name: Attributes specify the name of each column within the
table. Each attribute has a specific data type.
• Domains: The set of possible values for each attribute. It specifies the type of
data that can be stored in each column or attribute, such as integer, string, or
date.
• Primary Key: The primary key is the key that uniquely identifies each tuple.
It should be unique and not be null.
• Foreign Key: The foreign key is the key that is used to connect two tables. It
refers to the primary key of another table.
• Constraints: Rules that ensure the integrity and validity of the data. Common
Department of Masterinclude
constraints of Computer
NOT NULL, UNIQUE, CHECK, and DEFAULT.
Applications www.cambridge
Example of Relation Schema
Student Table
student (rollNo, name, degree, year, sex, deptNo, advisor)
Department Table
department (deptId, name, hod, phone)
Course Table
course (coursId, ename, credits, deptNo)
Professor Table
professor (empId, name, sex, startYear, deptNo, phone)
Enrollment table
enrollment (rollNo, coursId, sem, year, grade)
Teaching table
teaching (empId, coursed, sem, year, Classroom)
prerequisite table
prerequisite
Department (preReqCourse,
of Master courseId)
of Computer
Applications www.cambridge
Example of Relation Schema

Department of Master of Computer


Applications www.cambridge
Update operations, transactions, and dealing with
constraint violations
There are mainly three operations that have the ability to change the state of
relations, these modifications are given below:

Insert –
To insert new tuples in a relation in the database.
Delete –
To delete some of the existing relation on the database.
Update (Modify) –
To make changes in the value of some existing tuples.

Department of Master of Computer


Applications www.cambridge
Update operations, transactions, and dealing with
constraint violations
INSERT may violate any of the constraints:
• Domain constraint:
if one of the attribute values provided for the new tuple is not
of the specified attribute domain
• Key constraint:
if the value of a key attribute in the new tuple already exists in
another tuple in the relation
• Referential integrity:
if a foreign key value in the new tuple references a primary
key value that does not exist in the referenced relation
• Entity integrity:
if the of
Department primary
Masterkeyofvalue is null in the new tuple
Computer
Applications www.cambridge
Update operations, transactions, and dealing with
constraint violations
DELETE may violate only referential integrity:
If the primary key value of the tuple being deleted is referenced from other tuples in
the database can be remedied by several actions: RESTRICT, CASCADE, SET NULL

RESTRICT option: reject the deletion


CASCADE option: propagate the new primary key value into the
foreign keys of the referencing tuples
SET NULL option: set the foreign keys of the referencing tuples
to NULL

One of the above options must be specified during database design for each foreign
key constraint.
Department of Master of Computer
Applications www.cambridge
Update operations, transactions, and dealing with
constraint violations
UPDATE may violate domain constraint and NOT NULL constraint on an
attribute being modified.
Any of the other constraints may also be violated, depending on the attribute
being updated:
Updating the primary key (PK):
Similar to a DELETE followed by an INSERT
Need to specify similar options to DELETE
Updating a foreign key (FK):
May violate referential integrity
Updating an ordinary attribute (neither PK nor FK):
Can only violate domain constraints
Department of Master of Computer
Applications www.cambridge
Unary Operations in Relational Algebra
• The unary operations in Relational algebra are selection, projection, and
rename.
• The select, project, and rename operators in relational algebra work on one
relation only so they are called unary operators.
• The operations that operate on only one relation are called unary operations in
relational algebra. The three unary operations in relational algebra are:
 Selection
 Projection
 Rename

Department of Master of Computer


Applications www.cambridge
Unary Operations in Relational Algebra
Selection Operation in Relational Algebra
• The selection operation is a unary operation that is performed on one relation. The
selection operation is used to retrieve tuples from the relation that satisfies the given
condition. It is denoted by σ. Selection operation in relational algebra is written as:
σcondition (Relationname)

• We can also add multiple conditions if required using the operators ∧ (AND), ⋁ and
(OR).
• These operators are used to combine multiple conditions as required in the problem.
σcondition1 operator condition2 … condition n (Relationname)

To select all the tuples of a relation we write the selection operation without any condition.
Department of Master of Computer σ (Relationname)
Applications www.cambridge
Unary Operations in Relational Algebra
Projection Operation in Relational Algebra
• The projection operation is a unary operation that is performed on a relation.
• A projection operation is used to retrieve all the values of one or more
attributes. It is denoted by π. Projection operation in relational algebra is
written as:
πcolumnname(Relationname)

• We can add multiple column names in projection operation using the comma
operator if required.
• The comma operator is used when we have to retrieve multiple column values.
Below is the projection operation representation to output multiple columns.
πcolumnname1, columnname2, …,columnnamen (Relationname)
Department of Master of Computer
Applications www.cambridge
Unary Operations in Relational Algebra
Rename Operation in Relational Algebra
• The rename operation is operation applied on one relation. Rename operation as the name
suggests is used to rename the relation, attributes or both. It is denoted by ρ.
• Rename Operation for Renaming Relation
Rename operation for renaming relation is written as:
ρ New_relation_name (Old_relation_name)

• For example: To rename relation R to S: ρ S(R)


• Rename Operation for Renaming Columns of Relation
Rename operation for renaming columns of relation is written as:
ρ (New_columnnames) (Relation_name)
For example: To rename columns of relation R (a, b, c) to (x, y, z): ρ (x, y, z) (R)
Department of Master of Computer
Applications www.cambridge
Unary Operations in Relational Algebra
Example 1: Consider the below given table and write the queries in relational algebra for the
questions given below

Student

Rollno Studentname Marks

1 A 80

2 B 50

3 C 95

4 D 62

5 A 70

Department of Master of Computer


Applications www.cambridge
Unary Operations in Relational Algebra
Q.1: Output all the tuples of the relation.
σ (Student)
Q.2: Retrieve only those tuples where Rollno is greater than 2.
σRollno > 2 (Student)
Q.3: Retrieve only those tuples where either Marks less than 70 or Studentname is A.
σMarks < 70 ∨ Studentname = A (Student)
Q.4: Retrieve the tuples with Student Name A and Marks greater than 75.
σMarks > 75 ∧ Studentname = A (Student)
Q.5: Output all the values in the column Rollno.
πRollno(Student)
Q.6: Retrieve all the values of columns Rollno and Marks
πRollno, Marks (Student)
Q.7: Retrieve the Rollno of students whose Marks is greater than or equal to 80.
πRollno [σMarks >=80 (Student)]

Department of Master of Computer


Applications www.cambridge
Unary Operations in Relational Algebra
Q8: Rename the relation as S and attributes of relation Rollno as r, Studentname as Sname and Marks as m.
ρ S (r, Sname, m) (Student)

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
Binary Operations:
• In database management systems, the ability to connect and retrieve data from multiple
tables.
• The JOIN and DIVISION operations are two binary relational operations that allow
users to combine or divide data from multiple tables based on specified conditions.

What is a JOIN operation?


• A JOIN operation combines rows from two or more tables based on a related column or
set of columns, known as a key.

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
There are several types of JOINs, each with its own unique characteristics and use cases:

INNER JOIN − An INNER JOIN combines rows from both tables that match the join condition. It
returns only the rows that satisfy the condition and discards the rest.
OUTER JOIN − An OUTER JOIN combines all rows from both tables, including those that do not
satisfy the join condition. There are three types of OUTER JOINs: LEFT JOIN, RIGHT JOIN, and
FULL JOIN.
LEFT JOIN − A LEFT JOIN returns all rows from the left table, along with any matching rows from
the right table. If there is no match, NULL values are returned for the right table's columns.
RIGHT JOIN − A RIGHT JOIN returns all rows from the right table, along with any matching rows
from the left table. If there is no match, NULL values are returned for the left table's columns.
FULL JOIN − A FULL JOIN returns all rows from both tables, along with NULL values for any non-
matching rows.

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
What is a DIVISION operation?
• The DIVISION operation is a binary relational operation that divides one set of rows
into another set of rows based on specified conditions.
• It is similar to a JOIN operation, but the resulting table contains only the rows that
belong to the first set and satisfy the division condition.

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
SQL Set Operation
The SQL Set operation is used to combine the two or more SQL SELECT statements.

Types of Set Operation


• Union
• Union All
• Intersect
• Minus

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
1. Union
• The SQL Union operation is used to combine the result of two or more SQL
SELECT queries.
• In the union operation, all the number of datatype and columns must be same
in both the tables on which UNION operation is being applied.
• The union operation eliminates the duplicate rows from its result set.
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;

Relational Algebra: σcondition (Relationname) U σcondition (Relationname)

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
Example
Using the above First and Second table.
SELECT * FROM First
UNION
SELECT * FROM Second;

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
2. Union All
Union All operation is equal to the Union operation. It returns the set
without removing duplication and sorting the data.
Syntax:
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;

Example: Using the above First and Second table.

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
Example
Using the above First and Second table.
SELECT * FROM First
UNION ALL
SELECT * FROM Second;

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
3. Intersect
• It is used to combine two SELECT statements. The Intersect operation
returns the common rows from both the SELECT statements.
• In the Intersect operation, the number of datatype and columns must be
the same.
• It has no duplicates and it arranges the data in ascending order by
default.
Syntax
SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;
Department of Master of Computer
Applications www.cambridge
Binary Operations in Relational Algebra
Example
Using the above First and Second table.

SELECT * FROM First


INTERSECT
SELECT * FROM Second;

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
4. Minus
• It combines the result of two SELECT statements. Minus operator is used
to display the rows which are present in the first query but absent in the
second query.
• It has no duplicates and data arranged in ascending order by default.

Syntax:
SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;

Department of Master of Computer


Applications www.cambridge
Binary Operations in Relational Algebra
Example
Using the above First and Second table.
Minus query will be:
SELECT * FROM First
MINUS
SELECT * FROM Second;

Department of Master of Computer


Applications www.cambridge
Additional Relational Operations
Aggregate Functions
An aggregate function takes a collection of values and returns a single value
as its result.
Examples of aggregate functions include:
• sum takes a collection of numerical values and adds them up.
• avg also takes a collection of numerical values and returns their mean.
• count returns how many items are in a given collection.
• min returns the minimum value in a collection.
• max returns the maximum value in a collection.

Department of Master of Computer


Applications www.cambridge
Additional Relational Operations
Example:

Syntax:
• G or F function(attribute)(Relation_Name)

• G Max(Marks)(Students)

Department of Master of Computer


Applications www.cambridge
Additional Relational Operations

Grouping

Syntax-
• Y (group_attribute), AVG(price)(R)

Example:
Y (items), AVG(price)(R)

Department of Master of Computer


Applications www.cambridge

You might also like