Unit 4
Unit 4
Important Terminologies
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:
1 RAM DELHI 9455123451 18
4
How Queries can be Categorized in Relational
Database?
The queries to deal with relational database can be categorized 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 data from a relational
database is:
1. SELECT [DISTINCT] Attribute_List FROM R1,R2….RM
2. [WHERE condition]
3. [GROUP BY (Attributes)[HAVING condition]]
4. [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.
Different Query Combinations
Case 1: If we want to retrieve attributes ROLL_NO and NAMEof all
students, the query will be:
SELECT ROLL_NO, NAME FROM STUDENT;
ROLL_NO NAME
1 RAM
2 RAMESH
3 SUJIT
4 SURESH
Case 2: If we want to retrieve ROLL_NO and NAME of the students
whose ROLL_NO is greater than 2, the query will be:
SELECT ROLL_NO, NAME FROM STUDENT
WHERE ROLL_NO>2;
ROLL_NO NAME
3 SUJIT
4 SURESH
Note:
ORDER BY AGEis 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.
ADDRESS
DELHI
GURGAON
ROHTAK
SUM(AGE)
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
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:
ADDRESS SUM(AGE)
DELHI 36
GURGAON 18
ROHTAK 20
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 aggregate function.
Data Models
2. Entity: Entities are objects in the real world, which can have
certain properties & these properties are referred to as attributes of
that particular entity. There are 2 types of entities: Strong and weak
entity, weak entity do not have a key attribute to identify them,
their existence solely depends on one 1-specific strong entity & also
have full participation in a relationship whereas strong entity does
have a key attribute to uniquely identify them.
Weak entity example: Loan -> Loan will be given to a customer
(which is optional) & the load will be identified by the customer_id to
whom the lone is granted.
3. Relationships: How data is logically related to each other
defines the relationship of that data with other entities. In simple
words, the association of one entity with another is defined here.
A relationship can be further categorized into – unary, binary, and
ternary relationships.
Unary: In this, the associating entity & the associated entity both
are the same. Ex: Employee Manages themselves, and students
are also given the post of monitor hence here the student
themselves is a monitor.
Binary: This is a very common relationship that you will come
across while designing a database.
Ex: Student is enrolled in courses, Employee is managed by
different managers, One student can be taught by many
professors.
Ternary: In this, we have 3 entities involved in a single
relationship. Ex: an employee works on a project for a client. Note
that, here we have 3 entities: Employee, Project & Client.
4. Attributes: Attributes are nothing but properties of a specific
entity that define its behavior. For example, an employee can have
unique_id, name, age, date of birth (DOB), salary, department,
Manager, project id, etc.
5. Normalization: After all the entities are put in place and the
relationship among data is defined, we need to look for loopholes or
possible ambiguities that may arise as a result of CRUD operations.
To prevent various Anomalies such as INSERTION, UPDATION, and
DELETION Anomalies.
Data Normalization is a basic procedure defined for databases to
eliminate such anomalies & prevent redundancy.
An Example of Logical Design
Physical Design
The main purpose of the physical design is to actually implement
the logical design that is, show the structure of the database along
with all the columns & their data types, rows, relations, relationships
among data & clearly define how relations are related to each other.
Following are the steps taken in physical design
Step 1: Entities are converted into tables or relations that consist of
their properties (attributes)
Step 2: Apply integrity constraints: establish foreign key, unique
key, and composite key relationships among the data. And apply
various constraints.
Step 3: Entity names are converted into table names, property
names are translated into attribute names, and so on.
Step 4: Apply normalization & modify as per the requirements.
Step 5: Final Schemes are defined based on the entities &
attributes derived in logical design.
Physical Design
In the enterprise, relational databases are used to organize data and identify
relationships between key data points. They make it easy to sort and find information,
which helps organizations make business decisions more efficiently and minimize
costs. They work well with structured data.
Categorizing data. Database administrators can easily categorize and store data
in a relational database that can then be queried and filtered to extract
information for reports. Relational databases are also easy to extend and aren't
reliant on physical organization. After the original database creation, a new data
category can be added without having to modify the existing applications.
Ease of use. Complex queries are easy for users to carry out with SQL, the main
query language used with relational databases.
Inflexibility. Relational databases are not ideal for handling large quantities
of unstructured data. Data that is largely qualitative, not easily defined or
dynamic is not optimal for relational databases. As the data changes or evolves,
the schema must evolve with it, which takes time.
SQL Operators
SQL Operators perform arithmetic, comparison, and logical
operations to manipulate and retrieve data from databases.
In this article, we will discuss Operators in SQL with examples, and
understand how they work in SQL.
Operators in SQL
Operators in SQL are symbols that help us to perform specific
mathematical and logical computations on operands. An operator
can either be unary or binary.
The unary operator operates on one operand, and the binary
operator operates on two operands.
Types of Operators in SQL
Different types of operators in SQL are:
Arithmetic operator
Comparison operator
Logical operator
Bitwise Operators
Compound Operators
SQL Arithmetic Operators
Arithmetic operators in SQL are used to perform mathematical
operations on numeric values in queries. Some common arithmetic
operators are:
Operator Description
/ This operator works with the ‘ALL’ keyword and it calculates division
operations.
= Equal to.
AND Logical AND compares two Booleans as expressions and returns true when
both expressions are true.
OR Logical OR compares two Booleans as expressions and returns true when one
of the expressions is true.
NOT Not takes a single Boolean as an argument and change its value from false to
true or from true to false.
| Bitwise OR operator
STUDENT
NAME ROLL_NO SECTION
Ravi 104 A
Sumathi 105 B
Raj 102 A
Sample Queries:
To display NAME, LOCATION, PHONE_NUMBER of the students from
DATABASE table whose section is A
SELECT NAME, LOCATION, PHONE_NUMBER
FROM DATABASE
WHERE ROLL_NO IN (SELECT ROLL_NO
FROM STUDENT
WHERE SECTION='A');
Explanation : First subquery executes “ SELECT ROLL_NO from
STUDENT where SECTION=’A’ ” returns ROLL_NO from STUDENT table
whose SECTION is ‘A’.Then outer-query executes it and return the
NAME, LOCATION, PHONE_NUMBER from the DATABASE table of the
student whose ROLL_NO is returned from inner subquery. Output:
NAME ROLL_NO LOCATION PHONE_NUMBER
Table2: Student2
NAME ROLL_NO LOCATION PHONE_NUMBER