DBMS M2 - Ktunotes - in
DBMS M2 - Ktunotes - in
DBMS M2 - Ktunotes - in
PREPARED BY
SHARIKA T R, SNGCE
SYLLABUS
• 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.
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Attributes
Relation name
EMPLOYEE
EMP_NO Name Address Mobile number Age Salar
y
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Domain
• It has three parts
▫ Name
▫ Data type
▫ Format
• A Domain is a set of atomic values.
• Atomic means each value in the domain is
indivisible to the relational model.
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
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
• Each value is derived from an appropriate
domain.
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Columns
• Columns in a table are also called attributes or
fields of the relation.
• A single cell in a table called field value, attribute
value or data element.
• For example, for the entity person, attributes
could include eye colour and height.
PREPARED BY
SHARIKA T R, SNGCE
Key of a Relation
• Each row has a value of a data item (or set of
items) that uniquely identifies that row in the
table
Called the key
• Sometimes row-ids or sequential numbers are
assigned as keys to identify the rows in a table
Called artificial key or surrogate key
PREPARED BY
SHARIKA T R, SNGCE
Relations (Tables)
• A table of values
• A relation may be thought of as a set of rows.
• A relation may alternately be thought of as a set
of columns.
• 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
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Schema of a Relation
• It is basically an outline of how data is organized
• It is denoted by R (A1, A2, .....An)
▫ Here R is relation name and
▫ it has some attributes A1 to An
• Each attribute have some domain and it is
represented by dom(Ai)
• Relation schema is used to describe a relation
and R is name of the relation
• Each attribute has a domain or a set of valid values.
▫ For example, the domain of Cust-id is 6 digit numbers.
PREPARED BY
SHARIKA T R, SNGCE
Degree of a relation
• Degree of a relation is number of attributes in a
relation
• Eg
• STUDENT(Id, Name, Age, Departmentno)
▫ Has degree 4
• Using datatype of each the definition can be
written as
• STUDENT(Id:Integer, Name:String,Age:integer,
Departmentno:integer)
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
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
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Alternative Definition of a
Relation
• ordering of values in a tuple unnecessary
• a relation schema R = {A1, A2, ..., An} is a set of
attributes, and
• a relation state r(R) is a finite set of mappings r =
{t1, t2, ..., tm}, where each tuple ti is a mapping
from R to D, and
• 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
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Definition Summary
Informal Terms Formal Terms
Table Relation
Column Header Attribute
All possible Column Domain
Values
Row Tuple
PREPARED BY
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
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
• Values in a tuple:
▫ All values are considered atomic (indivisible).
▫ Each value in a tuple must be from the domain of
the attribute for that column
If tuple t = <v1, v2, …, vn> is a tuple (row) in the
relation state r of R(A1, A2, …, An)
Then each vi must be a value from dom(Ai)
PREPARED BY
SHARIKA T R, SNGCE
• Notation:
▫ We refer to component values of a tuple t by:
t[Ai] or t.Ai
This is the value vi of attribute Ai for tuple t
▫ Similarly, t[Au, Av, ..., Aw] refers to the subtuple
of t containing the values of attributes Au, Av, ...,
Aw, respectively in t
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Key Constraints
• Superkey of R:
▫ Is a set of attributes SK of R with the following
condition:
No two tuples in any valid relation state r(R)
will have the same value for SK
That is, for any distinct tuples t1 and t2 in
r(R), t1[SK] t2[SK]
This condition must hold in any valid state
r(R)
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
• Key of R:
▫ A "minimal" superkey
▫ That is, a key is a superkey K such that
removal of any attribute from K results in a set
of attributes that is not a superkey (does not
possess the superkey uniqueness property)
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
• In general:
▫ Any key is a superkey (but not vice versa)
▫ Any set of attributes that includes a key is a
superkey
▫ A minimal superkey is also a key
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
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
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Referential Integrity
• A constraint involving two relations
▫ The previous constraints involve a single relation.
• Used to specify a relationship among tuples in
two relations:
▫ The referencing relation and the referenced
relation.
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
homework
• Consider the following relations for a database that keeps
track of student enrollment in courses and the books adopted
for each course:
• STUDENT(SSN, Name, Major, Bdate)
• COURSE(Course#, Cname, Dept)
• ENROLL(SSN, Course#, Quarter, Grade)
• BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
• TEXT(Book_ISBN, Book_Title, Publisher, Author)
• Draw a relational schema diagram specifying the foreign
keys for this schema.
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Relational algebra
• Relational algebra is a procedural query
language, which takes instances of relations as
input and yields instances of relations as output.
• It uses operators to perform queries. An
operator can be either unary or binary.
• They accept relations as their input and yield
relations as their output.
• Relational algebra is performed recursively on a
relation and intermediate results are also
considered relations.
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Relational Calculus
• Relational calculus is a non-procedural query language,
and instead of algebra, it uses mathematical predicate
calculus.
• When applied to databases, it is found in two forms.
▫ Tuple relational calculus which was originally proposed by
Codd in the year 1972 and
▫ Domain relational calculus which was proposed by Lacroix
and Pirotte in the year 1977
• In first-order logic or predicate calculus, a predicate is a
truth-valued function with arguments. When we replace
with values for the arguments, the function yields an
expression, called a proposition, which will be either true
or false.
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PROJECT Operation
• 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
• vertical partition of the relation into two
relations
▫ one has the needed columns (attributes) and
contains the result of the operation, and
▫ the other contains the discarded columns.
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Example
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Rename
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
PREPARED BY
MINUS Operations
• 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.
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Example
• retrieve the Social Security numbers of all
employees who either work in department 5 or
directly supervise an employee who works in
department 5, we can use the UNION operation
as follows
PREPARED BY
SHARIKA T R, SNGCE
DEP5_EMPS
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Example
• We want to retrieve a list of names of each
female employee’s dependents
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Example
• Retrieve the name of the manager of each
department.
▫ To get the manager’s name, we need to combine
each department tuple with the employee tuple
whose Ssn value matches the 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
PREPARED BY
SHARIKA T R, SNGCE
RESULT
Dname Lname Fname
Research Wong Franklin
Administration Wallance Jennifer
Headquaters Borg James
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
THETA JOIN
• where each <condition> is of the form Ai θ Bj, Ai is
an attribute of R, Bj is an attribute of S, Ai and Bj
have the same domain, and
• θ (theta) is one of the comparison operators {=, <,
≤, >, ≥, ≠}.
• Tuples whose join attributes are NULL or for which
the join condition is FALSE do not appear in the
result
• 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
SHARIKA T R, SNGCE
EQUIJOIN
• a JOIN, where the only comparison operator
used is =, is called an EQUIJOIN
• in the result of an EQUIJOIN we always have
one or more pairs of attributes that have
identical values in every tuple
PREPARED BY
SHARIKA T R, SNGCE
NATURAL JOIN
• NATURAL JOIN requires that the two join
attributes (or each pair of join attributes) have
the same name in both relations.
• If this is not the case, a renaming operation is
applied first.
PREPARED BY
SHARIKA T R, SNGCE
Example
• combine each PROJECT tuple with the
DEPARTMENT tuple that controls the project
▫ first we 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
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
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
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
SYLLABUS
• 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.
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
SQL
• SQL provides
▫ A data definition language (DDL)
▫ A data manipulation language (DML)
▫ A data control language (DCL)
• In addition SQL
▫ Can be used from other languages
▫ Is often extended to provide common
programming constructs (such as if-then tests,
loops, variables, etc.)
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
Cont..
• SQL is a declarative (non-procedural) language
▫ Procedural - say exactly what the computer has
to do
▫ Non-procedural – describe the required result
(not the way to compute it)
• SQL is based on the relational model
▫ 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
SHARIKA T R, SNGCE
Cont..
• E/R designs can be implemented in SQL
▫ Entities, attributes, and relationships can all
be expressed in terms of SQL
▫ Many-to-many relationships are a problem, so
should be removed
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
SQL environment
• SQL environment is basically an installation of
an SQL-compliant RDBMS on a computer
system
• SQL uses the concept of a catalog a named
collection of schemas in an SQL environment.
• A catalog always contains a special schema
called INFORMATION_SCHEMA,
▫ which provides information on all the schemas in
the catalog and all the element descriptors in these
schemas
PREPARED BY
SHARIKA T R, SNGCE
Cont..
• 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.
PREPARED BY
SHARIKA T R, SNGCE
DATABASE LANGUAGES
1. 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 are
a.CREATE TABLE:
ALTER TABLE
DROP TABLE
TRUNCATE
COMMENT
RENAME
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
CREATE TABLE
• Specifies a new base relation by giving it a name, and
specifying each of its attributes and their data types
(INTEGER, FLOAT, DECIMAL(i,j), CHAR(n),
VARCHAR(n))
• A constraint NOT NULL may be specified on an
attribute
CREATE TABLE DEPARTMENT (
DNAME VARCHAR(10) NOT
NULL,
DNUMBER INTEGER NOT
NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );
Downloaded from Ktunotes.in
PREPARED BY SHARIKA T R SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
• UNIQUE
▫ specifies alternate (secondary) keys
▫ can also be specified directly for a secondary key if
the secondary key is a single attribute
PREPARED BY
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
DROP TABLE
• The SQL DROP TABLE statement is used to
remove a table definition and all the data,
indexes, triggers, constraints and permission
specifications for that table.
• You should be very careful while using this
command because once a table is deleted then
all the information available in that table will
also be lost forever.
PREPARED BY
SHARIKA T R, SNGCE
ALTER TABLE
• The SQL ALTER TABLE command is used to add, delete
or modify columns in an existing table.
• You should also use the ALTER TABLE command to add
and drop various constraints on an existing table.
• The basic syntax of an ALTER TABLE command to add a
New Column in an existing table is as follows.
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
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
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
PREPARED BY
SHARIKA T R, SNGCE
MODULE 2 ENDS