23IT204T - DBMS Unit 1
23IT204T - DBMS Unit 1
CSE
DBMS – Definition
A database-management system (DBMS) is a collection of interrelated data and
a set of programs to access those data. The collection of data, usually referred
to as the database, contains information relevant to an enterprise.
The primary goal of a DBMS is to provide a way to store and retrieve database
information that is both convenient and efficient.
Database Applications
Databases are widely used. Here are some representative applications:
• Enterprise Information
◦ Sales: For customer, product, and purchase information.
◦ Accounting: For payments, receipts, account balances, assets
and other accounting information.
◦ Human resources: For information about employees, salaries,
payroll taxes, and benefits, and for generation of paychecks.
◦ Manufacturing: For management of the supply chain
◦ Online retailers: For sales data noted above plus online order
tracking, generation of recommendation lists, and maintenance of online
product evaluations.
• Banking and Finance
◦ Banking: For customer information, accounts, loans, and
banking transactions.
◦ Credit card transactions: For purchases on credit cards and
generation of monthly statements.
◦ Finance: For storing information about holdings, sales, and purchases
of financial instruments such as stocks and bonds;
• Universities: For student information, course registrations, and
grades (in addition to standard enterprise information such as human
resources and accounting).
•
Airlines: For reservations and schedule information. Airlines were among
Dr.S.SRIDEVI, AP/CSE
1
VEC – IV Semester – II Year – 23IT204T –
CSE
VIEWS OF DATA
A major purpose of a database system is to provide users with an abstract view
of the data. That is, the system hides certain details of how the data are stored
and maintained.
Data Abstraction
For the system to be usable, it must retrieve data efficiently. The need for
efficiency has led designers to use complex data structures to represent data in
the database. Since many database-system users are not computer trained,
developers hide the complexity from users through several levels of abstraction,
to simplify users’ interactions with the system:
Physical level. The lowest level of abstraction describes how the data are
actually stored. The physical level describes complex low-level data
structures in detail.
View level. The highest level of abstraction describes only part of the
entire database. Even though the logical level uses simpler structures,
complexity remains because of the variety of information stored in a large
database.
This code defines a new record type called instructor with four fields.
There are a number of different data models that we shall cover in the text. The
data models can be classified into four different categories:
Relational Model.
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.
Tables are also known as relations. The relational model is an example of
a
record-based model.
Table: Student
Student ID Student Name Department Date of Birth
111 Ajay CSE 23-June-1999
112 Aravind CSE 20-Jan-1998
113 Balakumaran CSE 21-Jun-1999
Dr.S.SRIDEVI, AP/CSE
4
VEC – IV Semester – II Year – 23IT204T –
CSE
Disadvantages
Significant hardware and software overhead
Not as good as transaction process modeling
May have slow processing time than the hierarchical and
network model
Entity-Relationship Model.
The entity-relationship (E-R) data model uses a collection of basic objects,
called entities, and relationships among these objects. An entity is a “thing” or
“object”
in the real world that is distinguishable from other objects. The entity-
relationship model is widely used in database design,
Dr.S.SRIDEVI, AP/CSE
5
VEC – IV Semester – II Year – 23IT204T –
CSE
Advantages
o Easy to develop relational model using ER model
o ER specifies mapping cardinalities
o Specifies key like primary key, foreign key
Disadvantages
o Used for design purpose only not implementation
Database Languages
A database system provides a data-definition language to specify the database
schema and a data-manipulation language to express database queries and
updates. In practice, the data-definition and data-manipulation languages are
not two separate languages; instead they simply form parts of a single database
language, such as the widely used SQL language.
Data-Manipulation Language
A data-manipulation language (DML) is a language that enables users to access
or manipulate data as organized by the appropriate data model. The types of
access are:
• Retrieval of information stored in the database
Dr.S.SRIDEVI, AP/CSE
6
VEC – IV Semester – II Year – 23IT204T –
CSE
Data-Definition Language
A database schema by a set of definitions expressed by a special language
called a data-definition language (DDL).The DDL is also used to specify
additional properties of the data.
Storage structure and access methods used by the database system are
specified by a set of statements in a special type of DDL called a data
storage and definition language.
These statements define the implementation details of the database
schemas, which are usually hidden from the users.
The data values stored in the database must satisfy certain consistency
constraints. For example, suppose the university requires that the account
balance of a department must never be negative.
Referential Integrity. There are cases where we wish to ensure that a value
that appears in one relation for a given set of attributes also appears in a
certain set of attributes in another relation (referential integrity).
Assertions. An assertion is any condition that the database must always satisfy.
Domain constraints and referential-integrity constraints are special forms of
assertions.
Storage Manager
The storage manager is the component of a database system that provides
the interface between the low-level data stored in the database and the
application programs and queries submitted to the system.
The storage manager is responsible for the interaction with the file
manager. The raw data are stored on the disk using the file system
provided by the operating system. The storage manager translates the
various DML statements into low-level file-system commands.
Dr.S.SRIDEVI, AP/CSE
8
VEC – IV Semester – II Year – 23IT204T –
CSE
• Indices, which can provide fast access to data items. Like the index in this
textbook, a database index provides pointers to those data items that hold a
particular value.
The DML compiler also performs query optimization; that is, it picks the
lowest cost evaluation plan from among the alternatives.
Query evaluation engine, which executes low-level instructions generated by
the DML compiler.
Dr.S.SRIDEVI, AP/CSE
9
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
10
VEC – IV Semester – II Year – 23IT204T –
CSE
Database Administrator
One of the main reasons for using DBMS is to have central control of both the
data and the programs that access those data. A person who has such
central control over the system is called a database administrator
(DBA).
The functions of a DBA include:
Schema definition. The DBA creates the original database schema by
executing a set of data definition statements in the DDL.
Storage structure and access-method definition.
Schema and physical organization modification. The DBA carries out
changes to the schema and physical organization to reflect the changing
needs of the organization, or to alter the physical organization to improve
performance.
Granting of authorization for data access. By granting different types
of authorization, the database administrator can regulate which parts of
the database various users can access.
Routine maintenance. Examples of the database administrator’s routine
maintenance activities are:
o Periodically backing up the database, either onto tapes or onto
remote servers, to prevent loss of data in case of disasters such as
flooding.
o Ensuring that enough free disk space is available for normal
operations, and upgrading disk space as required.
o Monitoring jobs running on the database and ensuring that
performance is not degraded by very expensive tasks submitted by
some users.
Dr.S.SRIDEVI, AP/CSE
11
VEC – IV Semester – II Year – 23IT204T –
CSE
Database Schema
Database schema, which is the logical design of the database
Database instance, which is a snapshot of the data in the database at a
given instant in time.
The concept of a relation corresponds to the programming-language
notion of a variable, while the concept of a relation schema corresponds
to the programming-language notion of type definition.
o student (ID, name, dept name, tot cred)
o advisor (s id, i id)
o takes (ID, course id, sec id, semester, year, grade)
o classroom (building, room number, capacity)
o time slot (time slot id, day, start time, end time)
Relation Instance
Select * from student where id = 111 or id =112;
ID Name Department Email Credits
111 Priya CSE Shan@vec.in 9.2
112 Shan EEE shanv@vec.in 8.7
Rows = 5
KEYS
We must have a way to specify how tuples within a given relation are
distinguished.
This is expressed in terms of their attributes.
That is, the values of the attribute values of a tuple must be such that they
can uniquely identify the tuple.
In other words, no two tuples in a relation are allowed to have exactly the
same value for all attributes.
Dr.S.SRIDEVI, AP/CSE
13
VEC – IV Semester – II Year – 23IT204T –
CSE
The term Primary key used to denote a candidate key that is chosen by the
database designer as the principal means of identifying tuples within a relation.
A foreign key is a set of attributes in a referencing relation, such that for each
tuple in the referencing relation, the values of the foreign key attributes are
guaranteed to occur as the primary key value of a tuple in the referenced
relation.
Dr.S.SRIDEVI, AP/CSE
14
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
15
VEC – IV Semester – II Year – 23IT204T –
CSE
The relational algebra provides a set of operations that take one or more
relationsasinputandreturnarelationasanoutput.Practicalquerylanguages such as
SQL are based on the relational algebra, but add a number of useful syntactic
features.
Dr.S.SRIDEVI, AP/CSE
16
VEC – IV Semester – II Year – 23IT204T –
CSE
SQL Fundamentals
SQL is a database computer language designed for the retrieval and
management of data in relational database. SQL stands for Structured Query
Language.
What is SQL?
Why SQL?
Allows users to access data in relational database management systems.
Allows users to describe the data.
Allows users to define the data in database and manipulate that data.
Allows embedding within other languages using SQL modules, libraries &
pre-compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures, and views
History:
1970 -- Dr. Edgar F. "Ted" Codd of IBM is known as the father of
relational databases. He described a relational model for databases.
1974 -- Structured Query Language appeared.
1978 -- IBM worked to develop Codd's ideas and released a product
named System/R.
1986 -- IBM developed the first prototype of relational database and
standardized by ANSI. The first relational database was released by
Relational Software and its later becoming Oracle.
SQL Process:
When you are executing an SQL command for any RDBMS, the system
determines the best way to carry out your request and SQL engine figures out
how to interpret the task. There are various components included in the
process. These components are Query Dispatcher, Optimization Engines,
Dr.S.SRIDEVI, AP/CSE
17
VEC – IV Semester – II Year – 23IT204T –
CSE
Classic Query Engine and SQL Query Engine, etc. Classic query engine handles
all non-SQL queries but SQL query engine won't handle logical files.
Following is a simple diagram showing SQL Architecture
Basic Types
The SQL standard supports a variety of built-in types, including:
char(n): A fixed-length character string with user-specified length n. The
full form, character, can be used instead.
varchar(n): A variable-length character string with user-specified
maximum length n. The full form, character varying, is equivalent.
int: An integer(a finite subsetof the integersthat ismachine
dependent).The full form, integer, is equivalent.
smallint: A small integer (a machine-dependent subset of the integer
type).
numeric(p,d):Afixed-pointnumberwithuser-specifiedprecision.Thenum-
ber consists of p digits (plus a sign), and d of the p digits are to the right
of the decimal point. Thus, numeric(3,1) allows 44.5 to be stored exactly,
but neither 444.5 or 0 .32 can be stored exactly in a field of this type.
real, double precision: Floating-point and double-precision floating-
point numbers with machine-dependent precision.
float(n): A floating-point number, with precision of at least n digits.
Integrity Constraints
Integrity constraints ensure that changes made to the database by
authorized users do not result in a loss of data consistency.
Thus, integrity constraints guard against accidental damage to the
database.
Integrity constraints include
o not null
o unique
o check(<predicate>)
1. Not Null Constraint
name varchar(20) not null budget numeric(12,2) not null
The not null specification prohibits the insertion of a null value for the attribute.
Any database modification that would cause a null to be inserted in an attribute
declared to be not null generates an error diagnostic.
2. Unique Constraint
SQL also supports an integrity constraint:
unique (Aj1, Aj2,...,Ajm)
The unique specification says that attributes Aj1, Aj2,...,Ajm form a candidate
key; that is, no two tuples in the relation can be equal on all the listed
attributes.
Dr.S.SRIDEVI, AP/CSE
19
VEC – IV Semester – II Year – 23IT204T –
CSE
However, candidate key attributes are permitted to be null unless they have
explicitly been declared to be not null.
CREATE – is used to create the database or its objects (like table, index,
function, views, store procedure and triggers).
DROP – is used to delete objects from the database.
ALTER-is used to alter the structure of the database.
TRUNCATE–is used to remove all records from a table, including all
spaces allocated for the records are removed.
COMMENT –is used to add comments to the data dictionary.
RENAME –is used to rename an object existing in the database.
Dr.S.SRIDEVI, AP/CSE
20
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
21
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
22
VEC – IV Semester – II Year – 23IT204T –
CSE
CREATE TABLE
Specifies a new base relation by giving it a name, and specifying each of
its attributes and their data types (INTEGER, FLOAT, DECIMAL(i,j),
CHAR(n), VARCHAR(n))
A constraint NOT NULL may be specified on an attribute
In SQL2, can use the CREATE TABLE command for specifying the primary
key attributes, secondary keys, and referential integrity constraints
(foreign keys).
Key attributes can be specified via the PRIMARY KEY and UNIQUE
phrases
TRUNCATE
TRUNCATE removes all rows from a table. The operation cannot be
rolled back and no triggers will be fired. As such, TRUCATE is faster and
doesn't use as much undo space as a DELETE.
TRUNCATE TABLE emp;
ALTER TABLE
The database users must still enter a value for the new attribute JOB for
each EMPLOYEE tuple. This can be done using the UPDATE command.
REFERENTIAL INTEGRITY OPTIONS
SQL has one basic statement for retrieving information from a database; the
SELECT statement
This is not the same as the SELECT operation of the relational algebra
Important distinction between SQL and the formal relational model; SQL
allows a table (relation) to have two or more tuples that are identical in all
their attribute values
Hence, an SQL relation (table) is a multi-set (sometimes called a bag) of
tuples; it is not a set of tuples
SQL relations can be constrained to be sets by specifying PRIMARY KEY or
UNIQUE attributes, or by using the DISTINCT option in a query
Basic form of the SQL SELECT statement is called a mapping or a SELECT-
FROM-WHERE block
Dr.S.SRIDEVI, AP/CSE
25
VEC – IV Semester – II Year – 23IT204T –
CSE
Query 1: Retrieve the name and address of all employees who work
for the 'Research' department.
In Q3, the alternate relation names E and S are called aliases or tuple
variables for the EMPLOYEE relation
Dr.S.SRIDEVI, AP/CSE
26
VEC – IV Semester – II Year – 23IT204T –
CSE
Query 5:
SELECT SSN, DNAME
FROM EMPLOYEE, DEPARTMENT
If more than one relation is specified in the FROM-clause and there is no join
condition, then the CARTESIAN PRODUCT of tuples is selected
SELECT * FROMEMPLOYEE
WHERE DNO=5
SELECT *
FROMEMPLOYEE, DEPARTMENT WHERE
DNAME='Research' AND
DNO=DNUMBER
USE OF DISTINCT
SQL does not treat a relation as a set; duplicate tuples can appear
To eliminate duplicate tuples in a query result, the keyword DISTINCT is
used
For example, the result of Q6 may have duplicate SALARY values whereas
Q11A does not have any duplicate values
Q6: SELECT SALARY
FROMEMPLOYEE
Q6A: SELECT DISTINCT SALARY FROMEMPLOYEE
SET OPERATIONS
SQL has directly incorporated some set operations
There is a union operation (UNION), and in some versions of SQL
there are set difference (MINUS) and intersection (INTERSECT)
operations
The resulting relations of these set operations are sets of tuples;
duplicate tuples are eliminated from the result
The set operations apply only to union compatible relations ; the two
relations must have the same attributes and the attributes must appear
in the same order
Query 7: Make a list of all project numbers for projects that involve an
employee whose last name is 'Smith' as a worker or as a manager of the
Dr.S.SRIDEVI, AP/CSE
27
VEC – IV Semester – II Year – 23IT204T –
CSE
(SELECT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND
LNAME='Smith')
UNION
(SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE WHERE
PNUMBER=PNO AND ESSN=SSN AND LNAME='Smith')
NESTING OF QUERIES
A complete SELECT query, called a nested query , can be specified
within the WHERE-clause of another query, called the outer query
Query 8: Retrieve the name and address of all employees who work for
the 'Research' department.
Query 9: Retrieve the name of each employee who has a dependent with
the same first name as the employee.
Dr.S.SRIDEVI, AP/CSE
28
VEC – IV Semester – II Year – 23IT204T –
CSE
E.FNAME=DEPENDENT_NAME)
In Q9, the nested query has a different result for each tuple in the
outer query
A query written with nested SELECT... FROM... WHERE... blocks and
using the = or IN comparison operators can always be expressed as a
single block query. For example, Q9 may be written as in Q9A
Q9A
SELECTE.FNAME, E.LNAME
FROM EMPLOYEE E, DEPENDENT D WHERE E.SSN=D.ESSN
AND E.FNAME=D.DEPENDENT_NAME
RELATIONAL ALGEBRA
Select
Project
Union
Set different
Cartesian product
Rename
Dr.S.SRIDEVI, AP/CSE
29
VEC – IV Semester – II Year – 23IT204T –
CSE
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is
450.
σsubject = "database" and price = 450 or year > 2010(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450
or those books published after 2010.
For example, to select the EMPLOYEE tuples whose department is
4, or those whose salary is greater than $30,000, we can individually specify each
Dr.S.SRIDEVI, AP/CSE
30
VEC – IV Semester – II Year – 23IT204T –
CSE
of
these two conditions with a SELECT operation as follows:
σDno = 4(EMPLOYEE)
σSalary > 30000(EMPLOYEE)
AND,OR
σ(Dno = 4 AND Salary > 25000) (EMPLOYEE)
SQL Query:
SELECT * FROM EMPLOYEE WHERE Dno=4 AND Salary>25000;
σ(Dno = 4 AND Salary > 25000) OR (Dno = 5 AND Salary > 30000) (EMPLOYEE)
∏Lname,Fname,Salary (EMPLOYEE)
Selects and projects columns named as Lname,Fname and Salary from the
relation EMPLOYEE.
If the attribute list includes only non-key attributes of R, duplicate tuples are
likely to
occur. The PROJECT operation removes any duplicate tuples, so the result of the
PROJECT operation is a set of distinct tuples, and hence a valid relation. This is
known as duplicate elimination. For example, consider the following PROJECT
operation:
Rename Operation
Dr.S.SRIDEVI, AP/CSE
32
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
33
VEC – IV Semester – II Year – 23IT204T –
CSE
In general, the result of R(A1, A2, ..., An) × S(B1, B2, ..., Bm) is a relation Q with
degree n + m attributes Q(A1, A2, ..., An, B1, B2, ..., Bm), in that order.
The resulting relation Q has one tuple for each combination of tuples—one from R
and one from S. Hence, if R has n R tuples (denoted as |R| = nR), and S has nS
tuples,
then R × S will have nR * nS tuples.
Dr.S.SRIDEVI, AP/CSE
34
VEC – IV Semester – II Year – 23IT204T –
CSE
The result of set difference operation is tuples, which are present in one relation
but are not in the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
The results of relational algebra are also relations but without any name. The
rename operation allows us to rename the output relation. 'rename' operation is
denoted with small Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
Set intersection
Assignment
Natural join
Dr.S.SRIDEVI, AP/CSE
35
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
36
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
37
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
38
VEC – IV Semester – II Year – 23IT204T –
CSE
Used to combine related tuples from two relations into single “longer”
tuples.
The general form is:
R S
<join condition>
In JOIN, only combinations of tuples satisfying the join condition appear in the
result, whereas in the CARTESIAN PRODUCT all combinations of tuples are
included in the result.
Example: Retrieve the name of the manager of each department.
To get the manager’s name, we need to combine each department tuple with the
employee tuple whose Ssn value matches the Mgr_ssn value in the department
tuple. We do this by using the JOIN operation and then projecting the result over
the necessary attributes, as follows:
Note that Mgr_ssn is a foreign key of the DEPARTMENT relation that references
Ssn, the primary key of the EMPLOYEE relation. This referential integrity
constraint plays a role in having matching tuples in the referenced relation
EMPLOYEE.
The JOIN operation can be specified as a CARTESIAN PRODUCT operation
followed by a SELECT operation. However, JOIN is very important because it is
used very frequently when specifying database queries. Consider the earlier
example illustrating CARTESIAN PRODUCT, which included the following
sequence of operations:
EMP_DEPENDENTS EMPNAMES X DEPENDENT
ACTUAL_DEPENDENTS σSsn =Essn (EMP_DEPENDENTS)
These two operations can be replaced with a single JOIN operation as follows:\
Dr.S.SRIDEVI, AP/CSE
39
VEC – IV Semester – II Year – 23IT204T –
CSE
Example:
student(sid, sname,age,addr,deptno)
dept(dno,dname,head,noofemployees)
course(cid,title,sid)
select sname from student, dept where dname = ‘CSE’ and student.deptno =
dept.dno
Query 1. Retrieve the name and address of all employees who work for the
‘Research’ department.
Query 2. For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s last name,
Dr.S.SRIDEVI, AP/CSE
40
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
41
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
42
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
43
VEC – IV Semester – II Year – 23IT204T –
CSE
Division Operator (÷): Division operator A÷B can be applied if and only if:
Attributes of B is proper subset of Attributes of A.
Dr.S.SRIDEVI, AP/CSE
44
VEC – IV Semester – II Year – 23IT204T –
CSE
ROLL_N
O
The DIVISION operation, denoted by ÷, is useful for a special kind of query that
sometimes occurs in database applications.
An example is Retrieve the names of employees who work on all the projects that
‘John Smith’ works on. To express this query using the DIVISION operation,
proceed as follows. First, retrieve the list of project numbers that ‘John Smith’
works on in the intermediate relation SMITH_PNOS:
Dr.S.SRIDEVI, AP/CSE
45
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
46
VEC – IV Semester – II Year – 23IT204T –
CSE
Query 3. Find the names of employees who work on all the projects
controlled
by department number 5.
Dr.S.SRIDEVI, AP/CSE
47
VEC – IV Semester – II Year – 23IT204T –
CSE
DEPT
5_PROJS RESULT_EMP_SSNS
Pn Ss
o n
RESULT
Lname Fname
Dr.S.SRIDEVI, AP/CSE
48
VEC – IV Semester – II Year – 23IT204T –
CSE
In this query, we retrieved the project numbers for projects that involve an
employee named Smith as a worker in SMITH_WORKER_PROJS.
SMITHS(Essn) ∏Ssn(σLname = ‘Smith’ (EMPLOYEE))
Then we retrieved the project numbers for projects that involve an employee
named Smith as manager of the department that controls the project in
SMITH_MGR_PROJS.
SMITH_WORKER_PROJS ∏Pno(WORKS_ON * SMITHS)
Dr.S.SRIDEVI, AP/CSE
49
VEC – IV Semester – II Year – 23IT204T –
CSE
Another common type of request involves grouping the tuples in a relation by the
value of some of their attributes and then applying an aggregate function
independently to each group.
An example would be to group EMPLOYEE tuples by Dno, so that each group
includes the tuples for employees working in the same department. We can then
list each Dno value along with, say, the average salary of employees within the
Query 5. List the names of all employees with two or more dependents.
We have to use the AGGREGATE FUNCTION operation with the COUNT
aggregate function. We assume that dependents of the same employee have
distinct Dependent_name values.
Dr.S.SRIDEVI, AP/CSE
50
VEC – IV Semester – II Year – 23IT204T –
CSE
Query 7. List the names of managers who have atleast one dependent.
In this query, we retrieve the Ssns of managers in MGRS, and the Ssns of
employees with at least one dependent in EMPS_WITH_DEPS, then we apply the
SET INTERSECTION operation to get the Ssns of managers who have at least one
dependent.
modifications and deletions are allowed in the relation. These are the restrictions
we impose on the relational database.
Explanation:
In the above relation, Name is a composite attribute and Phone is a multi-values
attribute, so it is violating domain constraint.
Dr.S.SRIDEVI, AP/CSE
52
VEC – IV Semester – II Year – 23IT204T –
CSE
Explanation:
In the above table, EID is the primary key, and first and the last tuple has the
same value in EID ie 01, so it is violating the key constraint.
Explanation:
In the above relation, EID is made primary key, and the primary key can’t take
NULL values but in the third tuple, the primary key is null, so it is a violating
Entity Integrity constraints.
Dr.S.SRIDEVI, AP/CSE
53
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
54
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
55
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
56
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
57
VEC – IV Semester – II Year – 23IT204T –
CSE
BASIS FOR
SUPER KEY CANDIDATE KEY
COMPARISON
One in other It is not compulsory that all All candidate keys are
keys.
Selection The set of super keys forms The set of candidate keys
primary key.
relation.
Dr.S.SRIDEVI, AP/CSE
58
VEC – IV Semester – II Year – 23IT204T –
CSE
SQL QUERIES
Query 1. Retrieve the name and address of all employees who work for the ‘Research’
department.
Q1: SELECT Fname, Lname, Address
FROM EMPLOYEE, DEPARTMENT
WHERE Dname=‘Research’ AND Dnumber=Dno;
Query 4. Make a list of all project numbers for projects that involve an employee whose
last name is ‘Smith’, either as a worker or as a manager of the department that controls
the project.
Q4A: (SELECT DISTINCT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn
AND Lname=‘Smith’ )
Dr.S.SRIDEVI, AP/CSE
59
VEC – IV Semester – II Year – 23IT204T –
CSE
UNION
( SELECT DISTINCT Pnumber
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber=Pno AND Essn=Ssn
AND Lname=‘Smith’ );
Query 5: List the names of all employees with two or more dependents
SELECT E.EmpName
FROM Emp E
JOIN Dependent D ON E.EmpId = D.EmpId
GROUP BY E.EmpId, E.EmpName
HAVING COUNT(D.DependentId) >= 2;
Query 6. Retrieve the names of employees who have no dependents.
Q6: SELECT Fname, Lname
FROM EMPLOYEE
WHERE NOT EXISTS ( SELECT *
FROM DEPENDENT
WHERE Ssn=Essn );
Query 7. List the names of managers who have at least one dependent.
Q7: SELECT Fname, Lname
FROM EMPLOYEE
WHERE EXISTS ( SELECT *
FROM DEPENDENT
WHERE Ssn=Essn )
AND
EXISTS ( SELECT *
FROM DEPARTMENT
WHERE Ssn=Mgr_ssn );
Query 8. For each employee, retrieve the employee’s first and last name and the first
and last name of his or her immediate supervisor.
Q8: SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;
Query 18. Retrieve the names of all employees who do not have supervisors.
Q18: SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;
Query 19. Find the sum of the salaries of all employees, the maximum salary, the
minimum salary, and the average salary.
Q19: SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
FROM EMPLOYEE;
Queries 21 and 22. Retrieve the total number of employees in the company (Q21) and
the number of employees in the ‘Research’ department (Q22).
Q21: SELECT COUNT (*)
FROM EMPLOYEE;
Q22: SELECT COUNT (*)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER AND DNAME=‘Research’;
ASSIGNMENT 1:
Dr.S.SRIDEVI, AP/CSE
60
VEC – IV Semester – II Year – 23IT204T –
CSE
1) Consider the following relational schemas for the employee database. CO1
C5
Employee(ename,city,street)
Works(ename,companyname,salary)
Company(companyname,city)
Manages(ename,mname)
Write both the SQL and relational algebra query for the following:
1. Find the names of all employees who work for the ‘fbc’.
2. Find the names, street addresses and cities of all employees who work for the ‘fbc’ and
earn more than 200000 per annum.
3. Find the names of all employees in this database who live in the same city as the
company for which they work.
4. Find the names of all employees who earn more than every employee of sbc.
5. Find the names of all employees who do not work for fbc.
6. Find the names of all employees who do not have manager.
7. Find the names of all employees who do have manager.
8. Find the companyname which has got the highest total salary.
9. Find the names of all employees whose first letter is ‘a’.
10. Delete the record of employee ‘john’.
11. Display the details of all employees in a sorted order.
12. Find the names of all employees who are living in Chennai.
13. Find the names of all employees who are not living in Bangalore and Hyderabad.
14. Find the names of all employees where fifth letter is ‘y’.
15. Change the name of the companyname column to cname.
SQL :
ENAME
--------------------
ajith
siva
Relational Algebra:
∏ename(σcompanyname=’fbc’(works))
2. Find the names, street addresses and cities of all employees who work for the ‘fbc’ and
earn more than 200000 per annum.
SQL :
SQL> select ename,street,city from employee where ename in (select ename from works
Dr.S.SRIDEVI, AP/CSE
61
VEC – IV Semester – II Year – 23IT204T –
CSE
or
Relational Algebra:
(works))
t2 ∏ename,city,street(t1 * Employee)
3. Find the names of all employees in this database who live in the same city as the
company for which they work.
SQL :
ENAME
--------------------
ajith
john
siva
vijay
Dr.S.SRIDEVI, AP/CSE
62
VEC – IV Semester – II Year – 23IT204T –
CSE
or
ENAME
--------------------
ajith
john
siva
vijay
Relational Algebra:
t1 ∏ename,city(Employee)
t2 ∏ename,companyname(Works)
t3 ∏ companyname,city(Company)
t4 t1 t1.ename = t3.ename t3
t5 t2 t2.companyname = t3.companyname t3
t6 t4 t4.city = t5.city t5
Result ∏ename(t6)
4. Find the names of all employees who earn more than every employee of sbc.
SQL :
SQL> select ename from works where salary>(select max(salary) from works where
companyname='sbc');
ENAME
--------------------
ajith
Dr.S.SRIDEVI, AP/CSE
63
VEC – IV Semester – II Year – 23IT204T –
CSE
vijay
siva
Relational Algebra:
5. Find the names of all employees who do not work for fbc.
SQL :
SQL> select ename from works where companyname not in ('fbc');
ENAME
--------------------
john
vijay
or
ENAME
--------------------
john
vijay
Relational Algebra:
∏ename(σcompanyname != ’fbc’(works))
6. Find the names of all employees who do not have manager.
SQL :
ENAME
--------------------
john
siva
Relational Algebra:
∏ename(σmname = ’NULL’(manages))
Dr.S.SRIDEVI, AP/CSE
64
VEC – IV Semester – II Year – 23IT204T –
CSE
SQL :
SQL> select ename from manages where mname is not null;
ENAME
--------------------
ajith
vijay
Relational Algebra:
∏ename(σmname != ’NULL’(manages))
8. Identify all possible primary key and foreign key.
SQL :
Relational Algebra:
9. Create a view to store the companyname and the number of employees in each
company and do all possible manipulations on the view. If not justify your answer.
SQL :
SQL> create view v1(cname,no_of_employees) as (select companyname,count(ename)
from works group by companyname);
View created.
CNAME NO_OF_EMPLOYEES
-------------------- ---------------
bbc 1
fbc 2
sbc 1
10. Find the companyname which has got the highest total salary.
SQL :
Dr.S.SRIDEVI, AP/CSE
65
VEC – IV Semester – II Year – 23IT204T –
CSE
companyname));
COMPANYNAME
--------------------
fbc
Relational Algebra:
11. Find the names of all employees whose first letter is ‘a’.
SQL :
SQL> select ename from employee where ename like 'a%';
ENAME
--------------------
ajith
Relational Algebra:
∏ename(σename = ’a%’(employee))
12. Delete the record of employee ‘john’.
SQL :
SQL> delete from employee where ename='john';
1 row deleted.
Dr.S.SRIDEVI, AP/CSE
66
VEC – IV Semester – II Year – 23IT204T –
CSE
Relational Algebra:
14. Find the names of all employees who are living in Chennai.
SQL :
SQL> select ename from employee where city in 'chennai';
ENAME
--------------------
ajith
siva
Relational Algebra:
∏ename(σcity = ’chennai’(employee))
15. Find the names of all employees who are not living in Bangalore and Hyderabad.
SQL :
ENAME
--------------------
ajith
siva
(employee))
Relational Algebra:
Dr.S.SRIDEVI, AP/CSE
67
VEC – IV Semester – II Year – 23IT204T –
CSE
1 row updated.
Table altered.
19. Find the names of all employees where fifth letter is ‘y’.
SQL :
SQL> select ename from employee where ename like '____y%';
ENAME
--------------------
vijay
Dr.S.SRIDEVI, AP/CSE
68
VEC – IV Semester – II Year – 23IT204T –
CSE
Relational Algebra:
∏ename(σename = ’____y%’(employee))
20. Find the names of all employees whose salary is in the range 100000 to 200000.
SQL :
SQL> select ename from works where salary between 100000 and 200000;
ENAME
--------------------
john
Relational Algebra:
View created.
ENAME COMPANYNAME
-------------------- --------------------
ajith fbc
john sbc
vijay bbc
siva fbc
SQL :
Table altered.
Dr.S.SRIDEVI, AP/CSE
69
VEC – IV Semester – II Year – 23IT204T –
CSE
Dr.S.SRIDEVI, AP/CSE
70