0% found this document useful (0 votes)
22 views111 pages

PPT

The document covers the structure and principles of relational databases, including integrity constraints, relational algebra, and SQL. It details the schema of relations, characteristics of relations, and the mapping of ER diagrams to relational schemas. Additionally, it discusses various types of integrity constraints, including domain, key, entity integrity, and referential integrity, along with the process of relational database design.

Uploaded by

A K2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views111 pages

PPT

The document covers the structure and principles of relational databases, including integrity constraints, relational algebra, and SQL. It details the schema of relations, characteristics of relations, and the mapping of ER diagrams to relational schemas. Additionally, it discusses various types of integrity constraints, including domain, key, entity integrity, and referential integrity, along with the process of relational database design.

Uploaded by

A K2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 111

Module 2

 Structure of Relational Databases


 Integrity Constraints
 Synthesizing ER diagram to relational schema

 Introduction to Relational Algebra


 Select, Project, Cartesian product operations

 Join - Equi-join, Natural join

 Query examples

 Introduction to Structured Query Language (SQL)


 Data Definition Language (DDL)
 Table definitions and operations
 CREATE, DROP, ALTER, INSERT, DELETE, UPDATE.
Schema of a Relation
 The Schema of a Relation: R (A1, A2, .....An)
 Relation schema R is defined over attributes A1, A2, .....An

 Degree(or arity)of a relation  Number of attributes n of its


relation schema.
 A relation state r(R) is a mathematical relation of degree n on
the domains
 {dom(A1) , dom(A2), ..., dom(Ai)},

 which is a subset of the Cartesian product of the domains

that define R:

 r(R) C= (dom(A1) X dom(A2) X... X dom(An))

 The Cartesian product specifies all possible combinations of


values from the underlying domains.

 Hence, if we denote the total number of values, or cardinality, in a


domain D by |D| (assuming that all domains are finite),the total
number of tuples in the Cartesian product is

COMPANY database schema Diagram

 There are 6 relations in this database.


 For each relation, there is different attributes or the header of the
column.
 EMPLOYEE relation has a degree of 10.
Characteristics of Relations
1. 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= to be ordered .

2. Ordering of Values with in a Tuple, and an Alternative


Definition of a Relation
 An n-tuple is an ordered list of n values, so the ordering of

values in a tuple- and hence of attributes in a relation schemais


important.
3. Values and Nulls in the Tuples:
 Each value in a tuple is atomic
 Hence, multivalued attributes must be represented by

separate relations, and

 Composite attributes are represented only by their simple


component attributes in the basic relational model.

 Nulls  Used to represent the values of attributes that may be


unknown or may not apply to a tuple.
 In general several meanings for null values
 "value unknown,"

 "value exists but is not available," or

 "attribute does not apply to this tuple."


4. Interpretation (Meaning) of a Relation:

 The relation schema can be interpreted as a declaration or a type


of assertion.

 The student schema assert that a student entity has Name, SSN,
HomePhone, Address, OfficePhone, Age, and CGPA.

 Each tuple of the database will be a fact or instance of the


assertion.

 In the case of STUDENT instance, each tuple is a fact about a


student.
Relational Model Notation
 A relation schema R of degree n is denoted by R(A1, A2,…., An)

 An n-tuple t in a relation r(R) is denoted by t = <Vi,V2……Vn>


 where vi is the value corresponding to attribute Ai.

 The following notation refers to component values of tuples:


 Both t[Ai] and t.Ai (and sometimes t[i] refer  the value Vi in t

for attribute Ai.

 Both t[Au, Aw,…., Az] and t.( Au, Aw,….,Az),where Au, Aw,….,Az is
a list of attributes from R, refer to the sub tuple of values from t
corresponding to the attributes specified in the list.

 The letters Q, R, S denote relation names.


 The letters q, r, s denote relation states.
 An attribute A can be qualified with the relation name R to which
it belongs by using the dot notation R. Example STUDENT.Name.

 This is because the same name may be used for two attributes in
different relations.

 However, all attribute names in a particular relation must be


distinct.
Relational Databases and Relational
Database Schemas
 A relational database schema S is a set of relation schemas
S= ( R1 , R2 ,..., Rm } and a set of integrity constraints IC.
 A relational database state DB of S is a set of relation states
DB= { r1 , r2 ,..., rm } such that each rj is a state of R, and such
that the rj relation states satisfy the integrity constraints specified
in IC.
 The underlined attributes are primary keys in each relation.

 A database state that does not obey all the integrity constraints is
called an invalid state, and
 A state that satisfies all the constraints in IC is called a valid state.

 In the DEPARTMENT and DEPT_LOCATIONS contains attributes


named DNUMBER, both have the same meaning in the mini world.

 There may be different attributes in different relation


which has same name.
 Eg: Attribute NAME can be used for project name, department

name and employee name.


 The concepts are different in the mini world.
 Database
 Table
 Relation
 Relation schema
 Relationship
 Tuple
 Attributes  Types
 Domain
 Entity type
 Entity Set
 Enity
 Keys  Types
 Constraints
 Relational database schema
 Valid table / Valid Relation  Integrity constraint
 Domain constraints

 Key constraints

 Entity integrity constraints

 Referential integrity constraints


Integrity Constraints
 Integrity Constraints are used to ensure accuracy and consistency of
data in a relational database.

 Constraints are conditions/ rules that must hold on all valid relation
instances.

 Constraints on databases can generally be divided into three main


categories:

1. Constraints that are inherent in the data model.  Inherent model


based constraints.

2. Constraints that can be directly expressed in the schemas of the data


model  Schema-based constraints.

3. Constraints that cannot be directly expressed in the schemas of the


data model, and hence must be expressed and enforced by the
application programs. Application-based constraints.
 Also Can be classified as

1. Domain Constraints
2. Key constraints
3. Entity integrity constraints
4. Referential Integrity constraints

 Domain Constraints
 Specify that within each tuple, the value of each attribute A

must be an atomic value from the domain dom(A).


 The data types associated with domains typically include

standard numeric data types for integer, flot, character, strings


etc…
Key Constraints
 The relation is a set of tuples.  The tuples must be distinct/
identifiable.
 So two tuples in a relation must not have same values for all attributes.
 There exist some set of attributes in a relation the combination of
those will be distinct for each tuple.
 Those set of attributes are termed as Super Key.
 The set of those attributes can be represent SK, then in a relation for
two tuples t1 and t2, there is a constrain:
t1[SK] != t2[SK]

 A super key SK specifies a uniqueness constraint that no two distinct


tuples in any state r of R can have the same value for SK.

 A key K of a relation schema R is a super key of R with the additional


property that removing any attribute A from K leaves a set of attributes
K' that is not a super key of R any more.
 If a relation has several candidate keys, one is chosen arbitrarily
to be the primary key.
 The primary key attributes are underlined.

1. Foreign key
2. Alternate Key
3. Unique Key
4. Composite/ Compound Key
Entity Integrity Constraints (Primary key
integrity constraints)
 Entity Integrity is the mechanism the system provides to maintain
primary keys.

 Each table in the database can have at most one PRIMARY KEY constraint.

 The implementation of the PRIMARY KEY constraint guarantees that both of


the following are true
 No 2 rows of a table have duplicate values in the specified column/set of

columns.
 The primary key columns do not allow nulls.
 i.e ., 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) for any R
Referential Integrity constraints
 There is other constrain which is related to the relationships
between the relations  referential constrain

 The referential integrity constraint is specified between two


relations and is used to maintain the consistency among tuples in
the two relations.

 Referential integrity constraint  Based on the concept of Foreign


key
 Example: the DNO in the EMPLOYEE relation refers the DNUMBER
in DEPARTMENT.

 So the DNO in EMPLOYEE must contain a value that exist as


DNUMBER of a tuple in DEPARTMENT.
Relational Database Design Using ER-to-
Relational Mapping
ER-to-Relational Mapping Algorithm

Step 1: Mapping of Regular Entity Types


Step 2: Mapping of Weak Entity Types
Step 3: Mapping of Binary 1:1 Relationship Types.
Step 4: Mapping of Binary 1:N Relationship Types.
Step 5: Mapping of Binary M: N Relationship Types
Step 6: Mapping of Multi-valued Attributes
Step 7: Mapping of N-ary Relationship Types
Step 1: Mapping of Regular Entity Types

 For each regular (strong) entity type E in the ER schema, create a


relation R that includes all the simple attributes of E.

 Include only the simple component attributes of a composite


attribute.

 Choose one of the key attributes of E as the primary key for R.

 If the chosen key of E is a composite, then the set of simple


attributes that form it will together form the primary key of R.
Eg
 Create the relations EMPLOYEE, DEPARTMENT, and PROJECT in Figure
to correspond to the regular entity types EMPLOYEE, DEPARTMENT, and
PROJECT

 The foreign key and relationship attributes, if any, are not included
yet; they will be added during subsequent steps.

 These include the attributes Super_ssn and Dno of EMPLOYEE, Mgr_ssn


and Mgr_start_date of DEPARTMENT, and Dnum of PROJECT.

 In our example, we choose Ssn, Dnumber, and Pnumber as primary


keys for the relations EMPLOYEE, DEPARTMENT, and PROJECT,
respectively.

 Knowledge that Dname of DEPARTMENT and Pname of PROJECT are


secondary keys is kept for possible use later in the design.
Step 2: Mapping of Weak Entity Types

 For each weak entity type W in the ER schema with owner entity
type E, create a relation R and include all simple attributes of W as
attributes of R.

 In addition, include as foreign key attributes of R, the primary key


attribute(s) of the relation(s) that correspond to the owner entity
type(s)

 The primary key of R is the combination of the primary key(s) of the


owner(s) and the partial key of the weak entity type W, if any

 If there is a weak entity type E2 whose owner is also a weak entity


type E1, then E1 should be mapped before E2 to determine its
Eg
 We create the relation DEPENDENT in this step to correspond to
the weak entity type DEPENDENT - Figure 9.3(b)

 We include the primary key Ssn of the EMPLOYEE relation.

 The primary key of the DEPENDENT relation is the combination


{Essn, Dependent_name}.
Step 3: Mapping of Binary 1:1 Relationship Types.

 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.

 There are three possible approaches:

1. Foreign key approach,


2. Merged relationship approach, and
3. Cross reference or relationship relation approach.
Foreign key approach:
 Choose one of the relations S, and 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 in the role of S.
 Include all the simple attributes (or simple components of composite
attributes) of the 1:1 relationship type R as attributes of S. P (P) 
T (F)

 EG: We map the 1:1 relationship type MANAGES from by choosing the
participating entity type DEPARTMENT to serve in the role of S because its
participation in the MANAGES relationship type is total (every department has
a manager)

 We include the primary key of the EMPLOYEE relation as foreign key in the
DEPARTMENT relation and rename it Mgr_ssn.

 We also include the simple attribute Start_date of the MANAGES relationship


type in the DEPARTMENT relation and rename it Mgr_start_date
Merged relation approach:

 An alternative mapping of a 1:1 relationship type is to merge the


two entity types and the relationship into a single relation.

 This is possible when both participations are total, as this would


indicate that the two tables will have the exact same number of
tuples at all times.
Cross-reference or relationship relation approach:

 3rd option is to set up a third relation R for the purpose of cross-


referencing the primary keys of the two relations S and T
representing the entity types.

 As we will see, this approach is required for binary M:N


relationships.

 The relation R will include the primary key attributes of S and T as


foreign keys to S and T.

 The primary key of R will be one of the two foreign keys, and the
other foreign key will be a unique key of R.

 The drawback is having an extra relation


Step 4: Mapping of Binary 1:N Relationship Types.

 For each regular binary 1:N relationship type R, identify the relation S
that represents the participating entity type at the N-side of the
relationship type.

 Include as foreign key in S the primary key of the relation T that


represents the other entity type participating in R;
 We do this because each entity instance on the N-side is related to

at most one entity instance on the 1-side of the relationship type.

 1(P)  N (F)

 Include any simple attributes (or simple components of composite


attributes) of the 1:N relationship type as attributes of S.
Eg
 We now map the 1:N relationship types WORKS_FOR, CONTROLS,
and SUPERVISION

 For WORKS_FOR we include the primary key Dnumber of the


DEPARTMENT relation as foreign key in the EMPLOYEE relation and
call it Dno.

 For SUPERVISION we include the primary key of the EMPLOYEE


relation as foreign key in the EMPLOYEE relation itself — because
the relationship is recursive — and call it Super_ssn.

 The CONTROLS relationship is mapped to the foreign key attribute


Dnum of PROJECT, which references the primary key Dnumber of
the DEPARTMENT relation.
Step 5: Mapping of Binary M: N Relationship Types

 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 relations


that represent the participating entity types; their combination will
form the primary key of S.

 Also include any simple attributes of the M:N relationship type (or
simple components of composite attributes) as attributes of S.

 Notice that we cannot represent an M:N relationship type by a single


foreign key attribute in one of the participating relations (as we did for
1:1 or 1:N relationship types) because of the M:N cardinality ratio; we
must create a separate relationship relation S.
Eg
 We map the M: N relationship type WORKS_ON by creating the
relation WORKS_ON

 We include the primary keys of the PROJECT and EMPLOYEE


relations as foreign keys in WORKS_ON and rename them Pno and
Essn, respectively.

 We also include an attribute Hours in WORKS_ON to represent the


Hours attribute of the relationship type.

 The primary key of the WORKS_ON relation is the combination of


the foreign key attributes {Essn, Pno}
Step 6: Mapping of Multi-valued Attributes

 For each multivalued attribute A, create a new relation R.

 This relation R will include an attribute corresponding to A, plus


the primary key attribute K—as a foreign key in R — of the
relation that represents the entity type or relationship type that
has A as a multivalued attribute.

 The primary key of R is the combination of A and K.

 If the multivalued attribute is composite, we include its simple


components.
Eg;
 We create a relation DEPT_LOCATIONS - 9.3(d).

 The attribute Dlocation represents the multivalued attribute


LOCATIONS of DEPARTMENT
 While Dnumber as foreign key — represents the primary key of

the DEPARTMENT relation.

 The primary key of DEPT_LOCATIONS is the combination of


{Dnumber, Dlocation}.
Step 7: Mapping of N-ary Relationship Types

 For each n-ary relationship type R, where n > 2, create a new


relation S to represent R.

 Include as foreign key attributes in S the primary keys of the


relations that represent the participating entity types.

 Also include any simple attributes of the n-ary relationship type


(or simple components of composite attributes) as attributes of S.

 The primary key of S is usually a combination of all the foreign


keys that reference the relations representing the participating
entity types.
Relational Algebra
 Relational algebra is the basic set of operations for the relational model

 These operations enable a user to specify basic retrieval requests (or


queries)

 The result of an operation is a new relation, which may have been formed
from one or more input relations

 The algebra operations produce new relations

 These can be further manipulated using operations of the same algebra

 A sequence of relational algebra operations forms a relational algebra


expression

 The result of a relational algebra expression is also a relation that


represents the result of a database query (or retrieval request)

 Relational Algebra consists of several groups of operations


1. Unary Operation:
 The unary operations are the operations which operate on one
operand.
 For example, SELECT, PROJECT, RENAME operation

2. Binary Operation:
 There are operations which operate on two operands known as
binary operations. For example,
JOIN, DIVISION

Relational Algebra operations from set theory


Include
 UNION, INTERSECTION, SET DIFFERENCE, CARTESIAN PRODUCT
etc.
Unary Operations
SELECT Operation
 Used to select a subset of the tuples from a relation that satisfy a
selection condition.

 One can consider the SELECT operation to be a filter that keeps only
those tuples that satisfy a qualifying condition.

 The general format of SELECT operation is:

 Where the symbol (sigma) is used to denote the SELECT operator,


 Selection condition is a Boolean expression specified on the
attributes of relation R
 The SELECT operator is unary  It is applied to a single relation

 Degree of the relation resulting from a SELECT operation  Its


number of attributes  Same as the degree of R.

 The number of tuples in the resulting relation is always less than


or equal to the number of tuples in R.

 That is

 The fraction of tuples selected by a selection condition is referred


to as the selectivity of the condition.

 SELECT operation is commutative; that is,


 The Boolean expression specified in <select condition> is made up
of a number of clauses of the form

 <attribute name>  name of an attribute of R,


 <comparison op>  normally one of the operators {=, <=, >, >=, !
=},
 <constant value>  Constant value from the attribute domain.

 Clauses can be arbitrarily connected by the Boolean operators AND,


OR, and NOT to form a general selection condition.

<cond1> AND <cond2>AND…..AND<condn> (R)


PROJECT Operation
 The PROJECT operation selects certain columns from the table and
discards the other columns.

 If we are interested in only certain attributes of a relation, we use the


PROJECT operation to project the relation over these attributes only.

 The result of the PROJECT operation can hence be visualized as a


vertical partition of the relation into two relations:

 The general form of the PROJECT operation is:

 Where π(pi) is the symbol used to represent the PROJECT operation

 <attribute list> is the desired list of attributes from the attributes of


relation R.
 R A relational algebra expression whose result is a relation

 The result of the PROJECT operation has only the attributes specified in
<attribute list> in the same order as they appear in the list.
 Hence, its degree is equal to the number of attributes in <attribute list>

 If the attribute list includes only non-key attributes of R, duplicate tuples


are likely to occur.

 The PROJECT operation removes any duplicate tuples, so the result of


the PROJECT operation is a set of tuples, and hence a valid relation.
 This is known as duplicate elimination.

 The number of tuples in a relation resulting from a PROJECT operation is


always less than or equal to the number of tuples in R.

 If the projection list is a super key of R-that is, it includes some key of R-
the resulting relation has the same number of tuples as R.
Sequences of Operations and the
RENAME Operation
 We can write the operations as a single relational algebra
expression by nesting the operations.

 It is often simpler to break down a complex sequence of


operations by specifying intermediate result relations than to
write a single relational algebra expression.
 Eg:

 By giving name to intermediate results the above expression can


be written as
 If no renaming is applied, the names of the attributes in the resulting
relation of a SELECT & PROJECT operation are the same as those in the
original relation and in the same order.

 A formal RENAME operation which can rename either the relation name or
the attribute names, or both.

 The general format of the RENAME operation is :

 Symbol (rho) denote the RENAME operator, S the new relation name
& (Bl, B2,..., Bn)  the new attribute names.

 First expression renames both the relation and its attributes,

 Second renames the relation only, and the third renames the attributes
only.
Relational Algebra operations from set
theory
 Binary operations that is applying on two sets.
 When this operation is applied in between two relations the type of
the tuples in both relation must be same.
 This condition is called compatible.

 Two relations R (A1, A2,..., An) and S (B1, B2,..., Bn) are said to be
union compatible
 if they have the same degree n and

 if dom(A) = dom(B)

 This means that the two relations have the same number of
attributes, and each corresponding pair of attributes has the same
domain.

 We can define the three operations UNION, INTERSECTION, and SET


DIFFERENCE on two union-compatible relations R and S as follows:
UNION:
 The result of this operation, denoted by R Ù S, is a relation that
includes all tuples that are either in R or in S or in both R and S.
 Duplicate tuples are eliminated.

INTERSECTION:
 The result of this operation, denoted by R Ω S, is a relation that
includes all tuples that are in both R and S.

SET DIFFERENCE (or MINUS):


 The result of this operation, denoted by R - S, is a relation that
includes all tuples that are in R but not in S.
The CARTESIAN PRODUCT (or
CROSSPRODUCT ) Operation
 The CARTESIAN PRODUCT is also called CROSS PRODUCT or
CROSS JOIN denoted by X.

 This is also a binary relation but the union compatibility of the


relation is not necessary.

 Suppose a relation R (A1, A2, …., An) X S (B1, B2, …., Bm) will
result to a relation Q (A1, A2, …., An, B1, B2, …., Bm ) which has
an n+m attributes.

 Let the numbers of tuples in R is nR and that of S is nS then the


number of tuples of Q will be
 nR* nS
Example:
 Suppose the list of dependents of female employees is needed
which can be produced using this operation.
 But the result is not correct, because it takes all the combination of
tuples in EMPNAMES and that in DEPENDENT.

 For getting the female employee with her dependent those tuples
where the SSN is equal to the ESSN have to filter.

 Then the ACTUAL_DEPENDENTS will be:

 The last step will result into a relation like :

 Then there need two operations. CARTESIAN PRODUCT & SELECT

 Which can be specified in a single operation by using new operation


called JOIN.
Binary Operations
The JOIN Operation

The JOIN operation, denoted by ⋈ is used to combine related
tuples from two relations into single “longer” tuples.
Example :
 To retrieve the list of the managers in the COMPANY the join
operation can be used.

 To get the manager’s name, we need to combine each


department tuple with the employee tuple whose Ssn value
matches the Mgr_ssn value in the department tuple.

 We do this by using the JOIN operation and then projecting the


result over the necessary attributes, as follows:
 The result of the JOIN is a relation Q with n + m attributes
Q(A1,A2, ...,An,B1,B2,... ,Bm) in that order;
 Q has one tuple for each combination of tuples—one from R and one
from S— whenever the combination satisfies the join condition.

 This is the main difference between CARTESIAN PRODUCT and


JOIN.

 In JOIN, only combinations of tuples satisfying the join condition appear


in the result, whereas in the CARTESIAN PRODUCT all combinations of
tuples are included

 The join condition is specified on attributes from the two relations R and
S and is evaluated for each combination of tuples.
 Each tuple combination for which the join condition evaluates to TRUE
is included in the resulting relation Q as a single combined tuple.
 A general join condition is of the form
The EQUIJOIN and NATURAL JOIN
 The most common use of JOIN involves join conditions with
equality comparisons only.
 Such a JOIN, where the only comparison operator used is =, is
called an EQUIJOIN.
 The example in the above join operation is EQUIJOIN.

 There is another version called NATURAL JOIN in which the two


attributes which will be compared, have same name.
 Then the JOIN operation will contain only one attribute.

 If the attribute in the two relations haven’t same name, the


rename operation must be performed.
 The NATURAL JOIN denoted by *

 Example :
 Here combining the PROJECT tuple with each DEPARTMENT tuple.

 First rename the Dnumber attribute of DEPARTMENT to Dnum—so


that it has the same name as the Dnum attribute in PROJECT—
and then we apply NATURAL JOIN.

 The result of above operation will be the combined tuples in


DEPARTMENT and PROJECT which have same Dnum value.
 The attribute Dnum is called the join attribute for the NATURAL
JOIN  Only attribute with the same name in both relations, only one
join attribute value is kept.

 If the attributes have same name, then the rename operation is not
necessary.

 A more general, but non standard definition for NATURAL JOIN is

 In this case, <list1> specifies a list of i attributes from R,


 and <list 2> specifies a list of i attributes from S.

 Only the list corresponding to attributes of the first relation R—<list


1>— is kept in the result Q.
 Notice that if no combination of tuples satisfies the join condition,
the result of a JOIN is an empty relation with zero tuples.

 In general, if R has nR tuples and S has nS tuples, the result of a


JOIN operation R ⋈ <join condition> S will have between zero and
nR*nS tuples.

 These operations are also known as inner joins, to distinguish


them from a different join variation called outer joins.

 If there is no join condition, all combinations of tuples qualify


and the JOIN degenerates into a CARTESIAN PRODUCT, also called
CROSS PRODUCT or CROSS JOIN.
Outer Join
 Developed for the case where
 The user wants to keep all the tuples in R, or

 All those in S, or

 All those in both relations in the result of the JOIN, regardless of

whether or not they have matching tuples in the other relation.

 Without losing any tuples for lack of matching values.

 There are two types of Outer Join operations LEFT OUTER JOIN and
RIGHT OUTER JOIN.
 The LEFT OUTER JOIN
 Keeps every tuple in the first or left, relation R; if no

matching tuple is found in S


 Then the attributes of Sin the join result are filled or padded
Outer Union
 The OUTER UNION operation was developed to take the union of
tuples from two relations that have some common attributes, but
are not union (type) compatible.

 This operation will take the UNION of tuples in two relations R(X,
Y) and S(X, Z) that are partially compatible, meaning that only
some of their attributes, say X, are union compatible.

 The attributes that are union compatible are represented only


once in the result, and those attributes that are not union
compatible from either relation are also kept in the result relation
T(X, Y, Z).

 It is therefore the same as a FULL OUTER JOIN on the common


attributes.
 Two tuples t1 in R and t2 in S are said to match if t1[X]=t2[X]
 These will be combined (unioned) into a single tuple in t.
 Tuples in either relation that have no matching tuple in the other
relation are padded with NULL values
Examples of Queries
Query 1.
 Retrieve the name and address of all employees who work for the
‘Research’ department

Query 2.
 For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s
last name, address, and birth date.

Query 3.
 Find the names of employees who work on all the projects
controlled by department number 5.
Query 4.
 Make a list of 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

Query 5.
 List the names of all employees with two or more dependents

Query 6.
 Retrieve the names of employees who have no dependents.

Query 7.
 List the names of managers who have at least one dependent.
Database Languages
 A DBMS is a software package that carries out many different
tasks including the provision of facilities to enable the user to
access and modify information in the database.

 To provide the various facilities to different types of users, a DBMS


normally provides one or more specialized programming
languages called Database languages.

 Some main languages are supported by the database are


Data Definition Language (DDL):
 It is used to specify a database conceptual schema using set of
definitions. It supports the definition or declaration of database objects.
 The more common DDL commands follow:
 CREATE TABLE: Creates a table with the column names the user
provides. The user also needs to specify a type for the data in each
column.
 ALTER TABLE: Adds or removes a column from a table. It also adds or
drops table constraints and alters column attributes.
 DROP TABLE: Delete all rows and removes the table definition from the
database.

 TRUNCATE : de-allocation operation  Like Delete in DML

 COMMENT : Comment about table/ table column


Data Manipulation Language (DML):

 Set of operations to support the basic data manipulation operations on the


data held in the database.
 It is used to query, update or retrieve data stored in a database.

Some of the tasks that come under DML:

 INSERT: Adds new rows to a table.


 INSERT is used to populate a newly created table or to add a new row(or rows) to an
already-existing table.
 UPDATE: Changes an existing value in a column or group of columns in a
table.
 DELETE: Removes a specified row or set of rows from a table.

 SELECT: Used to query and display data from a database.


 The SELECT statement specifies which columns to include in the result

set.
 The vast majority of the SQL commands used in applications are SELECT
Structured Query Language
SQL Commands
 The standard SQL commands to interact with relational databases
are CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETE.
 These commands can be classified into the following groups
based on their nature

 DDL - Data Definition Language


 DML - Data Manipulation Language

 DQL-Data Query Language


SQL - CREATE Table
 Creating a basic table involves naming the table and defining its
columns and each column's data type.
 The SQL CREATE TABLE statement is used to create a new table.
 Basic syntax of CREATE TABLE statement is as follows:

CREATE TABLE table_name ( column1 datatype, column2


datatype, column3 datatype, ..... columnN datatype, PRIMARY
KEY( one or more columns ));

Example:

 CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR


(20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY
INT (10), PRIMARY KEY (ID));
SQL - ALTER Table
 The SQL ALTER TABLE command is used to add, delete or modify
columns in an existing table.
 You would also use ALTER TABLE command to add and drop
various constraints on an existing table.
 The basic syntax of ALTER TABLE to add a new column in an
existing table is as follows:
ALTER TABLE table_name ADD column_name datatype;

 Eg: ALTER TABLE CUSTOMERS ADD CITY CHAR(20);

 The basic syntax of ALTER TABLE to DROP COLUMN in an existing


table is as follows:
ALTER TABLE table_name DROP COLUMN column_name;
 The basic syntax of ALTER TABLE to change the DATA TYPE of a column
in a table is as follows:
ALTER TABLE table_name MODIFY COLUMN column_name
datatype;

 The basic syntax of ALTER TABLE to add a NOT NULL constraint to a


column in a table is as follows:
ALTER TABLE table_name MODIFY column_name datatype NOT
NULL;

 The basic syntax of ALTER TABLE to ADD PRIMARY KEY constraint to a


table is as follows:
ALTER TABLE table_name ADD PRIMARY KEY (Column_name);

 The basic syntax of ALTER TABLE to DROP PRIMARY KEY constraint from
a table is as follows:
ALTER TABLE table_name DROP PRIMARY KEY;
SQL - DROP Table
 The SQL DROP TABLE statement is used to remove a table
definition and all data, constraints, and permission specifications
for that table.

 Basic syntax of DROP TABLE statement is as follows:

DROP TABLE table_name;

 Eg: DROP TABLE CUSTOMERS;


SQL - INSERT Query
 Used to add new rows of data to a table in the database.

 2 type reprasentation

INSERT INTO TABLE_NAME (column1, column2,


column3,...columnN) VALUES (value1, value2, value3,...valueN);

 Specify the column(s) name in the SQL query if you are adding values
for all the columns of the table.
 But make sure the order of the values is in the same order as the
columns in the table.
 Syntax:
INSERT INTO TABLE_NAME VALUES
(value1,value2,value3,...valueN);

 Example: INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
SQL - UPDATE Query
 The SQL UPDATE Query is used to modify the existing records in a
table.

 Use WHERE clause with UPDATE query to update selected rows


otherwise all the rows would be affected.

 Syntax:

UPDATE table_name SET column1 = value1, column2 = value2....,


columnN = valueN WHERE [condition];

 Eg:
 UPDATE CUSTOMERS SET ADDRESS = “Kannur” where ID = 1;
SQL - DELETE Query
 The SQL DELETE Query is used to delete the existing records from a
table.
 Use WHERE clause with DELETE query to delete selected rows,
otherwise all the records would be deleted.
 Syntax

DELETE FROM table_name WHERE [condition];

 Eg: DELETE FROM CUSTOMERS WHERE ID = 6;

 With out WHERE

 DELETE FROM CUSTOMERS;

 Drop statement is used to delete the entire table structure.


 Delete is used just to delete the data. Table structure will be unaffected.
SQL - SELECT Query
 SQL SELECT statement is used to fetch the data from a database
table which returns data in the form of result table. These result
tables are called result-sets.

Syntax:
SELECT column1, column2, columnN FROM table_name;

 Here, column1, column2...are the fields of a table whose values


you want to fetch.
 If you want to fetch all the fields available in the field, then you
can use the following syntax:

SELECT * FROM table_name;


Exercises
Exercises – ER to Relations
Consider the following relations:
 FACULTY (FNO, NAME, GENDER, AGE, SALARY, DNUM)
 DEPARTMENT (DNO, DNAME, DPHONE)
 COURSE (CNO, CNAME, CREDITS, ODNO)
 TEACHING (FNO, CNO, SEMESTER)
DNUM is a foreign key that identifies the department to which a
faculty belongs.
ODNO is a foreign key identifying the department that offers a course.

Write SQL expressions for the following queries:


a) Names and department number of faculty members.
b) Names and Phone number of department No 2.
c) Names and credits of Course no 5.
d) Faculty number and corresponding course no of 4 th semester.
 SELECT NAME, DNUM FROM FACULTY;

 SELECT DNAME, DPHONE FROM DEPARTMENT WHERE DNO=2;

 SELECT CNAME, CREDIT FROM COURSE WHERE CNO = 5;

 SELECT FNO,CNO FROM TEACHING WHERE SEMESTER = 4;


Consider the following relations:
 FACULTY (FNO, NAME, GENDER, AGE, SALARY, DNUM)
 DEPARTMENT (DNO, DNAME, DPHONE)
 COURSE (CNO, CNAME, CREDITS, ODNO)
 TEACHING (FNO, CNO, SEMESTER)
DNUM is a foreign key that identifies the department to which a faculty
belongs.
ODNO is a foreign key identifying the department that offers a course.

Write SQL expressions for the following queries:

a) Names and department names of faculty members.


b) Names of faculty members not offering any course.
c) Names of departments offering more than three courses, in
alphabetic order.
 SELECT NAME, DNAME FROM FACULTY, DEPARTMENT WHERE
DNUM=DNO;

 SELECT NAME FROM FACULTY WHERE FNO NOT IN(SELECT FNO


FROM TEACHING);

 SELECT DNAME FROM DEPARTMENT WHERE DNO IN (SELECT


ODNO FROM COURSE GROUP BY ODNO HAVING COUNT(CNO)>3);
Consider the following relations:
FACULTY(FNO, NAME, GENDER, AGE, SALARY, DNUM)
DEPARTMENT(DNO, DNAME, DPHONE)
COURSE(CNO, CNAME, CREDITS, ODNO)
TEACHING(FNO, CNO, SEMESTER)

DNUM is a foreign key that identifies the department to which a faculty


belongs. ODNO is a foreign key identifying the department that offers
a course.
Write SQL expressions for the following queries:
a) Course numbers and names of 3-credit courses offered by ‘CS’
department.
b) Names of faculty members teaching maximum 3 courses.
c) Names of departments along with number of courses offered by each
of them, in the increasing order of number of courses; exclude
 SELECT CNO CNAME FROM COURSE WHERE CREDITS = 3 AND
ODNO = {SELECT DNO FROM DEPARTMENT WHERE
DNAME = “CS” };

 SELECT NAME FROM FACULTY, TEACHING, COURSE WHERE


FACULTY.FNO= TEACHING.FNO AND TEACHING.CNO =
COURSE.CNO GROUP BY FNO HAVING COUNT(CNO)<= 3;

 SELECT DNAME, COUNT(CNO) FROM DEPARTMENT,COURSE


WHERE DNO=ODNO GROUP BY DNAME ORDER BY COUNT(CNO);
Syllabus - Module - II
Thanks

You might also like