Dbms Module II
Dbms Module II
The principles of the relational model were first outlined by Dr. E.F. Codd in 1970 in a classic paper called
“A relational Model of Data for Large Shared Data Banks”. In this paper, Dr. Codd proposed the
relational model for the database.
The more popular models used at the time were hierarchical and network, or even simple flat file data
structures. Relational database management systems soon became very popular especially for their
ease of use and flexibility in structure. In addition, a number of innovative vendors, such as Oracle,
supplemented the RDBMS with a suite of powerful application development and user products,
providing a total solution.
Database
EMP DEPT
Page 1
MODULE –II DBMS -18CS53
A relational database is a collection of relations or two-dimensional tables to store information.
For Example, you might want to store information about all the employees in your company. In a
relational database, you create several tables to store different pieces of information about your
employees, such as an employee table, a department table and a salary table
Informal Definitions
Example of a Relation
⚫ The data type describing the types of values that can appear in each columns is called a domain.
⚫ 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
Eg:- In the STUDENT table, SSN is the key.
⚫ Sometimes row-ids or sequential numbers are assigned as keys to identify the rows in a
table called artificial key or surrogate key
Page 2
MODULE –II DBMS -18CS53
Formal Definitions – Schema
Page 3
MODULE –II DBMS -18CS53
Characteristics of Relations
⚫ Ordering of tuples in a relation r(R):
⚫ Tuples ordering is not part of a relation definition because it is defined as a set of tuples.
⚫ Many logical orders can be specified on the relation.
⚫ There is no preference for one logical ordering over another.
⚫ When a relation is implemented as a file, a physical ordering may be specified on the
records of the file.
⚫ Ordering of values within each tuple:
⚫ The tuple is an ordered list, so the ordering of values in a tuple is important.
⚫ At a logical level, the order is not important as long as the correspondence between the
attribute and its value is mentioned.
- For example, schema of the STUDENT relation interprets that, a student entity has Name,
Page 4
MODULE –II DBMS -18CS53
Relational Model Notation
Schema-based constraints
Page 5
MODULE –II DBMS -18CS53
⚫ - It is a minimal superkey – that is , a superkey from which we cannot remove any
attributes and still have the uniqueness property in condition 1 hold.
⚫ Key is determined from the meaning of the attributes, and the property is time-
invariant.
⚫ It must continue to hold when we insert new tuples in the relation
⚫ Example:
⚫ The attribute {SSN} in the Student relation is a key.
⚫ Any set includes {SSN} is a superkey of the relation student. Eg:- {SSN, Name, Age}
⚫ In general:
⚫ Any key is a superkey (but not vice versa).
⚫ Any set of attributes that includes a key is a superkey.
⚫ A minimal superkey is also a key.
⚫ A relation schema may have more than one key.
⚫ Such keys are called the candidate keys.
⚫ The primary key is one of the candidate keys which is selected to identify tuples in the
relation.
⚫ The attributes that form the primary key are underlined in the schema.
⚫ The primary key value is used to uniquely identify each tuple in a relation.
⚫ Null Constraint:
⚫ Not Null constraint specifies that an attribute must have a valid value (e.g. Student
name).
Relational Database Schema
⚫ A relational database schema S is a set of relation schemas that belong to the same database.
⚫ S is the name of the whole database schema.
⚫ S = {R1, R2, …, Rm} and a set of integrity constraints IC.
⚫ R1, R2, …, Rm are the names of the individual relation schemas within the database S.
⚫ The integrity constraints are specified on a DB schema and are expected to hold on every DB
state.
⚫ Example: Company Database
Page 6
MODULE –II DBMS -18CS53
Transition constraints
Ex: the salary of an employee can only increase. Such constraints typically enforced by
the application programs or specified using active rules and triggers.
Page 8
MODULE –II DBMS -18CS53
⚫ if the primary key value is null in the new tuple.
⚫ Example:-
⚫ Operation
Insert <‘Vinod’, ’S’, ‘Joseph’, NULL, ‘1986-04-05’, ‘#123, Bangalore’, F,
⚫ Operation
Insert <‘Alice’, ’J’, ‘Zelaya’, ‘999887777’, ‘1986-04-05’, ‘#334, Bangalore’, F,
⚫ Operation
Insert <‘Cecilia’, ’F’, ‘Kolonsky’, ‘677678989’, ‘1986-04-05’, ‘#454, Bangalore’,
⚫ Operation
Delete the EMPLOYEE tuple with SSN= ‘999887777’.
Page 9
MODULE –II DBMS -18CS53
⚫ Operation
Delete the EMPLOYEE tuple with SSN= ‘333445555’.
Operation
Result: Acceptable
Operation
⚫ Operation
Update the SSN of the EMPLOYEE tuple with SSN=‘999887777’ to ‘987654321’.
Page 10
MODULE –II DBMS -18CS53
ER-to-Relational Mapping
Step 1
» For each regular entity type E in the ER schema, create a relation R that includes all the simple
attributes of E
» If the chosen key was a composite, the set of simple attributes that form it will together be the
primary key of R.
Step 2
» For each weak entity type W in the ER schema with owner entity type E, create a relation R that
includes all simple attributes (or simple components of composites) of W.
» Include as foreign key attributes of R the primary key attribute of the relation that corresponds to the
owner entity type E.
» The primary key of R is the combination of the primary key of the owner and the partial key of the
weak entity type, if any.
Step 3
» For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that
correspond to the entity types participating in R.
» Choose one of the relations (say S). Include as a foreign key in S the primary key of T.
» It is better to choose an entity type with total participation in R for the role of S.
Step 4
» For each binary 1:N relationship type R, identify the relation S that represents the entity type
participating at the N-side of the relationship
» Include as a foreign key in S the primary key of the relation T that represents the other entity type
participating in R.
Page 11
MODULE –II DBMS -18CS53
Step 5
» For each binary M:N relationship type R, create a new relation S to represent R.
» Include as foreign key attributes in S the primary keys of the participating entity types.
Step 6
» R will include an attribute corresponding to A, plus the primary key attribute K of the relation that has
A as an attribute.
Step 7
» For each n-ary relationship type R, where n>2, create a new relation S.
» Include as foreign key attributes in S the primary keys of the relations that represent the participating
entity types
The ER model is convenient for representing an initial, high-level database design. Given an ER diagram
describing a databa'3e, a standard approach is taken to generating a relational database schema that
closely approximatesthe ER design. We now describe how to translate an ER diagram into a collection of
tables with associated constraints, that is, a relational database schema.
An entity set is mapped to a relation in a straightforward way: Each attribute of the entity set becomes
an attribute of the table. Note that we know both the domain of each attribute and the (primary) key of
an entity set.
Page 12
MODULE –II DBMS -18CS53
A relationship set, like an entity set, is mapped to a relation in the relational model.
To represent a relationship, we must be able to identify each participating entity and give values to the
descriptive attributes of the relationship. Thus, the attributes of the relation include:
• The primary key attributes of each participating entity set, as foreign key fields.
The set of nondescriptive attributes is a superkey for the relation. If there are no key constraints, this set
of attributes is a candidate key.
Page 13
MODULE –II DBMS -18CS53
CREATE TABLE Works_In2 ( ssn CHAR(11), did INTEGER, address CHAR(20) , since DATE, PRIMARY KEY
(8sn, did, address), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (address) REFERENCES
Locations, FOREIGN KEY (did) REFERENCES Departments);
Page 14
MODULE –II DBMS -18CS53
If a relationship set involves n entity sets and somem of them are linked via arrows in the ER diagTam,
the key for anyone of these m entity sets constitutes a key for the relation to which the relationship set
is mapped.
The following SQL statement, defining a DepLMgr relation that captures the information in both
Departments and Manages, illustrates the approach to translating relationship sets with key constraints:
CREATE TABLE DepLMgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR (11) , since DATE,
PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)
Consider the ER diagram in Figure 3.13, which shows two relationship sets, Manages and "Works_In.
Every department is required to have a manager, due to the participation constraint, and at most one
manager, due to the key constraint.
Page 15
MODULE –II DBMS -18CS53
It also captures the participation constraint that every department must have a manager: Because ssn
cannot take on null values, each tuple of Dep-Mgr identifies a tuple in Employees (who is the manager).
The NO ACTION specification, which is the default and need not be explicitly specified, ensures that an
Employees tuple cannot be deleted while it is pointed to by a Dept-Mgr tuple.
A weak entity set always participates in a one-to-many binary relationship and has a key constraint and
total participation.we must take into account that the weak entity has only a partial key. Also, when an
owner entity is deleted, we want all owned weak entities to be deleted.
Page 16
MODULE –II DBMS -18CS53
Page 17
MODULE –II DBMS -18CS53
3.5.8 ER to Relational: Additional Examples
CREATE TABLE Dependents (pnameCHAR(20) , age INTEGER, policyid INTEGER, PRIMARY KEY (pname,
policyid), FOREIGN KEY (policyid) REFERENCES Policies ON DELETE CASCADE);
Page 18
MODULE –II DBMS -18CS53
SQL
The SQL language may be considered one of the major reasons for the commercial success of relational
databases. The name SQL is presently expanded as Structured Query Language. Originally, SQL was
called SEQUEL (Structured English QUEry Language) and was designed and implemented at IBM
Research as the interface for an experimental relational database system called SYSTEM R. SQL is now
the standard language for commercial relational DBMSs.
SQL is a comprehensive database language: It has statements for data definitions, queries, and updates.
Hence, it is both a DDL and a DML. It also has rules for embedding SQL statements into a general-
purpose programming language such as Java, COBOL, or C/C++.
SQL uses the terms table, row, and column for the formal relational model terms relation, tuple, and
attribute, respectively. We will use the corresponding terms interchangeably. The main SQL command
for data definition is the CREATE statement, which can be used to create schemas, tables (relations), and
domains (as well as other constructs such as views, assertions, and triggers).
The concept of an SQL schema was incorporated starting with SQL2 in order to group together tables
and other constructs that belong to the same database application. An SQL schema is identified by a
schema name, and includes an authorization identifier to indicate the user or account who owns the
schema, as well as descriptors for each element in the schema. Schema elements include tables,
constraints, views, domains, and other constructs (such as authorization grants) that describe the
schema.
A schema is created via the CREATE SCHEMA statement, which can include all the schema elements’
definitions. Alternatively, the schema can be assigned a name and authorization identifier, and the
elements can be defined later. For example, the following statement creates a schema called COMPANY,
owned by the user with authorization identifier ‘Jsmith’.Note that each statement in SQL ends with a
semicolon.
In general, not all users are authorized to create schemas and schema elements. The privilege to create
schemas, tables, and other constructs must be explicitly granted to the relevant user accounts by the
system administrator or DBA.
Page 19
MODULE –II DBMS -18CS53
4.1.2 The CREATE TABLE Command in SQL
The CREATE TABLE command is used to specify a new relation by giving it a name and specifying its
attributes and initial constraints. The attributes are specified first, and each attribute is given a name, a
data type to specify its domain of values, and any attribute constraints, such as NOT NULL. The key,
entity integrity, and referential integrity constraints can be specified within the CREATE TABLE
statement after the attributes are declared, or they can be added later using the ALTER TABLE
command. we can explicitly attach the schema name to the relation name, separated by a period.
rather than
we can explicitly (rather than implicitly) make the EMPLOYEEtable part of the COMPANY schema.
The relations declared through CREATE TABLE statements are called base tables (or base relations); this
means that the relation and its tuples are actually created and stored as a file by the DBMS.Base
relations are distinguished from virtual relations, created through the CREATE VIEWstatement which
may or may not correspond to an actual physical file.
Example:
CREATE TABLE EMPLOYEE ( Fname VARCHAR(15) NOT NULL, Minit CHAR, Lname VARCHAR(15) NOT
NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Address VARCHAR(30), Sex CHAR, Salary DECIMAL(10,2),
Super_ssn CHAR(9), Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Super_ssn) REFERENCES
EMPLOYEE(Ssn), FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber)) ;
CREATE TABLE DEPARTMENT ( Dname VARCHAR(15) NOT NULL, Dnumber INT NOT NULL, Mgr_ssn
CHAR(9) NOT NULL, Mgr_start_date DATE, PRIMARY KEY (Dnumber), UNIQUE (Dname), FOREIGN KEY
(Mgr_ssn) REFERENCES EMPLOYEE(Ssn));
The basic data types available for attributes include numeric, character string, bit
string,Boolean,date,and time.
■ Numeric data types include integer numbers of various sizes (INTEGER or INT, and SMALLINT) and
floating-point (real) numbers of various precision (FLOAT or REAL, and DOUBLE PRECISION). Formatted
numbers can be declared by using DECIMAL(i,j)—or DEC(i,j) or NUMERIC(i,j)—where i, the precision, is
the total number of decimal digits and j, the scale, is the number of digits after the decimal point.
Page 20
MODULE –II DBMS -18CS53
■ Character-string data types are either fixed length—CHAR(n) or CHARACTER(n), where n is the number
of characters—or varying length— VARCHAR(n) or CHAR VARYING(n) or CHARACTER VARYING(n),where
n is the maximum number of characters. When specifying a literal string value, it is placed between
single quotation marks (apostrophes), and it is case sensitive (a distinction is made between uppercase
and lowercase).3 For fixed length strings, a shorter string is padded with blank characters to the right.
For example, if the value ‘Smith’ is for an attribute of type CHAR(10), it is padded with five blank
characters to become ‘Smith ’ if needed.
■ Bit-string data types are either of fixed length n—BIT(n)—or varying length—BIT VARYING(n), where n
is the maximum number of bits. The default for n, the length of a character string or bit string, is 1.
Literal bit strings are placed between single quotes but preceded by a B to distinguish them from
character strings;
for example,B‘10101’
■ A Boolean data type has the traditional values of TRUE or FALSE. In SQL, because of the presence of
NULL values, a three-valued logic is used, so a third possible value for a Boolean data type is UNKNOWN.
■ The DATE data type has ten positions, and its components are YEAR, MONTH, and DAY in the form
YYYY-MM-DD.
The TIME data type has at least eight positions, with the components HOUR, MINUTE, and SECOND in the
form HH:MM:SS.
■ A timestamp data type (TIMESTAMP) includes the DATE and TIME fields, plus a minimum of six
positions for decimal fractions of seconds and an optional WITH TIME ZONE qualifier. Literal values are
represented by single quoted strings preceded by the keyword TIMESTAMP, with a blank space between
data and time;
■ Another data type related to DATE, TIME, and TIMESTAMP is the INTERVAL data type. This specifies an
interval—a relative value that can be used to increment or decrement an absolute value of a date, time,
or timestamp. Intervals are qualified to be either YEAR/MONTH intervals or DAY/TIME intervals.
It is possible to specify the data type of each attribute directly, as in Figure 4.1;alternatively, a domain
can be declared, and the domain name used with the attribute specification. This makes it easier to
change the data type for a domain that is used by numerous attributes in a schema, and improves
schema readability. For example, we can create a domain SSN_TYPE by the following statement:
We can use SSN_TYPE in place of CHAR(9) in the above create table for the attributes Ssn and
Super_ssn of EMPLOYEE, Mgr_ssn of DEPARTMENT, Essn of WORKS_ON, and Essn of DEPENDENT.
Page 21
MODULE –II DBMS -18CS53
4.2 Specifying Constraints in SQL
Now will describe the basic constraints that can be specified in SQL as part of table creation. These
include key and referential integrity constraints, restrictions on attribute domains and NULLs,and
constraints on individual tuples within a relation.
Because SQL allows NULLs as attribute values, a constraint NOT NULL may be specified if NULL is not
permitted for a particular attribute. This is always implicitly specified for the attributes that are part of
the primary key of each relation, but it can be specified for any other attributes whose values are
required not to be NULL.
It is also possible to define a default value for an attribute by appending the clause DEFAULT<value> to
an attribute definition.
Another type of constraint can restrict attribute or domain values using the CHECK clause following an
attribute or domain definition.
For example, suppose that department numbers are restricted to integer numbers between 1 and 20;
then, we can change the attribute declaration of Dnumber in the DEPARTMENT table to the following:
Dnumber INT NOT NULL CHECK (Dnumber> 0 AND Dnumber< 21);
Ex:
CREATE TABLE EMPLOYEE ( Dno INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK PRIMARY KEY (Ssn),
CONSTRAINT EMPDEPTFK FOREIGN KEY(Dno) REFERENCES DEPARTMENT(Dnumber) ON DELETE SET
DEFAULT ON UPDATE CASCADE);
The CHECK clause can also be used in conjunction with the CREATE DOMAIN
CREATE DOMAIN D_NUM AS INTEGER CHECK (D_NUM > 0 AND D_NUM <21 );
We can then use the created domain D_NUM as the attribute type for all attributes that refer to
department numbers In create table , such as Dnumber of DEPARTMENT, Dnum of PROJECT, Dno of
EMPLOYEE, and so on.
The PRIMARY KEY clause specifies one or more attributes that make up the primary key of a relation. If a
primary key has a single attribute, the clause can follow the attribute directly.
Page 22
MODULE –II DBMS -18CS53
The UNIQUE clause specifies alternate (secondary) keys, as illustrated in the DEPARTMENT and PROJECT
table declarations.
Referential integrity is specified via the FOREIGN KEY clause. A referential integrity constraint can be
violated when tuples are inserted or deleted, or when a foreign key or primary key attribute value is
modified. The default action that SQL takes for an integrity violation is to reject the update operation
that will cause a violation, which is known as the RESTRICT option. However, the schema designer can
specify an alternative action to be taken by attaching a referential triggered action clause to any foreign
key constraint. The options include SET NULL, CASCADE, and SET DEFAULT. An option must be qualified
with either ON DELETE or ON UPDATE. We illustrate this with the examples shown in create table Here,
the database designer chooses ON DELETE SET NULL and ON UPDATE CASCADE for the foreign key
Super_ssn of EMPLOYEE. This means that if the tuple for a supervising employee is deleted, the value of
Super_ssnis automatically set to NULL for all employee tuples that were referencing the deleted
employee tuple. On the other hand, if the Ssnvalue for a supervising employee is updated (say, because
it was entered incorrectly),the new value is cascaded to Super_ssn for all employee tuples referencing
the updated employee tuple.8
A constraint may be given a constraint name, following the keyword CONSTRAINT. The names of all
constraints within a particular schema must be unique. A constraint name is used to identify a particular
constraint in case the constraint must be dropped later and replaced with another constraint. Giving
names to constraints is optional.
In addition to key and referential integrity constraints, which are specified by special keywords, other
table constraints can be specified through additional CHECK clauses at the end of a CREATE TABLE
statement. These can be called tuple-based constraints because they apply to each tuple individually
and are checked whenever a tuple is inserted or modified. For example, suppose that the DEPARTMENT
table in Figure 4.1 had an additional attribute Dept_create_date, which stores the date when the
department was created. Then we could add the following CHECK clause at the end of the CREATE TABLE
statement for the DEPARTMENT table to make sure that a manager’s start date is later than the
department creation date.
Page 23
MODULE –II DBMS -18CS53
4.3 Basic Retrieval Queries in SQL
SQL allows a table (relation) to have two or more tuples that are identical in all their attribute values.
Hence, in general, an SQL table is not a set of tuples, because a set does not allow two identical
members; rather, it is a multiset (sometimes called a bag) of tuples.
The basic form of the SELECT statement, sometimes called a mapping or a select-from-where block,
isformed of the three clauses SELECT, FROM, and WHERE and has the following form:
Page 24
MODULE –II DBMS -18CS53
SELECT <attribute list>
FROM <table list>
WHERE <condition>;
where
■ <attribute list> is a list of attribute names whose values are to be retrieved by the query.
■ <table list> is a list of the relation names required to process the query.
■ <condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the
query.
In SQL, the basic logical comparison operators for comparing attribute values with one another and with
literal constants are =, <, <=, >, >=, and <>(not equal operator).
Query 1.
Ex:Retrieve the birth date and address of the employee(s) whose name is ‘John B.Smith’.
Query 1.a.
Retrieve the name and address of all employees who work for the ‘Research’department.
SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname=‘Research’ AND
Dnumber=Dno;
In the WHERE clause of query, the condition Dname = ‘Research’is a selection condition that chooses the
particular tuple of interest in the DEPARTMENT table, because Dname is an attribute of DEPARTMENT.
The condition Dnumber = Dno is called a join condition, because it combines two tuples: one from
DEPARTMENT and one from EMPLOYEE, whenever the value of Dnumber in DEPARTMENT is equal to
the value of Dno in EMPLOYEE.
A query that involves only selection and join conditions plus projection attributes is known as a select-
project-join query. The next example is a select-project-join query with two join conditions.
Page 25
MODULE –II DBMS -18CS53
Query 2.
For every project located in ‘Stafford’, list the project number, the controlling department number,
and the department manager’s last name, address,and birth date.
SELECT Pnumber, Dnum, Lname, Address, Bdate FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE
Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation=‘Stafford’;
The join condition Dnum = Dnumber relates a project tuple to its controlling department tuple,whereas
the join condition Mgr_ssn=Ssnrelates the controlling department tuple to the employee tuple who
manages that department.
In SQL,the same name can be used for two (or more) attributes as long as the attributes are in different
relations.If this is the case,and a multitable query refers to two or more attributes with the same name,
we must qualify the attribute name with the relation name to prevent ambiguity. This is done by
prefixing the relation name to the attribute name and separating the two by a period.
We can also create an alias for each table name to avoid repeated typing of long table names.
For each employee, retrieve the employee’s first and last name and the first and last name of his or
her immediate supervisor.
Page 26
MODULE –II DBMS -18CS53
In this case, we are required to declare alternative relation names E and S, called aliases or tuple
variables, for the EMPLOYEE relation. An alias can follow the keyword AS, as shown in Query,or it can
directly follow the relation name—for example, by writing EMPLOYEE E, EMPLOYEE S in the FROM
clause of Query.
It is also possible to rename the relation attributes within the query in SQL by giving them aliases. For
example,if we write EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd, Addr, Sex, Sal, Sssn, Dno) in the FROMclause,Fn
becomes an alias for Fname,Mi for Minit,Ln for Lname,and so on.
In Query,we can think of E and S as two different copies of the EMPLOYEErelation;the first, E, represents
employees in the role of supervisees or subordinates; the second, S, represents employees in the role of
supervisors.
A missing WHERE clause indicates no condition on tuple selection; hence, all tuples of the relation
specified in the FROM clause qualify and are selected for the query result.If more than one relation is
specified in the FROM clause and there is no WHERE clause, then the CROSS PRODUCT—all possible
tuple combinations—of these relations is selected.
Select all EMPLOYEE Ssns (Q9) and all combinations of EMPLOYEE Ssn and DEPARTMENT Dname (Q10) in
the database.
Page 27
MODULE –II DBMS -18CS53
Q10: SELECT Ssn, Dname FROM EMPLOYEE, DEPARTMENT;
It is extremely important to specify every selection and join condition in the WHERE clause; if any such
condition is overlooked, incorrect and very large relations may result.
SQL usually treats a table not as a set but rather as a multiset; duplicate tuples can appear more than
once in a table,and in the result of a query. SQL does not automatically eliminate duplicate tuples in the
results of queries.
If we do want to eliminate duplicate tuples from the result of an SQL query, we use the keyword
DISTINCT in the SELECT clause, meaning that only distinct tuples should remain in the result.
Query 11. Retrieve the salary of every employee (Q11) and all distinct salary values
(Q11A).
Page 28
MODULE –II DBMS -18CS53
Q11A: SELECT DISTINCT Salary FROM EMPLOYEE;
In general, a query with SELECT DISTINCT eliminates duplicates, whereas a query with SELECT ALL does
not.
SQL has directly incorporated some of the set operations from mathematical set theory,which are also
part of relational algebra (see Chapter 6).There are set union (UNION), set difference (EXCEPT) and set
intersection (INTERSECT) operations. The relations resulting from these set operations are sets of tuples;
that is, duplicate tuples are eliminated from the result. These set operations apply only to union-
compatible relations, so we must make sure that the two relations on which we apply the operation
have the same attributes and that the attributes appear in the same order in both relations.The next
example illustrates the use of UNION.
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.
(SELECT DISTINCT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND
Mgr_ssn=Ssn AND Lname=‘Smith’)
UNION
( SELECT DISTINCT Pnumber FROM PROJECT, WORKS_ON, EMPLOYEE WHERE Pnumber=Pno AND
Essn=Ssn AND Lname=‘Smith’);
Page 29
MODULE –II DBMS -18CS53
4.3.5 Substring Pattern Matching and Arithmetic Operators
In this section we discuss several more features of SQL.The first feature allows comparison conditions on
only parts of a character string, using the LIKE comparison operator.This can be used for string pattern
matching. Partial strings are specified using two reserved characters: % replaces an arbitrary number of
zero or more characters,and the underscore (_) replaces a single character.Forexample,consider the
following query.
Query 12A.Find all employees who were born during the 1950s.
Another feature allows the use of arithmetic in queries. The standard arithmetic operators for addition
(+), subtraction (–), multiplication (*), and division (/) can be applied to numeric values or attributes with
numeric domains.
Query 13.Show the resulting salaries if every employee working on the ‘ProductX’project is given a 10
percent raise.
Another comparison operator, which can be used for convenience,isBETWEEN, which is illustrated in
Query 14.
Query 14.Retrieve all employees in department 5 whose salary is between $30,000 and $40,000.
SELECT * FROM EMPLOYEE WHERE (Salary BETWEEN 30000 AND 40000) AND Dno = 5;
SQL allows the user to order the tuples in the result of a query by the values of one or more of the
attributes that appear in the query result, by using the ORDER BY clause.
The default order is in ascending order of values.We can specify the keyword DESC if we want to see the
result in a descending order of values.The keyword ASCcan be used to specify ascending order explicitly.
Page 30
MODULE –II DBMS -18CS53
4.3.7 Discussion and Summary of Basic SQL Retrieval Queries
A simple retrieval query in SQL can consist of up to four clauses, but only the first two—SELECT and
FROM—are mandatory. The clauses are specified in the following order,with the clauses between
square brackets [ ... ] being optional:
SELECT <attribute list> FROM <table list>[ WHERE<condition> ] [ ORDER BY <attribute list> ];
The SELECT clause lists the attributes to be retrieved, and the FROM clause specifies all relations
(tables) needed in the simple query. The WHERE clause identifies the conditions for selecting the tuples
from these relations, including join conditions if needed. ORDER BY specifies an order for displaying the
results of a query.
In its simplest form, INSERT is used to add a single tuple to a relation. We must specify the relation name
and a list of values for the tuple. The values should be listed in the same order in which the
corresponding attributes were specified in the CREATE TABLE command.
A second form of the INSERT statement allows the user to specify explicit attribute names that
correspond to the values provided in the INSERT command. This is useful if a relation has many
attributesbut only a few of those attributes are assigned values in the new tuple.
INSERT INTO EMPLOYEE (Fname, Lname, Dno, Ssn) VALUES (‘Richard’,‘Marini’, 4,‘653298653’);
Attributes not specified in the above query are set to their DEFAULT or to NULL, and the values are listed
in the same order as the attributes are listed in the INSERTcommand itself.
A variation of the INSERT command inserts multiple tuples into a relation in conjunction with creating
the relation and loading it with the result of a query.
Page 31
MODULE –II DBMS -18CS53
4.4.2 The DELETE Command
The DELETE command removes tuples from a relation. It includes a WHERE clause, similar to that used in
an SQL query, to select the tuples to be deleted. Tuples are explicitly deleted from only one table at a
time. However, the deletion may propagate to tuples in other relations if referential triggered actions
are specified in the referential integrity constraints of the DDL. Depending on the number of tuples
selected by the condition in the WHERE clause, zero, one, or several tuples can be deleted by a single
DELETE command. A missing WHERE clause specifies that all tuples in the relation are to be deleted;
however, the table remains in the database as an empty table.
The above DELETE commands if applied independently to the database in Figure 3.6,will delete
zero,one,four,and all tuples,respectively,from the EMPLOYEErelation:
The UPDATE command is used to modify attribute values of one or more selected tuples. As in the
DELETE command, a WHERE clause in the UPDATE command selects the tuples to be modified from a
single relation. However, updating a primary key value may propagate to the foreign key values of tuples
in other relations if such a referential triggered action is specified in the referential integrity constraints
of the DDL. An additional SET clause in the UPDATE command specifies the attributes to be modified and
their new values.
For example, to change the location and controlling department number of project number 10 to
‘Bellaire’and 5,respectively,we use the below query
Several tuples can be modified with a single UPDATE command. An example is to give all employees in
the ‘Research’department a 10 percent raise in salary.
Page 32
MODULE –II DBMS -18CS53
4.5 Additional Features of SQL
SQL has various techniques for specifying complex retrieval queries, including nested queries, aggregate
functions, grouping, joined tables, outer joins, and recursive queries; SQL views, triggers, and assertions;
and commands for schema modification.
■ SQL has various techniques for writing programs in various programming languages that include SQL
statements to access one or more databases. These include embedded (and dynamic) SQL, SQL/CLI (Call
Level Interface) and its predecessor ODBC (Open Data Base Connectivity), and SQL/PSM (Persistent
Stored Modules).
■ Each commercial RDBMS will have, in addition to the SQL commands, a set of commands for
specifying physical database design parameters, file structures for relations, and access paths such as
indexes.
SQL has transaction control commands. These are used to specify units of database processing for
concurrency control and recovery purposes.
. ■ SQL has language constructs for specifying the granting and revoking of privileges to users.Privileges
typically correspond to the right to use certain SQL commands to access certain relations. Each relation
is assigned an owner, and either the owner or the DBA staff can grant to selected users the privilege to
use an SQL statement—such as SELECT, INSERT, DELETE, or UPDATE—to access the relation.
In addition, the DBA staff can grant the privileges to create schemas, tables, or views to certain users.
These SQL commands—called GRANT and REVOKE—
■ SQL has language constructs for creating triggers. These are generally referred to as active database
techniques, since they specify actions that are automatically triggered by events such as database
updates.
■ SQL has incorporated many features from object-oriented models to have more powerful
capabilities, leading to enhanced relational systems known as object-relational.
■ SQL and relational databases can interact with new technologies such as XML .
Page 33