RDBMS
RDBMS
Constraint: A constraint is a rule that restrict the values in a database. We will consider the
following constraints.
1) Domain Constraints: A domain is the set of values that may be assigned to an attribute.
A domain definition usually consists of the following components: domain name, meaning,
data type, size and allowable range. All of the values that appear in a column of a relation
must be taken from the same domain.
A domain constraint is a specification of the characteristics of the data values that can be
associated with one or more attributes. A domain constraint states the collection of values
from which each of values of one or more attributes are taken.
2) Entity Integrity: Any key attribute data values in a relation should be valid that is not
null. No key attribute of a row in a relation may be null. The entity integrity rule is designed
to assure that every relation has a primary key and primary key value should be not a null.
3) Referential Integrity: The value of a non- null foreign key must be an actual key value in
some relation.
(Or)
A referential integrity constraint is a rule that maintains consistency among the rows of two
relations. The rule states that if there is a foreign key in one relation, either each foreign key
value in other relation or else the foreign key value must be null.
Ex: SKILL-TYPE is used in the WORKER relation to tell us the principal skill of each
employee so that the value of SKILL- TYPE in any employee’s row corresponds to an actual
SKILL-TYPE value in the SKILL relation. Otherwise, the employee’s SKILL- TYPE would
point no where.
Relational Algebra and calculus
SP_SUBORD
10 Jones 27 Chicago 10
14 Masaji Mastu 44 Tokyo 11
23 Francois 35 Brussels 9
37 A 12 B.A 13
39 B 44 Tokyo 10
44 C 27 Tokyo 12
35 D 27 Brussels 11
12 E 27 B.A 10
SP_Mar
Sal Per Id Sal Per_Name Manager_Id Office Com %
27 Terry 27 Chicago 15
44 C 27 Tokyo 12
35 D 27 Brussels 11
12 E 27 B.A 10
Salesperson= SP_SUBORD U SP_SUBORD
The salesperson relation contains all salesperson details. In this relation
columns will be same.
Result of UNION operation is…..
SALESPERSON
4) PRODUCT
Its symbols is ‘*’. This operation creates the Cartesian product of two
relations
. Ex :
A
B
X Y V Z
10 22 33 54
11 25 37 98
42
100
C: =A * B
X V W Z
10 22 33 54
10 22 37 98
10 22 42 100
11 25 33 54
11 25 37 98
11 25 42 100
SP_TOKYO
6) PROJECT
This operation creates a relation by deleting unwanted columns from a
existing relation. Here SELECT operation required rows will be given,
PROJECT operation takes required columns will be given. (π) is the symbol used
for .
Ex: SP_Tokyo (SALPARS_NAME) Project
SALPERS_NAME
Masuj
Goro
Albert
7) JOIN
The operation joins together two tables on the basis of common values in a
column. The join operating is denoted by ><, is used to combine related tuples
from two relations in to single tuples. This operation allows to process
relationships among relations. Join connects relations on columns containing
comparable containing comparable information. There are 3 types of join.
i) Natur
al
Join
ii) Theta
Join
iii) Outer Join
8) DIVIDE
9) ASSIGNMENT
Assignment operation gives names to a relation.
Ex: A: =SELECT (SALESPERSON: OFFICE =
‘TOKYO) “:=” is a symbol for ASSIGNMENT.
UNIT- II
SQL
Data types: - SQL provides predefined data types and user defined data types in the
relational data types in the relational data base model. There are five data types.
1) CHAR (size): - this data type is used to store character strings values of fixed length.
The size in brackets determines the no., of characters the cell can hold. The maximum
number of characters in this data type can hold is 255 characters.
Oracle compares CHAR values using blank padded comparison, semantic i.e., if a value
that is inserted in a cell of CHAR data type is shorter then the size it is defined for then it
will be padded with spaces on the right until it reaches the size character in length.
2) VARCHAR (size):- this data type is used to store variable length alpha numeric data.
The maximum data type can hold 2000 characters. One difference between this data type and
the CHAR data type is oracle compares VARCHAR values using non-padded comparison
semantics i.e., the inserted values will not be padded with spaces.
3) NUMBER (p, s):- the number data type is used to store number (fixed, floating).
The numbers may be stored upto 38 digits of precision. The precision “P” determines the
maximum length of the data, where as the scale “S” determines the number of places t
the right of the decimal. If scale is omitted then the default is zero.
4) Date: - this data type is used to represent data and time. The standard format is
DD-MM-YY.
5) Long: - this data type is used to store variable length character string screen containing
upto 2GB. Long data can be used to store arrays f binary data in ASCII format.
Syntax: - create table <table name > (column name 1 data type (size), column name 2 data
type (size), columnname3 data type (size));
Ex:-employee (emp no, emp name, adder, salary, dept)
To create table first set data types for each field.
Column name Data type Size
Emp no., Varchar 6
Emp name Varchar 20
adder Varchar 30
Salary Number 10,2
dept Varchar 10
Creation of a Table: -
Sql>create table employee (emp no varchar (6), empname varchar (20), address
varchar(30), salary number(10, 2), dept varchar (10));
II. Load the values passed into all the columns specified
Syntax: - insert into <table name> (column1, column2...) Values (exp1, exp2...);
Ex:- sql>insert into employee (emp no, emp name, addr, salary, dept) values (102,
‘pavani’, ‘hyd’, 12000, ‘accounts’);
In insert command sentence the columns and values have a one to one relationship
i.e., the first value described into the first column, the second value describe into the
second column and so on.
Data Manipulation:-
Simple query:-
A query involving only one data base table.
Viewing the data in the tables:-
Once data as been inserted into a table. The next most logical operation would
be view what has been interred. The select command is used.
SYNTAX:-
SELECT * from <table name >;
SELECT * from <table name> where <condition>;
Select Clauses:-
Identifies the columns desired in query. If the desired result contains more than
one column, the columns are all listed in the SELECT clause all separated by commas.
When data from all rows & columns from the table are to be viewed, we are
(*) char risk.
From clause:-from clause lists the existing tables referenced by the query.
Where clause:-It gives the condition for selected rows from identified tables. Where
clause is applied to each row, one by one any row not satisfying the where clause is
eliminated from consideration. These rows that satisfy the where clause are then
processed by the select clause.
Select command examples;-
Selecting all rows from a table.
Sql>Select * from Employee;
Selecting columns from a table.
Sql> Select Empno, Empname from Employee;
Sql> Select * from employee where DEPT = “Accountants”
Comparison Operators: - =,(not equals), <,>,<=,>=.
There are 6 comparison operator may be used to compare columns
with other columns.
SELECTING ROWS FROM TABLE USING COMPARISION OPERATORS.
1) Display ROWS FROM A TABLE WHOSE SALARY IS MORE THAN 10000.
Sql>SELECT * FROM EMPLOYEE WHERE SAL > 10000;
2) Display rows from a table whose employees are in commerce dept or are in
computer department.
Sql> SELECT * from employee WHERE dept = ‘commerce’ or dept = ‘computer’;
UPDATE: - Operation that changes column values in rows. UPDATE Operations always
apply to all the rows that satisfy the WHERE clause in the UPDATE statement. TO
UUPDATE
*All the rows from a table
Or
*A select set of rows from a table.
UPDATING OF ALL ROWS:
Syntax: - update table name SET columnname1= Expression, columnname2=Expression2;
Ex:-Change Dept= sales where dept= sales1 of a employee table.
Sql>Update Employee SET DEPT= ‘sales’ where dept =’sales1’;
Multiple- Table Queries:-Some times data needed to solve the query from two tables.
This is accomplished through the relational calculus i.e., “Join”.
Ex:-1 Worker :-( worker ID, Name, Howdy-Rate, Skill type, SOPV-ID)
2 ASSIGNMENT (Worker ID, BLPG-ID, Start Data, NUM-Days)
After creating this gaintg table, the system applies the WHERE clause as before.
Each row of the relation created by the FROM clause is examined for conformity to the
WHERE clause. Those not satisfying the WHERE condition are eliminated from
consideration.
SUB QUERIES:
Sub query is a query with in a Query, can be placed with in the WHERE clause of a Query,
resulting in the expansion of the WHERE clauses capability.
Query:-What are the skill types of worker assigned to building 435?
Sql> SELECT SKILL TYPE FROM WORKER WHERE WORKER_ID IN
(Select worker ID FROM Assignment where BLDCT_ID=435);
2 Example:--
Retrieve all orders placed by a client named ‘Rahul Desai’ from the sales order table
Table Name: - Sales order :( Order no, client no, Order date)
Sales order:
ORDERNO CLIENT NO ORDER DATE
RESULT/OUTPUT:-
ORDER NO CLIENT NO ORDER DATE
019003 03-08-97
019006 12-01-97
C00007
C00007
1
UNIT- III
RELATIONAL DATA MODEL :( Transferring Conceptual Model into Relational Model)
A Relation data model is the data model representing data in the form of tables.
Relation is a two- dimensional table containing rows and columns of data.
The following conceptual data model contains three object sets WORKER, BUILDIN, and SKILL, and one
aggregate object set, ASSIGNMENT. Each and every Object is having their own attributes.
WORKER-ID
ADDRESS
HOURLY- RATE
BUILDING-ID
NAME
Worker Relation:
WORKER- ID NAME HOURLY- RATE SKILL-TYPE SUPV-ID
1235 M. Faraday 12.50 Electric 1311
1412 C. Nemo 13.75 Plumbing 1520
2920 R.Garret 10.00 Roofing rows or
3321 P. Mason 17.40 Framing Tuples
1520 H. Rickover 15.40 Framing
Columns
2
Keys:
Worker Relation:
WORKER- ID NAME HOURLY- RATE SKILL-TYPE SUPV-ID
ASSIGNMENT
WORKER- ID BLDG- ID START- DATE NUM- DAYS 1235
312 10/10 5
1412 312 10/01 10
2920 515 10/17 22
3321 460 12/08 18
1520 425 10/15 15
BUILDING
BLDG- ID BLDG- ADDRESS TYPE QLTY- LEVEL
SKILL
SKILL- TYPE BONUS- RATE HRS- PER- WEEK
Plumbing 3.00 35
Electric 3.50 37
Roofing 2.00 40
Framing 5.00 35
Super key: A set of attributes that uniquely identifies each row in a relation (table).
Ex: In the above example WORKER relation, the values of the attribute set {WORKER- ID, NAME}
uniquely identify any tuple in the relation. Therefore this set is a superkey for worker.
Key: A minimal set of attributes that uniquely identifies each row in a relation. (Or) A key functionally
determines each attribute value in tuple.
Ex: In the above example WORKER- ID by itself is a key, since any row in the relation is uniquely identified
by worker- id
3
Candidate key: Any set of attributes that could be chosen as a key of a relation. In any relation, there may be
more than one set of attributes that could be chosen as a key. These are called Candidate keys.
Ex: WORKER- ID & NAME are candidate keys in the WORKER relation. This would be so if we could
assume that NAME is a candidate in the WORKER relation if we could assume name will always be unique.
Primary key: The candidate key designated for principal use in uniquely identifying rows in a relation.
Generally a key in a relation is always a Primary key.
Ex: in the above example WORKER- ID is a primary key in WORKR relation.
Foreign key: A set of attributes in one relation that constitutes a key in some other relation.
Ex: SKILL- TYPE in the WORKER relation and BLDG- ID in the ASSIGNMENT relation are examples of
foreign keys, since SKILL- TYPE is the key of the SKILL relation, and BLDG- ID is the key of the BLDG
relation.
Foreign key attributes need not have the same names as the key attributes to which they correspond. For
example, WORKER- ID and SUPV- ID in the WORKER relation have different names but both take their
values from the domain of worker identification numbers.
Recursive Foreign key: A foreign key that references its own relation.
Ex: in the above example the supervisor of worker 1235 is worker 1311. Here SUPV- ID is an example of a
recursive foreign key.
Relational database schema: A listing showing relation names, attribute names, key attributes, and foreign
keys.
An entity- relationship model is a detailed, logical representation of the data for an organization or for a
business area.
The E-R model is expressed in terms of entities in the business environment, the relationships among
those entities, and the attributes of both entities and their relationships.
An E-R model is normally expressed as an E-R diagram.
Entities:
An entity is a person, place, object, event, or concept in the user environment about which the
organization wishes to maintain data. Some examples
Person: Employee, student, patient
Place: city, country
4
Entity STUDENT
A Weak entity type is an entity type whose existence depends on some other entity type. The entity type
on which the weak entity type depends is called as the Identifying Owner.
Dependent is the entity type that is depending on Employee type, so Dependent is the weak Entity type.
We will denote the weak entity using double rectangle box.
DEPENDENT
ATTRIBUTES:
Student_ID Student_Name
A Composite Attribute is an attribute that can be broken down into component parts. Ex: Address, address
can be divided into h_no, street_name, city, state
Address
Multivalued Attributes:
A Multivalued attribute is an attribute that may take more than one value for a given entity instance.
We indicate a multivalued attribute with an ellipse with double lines.
Ex: Skill for Employee Entity, because skill is having more than one value like C, C++, Java etc.
STUDENT Skill
A derived attribute is an attribute is an attribute whose values can be calculated from related attribute values.
It is denoted using dashed ellipse.
An Identifier is an attribute that uniquely identifies individual instances of entity type. Ex: Student_ID
It is denoted as underlined attribute
Student_ID Student_Name
STUDENT
A Composite Attribute is an Identifier that consists of a composite attribute. Ex Flight_ID
Flight_Number Date
er
Student_Name
Flight_ID
FLIGHT
RELATIONSHIPS
A relationship is an association among the instances of one or more entity types that is of interest of the
organization.
Relationship Type is a meaningful association between entity types. A relation type is denoted by a diamond
symbol containing the name of the relationship.
EMPLOYEE COURSE
Complete
s
6
Relationship Instance is an association between entity instances, where each relationship instance includes
exactly one entity from each participating entity type.
Venkat Java
Ramu C++
John UNIX
Associative Entity:
Associative entity is an entity type that associates the instances of one or more entity types and contains
attributes that are peculiar to the relationship between those entity instances. An associative entity is
represented with the diamond relationship symbol enclosed within the entity box.
Associative entity
EMPLOYEE ` COURSE
Certificat
e
Degree of Relationship:
The degree of relationship is the number of entity type that participates in that relationship.
Unary Relationship: it is a relationship between the instances of a single entity type. These are also called
recursive relationship
Ex:
EMPLOYEE
Manage
s
7
PART
` `
VENDOR Supply WAREHOUSE
Schedul
e
Shipping_mode Unit_cost
1) One- to – many (1: M or 1...*) relationship: A painter paints many different paintings, but each one of
them is painted by only one painter. Thus the one painter is related to many paintings. Therefore database
designers label the relationship PAINTER paints PAINTINGS as 1: M.
2) Many- to – many (M: N or *...*) relationship: A student can classes and each class can be taken by many
students, thus database designers label the relationship STUDENT takes CLASS as M: N
(Many -to -many)
3) One- to – one (1: 1 or 1...1) relationship: A retail company’s management structure may require that each
of its stores be managed by a single employee. In turn, each store manager, who is an employee, manages only
a single store. Therefore, the relationship EMPLOYEE manages STORE is 1:1.
E- R Model Notation:
Identifying
relationship Derived Attribute
Mandatory one
Unary
Optional one
Optional many
Ternary
Objects: object represent things that are important to users in the portion of reality we want to model.
Lexical Object Set: An object set consisting of instances that can be printed. Ex: NAME
9
Abstract Object Set: An object set consisting of instances that cannot be printed. Ex: PERSON
An abstract object instance would be represented by an internal number which has no meaning outside the
system. This internal number is sometimes called an Object identity, or a Surrogate key.
Surrogate key: A unique computer system identifier for an abstract object instances; it has no meaning outside
the computer system.
Specialization: An object set that is a subset of another object set. Ex: PERSON
Generalization: An object set that is superset of another object set. EX: MAN
PERSON
PERSON
MAN
MAN
PERSON
George
MAN
George
1, 1 1,*
SUPERVISOR SUPERVISE WORKER
S
10
Here the maximum and minimum cardinality is One in supervisor direction, one and many in worker
direction.
Cardinalities of Generalization/ Specialization
PERSON
1,1
0, 1
MAN
Functional Relationship: a relationship having s maximum cardinality one in at least one direction. One- to
– one (1: 1 or 1-1) relationship: the relationship cardinalities of one in both directions,
One- to – many (1: M or 1-*) relationship: The relationship cardinalities one in one direction and many in
other direction.
Many- to – many (M: M or *-*) relationship: relationship cardinalities of many in both directions.
Name
SS# Addres
s
PERSON
SPOUSE
MARRIED
PERSON
11
MARRIED PERSON is a specialization of PERSON. Thus, a married person has a name, social
security number, address, and so on. So the MARRIED PERSON object set inherits these inherits attributes
from the PERSON object set. In addition, the specialization object set can have attributes of its own. For
example, SPOUSE would be an attribute of MARRIED PERSON, but not of PERSON.
INHERTITING RELATIONSHIPS
Aggregation:
Aggregate: A relationship viewed as an Object set.
A Higher- level relationship: A relationship which involves three or more object sets.
Anniversary
EX: date
MARRIED- COUPLE
MARRIED IS-
MARRIED
MAN MARRIED- WOMAN
TO
In the above figure each man and woman who are related to is- married- to relationship constitute
a married couple, which itself an object. As such married couple can have its own attributes, such as
anniversary date, address. So here married couple is an aggregate.
* *
PRODUCT IS-SOLD-IN COUNTRY
12
QUANTIT
USING TWO BINARY RELATIONSHIPS Y
* *
PRODUCT IS-SOLD-IN COUNTRY
*
SOLD-ON
*
DATE
* SOLD *
PRODUCT COUNTRY
*
DATE
UNIT –IV
Functional Dependencies: The value of an attribute in a tuple determines the value of another attribute in the
tuple.
Ex: In every tuple in worker table WORKER-ID uniquely determines NAME, and WORKER- ID uniquely
determines SKILL-TYPE. We write these two functional dependencies as:
We can define a functional dependency as follows: if A and B are attributes in a relation R, then
FD: A B
The notation is read as functionally determines. In the above example WORKER- ID functionally
determines the NAME, WORKER- ID functionally determines the SKILL-TYPE and A functionally
determines B.
Determinant: The attribute on the left- hand side of an FD is called a determinant because its value
determines the value of other attribute in the tuple.
NORMALIZAITON PROCESS:
Redundancies in a table may result in errors or inconsistencies (known as anomalies) when a user attempts to
update the data in the table.
We have three types of anomalies:
1. Update anomaly: Data Inconsistency resulting from data redundancy and partial update.
Ex: for example if a worker is existing more than once in a relation and we are updating only one row
then the remaining rows also need to be updated. This is said to be update anomaly.
2. Deletion anomaly: Unintended loss of data due to deletion of other data.
Ex: in Assignment table for suppose the worker assigned to one building had completed his work. If it
is decided to delete all the rows containing information about completed buildings from the relation,
then the information about the worker will be lost.
3. Insertion anomaly: Inability to add data to the data to the database due to absence of other data.
Ex: if there are no assignments for the worker, we cannot insert those workers details in the assignment
table, because we have no values for BLDG-ID. This is said to insertion anomaly.
Decomposition of Relations: The process of splitting a relation into multiple relations to eliminate anomalies
and maintain data Integrity. To do this, we use normal forms or rules for structuring relations.
First Normal Form (1NF): A relation is in First Normal Form (1NF) if the values in the relation are atomic
for every attribute in the relation. Atomic value means the value that is not a set of values or a repeating group.
(Or)
A relation which contains no multivalued attributes.
Ex:
The above table contains multivalued attributes. So the above relation is not in 1NF.
A table with multivalued attributes is converted to a relation in first normal form by extending the data in each
column to fill cells that are empty because of the multivalued attributes.
Second Normal Form (2NF): A relation is in Second Normal Form (2NF) if no nonkey attribute may be
functionally dependent on just a part of the key. Thus 2NF can be violated only when a key is a composite key
or, in other words, one that consists of more than one attribute.
Ex:
ASSIGNMENT
WORKER- ID BLDG- ID START- DATE NAME 1235
312 10/10 M. Faraday
1412 312 10/01 C. Nemo
1235 515 10/17 M. Faraday
1412 515 10/15 C. Nemo
14
1. The worker name is repeated in every row that refers to an assignment for that worker.
2. If the name of the worker changes, every row recording an assignment of that worker must be updated.
This is an update anomaly.
3. Because of the redundancy, the data might become inconsistent, with different rows showing different
names for the same worker.
4. If at some time there are no assignments for the worker, there may be no row in which to keep the
worker’s name. This is an insertion anomaly.
To resolve these problems, the relation can be decomposed into the following two relation schemes, both of
which are 2NF.
ASSIGNMENT (WORKER-ID, BLDG-ID, START-DATE)
Foreign key: WORKER- ID REFERENCES WORKER
WORKER (WORKER- ID, NAME)
These two relations are in 2NF. These two smaller relations are called Projections of the original relation.
Projection of a relation: A relation consisting of selected attributes from another relation.
Third Normal Form (3NF): A relation is in third normal form (3NF) if every determinant is a key in that
relation.
(or) A relation is in 3NF if for every FD: X-> Y, X is a key.
(OR)
A relation is in 3NF if it has no Transitive Dependencies.
Transitive dependency: It occurs when a non key attribute is functionally dependent on one or more other
non key attributes.
If a relation is in 3NF, then it is also 2NF.
Ex: consider the following relation WORKER
FD: SKILL- TYPE BONUS- RATE is also a functional dependency. It is clear that 3NF is
satisfied for the first two of these FD’s, but SKILL-TYPE is not a key, so the 3NF criterion fails. So the worker
is not 3NF, but it is in 2NF.
According the definition of 3NF, whichever, the attribute is determining the other attributes, that
attribute we have to make it as determinant (key attribute). Therefore the worker relation is decomposed into
1. WORKER1(worker_id, Skill_Type)
2. WORKER2( Skill_Type, Bonus_Rate)
15
Here Skill_Type in WORKER1 relation is a foreign key and Skill_Type in WORKER2 is a primary key.
Boyce Codd Normal Form (BCNF): Boyce Codd Normal Form is a special case of 3NF.
A relation is in BCNF if every determinant in that relation is a candidate key.
When a relation has more than one candidate key, anomalies may result even though that relation is in 3NF.
Ex:
STUDENT_ADVISOR (Sid, Major, Advisor, Maj-GPA)
The above relation is in 3NF but not in BCNF. To convert this relation into BCNF we need to decompose the
relation as follows.
STUDENT_ADVISOR (Sid, Advisor, Maj-GPA)
STUDENT_ADVISOR (Advisor, Major)
Now the above two relations are in BCNF.
Fourth Normal Form (4NF): A relation is in 4NF if it is in 3NF and No independent Multi Valued
dependencies.
Multivalued dependency (MVD): A constraint that guarantees the mutual independence of multivalued
attributes.