Mod 2
Mod 2
BCS403
MODULE-2
Relational Model
Course Coordinator
Dr Archana R A
6/19/24 1
Department of Computer Science and Engg
Transform Here
Slide 5- 2
Department of Computer Science and Engg
Transform Here
Informal Definitions
Transform Here
Example of a Relation
Slide 5- 4
Department of Computer Science and Engg
Transform Here
Informal Definitions
• Key of a Relation:
•Each row has a value of a data item (or set of
items) that uniquely identifies that row in the
table
•Called the key
•In the STUDENT table, SSN is the key
Slide 5- 5
Department of Computer Science and Engg
Transform Here
Slide 5- 6
Department of Computer Science and Engg
Transform Here
Formal Definitions -
Tuple
•A tuple is an ordered set of values (enclosed in angled
brackets ‘< … >’)
•Each value is derived from an appropriate domain.
•A row in the CUSTOMER relation is a 4-tuple and
would consist of four values, for example:
• <632895, "John Smith", "101 Main St. Atlanta, GA
30332", "(404) 894-2000">
• This is called a 4-tuple as it has 4 values
• A tuple (row) in the CUSTOMER relation.
•A relation is a set of such tuples (rows)
Slide 5- 7
Department of Computer Science and Engg
Transform Here
Transform Here
Formal Definitions -
State
• The relation state is a subset of the Cartesian product of the
domains of its attributes
•each domain contains the set of all
possible values the attribute can take.
• Example: attribute Cust-name is defined over the domain
of character strings of maximum length 25
•dom(Cust-name) is varchar(25)
• The role these strings play in the CUSTOMER relation is that of the
name of a customer.
Slide 5- 9
Department of Computer Science and Engg
Transform Here
•Formally,
• Given R(A1, A2, .........., An)
• r(R) ⊂ dom (A1) X dom (A2) X ....X dom(An)
•R(A1, A2, …, An) is the schema of the relation
•R is the name of the relation
•A1, A2, …, An are the attributes of the relation
•r(R): a specific state (or "value" or “population”) of
relation
R – this is a set of tuples (rows)
• r(R) = {t1, t2, …, tn} where each ti is an n-tuple
• ti = <v1, v2, …, vn> where each vj element-of dom(Aj)
Slide 5- 10
Department of Computer Science and Engg
Transform Here
Transform Here
Definition Summary
Informal Terms Formal Terms
Table Relation
Column Header Attribute
All possible Column Domain
Values
Row Tuple
Transform Here
Slide 5- 13
Department of Computer Science and Engg
Transform Here
Characteristics Of Relations
• Ordering of tuples in a relation r(R):
•The tuples are not considered to be ordered,
even though they appear to be in the tabular
form.
• Ordering of attributes in a relation schema R (and of values
within each tuple):
•We will consider the attributes in R(A1, A2,
..., An) and the values in t=<v1, v2, ..., vn>
to be ordered .
• (However, a more general alternative definition of relation does not require this
ordering).
Slide 5- 14
Department of Computer Science and Engg
Transform Here
Slide 5- 15
Department of Computer Science and Engg
Transform Here
Characteristics Of Relations
• Values in a tuple:
•All values are considered atomic (indivisible).
•Each value in a tuple must be from the domain
of the attribute for that column
• If tuple t = <v1, v2, …, vn> is a tuple (row) in the relation state r of R(A1, A2, …, An)
• Then each vi must be a value from dom(Ai)
Slide 5- 16
Department of Computer Science and Engg
Transform Here
Characteristics Of Relations
• Notation:
•We refer to component values of a tuple t by:
• t[Ai] or t.Ai
• This is the value vi of attribute Ai for tuple t
Slide 5- 17
Department of Computer Science and Engg
Transform Here
Transform Here
Key
Constraints
•Superkey of R:
• Is a set of attributes SK of R with the following condition:
• No two tuples in any valid relation state r(R) will have the
same value for SK
• That is, for any distinct tuples t1 and t2 in r(R), t1[SK] ≠ t2[SK]
• This condition must hold in any valid state r(R)
•Key of R:
• A "minimal" superkey
• That is, a key is a superkey K such that removal of any
attribute from K results in a set of attributes that is not a
superkey (does not possess the superkey uniqueness
property)
Slide 5- 19
Department of Computer Science and Engg
Transform Here
Slide 5- 20
Department of Computer Science and Engg
Transform Here
Slide 5- 22
Department of Computer Science and Engg
Transform Here
Slide 5- 23
Department of Computer Science and Engg
Transform Here
Slide 5- 24
Department of Computer Science and Engg
Transform Here
Entity Integrity
•Entity Integrity:
•The primary key attributes PK of each relation schema
R in S cannot have null values in any tuple of r(R).
• This is because primary key values are used to identify the
individual tuples.
• t[PK] ≠ null for any tuple t in r(R)
• If PK has several attributes, null is not allowed in any of these
attributes
•Note: Other attributes of R may be constrained to
disallow null values, even though they are not
members of the primary key.
Slide 5- 25
Department of Computer Science and Engg
Transform Here
Referential Integrity
Slide 5- 26
Department of Computer Science and Engg
Transform Here
Referential Integrity
Slide 5- 27
Department of Computer Science and Engg
Transform Here
Slide 5- 28
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 5- 30
Department of Computer Science and Engg
Transform Here
Slide 5- 31
Department of Computer Science and Engg
Transform Here
Populated database
state
•Each relation will have many tuples in its current
relation state
•The relational database state is a union of all the
individual relation states
•Whenever the database is changed, a new state arises
•Basic operations for changing the database:
• INSERT a new tuple in a relation
• DELETE an existing tuple from a relation
• MODIFY an attribute of an existing tuple
•Next slide shows an example state for the COMPANY
database
Slide 5- 32
Populated database state for
COMPANY
Transform Here
• INSERT a tuple.
• DELETE a tuple.
• MODIFY a tuple.
• Integrity constraints should not be violated by the update
operations.
• Several update operations may have to be grouped together.
• Updates may propagate to cause other updates automatically. This
may be necessary to maintain integrity constraints.
Slide 5- 34
Department of Computer Science and Engg
Transform Here
Slide 5- 35
Department of Computer Science and Engg
Transform Here
Slide 5- 36
Department of Computer Science and Engg
Transform Here
Slide 5- 37
Department of Computer Science and Engg
Transform Here
Slide 5- 38
Department of Computer Science and Engg
Transform Here
Slide 6- 39
Department of Computer Science and Engg
Transform Here
Slide 6- 40
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 6- 42
Department of Computer Science and Engg
Transform Here
Slide 6- 43
Department of Computer Science and Engg
Transform Here
Slide 6- 44
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 6- 46
Department of Computer Science and Engg
Transform Here
Slide 6- 47
Department of Computer Science and Engg
Transform Here
Slide 6- 48
Department of Computer Science and Engg
Transform Here
Slide 6- 49
Department of Computer Science and Engg
Transform Here
Slide 6- 50
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 6- 52
Department of Computer Science and Engg
Transform Here
Slide 6- 53
Department of Computer Science and Engg
Transform Here
•ρS(R) changes:
• the relation name only to S
Slide 6- 54
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 6- 56
Department of Computer Science and Engg
Transform Here
Transform Here
Transform Here
Slide 6- 59
Department of Computer Science and Engg
Transform Here
Slide 6- 60
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 6- 62
Department of Computer Science and Engg
Transform Here
Slide 6- 63
Department of Computer Science and Engg
Transform Here
Slide 6- 64
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 6- 66
Department of Computer Science and Engg
Transform Here
Slide 6- 67
Department of Computer Science and Engg
Transform Here
Slide 6- 68
Department of Computer Science and Engg
Transform Here
(cont.)
•Example: Suppose that we want to 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 MGRSSN value in the department tuple.
• We do this by using the join operation.
• DEPT_MGR ← EMPLOYEE
MGRSSN=SSN
DEPARTMENT
•MGRSSN=SSN is the join condition
• Combines each department record with the employee who
manages the department
• The join condition can also be specified as
DEPARTMENT.MGRSSN= EMPLOYEE.SSN
Slide 6- 70
Department of Computer Science and Engg
Transform Here
Slide 6- 71
Department of Computer Science and Engg
Transform Here
Slide 6- 72
Department of Computer Science and Engg
Transform Here
Slide 6- 73
Department of Computer Science and Engg
Transform Here
Slide 6- 75
Department of Computer Science and Engg
JOIN
(contd.)
• Example: To apply a natural join on the DNUMBER attributes of
DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write:
• DEPT_LOCS ← DEPARTMENT * DEPT_LOCATIONS
• Only attribute with the same name is DNUMBER
• An implicit join condition is created based on this attribute:
DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER
Slide 6- 76
Department of Computer Science and Engg
Transform Here
Slide 6- 77
Department of Computer Science and Engg
Transform Here
Slide 6- 78
Department of Computer Science and Engg
Transform Here
•DIVISION Operation
• The division operation is applied to two relations
• R(Z) ÷ S(X), where X subset Z. Let Y = Z - X (and hence Z = X
∪ Y); that is, let Y be the set of attributes of R that are not
attributes of S.
Transform Here
Example of DIVISION
Slide 6- 80
Department of Computer Science and Engg
Transform Here
Slide 6- 81
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 6- 83
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 6- 85
Department of Computer Science and Engg
Transform Here
Slide 6- 86
Department of Computer Science and Engg
Transform Here
Slide 6- 87
Department of Computer Science and Engg
Transform Here
Slide 6- 88
Department of Computer Science and Engg
Transform Here
Slide 6- 89
Department of Computer Science and Engg
Transform Here
Slide 6- 90
Department of Computer Science and Engg
Transform Here
Slide 6- 91
Department of Computer Science and Engg
Transform Here
Slide 6- 92
Department of Computer Science and Engg
Transform Here
Transform Here
Transform Here
EMPS_WITH_DEPS(SSN) ← π
ESSN (DEPENDENT)
EMPS_WITHOUT_DEPS ← (ALL_EMPS - EMPS_WITH_DEPS)
RESULT ← π LNAME, FNAME (EMPS_WITHOUT_DEPS *
EMPLOYEE)
Slide 6- 95
Department of Computer Science and Engg
Transform Here
Slide 8- 96
Department of Computer Science and Engg
Transform Here
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
DNAME VARCHAR(10) NOT NULL,
DNUMBER
CREATE DEPARTMENT ( NOT NULL,
INTEGER
TABLE
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );
Slide 8- 97
Department of Computer Science and Engg
Transform Here
CREATE
TABLE
• 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
CREATE
DNAME TABLE DEPTVARCHAR(10)
( NOT
NULL,
DNUMBER INTEGERNOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
);
Slide 8- 98
Department of Computer Science and Engg
TABLE
• Used to remove a relation (base table) and its definition
• The relation can no longer be used in queries, updates, or any
other
commands since its description no longer exists
• Example:
Slide 8- 99
Department of Computer Science and Engg
Transform Here
ALTER TABLE
• Used to add an attribute to one of the base relations
•The new attribute will have NULLs in all the tuples of
the relation right after the command is executed;
hence, the NOT NULL constraint is not allowed
for such an attribute
• Example:
ALTER TABLE EMPLOYEE ADD JOB
VARCHAR(12);
• 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.
Slide 8- 100
Department of Computer Science and Engg
Transform Here
Slide 8- 101
Department of Computer Science and Engg
Transform Here
CREATE SCHEMA
• Specifies a new database schema by giving it a name
Slide 8- 102
Department of Computer Science and Engg
Transform Here
OPTIONS
(continued)
CREATE TABLE EMP(
ENAME VARCHAR(30) NOT NULL,
ESSN CHAR(9),
BDATE DATE,
DNO INTEGER DEFAULT 1,
SUPERSSNCHAR(9),
PRIMARY KEY (ESSN),
FOREIGN KEY (DNO) REFERENCES DEPT
ON DELETE SET DEFAULT ON UPDATE
CASCADE,
FOREIGN KEY (SUPERSSN) REFERENCES EMP
ON DELETE SET NULL ON UPDATE
Slide 8- 104
CASCADE);
Department of Computer Science and Engg
Transform Here
Slide 8- 105
Department of Computer Science and Engg
Transform Here
Transform Here
Retrieval Queries in
SQL
•SQL has one basic statement for retrieving Computer 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 Slide 8- 107
Department of Computer Science and Engg
Transform Here
Slide 8- 108
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 8- 110
Department of Computer Science and Engg
Transform Here
Slide 8- 111
Department of Computer Science and Engg
Transform Here
Transform Here
Transform Here
• EMPLOYEE.LNAME, DEPARTMENT.DNAME
Slide 8- 114
Department of Computer Science and Engg
Transform Here
ALIASES
• Some queries need to refer to the same relation twice
• In this case, aliases are given to the relation name
• Query 8: For each employee, retrieve the employee's name, and the
name of his or her immediate supervisor.
Q8: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE E S
WHERE E.SUPERSSN=S.SSN
• In Q8, the alternate relation names E and S are called aliases or tuple
variables for the EMPLOYEE relation
• We can think of E and S as two different copies of EMPLOYEE;
E represents employees in role of supervisees and S
represents employees in role of supervisors
Slide 8- 115
Department of Computer Science and Engg
Transform Here
ALIASES
(contd.)
• Aliasing can also be used in any SQL query for
convenience
• Can also use the AS keyword to specify aliases
Slide 8- 116
Department of Computer Science and Engg
WHERE-claus
e
•A missing WHERE-clause indicates no condition; hence,
all tuples of the relations in the FROM-clause are
selected
• This is equivalent to the condition WHERE TRUE
•Query 9: Retrieve the SSN values for all employees.
• Q9: SELECT SSN
FROM EMPLOYEE
Slide 8- 117
Department of Computer Science and Engg
WHERE-clause
(contd.)
• Example:
Slide 8- 118
Department of Computer Science and Engg
Transform Here
USE OF *
Slide 8- 119
Department of Computer Science and Engg
Transform Here
USE OF DISTINCT
Slide 8- 120
Department of Computer Science and Engg
Transform Here
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
Slide 8- 121
Department of Computer Science and Engg
Transform Here
• Query 4: 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 department that controls the project.
Slide 8- 122
Department of Computer Science and Engg
Transform Here
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
• Many of the previous queries can be specified in an
alternative form using nesting
•Query 1: Retrieve the name and address of all
employees who work for the 'Research' department.
Slide 8- 123
Department of Computer Science and Engg
Transform Here
Transform Here
Transform Here
Slide 8- 126
Department of Computer Science and Engg
Transform Here
Transform Here
Slide 8- 128
Department of Computer Science and Engg
Transform Here
Slide 8- 129
Department of Computer Science and Engg
Transform Here
Slide 8- 130
Department of Computer Science and Engg
Transform Here
Slide 8- 131
Department of Computer Science and Engg
Transform Here
Slide 8- 132
Department of Computer Science and Engg
Transform Here
EXPLICIT SETS
Slide 8- 133
Department of Computer Science and Engg
Transform Here
Slide 8- 134
Department of Computer Science and Engg
Transform Here
Joined Relations
Feature in SQL2
• Can specify a "joined relation" in the FROM-clause
•Looks like any other relation but is the result of a
join
•Allows the user to specify different types of joins
(regular "theta" JOIN, NATURAL JOIN, LEFT OUTER
JOIN, RIGHT OUTER JOIN, CROSS JOIN,
•etc)
Slide 8- 135
Department of Computer Science and Engg
Transform Here
Joined Relations
Feature in SQL2
(contd.)
•Examples:
Q8: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE E S
WHERE E.SUPERSSN=S.SSN
Slide 8- 136
Department of Computer Science and Engg
Feature in SQL2
(contd.)
•Examples:
Q1: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND DNUMBER=DNO
•could be written as:
Q1: SELECT FNAME, LNAME, ADDRESS FROM
ON DNUMBER=DNO)
(EMPLOYEE
WHERE JOIN DEPARTMENT
DNAME='Research’
•or as:
Q1: SELECT FNAME, LNAME, ADDRESS
FROM (EMPLOYEE NATURAL JOIN DEPARTMENT
AS DEPT(DNAME, DNO, MSSN,
WHERE MSDATE)
DNAME='Research’
Slide 8- 137
Department of Computer Science and Engg
Feature in SQL2
(contd.)
• Another Example: Q2 could be written as follows; this
illustrates multiple joins in the joined tables
Q2: SELECT PNUMBER, DNUM,
LNAME, BDATE, ADDRESS
FROM(PROJECT JOIN
DEPARTMENT ON
DNUM=DNUMBER) JOIN
EMPLOYEE ON
MGRSSN=SSN) )
WHERE PLOCATION='Stafford’
Slide 8- 138
Department of Computer Science and Engg
Transform Here
AGGREGATE FUNCTIONS
• Some SQL implementations may not allow more than one function
in the SELECT-clause
Slide 8- 139
Department of Computer Science and Engg
Transform Here
• Query 16: Find the maximum salary, the minimum salary, and
the average salary among employees who work for the
'Research' department.
Q16: SELECT MAX(SALARY),
MIN(SALARY),
FROM EMPLOYEE,
AVG(SALARY)
WHERE DEPARTMENT
DNAME='Research
DNO=DNUMBER AND
'
Slide 8- 140
Department of Computer Science and Engg
Transform Here
Slide 8- 141
Department of Computer Science and Engg
Transform Here
GROUPING
• In many cases, we want to apply the aggregate functions to
subgroups of tuples in a relation
• Each subgroup of tuples consists of the set of tuples that have the
same value for the grouping attribute(s)
• The function is applied to each subgroup independently
• SQL has a GROUP BY-clause for specifying the grouping
attributes,
which must also appear in the SELECT-clause
Slide 8- 142
Department of Computer Science and Engg
Transform Here
GROUPING
(contd.)
•Query 20: For each department, retrieve the department
number, the number of employees in the department,
and their average salary.
Q20: SELECT DNO, COUNT (*), AVG (SALARY)
FROM EMPLOYEE
GROUP BY DNO
• In Q20, the EMPLOYEE tuples are divided into groups-
• Each group having the same value for the grouping attribute DNO
• The COUNT and AVG functions are applied to each such
group of tuples separately
• The SELECT-clause includes only the grouping attribute and
the
functions to be applied on each group of tuples
• A join condition can be used in conjunction with grouping Slide 8- 143
Department of Computer Science and Engg
Transform Here
GROUPING (contd.)
Slide 8- 144
Department of Computer Science and Engg
Transform Here
THE HAVING-CLAUSE
• Sometimes we want to retrieve the values of these functions for
only those groups that satisfy certain conditions
• The HAVING-clause is used for specifying a selection condition on
groups (rather than on individual tuples)
Slide 8- 145
Department of Computer Science and Engg
Transform Here
Slide 8- 146
Department of Computer Science and Engg
Transform Here
SUBSTRING COMPARISON
• The LIKE comparison operator is used to compare partial strings
• Two reserved characters are used: '%' (or '*' in some
implementations) replaces an arbitrary number of characters, and
'_' replaces a single arbitrary character
Slide 8- 147
Department of Computer Science and Engg
Transform Here
Slide 8- 148
Department of Computer Science and Engg
Transform Here
Transform Here
ARITHMETIC OPERATIONS
•The standard arithmetic operators '+', '-'. '*', and '/'
(for addition, subtraction, multiplication, and division,
respectively) can be applied to numeric values in an
SQL query result
•Query 27: Show the effect of giving all employees who
work on the 'ProductX' project a 10% raise.
Q27: SELECT FNAME, LNAME, 1.1*SALARY
FROM EMPLOYEE,
PROJECT
WORKS_ON,
WHERE SSN=ESSN AND PNO=PNUMBER
AND PNAME='ProductX’
Slide 8- 150
Department of Computer Science and Engg
Transform Here
ORDER
BY
•The ORDER BY clause is used to sort the tuples in a
query result based on the values of some attribute(s)
•Query 28: Retrieve a list of employees and the projects each
works in, ordered by the employee's department, and
within each department ordered alphabetically by
employee
Q28: last name.
SELECT DNAME, LNAME, FNAME, PNAME
FROM DEPARTMENT, EMPLOYEE,
WORKS_ON, PROJECT
WHERE DNUMBER=DNO AND SSN=ESSN
AND PNO=PNUMBER
ORDER BY DNAME, LNAME
Slide 8- 151
Department of Computer Science and Engg
Transform Here
ORDER BY (contd.)
• The default order is in ascending order of values
• We can specify the keyword DESC if we want a descending order;
the keyword ASC can be used to explicitly specify ascending
order, even though it is the default
Slide 8- 152
Department of Computer Science and Engg
Transform Here
Slide 8- 153
Department of Computer Science and Engg
Transform Here
Slide 8- 154
Department of Computer Science and Engg
Transform Here
Slide 8- 155
Department of Computer Science and Engg
Transform Here
INSERT
• In its simplest form, it is used to add one or more tuples to a
relation
• Attribute values should be listed in the same order as the attributes
were specified in the CREATE TABLE command
Slide 8- 156
Department of Computer Science and Engg
INSERT
Transform Here
(contd.)
•Example:
U1: INSERT INTO EMPLOYEE
VALUES ('Richard','K','Marini', '653298653', '30-DEC-52',
'98 Oak Forest,Katy,TX', 'M', 37000,'987654321', 4 )
INSERT
Transform Here
(contd.)
• Important Note: Only the constraints specified in the DDL commands
are automatically enforced by the DBMS when updates are
applied to the database
•Another variation of INSERT allows insertion
of multiple tuples resulting from a query into a
relation
Slide 8- 158
Department of Computer Science and Engg
Transform Here
INSERT
(contd.)
• Example: Suppose we want to create a temporary table that has the
name, number of employees, and total salaries for each department.
• A table DEPTS_INFO is created by U3A, and is loaded with the
summary Computer retrieved from the database by the query in
U3B.
U3A: CREATE TABLE DEPTS_INFO
(DEPT_NAME VARCHAR(10),
NO_OF_EMPS INTEGER,
TOTAL_SAL INTEGER);
U3B: INSERT INTO DEPTS_INFO (DEPT_NAME,
NO_OF_EMPS, TOTAL_SAL)
SELECT DNAME, COUNT (*), SUM (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME ;
Slide 8- 159
Department of Computer Science and Engg
(contd.)
• Note: The DEPTS_INFO table may not be up-to-date if we change the
tuples in either the DEPARTMENT or the EMPLOYEE relations after
issuing U3B. We have to create a view (see later) to keep such a
table up to date.
Slide 8- 160
Department of Computer Science and Engg
Transform Here
DELETE
•Removes tuples from a relation
• Includes a WHERE-clause to select the tuples to be deleted
• Referential integrity should be enforced
• Tuples are deleted from only one table at a time (unless
CASCADE is specified on a referential integrity
constraint)
• A missing WHERE-clause specifies that all tuples in the relation
are to be deleted; the table then becomes an empty table
• The number of tuples deleted depends on the number of
tuples in the relation that satisfy the WHERE-clause
Slide 8- 161
Department of Computer Science and Engg
(contd.)
• Examples:
U4A: DELETE FROM EMPLOYEE
WHERE LNAME='Brown’
U4B: DELETE FROM EMPLOYEE
WHERE SSN='123456789’
U4C: DELETE FROM EMPLOYEE
WHERE DNO IN
(SELECT DNUMBER
FROM
DEPARTMENT
WHERE
DNAME='Research')
U4D: DELETE FROM EMPLOYEE
Slide 8- 162
Department of Computer Science and Engg
Transform Here
UPDAT
E
• Used to modify attribute values of one or more selected tuples
• A WHERE-clause selects the tuples to be modified
• An additional SET-clause specifies the attributes to be modified and
their new values
• Each command modifies tuples in the same relation
• Referential integrity should be enforced
Slide 8- 163
Department of Computer Science and Engg
Transform Here
UPDATE
(contd.)
• Example: Change the location and controlling department number
of project number 10 to 'Bellaire' and 5, respectively.
U5: PROJECT
UPDAT PLOCATION =
DNUM = 5
E SET 'Bellaire',
WHERE PNUMBER=10
Slide 8- 164
Department of Computer Science and Engg
Transform Here
UPDATE
(contd.)
•Example: Give all employees in the 'Research' department
a 10% raise in salary.
U6: UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT
FROM DEPARTMENT
DNUMBER
WHERE DNAME='Research')
Transform Here
Transform Here
Contact Details:
Dr.Manjunath T N
Professor and Dean – ER
Department of Computer Science and Engg
BMS Institute of Technology and Management
Mobile: +91-9900130748
E-Mail: [email protected] / [email protected]
10-09-2020 167