Introduction of DBMS (Database Management System) _ Set 1
Introduction of DBMS (Database Management System) _ Set 1
Important Terminology
DDL is the short name of Data Definition Language, which deals with database
schemas and descriptions of how the data should reside in the database.
CREATE: to create a database and its objects like (table, index, views, store
procedure, function, and triggers)
TRUNCATE: remove all records from a table, including all spaces allocated for
the records are removed
COMMENT: add comments to the data dictionary
ER Model is used to model the logical view of the system from data perspective
which consists of these components:
An Entity is an object of Entity Type and set of all entities is called an entity set.
e.g.; E1 is an entity having Entity Type Student and set of all students is called
Entity Set. In ER diagram, Entity Type is represented as:
Attribute(s):
Attributes are the properties which define the entity type. For example,
Roll_No, Name, DOB, Age, Address, Mobile_No are the attributes which defines
entity type Student. In ER diagram, attribute is represented by an oval.
.
1. Key Attribute –
The attribute which uniquely identifies each entity in the entity set is called key
attribute.For example, Roll_No will be unique for each student. In ER diagram,
key attribute is represented by an oval with underlying lines.
2. Composite Attribute –
An attribute consisting more than one value for a given entity. For example,
Phone_No (can be more than one for a given student). In ER diagram,
multivalued attribute is represented by double oval.
4. Derived Attribute –
An attribute which can be derived from other attributes of the entity type is
known as derived attribute. e.g.; Age (can be derived from DOB). In ER diagram,
derived attribute is represented by dashed oval.
The complete entity type Student with its attributes can be represented as:
1. Unary Relationship –
When there is only ONE entity set participating in a relation, the relationship
is called as unary relationship. For example, one person is married to only one
person.
2. Binary Relationship –
When there are TWO entities set participating in a relation, the relationship is
called as binary relationship.For example, Student is enrolled in Course.
3. n-ary Relationship –
When there are n entities set participating in a relation, the relationship is called
as n-ary relationship.
Cardinality:
1. One to one – When each entity in each entity set can take part only once in
the relationship, the cardinality is one to one. Let us assume that a male can
marry to one female and a female can marry to one male. So the relationship will
be one to one.
Using Sets, it can be represented as:
2. Many to one – When entities in one entity set can take part only once in the
relationship set and entities in other entity set can take part more than
once in the relationship set, cardinality is many to one. Let us assume that a
student can take only one course but one course can be taken by many students.
So the cardinality will be n to 1. It means that for one course there can be n
students but for one student, there will be only one course.
Using Sets, it can be represented as:
In this case, each student is taking only 1 course but 1 course has been taken by
many students.
3. Many to many – When entities in all entity sets can take part more than
once in the relationship cardinality is many to many. Let us assume that a
student can take more than one course and one course can be taken by many
students. So the relationship will be many to many.
1. Total Participation – Each entity in the entity set must participate in the
relationship. If each student must enroll in a course, the participation of student
will be total. Total participation is shown by double line in ER diagram.
2. Partial Participation – The entity in the entity set may or may NOT
participate in the relationship. If some courses are not enrolled by any of the
student, the participation of course will be partial.
The diagram depicts the ‘Enrolled in’ relationship set with Student Entity set
having total participation and Course Entity set having partial participation.
As discussed before, an entity type has a key attribute which uniquely identifies
each entity in the entity set. But there exists some entity type for which key
attribute can’t be defined. These are called Weak Entity type.
ADDRE A
ROLL_NO NAME PHONE
SS
ROHTA 9156253 2
3 SUJIT
K 131
SURES 9156768 1
4 DELHI
H 971
●
TABLE 1
These are some important terminologies that are used in terms of relation.
Attribute: Attributes are the properties that define a relation. e.g.;
ROLL_NO, NAME etc.
Tuple: Each row in the relation is known as tuple. The above relation
contains 4 tuples, one of which is shown as:
9455123 1
1 RAM DELHI
451
●
Degree: The number of attributes in the relation is known as 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.
Column: Column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from relation STUDENT.
RO
L
L
_
N
O
1
●
The queries to deal with relational database can be categories as:
Data Definition Language: It is used to define the structure of the
database. e.g; CREATE TABLE, ADD COLUMN, DROP COLUMN and
so on.
Data Manipulation Language: It is used to manipulate data in the
relations. e.g.; INSERT, DELETE, UPDATE and so on.
Data Query Language: It is used to extract the data from the relations.
e.g.; SELECT
So first we will consider the Data Query Language. A generic query to
retrieve from a relational database is:
○ SELECT [DISTINCT] Attribute_List FROM R1,R2….RM
○ [WHERE condition]
○ [GROUP BY (Attributes)[HAVING condition]]
○ [ORDER BY(Attributes)[DESC]];
● Part of the query represented by statement 1 is compulsory if you want to
retrieve from a relational database. The statements written inside [] are
optional. We will look at the possible query combination on relation shown
in Table 1.
Case 1: If we want to retrieve attributes ROLL_NO and NAME of all
students, the query will be:
SELECT ROLL_NO, NAME FROM STUDENT;
ROLL_
NAME
NO
1 RAM
RAMES
2
H
3 SUJIT
SURES
4
H
ROLL_
NAME
NO
3 SUJIT
SURES
4
H
91562
SUJI ROHT 2
3 531
T AK
31
SUR
91567
E 1
4 DELHI 689
S
71
H
●
CASE 4: If we want to represent the relation in ascending order by AGE,
we can use ORDER BY clause as:
SELECT * FROM STUDENT ORDER BY AGE;
ROLL ADDR
NAM PHON A
_N ES
E E
O S
94551
1
1 RAM DELHI 234
51
RAM
GURG 96524
E 1
2 AO 315
S
N 43
H
91562
SUJI ROHT 2
3 531
T AK
31
●
Note: ORDER BY AGE is equivalent to ORDER BY AGE ASC. If we want
to retrieve the results in descending order of AGE, we can use ORDER BY
AGE DESC.
CASE 5: If we want to retrieve distinct values of an attribute or group of
attribute, DISTINCT is used as in:
SELECT DISTINCT ADDRESS FROM STUDENT;
ADDRE
SS
DELHI
GURGA
ON
ROHTA
K
●
If DISTINCT is not used, DELHI will be repeated twice in result set.
Before understanding GROUP BY and HAVING, we need to
understand aggregations functions in SQL.
AGGRATION FUNCTIONS: Aggregation functions are used to perform
mathematical operations on data values of a relation. Some of the
common aggregation functions used in SQL are:
○ COUNT: Count function is used to count the number of rows
in a relation. e.g;
● SELECT COUNT (PHONE) FROM STUDENT;
COUNT(P
HONE)
SUM(A
GE)
74
In the same way, MIN, MAX and AVG can be used. As we have seen above, all
aggregation functions return only 1 row.
AVERAGE: It gives the average values of the tupples. It is also defined as sum
divided by count values.
Syntax:AVG(attributename)
OR
Syntax:SUM(attributename)/COUNT(attributename)
The above mentioned syntax also retrieves the average value of tupples.
MAXIMUM:It extracts the maximum value among the set of tupples.
Syntax:MAX(attributename)
MINIMUM:It extracts the minimum value amongst the set of all the tupples.
Syntax:MIN(attributename)
GROUP BY: Group by is used to group the tuples of a relation based on an
attribute or group of attribute. It is always combined with aggregation function
which is computed on group. e.g.;
SELECT ADDRESS, SUM(AGE) FROM STUDENT
● GROUP BY (ADDRESS);
In this query, SUM(AGE) will be computed but not for entire table but for
each address. i.e.; sum of AGE for address DELHI(18+18=36) and
similarly for other address as well. The output is:
ADDRE SU
SS M
(
A
G
E
)
DELHI 36
GURGA
18
ON
ROHTA
20
K
If we try to execute the query given below, it will result in error because although
we have computed SUM(AGE) for each address, there are more than 1
ROLL_NO for each address we have grouped. So it can’t be displayed in result
set. We need to use aggregate functions on columns after SELECT statement to
make sense of the resulting set whenever we are using GROUP BY.
SELECT ROLL_NO, ADDRESS, SUM(AGE) FROM STUDENT
● GROUP BY (ADDRESS);
NOTE: An attribute which is not a part of GROUP BY clause can’t be
used for selection. Any attribute which is part of GROUP BY CLAUSE
can be used for selection but it is not mandatory. But we could use
attributes which are not a part of the GROUP BY clause in an
aggregrate function.