dbms 2
dbms 2
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Attributes
Relation name
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Tuples (rows)
• A tuple is an ordered set of values
• Tuple is a portion of a table containing data that
described only entity, relationship, or object
• Also known as record
• <Kumar, Singh, 52/57 store, 223001,
• Each value is derived from an appropriate 9889898989> is a tuple belonging to the
domain. CUSTOMER relation.
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Relations (Tables)
• A table of values • Sometimes row-ids or sequential numbers are
• A relation may be thought of as a set of rows. assigned to identify the rows in the table.
• A relation may alternately be thought of as a set • Each column typically is called by its column
of columns. name or column header or attribute name.
• That is a table is perceived as a two-dimensional
structure composed of rows and columns.
• Each row represents a fact that corresponds to a
real-world entity or relationship.
• Each row has a value of an item or set of items
that uniquely identifies that row in the table
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Relation State
• The relation state is a subset of the Cartesian
product of the domains of its attributes
▫ each domain contains the set of all possible values
the attribute can take.
• Example: attribute Cust-name is defined over
the domain of character strings of maximum
length 25
▫ dom(Cust-name) is varchar(25)
• The role these strings play in the CUSTOMER
relation is that of the name of a customer.
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
• A relation state r(R) is a mathematical relation • Cardinality in domain D by |D| then the total
of degree n on the domains dom(A1), number of tuples in cartesiean product is
dom(A2)…, dom(An) which is a subset of
Cartesian product(X) of domains that define R
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Alternative Definition of a
Current relation state
Relation
• Reflects only the valid tuples that represent a • ordering of values in a tuple unnecessary
• a relation schema R = {A1, A2, ..., An} is a set of
particular state of real world attributes, and
• As the state of real world changes, so does the • a relation state r(R) is a finite set of mappings r =
relation state, by being transformed into another {t1, t2, ..., tm}, where each tuple ti is a mapping
from R to D, and
relation state • D is the union (denoted by ∪) of the attribute
domains; that is, D = dom(A1) ∪ dom(A2) ∪ ... ∪
dom(An).
• Here t[Ai] must be in dom(Ai) for 1 ≤ i ≤ n for each
mapping t in r.
• Each mapping ti is called a tuple.
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Definition Summary
Formal Definitions - Example Informal Terms Formal Terms
• Let R(A1, A2) be a relation schema: Table Relation
▫ Let dom(A1) = {0,1}
▫ Let dom(A2) = {a,b,c} Column Header Attribute
• Then: dom(A1) X dom(A2) is all possible combinations: All possible Column Domain
{<0,a> , <0,b> , <0,c>, <1,a>, <1,b>, <1,c> } Values
Row Tuple
• The relation state r(R) dom(A1) X dom(A2)
• For example: r(R) could be {<0,a> , <0,b> , <1,c> }
▫ this is one possible state (or “population” or “extension”) r Table Definition Schema of a Relation
of the relation R, defined over A1 and A2.
▫ It has three 2-tuples: <0,a> , <0,b> , <1,c> Populated Table State of the Relation
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Characteristics of Relation
• Ordering of tuples in a relation r(R):
▫ The tuples are not considered to be ordered,
even though they appear to be in the tabular
form.
• Ordering of attributes in a relation schema R
(and of values within each tuple):
▫ We will consider the attributes in R(A1, A2, ...,
An) and the values in t=<v1, v2, ..., vn> to be
ordered .
(However, a more general alternative definition of
relation does not require this ordering).
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Entity Integrity
• Entity Integrity:
▫ The primary key attributes PK of each relation
schema R in S cannot have null values in any tuple of
r(R).
This is because primary key values are used to
identify the individual tuples.
t[PK] null for any tuple t in r(R)
If PK has several attributes, null is not allowed in
any of these attributes
▫ Note: Other attributes of R may be constrained to
disallow null values, even though they are not
members of the primary key.
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Relational Calculus
• The basic set of operations for the relational model is the • Relational calculus is a non-procedural query language,
relational algebra. and instead of algebra, it uses mathematical predicate
• These operations enable a user to specify basic retrieval calculus.
requests as relational algebra expressions. • When applied to databases, it is found in two forms.
▫ Tuple relational calculus which was originally proposed by
• The result of a retrieval is a new relation, which may Codd in the year 1972 and
have been formed from one or more relations. ▫ Domain relational calculus which was proposed by Lacroix
• The algebra operations thus produce new relations, and Pirotte in the year 1977
which can be further manipulated using operations of • In first-order logic or predicate calculus, a predicate is a
the same algebra. truth-valued function with arguments. When we replace
with values for the arguments, the function yields an
• A sequence of relational algebra operations forms a expression, called a proposition, which will be either true
relational algebra expression, whose result will also be a or false.
relation that represents the result of a database query
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
• Select the EMPLOYEE tuples whose department • In general, the SELECT operation is denoted by
is 4, or those whose salary is greater than
$30,000
• R is generally a relational algebra expression
whose result is a relation—the simplest such
expression is just the name of a database
relation.
• Therelation resulting from the SELECT
operation has the same attributes as R.
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Example
• 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.
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Rename
Example- Intermediate Relation
• To rename the attributes in a relation, we simply
list the new attribute names in parentheses,
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
Example
SHARIKA T R, SNGCE
MINUS Operations
• UNION: • retrieve the Social Security numbers of all
▫ The result of this operation, denoted by R ∪ S, is a employees who either work in department 5 or
relation that includes all tuples that are either in R or directly supervise an employee who works in
in S or in both R and S.
department 5, we can use the UNION operation
▫ Duplicate tuples are eliminated.
• INTERSECTION:
as follows
▫ 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 • UNION operation produces the tuples that are in
relation that includes all tuples that are in R but not either RESULT1 or RESULT2 or both, while
in S. eliminating any duplicates.
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
DEP5_EMPS
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
THETA JOIN
EQUIJOIN
• where each <condition> is of the form Ai θ Bj, Ai is
an attribute of R, Bj is an attribute of S, Ai and Bj • a JOIN, where the only comparison operator
have the same domain, and
• θ (theta) is one of the comparison operators {=, <, used is =, is called an EQUIJOIN
≤, >, ≥, ≠}. • in the result of an EQUIJOIN we always have
• Tuples whose join attributes are NULL or for which one or more pairs of attributes that have
the join condition is FALSE do not appear in the
result identical values in every tuple
• the JOIN operation does not necessarily preserve
all of the information in the participating relations,
▫ because tuples that do not get combined with
matching ones in the other relation do not appear in
the result.
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Example
• combine each PROJECT tuple with the
NATURAL JOIN DEPARTMENT tuple that controls the project
• NATURAL JOIN requires that the two join ▫ first we rename the Dnumber attribute of
attributes (or each pair of join attributes) have DEPARTMENT to Dnum
the same name in both relations. ▫ so that it has the same name as the Dnum attribute
in PROJECT—and then we apply NATURAL JOIN
• If this is not the case, a renaming operation is
applied first.
• The same query can be done in two steps by
creating an intermediate table DEPT
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
DIVISION Operation
• Retrieve the names of employees who work on all the projects that
‘John Smith’ works on.
▫ query using the DIVISION operation, proceed as follows.
▫ First, retrieve the list of project numbers that ‘John Smith’ works on in
the intermediate relation SMITH_PNOS:
▫ Next, create a relation that includes a tuple <Pno, Essn> whenever the
employee whose Ssn is Essn works on the project whose number is Pno
in the intermediate relation SSN_PNOS:
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
• Finally, apply the DIVISION operation to the • Query 1. Retrieve the name and address of all
employees who work for the ‘Research’
two relations, which gives the desired department.
employees’ Social Security numbers:
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
SYLLABUS SQL
• Structure of Relational Databases - Integrity • SQL provides
Constraints, Synthesizing ER diagram to ▫ A data definition language (DDL)
relational schema
▫ A data manipulation language (DML)
• Introduction to Relational Algebra - select,
project, cartesian product operations, join - ▫ A data control language (DCL)
Equi-join, natural join. query examples, • In addition SQL
• Introduction to Structured Query Language ▫ Can be used from other languages
(SQL), Data Definition Language (DDL), Table ▫ Is often extended to provide common
definitions and operations – CREATE, DROP, programming constructs (such as if-then tests,
ALTER, INSERT, DELETE, UPDATE. loops, variables, etc.)
Downloaded from Ktunotes.in Downloaded from Ktunotes.in
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Cont.. Cont..
• SQL is a declarative (non-procedural) language • E/R designs can be implemented in SQL
▫ Procedural - say exactly what the computer has ▫ Entities, attributes, and relationships can all
to do
▫ Non-procedural – describe the required result be expressed in terms of SQL
(not the way to compute it) ▫ Many-to-many relationships are a problem, so
• SQL is based on the relational model should be removed
▫ It has many of the same ideas
▫ Databases that support SQL are often
described as relational databases
▫ It is not always true to the model
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
Specifying Constraints on Tuples Using • The following SQL creates a CHECK constraint
CHECK on the "Age" column when the "Persons" table is
• CHECK clauses is specified at the end of a created. The CHECK constraint ensures that the
CREATE TABLE statement age of a person must be 18, or older:
• These can be called tuple-based constraints
because they apply to each tuple individually
and are checked whenever a tuple is inserted or
modified
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
• The basic syntax of an ALTER TABLE command • The basic syntax of an ALTER TABLE command
to DROP COLUMN in an existing table is as to add a NOT NULL constraint to a column in a
follows. table is as follows.
• The basic syntax of an ALTER TABLE command • The basic syntax of ALTER TABLE to ADD
to change the DATA TYPE of a column in a table UNIQUE CONSTRAINT to a table is as follows
is as follows.
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
INSERT Command
• INSERT is used to add a single tuple to a
relation.
• We must specify the relation name and a list of
values for the tuple.
• The values should be listed in the same order in
which the corresponding attributes were
specified in the CREATE TABLE command.
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
MODULE 2 ENDS