0% found this document useful (0 votes)
3 views

dbms 2

The document provides an overview of the relational data model, covering the structure of relational databases, relational algebra, and SQL operations. It explains key concepts such as relations (tables), tuples (rows), attributes (columns), domains, and integrity constraints. Additionally, it discusses the formal definitions and characteristics of relations, including the importance of keys and constraints in maintaining data integrity.

Uploaded by

adithya223002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

dbms 2

The document provides an overview of the relational data model, covering the structure of relational databases, relational algebra, and SQL operations. It explains key concepts such as relations (tables), tuples (rows), attributes (columns), domains, and integrity constraints. Additionally, it discusses the formal definitions and characteristics of relations, including the importance of keys and constraints in maintaining data integrity.

Uploaded by

adithya223002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

PREPARED BY SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE

SYLLABUS Relational data model


• Structure of Relational Databases - Integrity • Represent database as a collection of relations
Constraints, Synthesizing ER diagram to • Relation is a table which has values and rows in
relational schema table is a collection of related data values
• Introduction to Relational Algebra - select,
project, cartesian product operations, join - • Each row in table is a fact
Equi-join, natural join. query examples, • Row in relational table is called a tuple, column
• Introduction to Structured Query Language header is attribute and table is a relation
(SQL), Data Definition Language (DDL), Table
definitions and operations – CREATE, DROP,
ALTER, INSERT, DELETE, UPDATE.
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

Attributes

Relation name

• In the relational model, all data is logically


EMPLOYEE structured within relations (also called table)
EMP_NO Name Address Mobile number Age Salar
y • Informally a relation may be viewed as a named
two-dimensional table representing an entity
101 RAM XYZ 9898989898 20 10000
set.
Tuples 102 SAM CVF 9999999999 21 20000
• A relation has a fixed number of named columns
103 SITA FDFD 888888888 22 30000 and variable number of rows.

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

Components of relational database Domain


• The main components of relational database • It has three parts
structure are as follows: ▫ Name
1. Domains ▫ Data type
2. Tuples (rows) ▫ Format
3. Columns • A Domain is a set of atomic values.
4. Keys • Atomic means each value in the domain is
5. Relations (Tables) indivisible to the relational model.

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 domain has a logical definition:


e.g.“USA_phone_numbers” are the set of 10
digit phone numbers valid in the U.S.
• A domain may have a data-type or a format
defined for it. The USA_phone_numbers may
have a format: (ddd)-ddd-dddd where each d is a
decimal digit. E.g., Dates have various formats
such as month name, date, year or yyyy-mm-dd,
or dd mm,yyyy etc
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

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.

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

Columns Key of a Relation


• Columns in a table are also called attributes or • Each row has a value of a data item (or set of
fields of the relation. items) that uniquely identifies that row in the
• A single cell in a table called field value, attribute table
value or data element.  Called the key
• For example, for the entity person, attributes • Sometimes row-ids or sequential numbers are
could include eye colour and height. assigned as keys to identify the rows in a table
 Called artificial key or surrogate key

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

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 SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE

Schema of a Relation Degree of a relation


• It is basically an outline of how data is organized • Degree of a relation is number of attributes in a
• It is denoted by R (A1, A2, .....An) relation
▫ Here R is relation name and • Eg
▫ it has some attributes A1 to An
• STUDENT(Id, Name, Age, Departmentno)
• Each attribute have some domain and it is
▫ Has degree 4
represented by dom(Ai)
• Relation schema is used to describe a relation • Using datatype of each the definition can be
and R is name of the relation written as
• Each attribute has a domain or a set of valid values. • STUDENT(Id:Integer, Name:String,Age:integer,
▫ For example, the domain of Cust-id is 6 digit numbers. Departmentno:integer)
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

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

• Cartesian product specifies all possible


combination of values from underlying domains

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

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 SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE

Formal Definitions - Summary


• a tuple can be considered as a set of • Formally,
(<attribute>, <value>) pairs, where each pair ▫ Given R(A1, A2, .........., An)
▫ r(R)  dom (A1) X dom (A2) X ....X dom(An)
gives the value of the mapping from an attribute
• R(A1, A2, …, An) is the schema of the relation
Ai to a value vi from dom(Ai).
• R is the name of the relation
• The ordering of attributes is not important, • A1, A2, …, An are the attributes of the relation
because the attribute name appears with its • r(R): a specific state (or "value" or “population”) of
value. relation R – this is a set of tuples (rows)
▫ r(R) = {t1, t2, …, tn} where each ti is an n-tuple
▫ ti = <v1, v2, …, vn> where each vj element-of dom(Aj)

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

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 SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

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

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

• Values in a tuple: • Notation:


▫ All values are considered atomic (indivisible). ▫ We refer to component values of a tuple t by:
▫ Each value in a tuple must be from the domain of  t[Ai] or t.Ai
the attribute for that column  This is the value vi of attribute Ai for tuple t
 If tuple t = <v1, v2, …, vn> is a tuple (row) in the ▫ Similarly, t[Au, Av, ..., Aw] refers to the subtuple
relation state r of R(A1, A2, …, An) of t containing the values of attributes Au, Av, ...,
 Then each vi must be a value from dom(Ai) Aw, respectively in t

▫ A special null value is used to represent values


that are unknown or inapplicable to certain tuples.
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

Relational Integrity Constraints Key Constraints


• Constraints are conditions that must hold on all valid • Superkey of R:
relation states.
▫ Is a set of attributes SK of R with the following
• There are three main types of constraints in the
relational model: condition:
▫ Key constraints  No two tuples in any valid relation state r(R)
▫ Entity integrity constraints will have the same value for SK
▫ Referential integrity constraints  That is, for any distinct tuples t1 and t2 in
• Another implicit constraint is the domain constraint r(R), t1[SK]  t2[SK]
▫ Every value in a tuple must be from the domain of its
attribute (or it could be null, if allowed for that attribute)  This condition must hold in any valid state
r(R)
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

• Key of R: • Example: Consider the CAR relation


▫ A "minimal" superkey schema:
▫ That is, a key is a superkey K such that ▫ CAR(State, Reg#, SerialNo, Make, Model,
removal of any attribute from K results in a set Year)
of attributes that is not a superkey (does not ▫ CAR has two keys:
possess the superkey uniqueness property)  Key1 = {State, Reg#}
 Key2 = {SerialNo}
▫ Both are also superkeys of CAR
▫ {SerialNo, Make} is a superkey but not a key.
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

• If a relation has several candidate keys, one is


chosen arbitrarily to be the primary key.
▫ The primary key attributes are underlined.
• In general: • Example: Consider the CAR relation schema:
▫ Any key is a superkey (but not vice versa) ▫ CAR(State, Reg#, SerialNo, Make, Model, Year)
▫ We chose SerialNo as the primary key
▫ Any set of attributes that includes a key is a
• The primary key value is used to uniquely
superkey identify each tuple in a relation
▫ A minimal superkey is also a key ▫ Provides the tuple identity
• Also used to reference the tuple from another
tuple
▫ General rule: Choose as primary key the smallest of
the candidate keys (in terms of size)
▫ Not always applicable – choice is sometimes subjective
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

CAR table with two candidate keys –


Relational Database Schema
LicenseNumber chosen as Primary Key
• Relational Database Schema:
▫ A set S of relation schemas that belong to the same
database.
▫ S is the name of the whole database schema
▫ S = {R1, R2, ..., Rn}
▫ R1, R2, …, Rn are the names of the individual
relation schemas within the database S
• Following slide shows a COMPANY database
schema with 6 relation schemas

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

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 SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE

Referential Integrity • Tuples in the referencing relation R1 have


attributes FK (called foreign key attributes)
• A constraint involving two relations that reference the primary key attributes PK of
▫ The previous constraints involve a single relation. the referenced relation R2.
• Used to specify a relationship among tuples in ▫ A tuple t1 in R1 is said to reference a tuple t2 in
two relations: R2 if t1[FK] = t2[PK].
▫ The referencing relation and the referenced • A referential integrity constraint can be
relation. displayed in a relational database schema as a
directed arc from R1.FK to R2.

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

Displaying a relational database


schema and its constraints
• Statement of the constraint • Each relation schema can be displayed as a row of
attribute names
▫ The value in the foreign key column (or columns) • The name of the relation is written above the attribute
FK of the the referencing relation R1 can be names
either: • The primary key attribute (or attributes) will be
underlined
 (1) a value of an existing primary key value of a • A foreign key (referential integrity) constraints is
corresponding primary key PK in the referenced displayed as a directed arc (arrow) from the foreign key
relation R2, or attributes to the referenced table
 (2) a null. ▫ Can also point the the primary key of the referenced
relation for clarity
• In case (2), the FK in R1 should not be a part of • Next slide shows the COMPANY relational schema
its own primary key. diagram

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

Other Types of Constraints


• Semantic Integrity Constraints:
▫ based on application semantics and cannot be
expressed by the model per se
▫ Example: “the max. no. of hours per employee for
all projects he or she works on is 56 hrs per week”
• A constraint specification language may
have to be used to express these
• SQL-99 allows triggers and ASSERTIONS to
express for some of these

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

Populated database state


• Each relation will have many tuples in its current
relation state
• The relational database state is a union of all the
individual relation states
• Whenever the database is changed, a new state arises
• Basic operations for changing the database:
▫ INSERT a new tuple in a relation
▫ DELETE an existing tuple from a relation
▫ MODIFY an attribute of an existing tuple
• Next slide shows an example state for the COMPANY
database
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

Update Operations on Relations Update Operations on Relations


• INSERT a tuple. • In case of integrity violation, several actions can
• DELETE a tuple. be taken:
• MODIFY a tuple. ▫ Cancel the operation that causes the violation
• Integrity constraints should not be violated by (RESTRICT or REJECT option)
the update operations. ▫ Perform the operation but inform the user of the
• Several update operations may have to be violation
grouped together. ▫ Trigger additional updates so the violation is
• Updates may propagate to cause other corrected (CASCADE option, SET NULL option)
updates automatically. This may be necessary to ▫ Execute a user-specified error-correction routine
maintain integrity constraints.
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

• DELETE may violate only referential integrity:


Possible violations for each operation ▫ If the primary key value of the tuple being deleted
is referenced from other tuples in the database
• INSERT may violate any of the constraints:  Can be remedied by several actions: RESTRICT,
▫ Domain constraint: CASCADE, SET NULL (see Chapter 8 for more
 if one of the attribute values provided for the new
tuple is not of the specified attribute domain details)
▫ Key constraint:  RESTRICT option: reject the deletion
 if the value of a key attribute in the new tuple  CASCADE option: propagate the new primary key value
already exists in another tuple in the relation into the foreign keys of the referencing tuples
▫ Referential integrity:  SET NULL option: set the foreign keys of the
 if a foreign key value in the new tuple references a referencing tuples to NULL
primary key value that does not exist in the
referenced relation ▫ One of the above options must be specified during
▫ Entity integrity: database design for each foreign key constraint
 if the primary key value is null in the new tuple
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

• UPDATE may violate domain constraint and


NOT NULL constraint on an attribute being homework
modified • Consider the following relations for a database that keeps
• Any of the other constraints may also be track of student enrollment in courses and the books adopted
for each course:
violated, depending on the attribute being
updated: • STUDENT(SSN, Name, Major, Bdate)
▫ Updating the primary key (PK): • COURSE(Course#, Cname, Dept)
 Similar to a DELETE followed by an INSERT • ENROLL(SSN, Course#, Quarter, Grade)
 Need to specify similar options to DELETE • BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
▫ Updating a foreign key (FK): • TEXT(Book_ISBN, Book_Title, Publisher, Author)
 May violate referential integrity
• Draw a relational schema diagram specifying the foreign
▫ Updating an ordinary attribute (neither PK nor FK): keys for this schema.
 Can only violate domain constraints
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

Introduction to Relational Algebra Relational algebra


• select, • Relational algebra is a procedural query
• project, language, which takes instances of relations as
input and yields instances of relations as output.
• cartesian product operations,
• It uses operators to perform queries. An
• join - Equi-join, operator can be either unary or binary.
• natural join • They accept relations as their input and yield
• query examples relations as their output.
• Relational algebra is performed recursively on a
relation and intermediate results are also
considered relations.
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

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 SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE

Unary Relational Operations: The SELECT Operation


1. SELECT and • The SELECT operation is used to choose a subset of
2. PROJECT the tuples from a relation that satisfies a selection
condition
• SELECT operation restricts the tuples in a relation
to only those tuples that satisfy the condition
• horizontal partition of the relation into two sets of
tuples
• those tuples that satisfy the condition and are
selected, and those tuples that do not satisfy the
conditionand are discarded.
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

• 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 SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

PREPARED BY PREPARED BY
SHARIKA T R, SNGCE SHARIKA T R, SNGCE

• Select the tuples for all employees who either


work in department 4 and make over $25,000
per year, or work in department 5 and make over
• The Boolean expression specified in <selection $30,000,
condition> is made up of a number of clauses
of the form

• Clauses can be connected by the standard


Boolean operators and, or, and not to form a
general selection condition
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

• the result of a SELECT operation can be


Interpretation of Boolean conditions
determined as follows:
• The <selection condition> is applied AND, OR, and NOT
independently to each individual tuple t in R. • (cond1 AND cond2) is TRUE
• This is done by substituting each occurrence of ▫ if both (cond1) and (cond2) are TRUE;
an attribute Ai in the selection condition with its ▫ otherwise, it is FALSE.
value in the tuple t[Ai]. • (cond1 OR cond2) is TRUE
• If the condition evaluates to TRUE, then tuple t ▫ if either (cond1) or (cond2) or both are TRUE;
is selected. ▫ otherwise, it is FALSE.
• All the selected tuples appear in the result of the • (NOT cond) is TRUE
SELECT operation. ▫ if cond is FALSE;
▫ otherwise, it is FALSE.
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

Degree of the relation from SELECT


Commutative Property of SELECT
• SELECT operation is commutative
operation
• its number of attributes
• is the same as the degree of R.
• The number of tuples in the resulting relation is • a sequence of SELECTs can be applied in any
always less than or equal to the number of order.
tuples in R. • we can always combine a cascade (or
sequence) of SELECT operations into a
single SELECT operation with a conjunctive
• The fraction of tuples selected by a selection (AND) condition; that is,
condition is referred to as the selectivity of the
condition.
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

PROJECT Operation The general form of PROJECT


• PROJECT operation selects certain columns
from the table and discards the other columns.
• If we are interested in only certain attributes of a • Example: list each employee’s first and last
relation, we use the PROJECT operation to
project the relation over these attributes only name and salary
• 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.

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

Duplicate Elimination in PROJECT


• The result of the PROJECT operation has only • The PROJECT operation removes any duplicate
the attributes specified in <attribute list> in the tuples, so the result of the PROJECT operation is
same order as they appear in the list. a set of distinct tuples, and hence a valid
• Hence, its degree is equal to the number of relation.
attributes in <attribute list>. • This is known as duplicate elimination.

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

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.

• as long as <list2> contains the attributes in


<list1>;
• otherwise, the left-hand side is an incorrect
expression.
• commutativity does not hold on PROJECT.
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

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

Sequences of Operations and the Example- In-Line relational algebra


RENAME Operation expression
• for most queries, we need to apply several • retrieve the first name, last name, and salary of
relational algebra operations one after the other all employees who work in department number
▫ Either we can write the operations as a single 5, we must apply a SELECT and a PROJECT
relational algebra expression by nesting the operation
operations, or
▫ we can apply one operation at a time and create
intermediate result relations
 we must give names to the relations that hold the
intermediate results.

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

Rename
Example- Intermediate Relation
• To rename the attributes in a relation, we simply
list the new attribute names in parentheses,

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

RENAME Operation General form SHARIKA T R, SNGCE

• RENAME operation when applied to a relation R of


degree n is denoted by any of the following three
forms
• If no renaming is applied, the names of the
attributes in the resulting relation of a SELECT • symbol ρ (rho) is used to denote the RENAME
operation are the same as those in the original operator,
relation and in the same order. • S is the new relation name, and
• a PROJECT operation with no renaming, the • B1, B2, ..., Bn are the new attribute names.
▫ The first expression renames both the relation and its
resulting relation has the same attribute names attributes,
as those in the projection list and in the same ▫ the second renames the relation only, and
order in which they appear in the list. ▫ the third renames the attributes only.
• If the attributes of R are (A1, A2, ..., An) in that
order, then each Ai is renamed as Bi.
Downloaded from Ktunotes.in Downloaded from Ktunotes.in

PREPARED BY SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

PREPARED BY PREPARED BY

The UNION, INTERSECTION, and SHARIKA T R, SNGCE

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

• UNION, INTERSECTION, and SET


DIFFERENCE are binary operations;
▫ that is, each is applied to two sets

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) Two union-compatible relations.


Union compatibility or Type
compatibility
• Two relations R(A1, A2, ..., An) and S(B1, B2, ..., (b) STUDENT ∪ INSTRUCTOR
Bn) are said to be union compatible (or
• type compatible)
▫ if they have the same degree n and if dom(Ai) =
dom(Bi) for 1 <=i<=n.
▫ This means that the two relations have the same (c) STUDENT ∩ INSTRUCTOR
number of attributes and each corresponding pair
of attributes has the same domain.

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

The UNION, INTERSECTION, and


MINUS Properties
(d) STUDENT − INSTRUCTOR
• UNION and INTERSECTION are commutative
operations

(e) INSTRUCTOR − STUDENT


• Both UNION and INTERSECTION can be
treated as n-ary operations applicable to any
number of relations because both are also
associative operations

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

CARTESIAN PRODUCT (CROSS


PRODUCT) OR CROSS JOIN
• The MINUS operation is not commutative; that • denoted by ×
is, in general, • This is also a binary set operation, but the relations
on which it is applied do not have to be union
compatible
• the result of R(A1, A2, ..., An) × S(B1, B2, ..., Bm) is
• INTERSECTION can be expressed in terms of a relation Q with degree n + m attributes Q(A1, A2,
union and set difference as follows ..., An, B1, B2, ..., Bm), in that order.
• The resulting relation Q has one tuple for each
combination of tuples—one from R and one from S.
• Hence, if R has nR tuples (denoted as |R| = nR), and
S has nS tuples, then R × S will have nR * nS tuples.
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

n-ary CARTESIAN PRODUCT Example


• produces new tuples by concatenating all • We want to retrieve a list of names of each
possible combinations of tuples from n female employee’s dependents
underlying relations

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

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

• The CARTESIAN PRODUCT creates tuples with


the combined attributes of two relations.
• We can SELECT related tuples only from the
two relations by specifying an appropriate
selection condition after the Cartesian product,
as we did in the preceding example.
• Because this sequence of CARTESIAN
PRODUCT followed by SELECT is quite
commonly used to combine related tuples from
two relations, a special operation, called JOIN,
was created to specify this sequence as a single
operation
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

The JOIN Operation Example


• The JOIN operation, denoted by , is used to • Retrieve the name of the manager of each
combine related tuples from two relations into department.
single “longer” tuples. ▫ To get the manager’s name, we need to combine
• This operation is very important for any each department tuple with the employee tuple
relational database with more than a single whose Ssn value matches the Mgr_ssn value in the
relation because it allows us to process department tuple.
relationships among relations. ▫ We do this by using the JOIN operation and then
projecting the result over the necessary attributes,
as follows

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

• The JOIN operation can be specified as a


CARTESIAN PRODUCT operation followed by a
SELECT operation.

RESULT • These two operations can be replaced with a


Dname Lname Fname single JOIN operation as follows
Research Wong Franklin
Administration Wallance Jennifer
Headquaters Borg James
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

Difference between JOIN and


General form of a JOIN
CARTESIAN PRODUCT
• JOIN operation on two relations5 R(A1, A2, ..., • In JOIN, only combinations of tuples satisfying the
An) and S(B1, B2, ..., Bm) is join condition appear in the result,
• whereas in the CARTESIAN PRODUCT all
combinations of tuples are included in the result.
• The result of the JOIN is a relation Q with n + m • The join condition is specified on attributes from the
attributes Q(A1, A2, ..., An, B1, B2, ... , Bm) in two relations R and S and is evaluated for each
that order; combination of tuples.
• Q has one tuple for each combination of tuples— • Each tuple combination for which the join condition
one from R and one from S—whenever the evaluates to TRUE is included in the resulting
combination satisfies the join condition relation Q as a single combined tuple.
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

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.

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

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

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

• If the attributes on which the natural join is


specified already have the same names in both
relations, renaming is unnecessary
• to apply a natural join on the Dnumber
attributes of DEPARTMENT and
DEPT_LOCATIONS
• In the PROJ_DEPT relation, each tuple
combines a PROJECT tuple with the
DEPARTMENT tuple for the department that
controls the project, but only one join attribute
value is kept.

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

• more general, but nonstandard definition for


• the join condition for NATURAL JOIN is NATURAL JOIN is
constructed by equating each pair of join
attributes that have the same name in the two • <list1> specifies a list of i attributes from R, and
relations and combining these conditions with • <list2> specifies a list of i attributes from S.
AND. • The lists are used to form equality comparison
• There can be a list of join attributes from each conditions between pairs of corresponding
attributes, and
relation, and each corresponding pair must have
• the conditions are then ANDed together.
the same name.
• Only the list corresponding to attributes of the first
relation R—<list1>— is kept in the result Q.
• if no combination of tuples satisfies the join
condition, the result of a JOIN is an empty relation
with zero tuples.
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

Different Types of SQL JOINs


• INNER JOIN
▫ Returns records that have matching values in both tables
1. Theta join
2. EQUI join
3. Natural join
• OUTER JOIN
▫ In an outer join, along with tuples that satisfy the
matching criteria, we also include some or all tuples that
do not match the criteria.
1. Left Outer JOIN
2. Right Outer Join
3. Full Outer Join
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

Left Outer Join


• In the left outer join, operation allows keeping all tuple
in the left relation.
• if there is no matching tuple is found in right relation,
then the attributes of right relation in the join result are
filled with null values.

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

Right Outer Join


• In the right outer join, operation allows keeping all tuple
in the right relation.
• However, if there is no matching tuple is found in the left
relation, then the attributes of the left relation in the join
result are filled with null values.

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

Full Outer Join


• In a full outer join, all tuples from both relations are
included in the result, irrespective of the matching
condition.

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

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:

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

• 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:

• This query could be specified in other ways; for


example, the order of the JOIN and
• SELECT operations could be reversed, or the JOIN
could be replaced by a NATURAL JOIN after
renaming one of the join attributes to match the
other join attribute name.

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

• Query 2. For every project located in


‘Stafford’, list the project number, the • Query 3. Make a list of project numbers
controlling department number, and the for projects that involve an employee
department manager’s last name, address, and birth whose last name is ‘Smith’, either as a worker or
date. as a manager of the department that controls the
project.

• we first select the projects located in Stafford, then


join them with their controlling departments, and
then join the result with the department managers.
• Finally, we apply a project operation on the desired
attributes

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

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 SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

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

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

Relations, Entities, Tables Implementing E/R Designs


• Given an E/R design
▫ The entities become SQL tables
▫ Attributes of an entity become columns in the
corresponding table
▫ Relationships may be represented by foreign
keys

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

Schema and Catalog Concepts in SQL


• Each entity becomes a • An SQL schema is identified by a schema name, and
table in the database includes an authorization identifier to indicate the
user or account who owns the schema, as well as
▫ The name of the
descriptors for each element in the schema.
table is often the
• Schema elements include tables, constraints, views,
name of the entity domains, and other constructs that describe the
▫ The attributes schema
become columns of • A schema is created via the CREATE SCHEMA
the table with the statement, which can include all the schema elements
same name definitions.

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

• Creates a schema called COMPANY, owned by


the user with authorization identifier ‘Jsmith’. SQL environment
• SQL environment is basically an installation of
• not all users are authorized to create schemas an SQL-compliant RDBMS on a computer
and schema elements. system
• SQL uses the concept of a catalog a named
• The privilege to create schemas, tables, and collection of schemas in an SQL environment.
other constructs must be explicitly granted to • A catalog always contains a special schema
the relevant user accounts by the system called INFORMATION_SCHEMA,
administrator or DBA. ▫ which provides information on all the schemas in
the catalog and all the element descriptors in these
schemas

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

Cont.. DATABASE LANGUAGES


• Integrity constraints such as referential integrity 1. Data Definition Language (DDL):
can be defined between relations only if they ▫ It is used to specify a database conceptual schema using set of
definitions.
exist in schemas within the same catalog. ▫ It supports the definition or declaration of database objects.
• Schemas within the same catalog can also share ▫ The more common DDL commands are
certain elements, such as domain definitions.  a.CREATE TABLE:
 ALTER TABLE
 DROP TABLE
 TRUNCATE
 COMMENT
 RENAME

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

2. Data Manipulation Language (DML)


▫ It provides a set of operations to support the basic data
manipulation operations on the data held in the database.
SQL Data Definition
▫ It is used to query, update or retrieve data stored in a
database.
• The set of relations in a database are specified using a
▫ Some of the tasks that come under DML are
data-definition language (DDL)
 SELECT • The SQL DDL allows specification of not only a set of
 Used to query and display data from a database. relations, but also information about each relation,
 INSERT including:
 Adds new rows to a table. ▫ The schema for each relation.
 UPDATE ▫ The types of values associated with each attribute.
 Changes an existing value in a column or group of columns ▫ The integrity constraints.
in a table.
▫ The set of indices to be maintained for each relation.
 DELETE:
▫ The security and authorization information for each
 Removes a specified row or set of rows from a table.
relation.
 MERGE.
▫ The physical storage structure of each relation on disk.
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

Basic Schema Definition: CREATE


CREATE TABLE
TABLE Command in SQL
• CREATE TABLE command is used to specify a • Specifies a new base relation by giving it a name, and
new relation by giving it a name and specifying specifying each of its attributes and their data types
(INTEGER, FLOAT, DECIMAL(i,j), CHAR(n),
its attributes and initial constraints. VARCHAR(n))
• The attributes are specified first, and each • A constraint NOT NULL may be specified on an
attribute is given a name, a data type to specify attribute
its domain of values, and any attribute CREATE TABLE DEPARTMENT (
constraints, such as NOT NULL. DNAME VARCHAR(10) NOT
• The key, entity integrity, and referential integrity NULL,
constraints can be specified within the CREATE DNUMBER INTEGER NOT
NULL,
TABLE statement MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );
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

• The relations declared through CREATE TABLE


statements are called base tables
• this means that the relation and its tuples are
actually created and stored as a file by the DBMS

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

Attribute Data Types and Domains in


SQL
• In SQL2, can use the CREATE TABLE command for • The basic data types available for attributes
specifying the primary key attributes, secondary keys,
and referential integrity constraints (foreign keys). include
• Key attributes can be specified via the PRIMARY KEY ▫ numeric,
and UNIQUE phrases ▫ character string,
CREATE TABLE DEPT (
DNAME VARCHAR(10) NOT ▫ bit string,
NULL,
DNUMBER INTEGER NOT ▫ Boolean,
NULL,
MGRSSN CHAR(9), ▫ date, and time
MGRSTARTDATE CHAR(9), ▫ timestamp
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
);
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

Specifying Attribute Constraints and


Specifying Constraints in SQL
Attribute Defaults
• Specifying Attribute Constraints and Attribute • a constraint NOT NULL may be specified if
Defaults NULL is not permitted for a particular attribute.
• Specifying Key and Referential Integrity • This is always implicitly specified for the
Constraints attributes that are part of the primary key of
each relation,
• Giving Names to Constraints ▫ but it can be specified for any other attributes
• Specifying Constraints on Tuples Using CHECK whose values are required not to be NULL
• It is also possible to define a default value for an
attribute by appending the clause DEFAULT
<value> to an attribute definition.
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

• suppose that department numbers are restricted


to integer numbers between 1 and 20;
• then, we can change the attribute declaration of
• If no default clause is specified, the default
Dnumber in the DEPARTMENT table
default value is NULL for attributes that do not
have the NOT NULL constraint.
• Another type of constraint can restrict attribute
or domain values using the CHECK clause
following an attribute or domain definition.

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

Specifiying Key and Referiential


• UNIQUE
• PRIMARY KEY ▫ specifies alternate (secondary) keys
▫ specifies one or more attributes that make up the ▫ can also be specified directly for a secondary key if
primary key of a relation. the secondary key is a single attribute
▫ If a primary key has a single attribute, the clause
can follow the attribute directly

Downloaded from Ktunotes.in Downloaded from Ktunotes.in

PREPARED BY SHARIKA T R SNGCE PREPARED BY SHARIKA T R SNGCE

PREPARED BY PREPARED BY

• FOREIGN KEY SHARIKA T R, SNGCE SHARIKA T R, SNGCE

▫ 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 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: • ON DELETE SET NULL and ON UPDATE CASCADE for the
SET NULL, foreign key Super_ssn of EMPLOYEE means
CASCADE, and • if the tuple for a supervising employee is deleted,
SET DEFAULT. • the value of Super_ssn is automatically set to NULL for all
 An option must be qualified with either employee tuples that were referencing the deleted employee
ON DELETE or tuple
 ON UPDATE • if the Ssn value for a supervising employee is updated the new
value is cascaded to Super_ssn for all employee tuples
referencing the updated employee tuple

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

Giving Names to Constraints


• a constraint may be given a constraint name,
following the keyword CONSTRAINT
• The names of all constraints within a particular
schema must be unique.
• A constraint name is used to identify a particular
constraint
▫ in case the constraint must be dropped later and
replaced with another constraint

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

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

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

DROP TABLE ALTER TABLE


• The SQL DROP TABLE statement is used to • The SQL ALTER TABLE command is used to add, delete
remove a table definition and all the data, or modify columns in an existing table.
indexes, triggers, constraints and permission • You should also use the ALTER TABLE command to add
and drop various constraints on an existing table.
specifications for that table.
• The basic syntax of an ALTER TABLE command to add a
• You should be very careful while using this New Column in an existing table is as follows.
command because once a table is deleted then
all the information available in that table will
also be lost forever.

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

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

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

• The basic syntax of an ALTER TABLE command


• The basic syntax of an ALTER TABLE command
to ADD CHECK CONSTRAINT to a table is as
to DROP CONSTRAINT from a table is as
follows.
follows.

• The basic syntax of an ALTER TABLE command


to ADD PRIMARY KEY constraint to a table is as
follows.

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

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.

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 second form of the INSERT statement allows the user to


specify explicit attribute names that correspond to the values • Attributes not specified in U1A are set to their
provided in the INSERT command. DEFAULT or to NULL, and the values are listed
• This is useful if a relation has many attributes but only a few in the same order as the attributes are listed in
of those attributes are assigned values in the new tuple. the INSERT command itself.
• However, the values must include all attributes with NOT • It is also possible to insert into a relation
NULL specification and no default value. multiple tuples separated by commas in a single
• Attributes with NULL allowed or DEFAULT values are the
INSERT command.
ones that can be left out.
• For example, to enter a tuple for a new EMPLOYEE for whom • The attribute values forming each tuple are
we know only the Fname, Lname, Dno, and Ssn attributes enclosed in parentheses.

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 variation of the INSERT command inserts multiple


• A DBMS that fully implements SQL should tuples into a relation in conjunction with creating the
support and enforce all the integrity constraints relation and loading it with the result of a query.
that can be specified in the DDL • For example,
▫ to create a temporary table that has the employee last
no department
no 2 is available
name, project name, and hours per week for each
employee working on a project

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

The DELETE Command


• The DELETE command removes tuples from a
relation.
• It includes a WHERE clause, similar to that used
in an SQL query, to select the tuples to be
deleted.
• Tuples are explicitly deleted from only one table
at a time.
• However, the deletion may propagate to tuples
in other relations if referential triggered actions
are specified in the referential integrity
constraints of the DDL

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

• Depending on the number of tuples selected by


the condition in the WHERE clause, zero, one, TRUNCATE TABLE COMMAND
or sev_x0002_eral tuples can be deleted by a
single DELETE command. • The SQL TRUNCATE TABLE command is used
• A missing WHERE clause specifies that all to delete complete data from an existing table.
tuples in the relation are to be deleted; however, • You can also use DROP TABLE command to
the table remains in the database as an empty delete complete table but it would remove
table.
complete table structure form the database and
• We must use the DROP TABLE command to
remove the table definition you would need to re-create this table once again
if you wish you store some data.

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

The UPDATE Command


• The UPDATE command is used to modify
attribute values of one or more selected tuples.
• As in the DELETE command, a WHERE clause
in the UPDATE command selects the tuples to
be modified from a single relation

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

• updating a primary key value may propagate to


the foreign key values of tuples in other relations
if such a referential triggered action is specified
in the referential integrity constraints of the
DDL
• An additional SET clause in the UPDATE
command specifies the attributes to be modified
and their new values.
• It is also possible to specify NULL or DEFAULT
as the new attribute value
• each UPDATE command explicitly refers to a
single relation only.
• To modify multiple relations, we must issue
several UPDATE commands.

Downloaded from Ktunotes.in Downloaded from Ktunotes.in

PREPARED BY SHARIKA T R SNGCE

PREPARED BY
SHARIKA T R, SNGCE

MODULE 2 ENDS

Downloaded from Ktunotes.in

You might also like