Database System - Notes - Unit-2
Database System - Notes - Unit-2
Database System - Notes - Unit-2
DATA MODELS
Data models in DBMS help to understand the design at the conceptual, physical, and
logical levels as it provides a clear picture of the data making it easier for developers
to create a physical database.
Data models are used to describe how the data is stored, accessed, and updated in a
DBMS. A set of symbols and text is used to represent them so that all the members of an
organization can understand how the data is organized. It provides a set of conceptual tools
that are vastly used to represent the description of data.
There are many types of data models that are used in the industry, some of them are -
Hierarchical Model
The hierarchical data model is one of the oldest data models, developed in
the 1950s by IBM. In this data model, the data is organized in a hierarchical tree-like
structure. This data model can be easily visualized because each record has one parent and
many children (possibly 0) as shown in the image given below.
Vehicle
Sedan SUVs
The above given image represents the data model of the Vehicle database, vehicle are
classified into two types Viz. two-wheelers and four-wheelers and then they are further
classified. The main drawback we can see here is we can only have one to many
relationships under this model, hence the hierarchical data model is very rarely used
nowadays.
Network Model
A network model is nothing but a generalization of the hierarchical data model as this
data model allows many to many relationships therefore in this model a record can also have
more than one parent.
The network model can be represented as a graph and hence it replaces the hierarchical
tree with a graph in which object types are the nodes and relationships are the edges. For
example
College
Director
Here you can see all the three departments are linked with the director which was not
possible in the hierarchical data model. In the network model, there can be many possible
paths to reach a node from the root node (College is the root node in the above case),
therefore the data can be accessed efficiently when compared to the hierarchical data
model. But, on the other hand, the process of insertion and deletion of data is quite complex.
Entity - Anything that has an independent existence about which we collect the
data. They are represented as rectangles in the ER diagram. For example - Car,
house, employee.
Entity Set - A set of the same type of entities is known as an entity set. For example
- Set of students studying in a college.
Attributes - Properties that define entities are called attributes. They are
represented by an ellipse shape.
Relationships - A relationship is used to describe the association between
entities. They are represented as diamond or rhombus shapes in the ER diagram.
In the above-represented ER diagram, we have two entities that are Employee and
Company and the relationship among them. Also, in the above-represented ER diagram, we
can see that both employee and company have some attributes and the relationship is of
"works in" type, which means employee works in a company.
Relational Model
This is the most widely accepted data model. In this model, the database is represented
as a collection of relations in the form of rows and columns of a two-dimensional
table. Each row is known as a tuple (a tuple contains all the data for an individual record)
while each column represents an attribute. For example -
The above table shows a relation "STUDENT" with attributes as Stu. Id, Name, and Branch
which consists of 4 records or tuples.
EName=”Ram” DeptId=”D101”
Eid=101 DeptName=”Software”
Gender=’M’ AddDept()
AddEmployee() UpdateDept()
UpdateEmployee()
Member Functions
In the above image, we have two objects that are Employee and Department in which all the
data is contained in a single unit (object). They are linked with each other as they share a
common attribute �.�.i.e. Department_Id.
It provides data structures and operations used in the relational model and also provides
features of object oriented models like classes, inheritance, etc. The only drawback of this
data model is that it is complex and quite difficult to handle.
The float data model consists of a single two-dimensional array of data elements. For
example, in the 2-d array we can have one column as username and the other as password.
One thing that can be noticed here is, there should not be duplicate entries in the table
(array).
The major drawbacks of this model are that it fails to store a huge amount of data secondly,
to access any data whole table needs to be searched which makes it inefficient.
The semi-structured data model is a generalized form of the relational model, which allows
representing data in a flexible way, hence we can not differentiate between data and
schema in this model because, in this model, some entities have a missing
attribute(s) and on the other hand, some entities might have some extra attribute(s)
which in turn makes it easy to update the schema of the database.
For example - We can say a data model to be semi-structured if in some attributes we are
storing both atomic values (values that can't be divided further, for example, Roll_No) as well
as a collection of values.
The biggest disadvantage of the data model is, one must know the characteristics of
physical data to build a data model.
Sometimes in big databases, it is quite difficult to understand the data model also the
cost incurred is very high.
Component of ER Diagram
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity
can be represented as rectangles.
An entity that depends on another entity called a weak entity. The weak entity
doesn't contain any key attribute of its own. The weak entity is represented by a
double rectangle.
2. Attribute
For example, id, age, contact number, name, etc. can be attributes of a
student.
c. Multivalued Attribute
An attribute can have more than one value. These attributes are known as a
multivalued attribute. The double oval is used to represent multivalued attribute.
For example, a student can have more than one phone number.
For example, A person's age changes over time and can be derived from
another attribute like Date of birth.
3. Relationship
For example, A female can marry to one male, and a male can marry to one
female.
For example, Scientist can invent many inventions, but the invention is done by
the only specific scientist.
For example, Student enrolls for only one course, but a course can have many
students.
d. Many-to-many relationship - When more than one instance of the entity on
the left, and more than one instance of an entity on the right associates with the
relationship then it is known as a many-to-many relationship.
For example, Employee can assign by many projects and project can have
many employees.
Here we will discuss how choosing an entity set vs an attribute can change the whole ER
design semantics. To understand this lets take an example, let’s say we have an entity set
Student with attributes such as student-name and student-id. Now we can say that the student-
id itself can be an entity with the attributes like student-class and student-section.
Now if we compare the two cases we discussed above, in the first case we can say that the
student can have only one student id, however in the second case when we chose student id as
an entity it implied that a student can have more than one student id.
It is hard to decide that an object can be best represented by an entity set or relationship set.
To comprehend and decide the perfect choice between these two (entity vs relationship), the
user needs to understand whether the entity would need a new relationship if a requirement
arise in future, if this is the case then it is better to choose entity set rather than relationship
set.
Let’s take an example to understand it better: A person takes a loan from a bank, here we
have two entities person and bank and their relationship is loan. This is fine until there is a
need to disburse a joint loan, in such case a new relationship needs to be created to define the
relationship between the two individuals who have taken joint loan. In this scenario, it is
better to choose loan as an entity set rather than a relationship set.
This may sound confusing so lets take an example to understand how we can convert an n-ary
relationship to multiple binary relationships. Now lets say we have to describe a relationship
between four family members: father, mother, son and daughter. This can easily be
represented in forms of multiple binary relationships, father-mother relationship as “spouse”,
son and daughter relationship as “siblings” and father and mother relationship with their child
as “child”.
The cardinality ratio in DBMS can help us determine in which scenarios we need to place
relationship attributes. It is recommended to represent the attributes of one to one or one to
many relationship sets with any participating entity sets rather than a relationship set.
An aggregate function in SQL performs a calculation on multiple values and returns a single
value. SQL provides many aggregate functions that include avg, count, sum, min, max, etc.
An aggregate function ignores NULL values when it performs the calculation, except for the
count function. An aggregate function in SQL returns one value after calculating multiple
values of a column. We often use aggregate functions with the GROUP BY and HAVING
clauses of the SELECT statement.
Count()
Sum()
Avg()
Min()
Max()
The following SQL statement fetches the number of products in the table.
The below-given command will display those product ids where the unit price is greater than
4.
Let's look at how we can use GROUP BY and HAVING functions with the COUNT
function.
SUM() Function - The SUM() function returns the total sum of a numeric column.
Example: The following SQL statement finds the sum of the "unit price" fields in the
"products" table:
This will produce the following result. Let’s look at how we can use GROUP BY and
HAVING functions with the SUM function.
The SQL command below will list the number of customers in each city, having a sum of
points greater than 3000.
AVG() Function - The AVG() function calculates the average of a set of values.
Example: The following SQL command calculates the average quantity in stock.
This will produce the following result.
MIN() Function - The MIN() aggregate function returns the lowest value (minimum)
in a set of non-NULL values.
Example:
The above code will give us the minimum quantity in stock in the products table.
MAX() Function - The MAX() aggregate function returns the highest value
(maximum) in a set of non-NULL values.
Example: The code depicted below will give us the maximum quantity in stock in the
products table.
In SQL, views contain rows and columns similar to a table, however, views don't hold the
actual data.A view as a virtual table environment that's created from one or more tables so
that it's easier to work with data.
We can create views in SQL by using the CREATE VIEW command. For example,
CREATE VIEW us_customers AS
SELECT customer_id, first_name
FROM Customers
WHERE Country = 'USA';
Here, a view named us_customers is created from the customers table.
Now to select the customers who lives in USA, we can simply run,
SELECT *
FROM us_customers;
Run Code
Updating a View
It's possible to change or update an existing view using the CREATE OR REPLACE
VIEW command. For example,
CREATE OR REPLACE VIEW us_customers AS
SELECT *
FROM Customers
WHERE Country = 'USA';
Run Code
Deleting a View
We can delete views using the DROP VIEW command. For example,
DROP VIEW us_customers;
Run Code
Note: If the view is not available, the above command throws an error.
Suppose A and B are two tables and we wan't to select data from both of the tables. For that,
we have to use SQL JOINS.
However using the JOIN each time could be a tedious task. For that, we can create a view to
fetch records easily.
Let's create a view,
SELECT *
FROM order_details;
Run Code
Here, the SQL command selects data from the view order_details.
Important Rule:
o A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
o A subquery is a query within another query. The outer query is known as the main
query, and the inner query is known as a subquery.
o Subqueries are on the right side of the comparison operator.
o A subquery is enclosed in parentheses.
o In the Subquery, ORDER BY command cannot be used. But GROUP BY command
can be used to perform the same function as ORDER BY command.
SQL subqueries are most frequently used with the Select statement.
Syntax
1. SELECT column_name
2. FROM table_name
3. WHERE column_name expression operator
4. ( SELECT column_name from table_name WHERE ... );
Example
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
1. SELECT *
2. FROM EMPLOYEE
3. WHERE ID IN (SELECT ID
4. FROM EMPLOYEE
5. WHERE SALARY > 4500);
4 Alina 29 UK 6500.00
o SQL subquery can also be used with the Insert statement. In the insert statement, data
returned from the subquery is used to insert into another table.
o In the subquery, the selected data can be modified with any of the character, date
functions.
Syntax:
Example
Now use the following syntax to copy the complete EMPLOYEE table into the
EMPLOYEE_BKP table.
The subquery of SQL can be used in conjunction with the Update statement. When a
subquery is used with the Update statement, then either single or multiple columns in a table
can be updated.
Syntax
1. UPDATE table
2. SET column_name = new_value
3. WHERE VALUE OPERATOR
4. (SELECT COLUMN_NAME
5. FROM TABLE_NAME
6. WHERE condition);
Example
1. UPDATE EMPLOYEE
2. SET SALARY = SALARY * 0.25
3. WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
4. WHERE AGE >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have the following
records.
ID NAME AGE ADDRESS SALARY
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
The subquery of SQL can be used in conjunction with the Delete statement just like any other
statements mentioned above.
Syntax
Example
This would impact three rows, and finally, the EMPLOYEE table would have the following
records.
ID NAME AGE ADDRESS SALARY
1 John 20 US 2000.00
manage control
dependent M N
of N
works PROJECT
N fo
(vi) Each strong entity set has a primary key(underlined) (vii) Weak entity set
has only secondary key
Department (relatio 2)
Dep# Dname Dlocation
Project (relation 3)
(ii) Add Primary key of the owner entity set into the relation and make it as the
foreign key as
Employee(relation 1)
Dependent (relation 4)
Department
Project
Employee
Project
Employee
Dependent
Department
Project
Works - for