Module 3 Scheme DBMS 5th Semester
Module 3 Scheme DBMS 5th Semester
MODULE 3
Chapter1:SQL
Introduction
SQL was called SEQUEL(Structured English Query Language)and was designed and implemented at
IBM Research. The SQL language may be considered one of the major reasons for the commercial
success of relational databases. SQL is a comprehensive database language. It has statements for data
definitions, queries, and updates. Hence, it is both a DDL and a DML. In addition,
ithasfacilitiesfordefiningviewsonthedatabase,forspecifyingsecurity and authorization, for defining
integrity constraints, and for specifying transaction controls. It also has rules for embedding SQL
statements into a general-purpose programming language such as Java, COBOL, or C/C++.
SQLDataDefinitionandDataTypes
SQL uses the terms table, row, and column for the formal relational model terms relation, tuple, and
attribute, respectively.The main SQL command for data definition is the CREATE statement, which
can be used to create schemas, tables (relations), domains, views, assertionsandtriggers.
SchemaandCatalogConceptsinSQL
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
theschema.Schemaelementsincludetables,constraints,views,domains,andother constructs
(suchasauthorizationgrants)thatdescribethe schema.A schemaiscreatedvia the CREATE
SCHEMA statement .
Forexample,thefollowingstatementcreatesaschemacalledCOMPANY,ownedbythe
CREATESCHEMACOMPANYAUTHORIZATION
In general, not all users are authorized to create schemas and schema elements. The privilegeto
create schemas, tables, and other constructs must be explicitly granted to the relevant user
accounts by the system administrator or DBA.
page1
[18CS53]
DatabaseManagementSystem[21CS53]
schemas. Integrity constraints such as referential integrity can be defined between relations
only if they exist in schemas within the same catalog. Schemas within the same catalog can
also share certain elements, such as domain definitions.
TheCREATETABLECommandinSQL
The CREATE TABLE commandis used to specify a new relation by giving it a nameandspecifying its
attributes andinitial constraints.Theattributes arespecifiedfirst,andeach attributeis 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
statementafterthe attributes are declared, or they can be addedlater using theALTER TABLE command.
Typically, the SQL schema in which the relations are declared is implicitly specified in
theenvironmentinwhichtheCREATETABLEstatementsareexecuted.Alternatively,wecan
explicitlyattachtheschemanametotherelationname,separatedbyaperiod.Forexample,by writing
CREATETABLECOMPANY.EMPLOYEE...
ratherthan
CREATETABLEEMPLOYEE...
The relations declared through CREATE TABLE statements are called base tables.
Examples:
page2
DatabaseManagementSystem[21CS53]
[18CS53]
page3
DatabaseManagementSystem [21CS53]
[18CS53]
2. Character-stringdatatypes
fixed length CHAR(n)orCHARACTER(n),wherenisthenumberofcharacters
varying length VARCHAR(n) or CHAR VARYING(n) or CHARACTER VARYING(n),
where n is the maximum number of characters
Whenspecifyingaliteralstringvalue,itisplacedbetweensinglequotationmarks (apostrophes), and it
is case sensitive
Forfixedlength strings,ashorterstringispaddedwith blankcharacterstotheright
Paddedblanksaregenerallyignoredwhen stringsarecompared
Anothervariable-lengthstringdatatypecalledCHARACTERLARGEOBJECTorCLOB is
alsoavailable to specify columns that have large text values, such as documents
The CLOB maximum length can be specified in kilobytes (K), megabytes (M), or gigabytes
(G)
Forexample,CLOB(20M)specifiesamaximumlengthof 20megabytes.
3. Bit-stringdatatypesareeitherof
fixed length n BIT(n) or varyinglength BIT VARYING(n), where n is the maximum
number of bits.
Thedefaultforn,thelength of acharacterstringorbitstring,is1.
page4
DatabaseManagementSystem [21CS53]
[18CS53]
LiteralbitstringsareplacedbetweensinglequotesbutprecededbyaBtodistinguishthem
Anothervariable-lengthbitstringdatatypecalledBINARYLARGEOBJECTorBLOBis also
available to specify columns thathave large binary values, such as images.
Themaximumlength of aBLOBcanbe specifiedin kilobits(K),megabits(M),orgigabits (G)
Forexample,BLOB(30G)specifiesamaximumlengthof 30gigabits.
4. 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 athird possible value for a Boolean
data type is UNKNOWN
5. The DATE data type has ten positions, and its components are YEAR, MONTH, and DAY in
the form YYYY-MM-DD
6. TheTIMEdatatypehasatleasteightpositions,withthecomponentsHOUR,MINUTE, and
SECOND in the form HH:MM:SS.
Onlyvaliddatesandtimesshouldbeallowedby theSQLimplementation.
7. TIME WITH TIME ZONE data type includes an additional six positions for specifying the
displacementfromthestandarduniversaltimezone,whichisintherange+13:00to 12:59 in units of
HOURS:MINUTES. If WITH TIME ZONE is not included, the default is the local time zone
for the SQL session.
Additionaldatatypes
It is possible to specify the data type of each attribute directly or a domain can be declared, and the
domain name used with the attributeSpecification. This makes it easier to change the data type for a
domain that is usedby numerous attributes in a schema, and improves schema readability. For example,
we can create a domain SSN_TYPE by the following statement:
CREATEDOMAINSSN_TYPE ASCHAR(9);
page5
[18CS53]
DatabaseManagementSystem [21CS53]
We can use SSN_TYPE in place of CHAR(9)for the attributes Ssn andSuper_ssn of EMPLOYEE,
Mgr_ssn of DEPARTMENT, Essn of WORKS_ON,and Essn of DEPENDENT
SpecifyingConstraintsinSQL
BasicconstraintsthatcanbespecifiedinSQLaspartoftablecreation:
keyandreferentialintegrityconstraints
RestrictionsonattributedomainsandNULLs
constraintsonindividualtupleswithinarelation
ItisalsopossibletodefineadefaultvalueforanattributebyappendingtheclauseDEFAULT
<value>toanattributedefinition.Thedefaultvalueisincludedinanynewtupleifanexplicitvalue is not
provided for that attribute.
(...,
Another type of constraintcan restrictattribute 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 tableto the following:
DnumberINTNOTNULLCHECK(Dnumber>0ANDDnumber<21);
The CHECK clause can also be used in conjunction with the CREATE DOMAIN statement.For
example, we can write the following statement:
CREATEDOMAIND_NUMASINTEGER
https://vtucode.in page6
DatabaseManagementSystem[21CS53]
[18CS53]
CHECK(D_NUM>0ANDD_NUM<21);
We can then use the created domain D_NUM as the attribute type for all attributes that refer to
departmentnumbersuchasDnumberofDEPARTMENT,DnumofPROJECT,Dnoof EMPLOYEE, and so
on.
DnumberINTPRIMARYKEY;
The UNIQUE clause can also be specified directly for a secondary key if the secondary key is a
single attribute, as in the following example:
DnameVARCHAR(15)UNIQUE;
ReferentialintegrityisspecifiedviatheFOREIGNKEYclause
FOREIGNKEY(Super_ssn)REFERENCESEMPLOYEE(Ssn),
FOREIGNKEY(Dno)REFERENCESDEPARTMENT(Dnumber
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 toreject the update operation thatwill cause aviolation, which is known as the
RESTRICT option.
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
page7
[18CS53]
DatabaseManagementSystem[21CS53]
Ingeneral,theactiontakenbytheDBMSforSETNULLorSETDEFAULTisthesamefor both
ONDELETE andON UPDATE: Thevalueof the affected referencingattributesis changed to NULL
for SET NULL and to the specified default value of the referencing attribute for SET DEFAULT.
TheactionforCASCADEONDELETEistodeleteallthereferencingtupleswhereastheaction for CASCADE
ON UPDATE is to change the value of the referencing foreign key attribute(s) to the
updated(new)primarykeyvalueforallthereferencingtuples.Itistheresponsibilityofthe database designer to
choose the appropriate action and to specify it in the database schema.As a general rule, the
CASCADE option is suitable for relations that
represent multivalued attributes, such as DEPT_LOCATIONS; and for relations that represent weak
entity types, such as DEPENDENT.
GivingNamestoConstraints
The names of all constraints within aparticular schemamustbe unique.A constraintnameis used to
identify a particular constraint in case the constraint must be dropped later and replaced with another
constraint.
SpecifyingConstraintsonTuplesUsingCHECK
In addition to key and referential integrity constraints, which are specified by special keywords,
othertableconstraintscanbespecifiedthroughadditional CHECKclausesattheendofa CREATE TABLE
statement. These can be calledtuple-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 had an additional attribute Dept_create_date,
which stores thedate when the departmentwas created. Then we couldadd the followingCHECK
clauseattheendoftheCREATETABLEstatementfortheDEPARTMENTtabletomakesure
CHECK(Dept_create_date<=Mgr_start_date);
BasicRetrievalQueriesin SQL
SQLhasonebasicstatementforretrievinginformationfromadatabase:theSELECTstatement.
TheSELECT-FROM-WHEREStructureofBasicSQLQueries
Thebasicform of theSELECTstatement,sometimescalledamappingoraselect-from-where
block,isformed of the three clauses SELECT,FROM,and WHEREand has the followingform:
page8
[18CS53]
DatabaseManagementSystem [21CS53]
SELECT
FROM<tablelist>
WHERE <condition>;
Where,
<attribute list> is a list of attribute names whose values are to be retrieved by the
query
<tablelist>isalistoftherelationnamesrequiredtoprocessthequery
<condition> is a conditional (Boolean) expression thatidentifies the tuples to be
retrievedby the query.
Examples:
1.
SELECTBdate,Address
FROMEMPLOYEE
WHERE AND AND
page9
[18CS53]
DatabaseManagementSystem [21CS53]
SELECTPnumber,Dnum,Lname,Address,Bdate
FROMPROJECT,DEPARTMENT,EMPLOYEE
WHEREDnum=DnumberANDMgr_ssn=SsnAND
The join condition Dnum = Dnumber relates a project tuple to its controlling department tuple,
whereas the join condition Mgr_ssn = Ssn relates the controlling departmenttuple to the employee
tuple who manages that department. Each tuple in the result will be a combination of one project,
one department, and one employee that satisfies the join conditions. The projection attributes are
used to choose the attributesto be displayedfrom each combined tuple.
AmbiguousAttributeNames,Aliasing,Renaming,and TupleVariables
In SQL, the same name can be used for two or more attributes as long as the attributes are in
differentrelations.If thisis the case,anda multitable query refers to two ormore attributes with the
same name, we must qualify the attribute name with the relation name to prevent ambiguity. Thisis
doneby prefixing the relationname to the attribute name and separating the two by a period.
department
SELECTFname,EMPLOYEE.Name,Address
FROMEMPLOYEE,DEPARTMENT
WHERE AND
DEPARTMENT.Dnumber=EMPLOYEE.Dnumber;
The ambiguity of attribute names also arises in the case of queries that refer to the same relation
twice. For example consider the query:
name and the first and last name of his or her immediate supervisor.
SELECTE.Fname,E.Lname,S.Fname,S.Lname
FROMEMPLOYEEASE,EMPLOYEEASS
WHEREE.Super_ssn=S.Ssn;
In this case, we are required to declare alternative relation names E and S, called aliases or tuple
variables,fortheEMPLOYEErelation.AnaliascanfollowthekeywordAS,oritcandirectly
followtherelationnameforexample,bywritingEMPLOYEEE,EMPLOYEES.Itisalso possible
torenamethe relation attributes within the query in SQLby givingthem aliases.For example, if we write
EMPLOYEEASE(Fn,Mi,Ln,Ssn,Bd,Addr,Sex,Sal,Sssn,Dno)
page10
[18CS53]
DatabaseManagementSystem [21CS53]
intheFROMclause,FnbecomesanaliasforFname,MiforMinit,LnforLname,andsoon
UnspecifiedWHEREClauseandUseoftheAsterisk
A missing WHERE clause indicates no condition on tuple selection; hence, all tuples of the relation
specifiedintheFROMclausequalifyandareselectedforthequeryresult.Ifmorethanonerelation is specified in
the FROM clause and there is no WHERE clause, then the CROSS PRODUCTall possible tuple
combinationsof these relations is selected.
Example:Selectall EMPLOYEESsnsandallcombinationsof EMPLOYEESsnand DEPARTMENT
Dname in the database.
SELECTSsn
FROMEMPLOYEE;
SELECTSsn,Dname
FROMEMPLOYEE,DEPARTMENT;
To retrieve all the attribute values of the selected tuples, we do not have to list the attribute names
explicitly in SQL; we just specify an asterisk (*), which stands for all the attributes. For example, the
following query retrieves all the attribute values of any EMPLOYEE who works in DEPARTMENT
number 5
SELECT*FROMEMPLOYEEWHEREDno=5;
SELECT*FROMEMPLOYEE,DEPARTMENTWHERE
ANDDno=Dnumber;
SELECT*FROMEMPLOYEE,DEPARTMENT;
TablesasSetsinSQL
SQL usually treats a table not as a set but rather as a multiset; duplicate tuples can appear more than
once in a table, andin the resultof aquery. SQL does not automatically eliminate duplicate tuples in the
results of queries, for the following reasons:
Duplicate elimination is an expensive operation. One way to implement it is to sort the tuples
first and then eliminate duplicates.
Theusermaywanttoseeduplicatetuplesin theresultof aquery.
When an aggregate function is applied to tuples, in most cases we do not want to eliminate
duplicates.
Ifwedowanttoeliminateduplicatetuplesfrom theresultofanSQLquery,weusethekeyword
DISTINCTintheSELECTclause,meaningthatonly distincttuplesshouldremainintheresult.
page11
DatabaseManagementSystem[21CS53]
[18CS53]
Example:Retrievethesalaryofeveryemployeeandalldistinctsalaryvalues
(a) SELECTALLSalaryFROMEMPLOYEE;
(b) SELECTDISTINCTSalaryFROMEMPLOYEE;
setunion(UNION)
set difference (EXCEPT)and
set intersection (INTERSECT)
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
makesurethatthetworelationsonwhichweapplytheoperationhavethesameattributesandthat the attributes
appear in the same order in both relations.
Example:Make a list of all project numbers for projects that involve an employee whose last name
worker or as a manager of the department that controls the project
(SELECTDISTINCTPnumberFROMPROJECT,DEPARTMENT,
EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND
UNION
(SELECTDISTINCTPnumberFROMPROJECT,WORKS_ON,EMPLOYEE
WHEREPnumber=PnoANDEssn=Ssn AND
page12
DatabaseManagementSystem [21CS53]
[18CS53]
SubstringPatternMatchingandArithmeticOperators
SeveralmorefeaturesofSQL
Thefirstfeatureallowscomparisonconditionsononlypartsofacharacterstring,usingtheLIKE
comparisonoperator.Thiscanbeusedforstringpatternmatching.Partialstringsarespecified using two
reserved characters:
% replaces an arbitrary numberof zero or more characters
_ (underscore) replaces a single character
Forexample,considerthefollowingquery:RetrieveallemployeeswhoseaddressisinHouston, Texas
SELECTFname,LnameFROMEMPLOYEEWHEREAddress
LIKE
Toretrieveallemployeeswhowerebornduringthe1950s,wecanuseQuery
SELECTFname,LnameFROMEMPLOYEE
WHERE Bdate LIKE
Ifanunderscoreor%isneededasaliteralcharacterinthestring,thecharactershouldbepreceded by an escape
character, which is specified after the string using the keyword ESCAPE. For example, \_CD\
\ \ is specified as the
escapecharacter.Also,weneedaruleto they
are to be included in a
needed, as
endingthestring.
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
withnumericdomains.Forexample,supposethatwewanttoseetheeffectofgivingallemployees
the following query:
SELECTE.Fname,E.Lname,1.1*E.SalaryASIncreased_sal
FROMEMPLOYEEASE,WORKS_ONASW,PROJECTASP
WHEREE.Ssn=W.EssnANDW.Pno=P.PnumberAND
Example:Retrieveallemployeesindepartment5whosesalaryisbetween$30,000and$40,000.
SELECT*FROMEMPLOYEEWHERE(SalaryBETWEEN30000AND
40000)ANDDno=5;
The condition (Salary BETWEEN 30000 AND 40000) is equivalent to the condition((Salary >=30000)
AND (Salary <= 40000)).
page13
[18CS53]
DatabaseManagementSystem [21CS53]
OrderingofQueryResults
SQLallowstheusertoorderthetuplesintheresultofaquerybythevaluesofoneormoreofthe attributes that appear in
the query result, by using the ORDER BY clause.
Example:Retrievealistofemployeesandtheprojectstheyareworkingon,orderedbydepartment and, within
each department, ordered alphabetically bylast name, then first name.
SELECTD.Dname,E.Lname,E.Fname,P.Pname
FROMDEPARTMENTD,EMPLOYEEE,WORKS_ONW,PROJECTP
WHERED.Dnumber=E.DnoANDE.Ssn=W.EssnANDW.Pno=P.Pnumber
ORDERBYD.Dname,E.Lname,E.Fname;
Thedefaultorderisinascendingorderofvalues.WecanspecifythekeywordDESCifwewantto see the resultin
a descending order of values. The keyword ASC can beused tospecify ascending
orderexplicitly.Forexample,if wewantdescendingalphabetical order onDnameandascending order on
Lname, Fname, the ORDER BY clause can be written as
ORDERBYD.DnameDESC,E.LnameASC,E.FnameASC
INSERT,DELETE,andUPDATEStatementsinSQL
TheINSERTCommand
INSERT is used to add a single tuple to a relation. We must specify the relation name and a list of
valuesfor the tuple.Thevalues should belistedin the same orderin which the corresponding attributes
were specified in the CREATE TABLE command.
Example:INSERT INTO EMPLOYEE VALUES -
12-
INSERTINTOEMPLOYEE(Fname,Lname,Dno,Ssn)
VALUES
AsecondformoftheINSERTstatementallowstheusertospecifyexplicitattributenamesthat
correspondtothevaluesprovidedintheINSERTcommand.Thevaluesmustincludeallattributes with NOT
NULL specification and no default value. Attributes with NULL allowed or DEFAULT values are the
ones that can be left out.
A variation of theINSERT commandinsertsmultipletuplesintoarelationin conjunctionwith
creatingtherelationandloadingitwiththeresultofaquery.Forexample,tocreateatemporary table thathas
theemployeelastname,projectname,andhours perweekfor each employeeworking on a project, we can
write the statements in U3A and U3B:
page14
[18CS53]
DatabaseManagementSystem[21CS53]
U3A:CREATETABLEWORKS_ON_INFO(
Emp_name VARCHAR(15),
Proj_name VARCHAR(15),
Hours_per_week DECIMAL(3,1) );
U3B:INSERTINTOWORKS_ON_INFO
(Emp_name,Proj_name,Hours_per_week)
SELECTE.Lname,P.Pname,W.Hours
FROMPROJECTP,WORKS_ONW,EMPLOYEEE
WHEREP.Pnumber=W.PnoANDW.Essn=E.Ssn;
A table WORKS_ON_INFO is created by U3A and is loaded with the joined information retrieved
from the database by the query in U3B. We can now query WORKS_ON_INFO as we would any
other relation;
TheDELETECommand
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. The deletion may propagate to tuples in other relations if referential triggered actions
are specified in the referential integrity constraints of the DDL.
Example:
DELETEFROMEMPLOYEEWHERE
Depending on the number of tuples selected by the condition in the WHERE clause, zero, one, or
several tuplescanbedeletedbyasingleDELETEcommand.AmissingWHEREclausespecifies that all tuples
in the relation are to be deleted; however, the table remains in the database as an empty table.
TheUPDATECommand
The UPDATE command is used to modify attribute values of one or more selected
Tuples.AnadditionalSETclauseintheUPDATEcommandspecifiestheattributestobemodifiedandtheir
newvalues.Forexample,tochangethelocationandcontrollingdepartmentnumberofproject
UPDATEPROJECTSETP WHEREPnumber=10;
As in the DELETE command, a WHERE clause in the UPDATE command selects the tuples to be
modifiedfromasinglerelation.However,updatingaprimarykeyvaluemaypropagatetothe foreign key values
of tuples in other relations if such a referential triggered action is specified in the referential integrity
constraints of the DDL.
page15
[18CS53]
DatabaseManagementSystem [21CS53]
SeveraltuplescanbemodifiedwithasingleUPDATEcommand.Anexampleistogiveall
a10percentraiseinsalary,asshownbythefollowing
query
UPDATEEMPLOYEE
SETSalary =Salary*1.1
WHEREDno=5;
Each UPDATE command explicitly refers to a single relation only. To modify multiple relations, we
must issue several UPDATE commands.
AdditionalFeaturesofSQL
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.
SQLhastransactioncontrolcommands.Theseareusedtospecifyunitsofdatabaseprocessing for
concurrency control and recovery purposes.
SQLhaslanguageconstructsforspecifyingthegrantingandrevokingofprivilegestousers.
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.
SQLandrelationaldatabasescaninteractwithnewtechnologiessuchasXML
page16
[18CS53]
DatabaseManagementSystem[21CS53]
21. Briefly discuss how the different updata operations on a relation deal with constraint
violations?
22. Considerthefollowingschemafora COMPANY database: EMPLOYEE
(Fname, Lname, Ssn, Address, Super-ssn, Salary, Dno)
DEPARTMENT (Dname, Dnumber, Mgr-ssn, Mgr-start-date)
DEPT-LOCATIONS (Dnumber, Dlocation)
PROJECT (Pname, Pnumber, Plocation, Dnum)
WORKS-ON (Ess!!, Pno, Hours)
DEPENDENT (Essn, Dependent-name, Sex, Bdate, Relationship)
Write the queries in relational algebra.
i) Retrievethenameandaddressofallemployeeswhoworkfor'Sales'department.
ii) Find the names of employees who work on all the projects controlled by the department
number 3.
iii) Listthenamesofallemployeeswithtwoormoredependents.
iv) Retrievethenamesofemployeeswhohavenodependents.
page17
[18CS53]
DatabaseManagementSystem [21CS53]
Module3
Chapter2:SQL-AdvancesQueries
MoreComplexSQLRetrievalQueries
Additionalfeaturesallowusersto specifymorecomplexretrievalsfromdatabase
ComparisonsInvolvingNULLandThree-ValuedLogic
SQLhasvariousrulesfordealingwithNULLvalues.NULLisusedtorepresentamissingvalue,but that it
usually hasone of three different interpretationsvalue
Example
1. Unknown value.
database.
2. Unavailableorwithheldvalue.Apersonhasahomephonebutdoesnotwantittobe listed,so it is
withheld and representedas NULL in thedatabase.
3. Notapplicableattribute.AnattributeCollegeDegreewouldbeNULLforapersonwhohasno
college degrees because it does not apply to that person.
EachindividualNULLvalueisconsideredtobedifferentfromeveryother NULLvalueinthevarious
database records.When a NULL is involved in a comparison operation, the result is considered to
be UNKNOWN (it maybe TRUE or it may be FALSE). Hence, SQL uses a three-valued logic with
values TRUE, FALSE, and UNKNOWN instead of the standard two-valued (Boolean) logic with
values TRUE or FALSE.It is therefore necessary to define the results (or truth values) of three-
valued logicalexpressions whenthelogical connectivesAND, OR,andNOTareused
page18
[18CS53]
DatabaseManagementSystem[21CS53]
The rows and columns represent the values of the results of comparison conditions, which would
typically appear in the WHERE clause of an SQL query.
Inselect-project-joinqueries,thegeneralruleisthatonlythosecombinationsoftuplesthatevaluate
thelogicalexpressionintheWHEREclauseofthequerytoTRUEareselected.Tuplecombinations that
evaluate to FALSE or UNKNOWN are not selected.
page19
[18CS53]
DatabaseManagementSystem[21CS53]
Thefirstnestedqueryselectstheprojectnumbersofprojectsthathaveanemployeewithlast name
Thesecondnestedquery selectsthe projectnumbersof projectsthat
. In the outer query, we use the OR
logical connectiveto retrieveaPROJECTtupleif thePNUMBER valueof that tuple isin the result
ofeither nested query.
SQL allows the use of tuples of values in comparisons by placing them within parentheses. For
example, the following query will select the Essns of all employees who work the same (project,
hours) on
Inthisexample,theINoperatorcomparesthesubtupleofvaluesinparentheses(Pno,Hours)within each
tuple inWORKS_ONwith thesetof type-compatible tuplesproducedby thenestedquery.
NestedQueries::ComparisonOperators
Other comparison operators can be used to compare a single value v to a set ormultiset V. The=
ANY (or = SOME) operator returns TRUE if the value v is equal to some value in the set V and is
henceequivalenttoIN.ThetwokeywordsANYandSOMEhavethesameeffect.ThekeywordALL can also
be combined with each of these operators. For example, the comparison condition (v > ALL V)
returns TRUE if the value v is greater than all the values in the set (or multiset) V. For
exampleisthefollowingquery,whichreturnsthenamesofemployeeswhosesalaryisgreaterthan the
salary of all the employees in department 5:
SELECTLname, Fname
FROMEMPLOYEE
WHERESalary>ALL(SELECTSalary
FROMEMPLOYEE
WHEREDno=5);
Ingeneral,wecanhaveseverallevelsofnestedqueries.Wecanonceagainbefacedwithpossible
ambiguity among attribute names if attributes of the same name existone in a relation in the
FROMclauseoftheouterquery, andanotherinarelation intheFROMclauseofthe nested query. The
rule is that a reference to an unqualified attribute refers to the relation declared in the innermost
nested query.
Toavoidpotentialerrorsandambiguities,createtuplevariables(aliases)foralltablesreferencedin SQL
query
page20
[18CS53]
DatabaseManagementSystem [21CS53]
Example:Retrievethenameofeachemployeewhohasadependentwiththesamefirstnameand is the
same sex as the employee
SELECT E.Fname, E.Lname
FROM EMPLOYEE AS
EWHEREE.SsnIN(SELECTEssn
FROM DEPENDENT AS D
WHEREE.Fname=D.Dependent_name
ANDE.Sex=D.Sex);
Intheabovenestedquery,wemustqualifyE.SexbecauseitreferstotheSexattributeof EMPLOYEE from
theouterquery, and DEPENDENT also hasan attributecalledSex.
CorrelatedNestedQueries
WheneveraconditionintheWHEREclauseofanestedqueryreferencessomeattributeofa relation declared in
theouterquery,the two queries are saidtobe correlated.
Example:
SELECT E.Fname, E.Lname
FROM EMPLOYEE AS
EWHEREE.SsnIN(SELECTEssn
FROM DEPENDENT AS D
WHEREE.Fname=D.Dependent_name
ANDE.Sex=D.Sex);
Thenestedqueryisevaluatedonceforeachtuple (orcombinationoftuples)intheouterquery. we can
think of query in above example as follows: For each EMPLOYEE tuple, evaluate the nested
query, whichretrievestheEssn valuesforallDEPENDENTtuples withthe same sexand nameas
thatEMPLOYEEtuple;if theSsnvalueof theEMPLOYEEtupleis intheresultofthenestedquery, then
select that EMPLOYEE tuple.
TheEXISTSandUNIQUEFunctionsinSQL
EXISTSFunctions
TheEXISTSfunctioninSQLisusedtocheckwhethertheresultofacorrelatednestedqueryis
empty(containsnotuples)ornot.TheresultofEXISTSisaBooleanvalue TRUE
if thenestedquery result containsat least one tuple, or FALSE if the
nested query result contains no tuples.
Forexample,thequerytoretrievethenameofeachemployeewhohas adependentwiththesame
firstnameand isthesame sex astheemployeecanbewritten usingEXISTSfunctions asfollows:
SELECTE.Fname,E.Lname
page21
[18CS53]
DatabaseManagementSystem [21CS53]
FROM EMPLOYEE AS E
WHEREEXISTS(SELECT*
FROM DEPENDENT AS D
WHEREE.Ssn=D.EssnANDE.Sex=D.Sex
ANDE.Fname=D.Dependent_name);
Example:Listthenamesofmanagerswhohaveatleastonedependent
SELECTFname,Lname
FROMEMPLOYEE
WHEREEXISTS(SELECT*
FROMDEPENDENT
WHERESsn=Essn)
AND
EXISTS(SELECT*
FROMDEPARTMENT
WHERESsn=Mgr_ssn);
Ingeneral,EXISTS(Q)returnsTRUEifthereisatleastonetupleintheresultofthenestedqueryQ, and it
returns FALSE otherwise.
NOTEXISTSFunctions
NOTEXISTS(Q)returnsTRUEifthereareno tuplesin theresultof nestedqueryQ, andit returns
FALSEotherwise.
Example:Retrieve thenamesofemployeeswhohavenodependents.
SELECTFname,Lname
FROMEMPLOYEE
WHERENOTEXISTS(SELECT*
FROMDEPENDENT
WHERESsn=Essn);
For each EMPLOYEE tuple, the correlated nested query selects all DEPENDENT tuples whose
Essn value matches the EMPLOYEE Ssn; if the result is empty, no dependents are related to the
employee, sowe select that EMPLOYEE tuple and retrieve itsFnameandLname.
Example:Retrievethenameofeachemployeewhoworksonalltheprojectscontrolled by
department number 5
SELECTFname,Lname
page22
[18CS53]
DatabaseManagementSystem[21CS53]
FROMEMPLOYEE
WHERENOTEXISTS((SELECTPnumber
FROM PROJECT
WHERE Dnum=5)
EXCEPT(SELECTPno
FROM WORKS_ON
WHERE Ssn=Essn));
UNIQUEFunctions
UNIQUE(Q)returnsTRUEifthereare noduplicatetuplesintheresultofqueryQ;otherwise,it
returnsFALSE.This canbeusedtotestwhether the resultof anestedquery isasetora multiset.
ExplicitSetsandRenamingofAttributesinSQL
INSQL it is possible to use anexplicit set ofvaluesin the WHERE clause, rather than a
nestedquery. Such a set is enclosed in parentheses.
Example:RetrievetheSocialSecuritynumbersofallemployeeswhoworkonprojectnumbers1,2, or 3.
SELECTDISTINCTEssn
FROM WORKS_ON
WHERE PnoIN(1,2,3);
InSQL, itis possible to rename any attribute that appearsin theresultofa query by adding the
qualifier AS followed by the desired new name
Example:Retrievethelastnameofeachemployeeandhisorhersupervisor
SELECTE.LnameASEmployee_name,
S.Lname AS Supervisor_name
FROMEMPLOYEEASE,
EMPLOYEE AS S
WHEREE.Super_ssn=S.Ssn;
page23
[18CS53]
DatabaseManagementSystem[21CS53]
JoinedTablesinSQLandOuterJoins
An SQL join clause combines records from two or more tables in a database. It creates a set that
can be saved as a table or used as is. A JOIN is a means for combining fields from two tables by
using values common to each. SQL specifiesfourtypes of JOIN
1. INNER,
2. OUTER
3. EQUIJOINand
4. NATURALJOIN
INNERJOIN
An inner join is the most common join operation used in applications and can be regarded as the
default join-type. Inner join createsanew result tableby combining column valuesof two tables(A
and B) based upon the join- predicate (the condition). The result of the join can be defined as the
outcome of first taking the Cartesian product (or Cross join) of all records in the tables (combining
every record in table A with every record in table B )then return all records which satisfy the join
predicate
Example:SELECT*FROMemployee
INNERJOINdepartment ON
employee.dno =department.dnumber;
EQUIJOINandNATURALJOIN
AnEQUIJOINisaspecifictypeofcomparator-basedjointhatusesonlyequalitycomparisonsinthe join-
predicate. Usingothercomparisonoperators (suchas<) disqualifiesa join asanequijoin.
NATURAL JOIN is a type of EQUIJOIN where the join predicate arises implicitly by comparing all
columnsinboth tablesthathavethe same column-names inthe joinedtables. The resultingjoined
table containsonly one column for each pair of equallynamed columns.
page24
[18CS53]
DatabaseManagementSystem[21CS53]
If thenamesofthejoinattributesarenotthesameinthebaserelations,itispossibletorenamethe
attributessothattheymatch,andthen toapplyNATURALJOIN.Inthiscase,theASconstruct can beused
to renamea relation and all itsattributes in the FROMclause.
CROSS JOIN returns the Cartesian product of rows from tables in the join. In other words, it will
producerows which combineeach rowfromthe first tablewitheach rowfrom the secondtable.
OUTERJOIN
Anouterjoindoesnotrequireeachrecordinthetwojoinedtablestohaveamatchingrecord.The
joinedtableretainseachrecord-evenif noothermatchingrecord exists. Outerjoinssubdivide further
into
Leftouterjoins
Rightouterjoins
Full outer joins
Noimplicitjoin-notationforouterjoinsexistsinstandardSQL.
page25
[18CS53]
DatabaseManagementSystem[21CS53]
page26
[18CS53]
DatabaseManagementSystem[21CS53]
MULTIWAYJOIN
It is alsopossibletonestjoinspecifications;thatis,oneofthetablesina joinmayitself bea joined
table.Thisallowsthespecificationofthe joinof threeormoretablesasasinglejoinedtable,which is called
a multiway join.
Example:
number,andthedepartmentm
SELECTPnumber,Dnum,Lname,Address,Bdate
FROM((PROJECTJOINDEPARTMENTONDnum=Dnumber)
JOINEMPLOYEEONMgr_ssn=Ssn)
WHERE
AggregateFunctionsinSQL
Aggregate functions are used to summarize information from multiple tuples into a single-tuple
summary.Anumberofbuilt-inaggregatefunctionsexist: COUNT,SUM, MAX, MIN,andAVG. The
COUNTfunctionreturnsthenumberoftuplesorvaluesasspecifiedinaquery.ThefunctionsSUM, MAX,
MIN, and AVG canbeappliedtoa set or multisetof numeric values andreturn, respectively, the sum,
maximum value, minimum value, and average (mean) of those values. These functions
canbeusedintheSELECTclauseorinaHAVINGclause(whichweintroducelater).Thefunctions MAXand
MINcanalsobeused withattributes thathavenonnumericdomainsif thedomain values have a total
ordering among one another.
Examples
1. Findthesumofthesalariesofallemployees,themaximumsalary,theminimumsalary,andthe
average salary.
SELECTSUM(Salary),MAX(Salary),MIN(Salary),AVG(Salary)
FROMEMPLOYEE;
2.
maximumsalary,theminimumsalary,andtheaveragesalaryinthisdepartment.
SELECTSUM(Salary),MAX(Salary),MIN(Salary),AVG(Salary)
FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)
WHERE
3. Countthenumberofdistinctsalaryvaluesinthedatabase.
SELECTCOUNT(DISTINCTSalary)
FROMEMPLOYEE;
page27
[18CS53]
DatabaseManagementSystem[21CS53]
4. Toretrievethenamesofallemployeeswhohavetwoormoredependents
SELECTLname, Fname
FROMEMPLOYEE
WHERE(SELECTCOUNT(*)
FROMDEPENDENT
WHERESsn=Essn)>=2;
Grouping:TheGROUPBYandHAVINGClauses
Grouping isused to create subgroupsoftuplesbefore summarization. Forexample, we maywant
tofindtheaverage salaryof employees ineachdepartment orthenumber of employees whowork on
each project. In these cases we need to partition the relation into non overlapping subsets (or
groups)oftuples.Eachgroup(partition)willconsistofthetuplesthathavethesamevalueofsome
attribute(s), called the grouping attribute(s).
SQL has a GROUP BY clause for this purpose. The GROUP BY clause specifies the grouping
attributes,whichshouldalsoappearintheSELECTclause,sothatthevalueresultingfromapplying each
aggregate function to a group of tuples appears along with the value of the grouping attribute(s).
Example:Foreachdepartment,retrievethedepartmentnumber,thenumberofemployeesinthe
department, and their average salary.
SELECTDno,COUNT(*),AVG(Salary)
FROMEMPLOYEE
GROUPBYDno;
page28
[18CS53]
DatabaseManagementSystem[21CS53]
Example: For each project, retrieve the project number, the project name, and the number of
employees who work on that project.
SELECTPnumber,Pname,COUNT(*)
FROMPROJECT,WORKS_ON
WHEREPnumber=Pno
GROUPBYPnumber,Pname;
Above query shows how we can use a join condition in conjunction with GROUP BY. In this case,
the groupingand functionsareapplied after the joiningof the two relations.
HAVINGprovidesaconditiononthesummaryinformationregardingthe groupoftuplesassociated
witheachvalueof thegroupingattributes.Onlythe groupsthatsatisfytheconditionareretrievedin the
result of the query.
Example: For each project on which more than two employees work, retrieve the project number,
the projectname,and thenumberof employees who work on theproject.
SELECTPnumber,Pname,COUNT(*)
FROMPROJECT,WORKS_ON
WHERE Pnumber=Pno
GROUPBYPnumber,Pname
HAVING COUNT (*) > 2;
page29
[18CS53]
DatabaseManagementSystem [21CS53]
Example: For each project, retrieve the project number, the project name, and the number of
employees from department 5 who work on the project.
SELECTPnumber,Pname,COUNT(*)
FROMPROJECT,WORKS_ON,EMPLOYEE
WHEREPnumber=PnoANDSsn=EssnANDDno=5
GROUPBYPnumber,Pname;
Example:Foreachdepartmentthathasmorethanfiveemployees,retrievethedepartmentnumber and
the numberof its employees who are making more than $40,000.
SELECTDnumber,COUNT(*)
FROMDEPARTMENT,EMPLOYEE
WHEREDnumber=DnoANDSalary>40000AND
( SELECT Dno
FROMEMPLOYEE
GROUP BY Dno
HAVINGCOUNT(*)>5);
DiscussionandSummaryofSQLQueries
A retrieval query in SQL can consist of up to six clauses, but only the fir st twoSELECT
andFROMare mandatory.The query can span several lines, and is ended by a semicolon. Query
termsareseparated byspaces, andparenthesescanbeusedtogroup relevantpartsof a query in
thestandardway.Theclausesarespecifiedinthefollowingorder,withtheclausesbetweensquare
brackets [ ... ] being optional:
page30
DatabaseManagementSystem[21CS53]
[18CS53]
In general,there are numerous waysto specify the same query in SQL.This flexibility in specifying
queries has advantages and disadvantages.
The main advantage is that users can choose the technique with which they are most
comfortablewhenspecifyingaquery.Forexample,manyqueriesmaybespecifiedwithjoin
conditions in the WHERE clause, or by using joined relations in the FROM clause, or with
someformofnestedqueriesandtheINcomparison.
withaslittlenestingandimpliedorderingaspossible.
The disadvantage of having numerous ways of specifying the same query is that this may
confuse the user, who may not know which technique to use to specify particular types of
queries.Anotherproblemisthatitmaybemoreefficienttoexecuteaqueryspecifiedinone way than
the same query specified in an alternative way
page31
[18CS53]
DatabaseManagementSystem [21CS53]
SpecifyingConstraintsasAssertionsandActionsasTriggers
SpecifyingGeneralConstraintsasAssertionsinSQL
Assertions are used to specify additional types of constraints outside scope of built-in relational
model constraints. In SQL, users can specify general constraints via declarative assertions, using
the CREATE ASSERTION statement of the DDL.Each assertion is givena constraint name and is
specified via a condition similar to the WHERE clause of an SQL query.
Generalform:
CREATEASSERTION<Name_of_assertion>CHECK(<cond>)
Fortheassertion to besatisfied, theconditionspecifiedafterCHECKclausemust returntrue.
For example, to specify the constraint that the salary of an employee must not be greater than the
salary of the manager of the department that the employee works for in SQL, we can write the
following assertion:
CREATEASSERTIONSALARY_CONSTRAINT
CHECK(NOTEXISTS(SELECT*FROMEMPLOYEEE,EMPLOYEEM, DEPARTMENT
DWHERE E.Salary>M.SalaryAND
E.Dno=D.DnumberANDD.Mgr_ssn=M.Ssn));
TheconstraintnameSALARY_CONSTRAINTisfollowedbythekeywordCHECK,whichisfollowed by a
condition in parentheses that must hold true on every database state for the assertion to be
satisfied.Theconstraintnamecanbeusedlatertorefertotheconstraintortomodifyordropit. Any
WHEREclauseconditioncanbeused,butmanyconstraintscanbespecifiedusingtheEXISTSand NOT
EXISTS style of SQL conditions.
ByincludingthisqueryinsideaNOTEXISTSclause,theassertionwillspecifythattheresultofthis
querymustbeemptysothattheconditionwillalwaysbeTRUE.Thus,theassertionisviolatedifthe result of
the query is not empty
Example:considerthebankdatabasewiththefollowingtables
page32
[18CS53]
DatabaseManagementSystem[21CS53]
1. Write an assertion to specify the constraint that the Sum of loans taken by a customerdoesnot
exceed 100,000
CREATEASSERTIONsumofloans
CHECK(100000>= ALL
SELECTcustomer_name,sum(amount)
FROMborrowerb,loanl
WHEREb.loan_number=l.loan_number
GROUPBYcustomer_name);
2. Writean assertion to specifythe constraintthatthe Numberof accountsforeach customerina given
branch is at most two
CREATEASSERTIONNumAccounts
CHECK(2>=ALL
SELECTcustomer_name,branch_name,count(*)
FROM accountA,depositor D
WHEREA.account_number=D.account_number
GROUPBYcustomer_name,branch_name);
IntroductiontoTriggersinSQL
A triggeris a procedure that runsautomatically when a certaineventoccursin the DBMS. In many
casesitis convenientto specify thetypeof actiontobetakenwhencertaineventsoccurand when certain
conditions are satisfied. The CREATE TRIGGER statement is used to implement such actions in
SQL.
Generalform:
CREATETRIGGER<name>
BEFORE|AFTER|<events>
FOREACHROW|FOREACHSTATEMENT
WHEN(<condition>)
<action>
Atriggerhasthreecomponents
1. Event: When this event happens, the trigger is activated
Three event types : Insert, Update, Delete
Two triggering times: Before the event
Aftertheevent
page33
[18CS53]
DatabaseManagementSystem[21CS53]
CreateTriggerABC CreateTriggerXYZ
BeforeInsertOn AfterUpdateOnStudents
Students
This trigger is activated when an insert statement This trigger is activated when an
updatestatementisissuedandaftertheupda
is issued, but before the new record is inserted
teis executed
Doesthetriggerexecuteforeachupdatedordeletedrecord,oroncefortheentire statement ?. We
define such granularity as follows:
CreateTrigger<name> Thisistheevent
Before| After Insert|Update|Delete
CreateTriggerXYZ
AfterUpdateON<tablename>
Foreachstatement
Thistriggerisactivatedonce(perUPDATE Thistriggerisactivatedbeforedeletingeach
statement) after all records are updated record
page34
[18CS53]
DatabaseManagementSystem[21CS53]
Intheaction,youmaywanttoreference:
The newvalues of inserted or updated records (:new)
Theold valuesof deleted or updated records(:old)
CreateTriggerEmpSal
AfterInsertorUpdateOnEmployee
ForEachRow
When(new.salary>150,000)
Begin
Triggerbody if(:new.
End;
Insidethetriggerbody,they
Examples:
1) Iftheemployeesalaryincreasedbymorethan10%,thenincrementtherankfieldby1.
CreateTriggerEmpSal
BeforeUpdateOfsalaryOnEmployee
ForEachRow
Begin
IF(:new.salary>(:old.salary*1.1))Then
:new.rank:=:old.rank+1; End
IF;
End;
/
Wechangedthenewvalueofrankfield
2) KeepthebonusattributeinEmployeetablealways3%ofthesalaryattribute
CreateTriggerEmpBonus Indicatetwoeventsatthesametime
BeforeInsertOrUpdateOnEmployee
ForEachRow
Begin
:new.bonus:=:new.salary*0.03;
End;
Thebonusvalueisalwayscomputed
automatically
page35
[18CS53]
DatabaseManagementSystem[21CS53]
3.
herdirectsupervisorintheCOMPANYdatabase
Several events can trigger this rule:
inserting a new employee record
SupposethattheactiontotakewouldbetocallanexternalstoredprocedureSALARY_VIOLATION which
will notify the supervisor
CREATETRIGGERSALARY_VIOLATION
BEFOREINSERTORUPDATEOFSALARY,SUPERVISOR_SSN
ONEMPLOYEE
FOREACHROW
WHEN(NEW.SALARY>(SELECTSALARYFROMEMPLOYEE
WHERESSN=NEW.SUPERVISOR_SSN))
INFORM_SUPERVISOR(NEW.Supervisor_ssn,NEW.Ssn);
ThetriggerisgiventhenameSALARY_VIOLATION,whichcanbeusedtoremoveor
deactivate the trigger later
salary,orchang
TheactionistoexecutethestoredprocedureINFORM_SUPERVISOR
Triggerscanbeusedinvariousapplications,suchasmaintainingdatabaseconsistency,monitoring database
updates.
Assertionsvs.Triggers
Assertions do notmodify the data,they only checkcertainconditions.Triggers aremore
powerfulbecause the can checkconditionsand also modify the data
Assertionsarenotlinkedtospecifictablesinthedatabaseandnotlinkedto specificevents. Triggers are
linked to specific tables and specificevents
Allassertionscanbeimplementedastriggers(oneormore).Notalltriggerscanbe implemented as
assertions
page36
[18CS53]
DatabaseManagementSystem[21CS53]
Example:Triggervs.Assertion
Weneedtriggers,assertionscannotbeused TriggerEvent:BeforeInsert
Views(VirtualTables) inSQL
ConceptofaViewinSQL
A view in SQL terminology is a single table that is derived from other tables. other tables can be
base tables or previously defined views. A view does not necessarily exist in physical form; it is
considered to be a virtual table, in contrast to base tables, whose tuples are always physically
stored inthedatabase.Thislimitsthepossibleupdateoperationsthat canbeapplied to views,but
itdoesnotprovideanylimitationsonqueryingaview.Wecanthinkofaviewasawayofspecifying a table
thatweneed toreference frequently,even though it maynot existphysically.
For example, referring to the COMPANY database, we may frequently issue queries that retrieve
the employee name and the project names that the employee works on. Rather than having to
specify the join of the three tables EMPLOYEE,WORKS_ON, and PROJECT every time we issue
this query, we can define a view that is specified as the result of these joins. Then we can issue
queriesontheview, whicharespecifiedassingletable retrievalsratherthan asretrievalsinvolving
twojoinsonthreetables.WecalltheEMPLOYEE,WORKS_ON,andPROJECTtablesthedefining tables
of the view.
page37
[18CS53]
DatabaseManagementSystem[21CS53]
SpecificationofViewsinSQL
InSQL,thecommandtospecifyaviewis CREATEVIEW.Theviewisgivena(virtual)tablename
(orviewname),a listof attributenames,andaquerytospecifythecontents of theview.If noneof the view
attributes results from applying functions or arithmetic operations, we do not have to specify new
attribute names for the view, since they would be the same as the names of the attributes of the
defining tables in the default case.
Example1:
CREATEVIEWWORKS_ON1
ASSELECTFname,Lname,Pname,Hours
FROMEMPLOYEE,PROJECT,WORKS_ON
WHERESsn=EssnANDPno=Pnumber;
Example2:
CREATEVIEW DEPT_INFO(Dept_name,No_of_emps,Total_sal)
ASSELECTDname,COUNT(*),SUM(Salary)
FROMDEPARTMENT,EMPLOYEE
WHEREDnumber=Dno
GROUPBYDname;
In example 1, we did not specify any new attribute names for the view WORKS_ON1. In this
case,WORKS_ON1inheritsthenamesoftheviewattributesfromthedefiningtablesEMPLOYEE,
PROJECT, and WORKS_ON.
Example 2 explicitly specifies new attribute names for the view DEPT_INFO, using a one-to-one
correspondencebetweentheattributes specifiedinthe CREATE VIEW clauseand those specified in
the SELECT clause of the query that defines the view.
project,wecanutilizetheWORKS_ON1viewandspecifythequeryas:
page38
[18CS53]
DatabaseManagementSystem[21CS53]
The same query would require the specification of two joins if specified on the base relations
directly.one of the main advantages of a view is to simplify the specification of certain queries.
Views are also used as a security and authorization mechanism.
ViewImplementation,ViewUpdateandInlineViews
Theproblemofefficientlyimplementingaviewforquerying iscomplex.Twomainapproacheshave been
suggested.
Onestrategy,calledquerymodification,involvesmodifyingortransformingtheviewquery (submittedby
theuser) intoa queryontheunderlyingbase tables.For example,thequery
SELECT Fname, Lname
FROM WORKS_ON1
WHERE
wouldbeautomaticallymodifiedtothefollowingquerybytheDBMS:
SELECTFname,Lname
FROMEMPLOYEE,PROJECT,WORKS_ON
WHERESsn=EssnANDPno=Pnumber
AND
Thedisadvantageofthisapproachisthatitisinefficientforviewsdefinedviacomplexqueriesthat
aretime-consumingtoexecute,especiallyif multiplequeriesare goingtobeappliedtothesame view
within a short period of time.
Thesecondstrategy,calledviewmaterialization,involvesphysically creatingatemporaryview table
when the view isfirstqueriedand keeping that table on the assumptionthatotherqueries onthe
viewwillfollow.In this case,anefficientstrategyforautomaticallyupdatingtheviewtable
whenthebasetablesareupdatedmustbedevelopedinordertokeeptheviewup-to-date.
page39
[18CS53]
DatabaseManagementSystem[21CS53]
Techniques using the concept of incremental update have been developed for this purpose,
where the DBMS can determine what new tuples must be inserted, deleted, or modified in a
materialized viewtable whenadatabase update is applied to oneof thedefiningbase tables.
Updating of views is complicated and can be ambiguous. In general, an update on a view defined
on a single table without any aggregate functions can be mapped to an update on the underlying
basetableundercertainconditions.Foraviewinvolvingjoins,anupdateoperationmaybemapped to
update operations on the underlying base relations in multiple ways. Hence, it is often not
possiblefor the DBMS to determine whichof theupdatesis intended.
To illustrate potential problems with updating a view defined on multiple tables, consider the
WORKS_ON1view,andsupposethatweissue thecommandtoupdate thePNAMEattributeof
UV1: UPDATEWORKS_ON1
SET
WHERE AND
AND
This query can be mapped into several updates on the base relations to give the desired update
effect on the view. In addition, some of these updates will create additional side effects that affect
the result of other queries.
Forexample,herearetwopossibleupdates,(a)and(b),onthebaserelationscorrespondingtothe view
update operation in UV1:
(a) :UPDATEWORKS_ON
SETPno=(SELECTPnumber
FROMPROJECT
WHERE
WHEREEssnIN(SELECTSsn FROM
EMPLOYEE
WHERE AND
AND
Pno=(SELECTPnumber
FROMPROJECT
WHERE
page40
DatabaseManagementSystem [18CS53]
[21CS53]
(b) :UPDATEPROJECTSET
WHERE
PROJECT tuple and is the most likely desired update. However, (b)would also give the
desiredupdate effect on the view, butit accomplishes this by
ItisquiteunlikelythattheuserwhospecifiedtheviewupdateUV1wantstheupdatetobe
interpretedasin(b),sinceitalsohasthesideeffectofchangingalltheviewtupleswithPname=
Someviewupdatesmaynotmakemuchsense;forexample,modifyingtheTotal_salattributeofthe
DEPT_INFOviewdoesnotmakesensebecauseTotal_salisdefinedtobethesumoftheindividual
employee salaries. This request is shown as UV2:
UV2:UPDATEDEPT_INFO
SETTotal_sal=100000
WHERE
Alargenumberofupdatesontheunderlyingbaserelationscansatisfythisviewupdate.
Generally, a view update is feasible when only one possible update on the base relations can
accomplishthedesiredupdateeffectontheview.Wheneveranupdateontheviewcanbemapped to more
than one update on the underlying base relations, we must have a certain procedure for choosing
one of the possible updates as the most likely one.
Insummary,wecanmakethefollowingobservations:
Aviewwithasingledefiningtableisupdatableiftheviewattributescontain theprimarykeyofthe base
relation,as wellasall attributeswiththe NOT NULLconstraint thatdonothave default values
specified.
Viewsdefinedonmultipletablesusingjoinsaregenerallynotupdatable.
Viewsdefinedusinggroupingandaggregatefunctionsarenotupdatable.
InSQL,theclauseWITHCHECKOPTIONmustbeaddedattheendof theviewdefinition ifaview is to be
updated. This allows the system to check for view updatability and to plan an execution strategyfor
view updates. It is also possible to define a viewtable in the FROM clause of an SQL query. Thisis
knownasanin-line view. In thiscase,theview isdefinedwithinthequery itself.
page41
DatabaseManagementSystem [18CS53]
[21CS53]
SchemaChangeStatementsinSQL
Schema evolution commands available in SQL can be used to alter a schema by adding or
dropping tables, attributes, constraints, and other schema elements. This can be done while the
databaseis operationalanddoesnotrequirerecompilation ofthedatabase schema.
TheDROPCommand
The DROP command can be used to drop named schema elements, such as tables, domains, or
constraints.Onecanalsodropaschema.Forexample,ifawhole schemaisnolongerneeded,the DROP
SCHEMA command can be used.
There are two drop behavior options: CASCADE and RESTRICT. For example, to remove the
COMPANYdatabaseschemaandallitstables,domains,andotherelements,the CASCADEoption is
used as follows:
DROPSCHEMACOMPANYCASCADE;
If the RESTRICT option is chosen in place of CASCADE, the schema is dropped only if it has no
elements in it;otherwise, the DROPcommand will notbeexecuted. To use the RESTRICT option,
theusermustfirst individuallydropeachelement in the schema,thendrop theschemaitself.
If abaserelationwithinaschemaisnolongerneeded,therelationanditsdefinitioncanbedeleted by using
the DROP TABLE command. For example, if we no longer wish to keep track of
dependentsofemployeesintheCOMPANYdatabase,,wecangetridoftheDEPENDENTrelation by
issuing the following command:
DROPTABLEDEPENDENTCASCADE;
If the RESTRICT option is chosen instead of CASCADE, a table is dropped only if it is not
referenced in any constraints (for example, by foreign key definitions in another relation) or views
or by any other elements. With the CASCADE option, all such constraints, views, and other
elements that reference the table being dropped are also dropped automatically from the schema,
along with the table itself.
The DROP TABLE command not only deletes all the records in the table if successful, but also
removes the table definition from the catalog. If it is desired to delete only the records but to leave
the table definition for future use, then the DELETE command should be used instead of DROP
TABLE.
The DROP command can also be used to drop other types of named schema elements, such as
constraints or domains.
TheALTERCommand
page42
[18CS53]
DatabaseManagementSystem[21CS53]
The definition of a base table or of other named schema elements can be changed by using the
ALTER command. For base tables, the possible alter table actions include adding or dropping a
column(attribute), changinga columndefinition,andadding ordroppingtable constraints.
For example, to add an attribute for keeping track of jobs of employees to the EMPLOYEE base
relation in the COMPANY schema , we can use the command:
ALTERTABLECOMPANY.EMPLOYEEADDCOLUMNJobVARCHAR(12);
Wemuststillentera valueforthenewattribute Jobforeachindividual EMPLOYEE tuple.Thiscan be
done either by specifying a default clause or by using the UPDATE command individually on
eachtuple.Ifnodefaultclauseisspecified, thenewattributewillhaveNULLsinallthetuplesofthe
relationimmediatelyafterthecommandisexecuted;hence,theNOTNULLconstraintisnotallowed in this
case.
Todropacolumn,wemustchooseeitherCASCADEorRESTRICTfordropbehavior.IfCASCADE is
chosen, all constraints and views that reference the column are dropped automatically from the
schema,alongwiththecolumn.IfRESTRICTischosen,thecommandissuccessfulonlyifnoviews or
constraints (orother schema elements) reference the column.
For example, the following command removes the attribute Address from the EMPLOYEE base
table:
ALTERTABLECOMPANY.EMPLOYEEDROPCOLUMNAddressCASCADE;
AlterTable-Alter/ModifyColumn
Tochangethedatatypeofacolumninatable,usethefollowingsyntax:
ALTERTABLEtable_name
MODIFYcolumn_namedatatype;
For example we can change the data typeof thecolumn named "DateOfBirth"fromdate to yearin the
"Persons" table using the following SQL statement:
ALTERTABLEPersons
ALTERCOLUMNDateOfBirthyear;
page43
[18CS53]
DatabaseManagementSystem[M23BCS404]
Noticethatthe "DateOfBirth" column is now oftypeyear andisgoing to hold ayearin a two- or four-
digit format.
page44