0% found this document useful (0 votes)
9 views30 pages

RDBMS

The document outlines various integrity constraints in databases, including domain constraints, entity integrity, and referential integrity, which ensure data validity and consistency. It also discusses relational algebra and calculus, detailing operations such as union, intersection, difference, product, select, project, join, divide, and assignment for manipulating relations. Additionally, it introduces SQL for data manipulation, covering table creation, data types, and basic commands like insert, rename, drop, and select.

Uploaded by

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

RDBMS

The document outlines various integrity constraints in databases, including domain constraints, entity integrity, and referential integrity, which ensure data validity and consistency. It also discusses relational algebra and calculus, detailing operations such as union, intersection, difference, product, select, project, join, divide, and assignment for manipulating relations. Additionally, it introduces SQL for data manipulation, covering table creation, data types, and basic commands like insert, rename, drop, and select.

Uploaded by

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

Integrity Constraints:

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

Two l a n g u a g e used w i t h relational database are Relational Algebra and Relation


calculus.
Relation Algebra: A procedural language for manipulating relations. Procedural language
that provides a step by step method for solving problems.
Relational Algebra is also important because it contributes much of the vocabulary and
many of the basic relational data manipulation concepts that are commonly found in
database languages.
Relation Algebra operations manipulations relations. These operations use one or two
existing relations to create a new relation. This new relation may then be used as input to
a new relation. The creation of new relations from old ones makes possible an infinite
variety of data manipulations Relational Algebra consists of the following Nine
operations:
1) UNION
2) INTERSECTION
3) DIFFERENCE
4) PRODUCT
5) SELECT
6) PROJECT
7) JOIN
8) DIVIDE
9) ASSIGNMENT
The first four operations are taken from mathematical set theory.
Next four operations that apply specifically the relational data model.
The last operation_assignment is the standard computer language operation of
giving a name to a value. Symbol “=” is used to assign value to a variable.

1) UNION (U) OPERATION


This operation allows to combine the data from relations. The result of this
operation denoted by WUM, where W & M are two relations, is a relation that
included all tuples that are either in W or on M or in both W and M. duplicate
tuples are eliminated.

Let consider SP_SUBORD and SP_Mar are two tables

SP_SUBORD

Salpers_id Sales Name Manager_id Office Com %

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

SalPers_Id Sales_Name Manager_id Office Com


%
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
27 Terry 27 Chicago
15
44 D 27 Tokyo
12
35 C 27 Brussels
11
12 E 27 B.A
10

2) INTERSECTION OPERATION (n)


This operation allows us to identify the rows that are common and two
relations. Let us take above tables SP_SUBD & SP_Mar. Intersection of these two
tables results in below table.
SP.SUBORD_Mar= SP_SUBORD n SP_MaR
SP_SUBORD_MAR
Salpers_id Salper_Name Manager_id Office Com %
44 C 27 Tokyo 12
35 D 27 Brussels 11
12 E 27 B.A 10
3) DIFFERENCE OPERATION
The difference operation (indicated by minus signs) allows us to
identify rows that are in one relation and in another. Suppose we are interested in
identifying the manager who is not subordinate to any other manager. Then we
take the difference between SP_MAR and SP_SUBORD.
SP_MAR_MAR: = (SP_MAR) _ (SP_SUBORD)
i.e. SP_MAR_MAR relation consists of all rows that in SP_MAR which are not
in SP_SUBORD. .ie.
SP_MAR_MAR
Saleper_Id Salpers_Name Manager_Id Ofice Com
%
27 Terry 27 Chicago
15

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

The product of A and B results C.

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

i.e A has 2 Rows 2 Columns


B has 3 Rows 2 Columns

C = A * B has 4 Columns 6 Rows i.e. 2 *3=6


5) SELECT OPERATION
The operation is used to create a relation from another relation by
selecting only those rows from the original relation that satisfied condition.
i.e. to retrieve selected rows from the table.
Query: Give all information or sales person who is in Tokyo office.
SP_Tokyo = SELECT (SALESPERSON: OFFICE = ‘TOKYO’)
City = ‘Tokyo’ (OFFICE)

SP_TOKYO

Salpers_id Salpers_name Manager_id Office Com


%
14 Masaj 44 Tokyo
11
39 B 44 Tokyo
10
44 C 22 Tokyo
12

In select operation we can use comparison operation. i.e. >,<


>=, <=

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

This operation creates a new relation by selecting the rows in one


relation that match every row in another
relation. Ex: A (X1, X2, X3…..Xm, Y1, Y2,
Y3….. Yn)
B (Y1, Y2, Y3…...Yn)

9) ASSIGNMENT
Assignment operation gives names to a relation.
Ex: A: =SELECT (SALESPERSON: OFFICE =
‘TOKYO) “:=” is a symbol for ASSIGNMENT.
UNIT- II
SQL

Data manipulation in data base


Introduction: All organizations need to collect process and store data for its human,
financial, material needs.
For example:- an organization could store information for pay role, accounts receivable
and payable, sales receivable and forecast or design and manufacturing.
Table or entity: - to represent this information one can use DBMS modeling. In a DBMS
a group of interest to an organization is called an entity. Entity information is stored in an
object called table. For example: - client is considered as an entity. Information about the
client entity can be stored in a client master table. A table is really a two dimensional
matrix that consists of rows and columns.
Attributes/columns/fields: - each entity can have a number of characteristics. A client can
have characteristics like name, address, telephone number, fax number, balance due etc.,
the characteristics of an entity are called an attribute. The values for this characteristics are
called attribute values. When entity information is stored in a table, the attributes
associated with the entity are stored in the table columns or table fields. Example: - clients
(name, address, telephone no., fax no., balance due)
Here entity : clients
Attribute: Name,
Tuple/record/row: -An organization will deal with many clients and the same
information must be recorded for each clients. Multiple fields paced in a horizontal plane
is called a record/ row / tuple.
NAME ADDRESS TELEPHONE FAX NO., BALANCEDUE
A HYD 2345 333 1000

B SEC 4565 323 100

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.

Rules of SQL syntax:-


1. An SQL statement starts with a verb. This verb may have additional nouns
and adjectives.
2. Each verb is followed by a number of clauses.

3. Each clause has 1 or more parameters.

4. A space separates clauses with in an SQL statement.

5. A comma separates parameters with in clauses.

6. A semi colon is used to terminate the SQL statement.

Ex: - create table employee (employee no., emp……);


Here verb=create, noun/clause=table employee, parameter=employee no., emp…
Creation of a table: - CREATE COMMAND
Table are defined in three steps
1. The name of the table is given.

2. Each column is defined including column constrains.

3. Table constrains are defined.

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

Insertion of data into tables:- INSERT COMMAND


Once a table is created, the next step is to load the table with data. When inserting row
of data into the table, the insert operation.
I. Creating a new row in the database table

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’);

Sql>Insert into employee values (&emp no,’&emp name’,’&addr’, &salary, ‘&dept’);

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.

Renaming a table: - RENAME COMMAND


Syntax; - Rename<old table name> to <new table name>
Example: - sql>Rename employee to employee details

Destroying /deleting a table:- Drop Command


To destroy a table syntax is
Syntax; - DROP <Table name>
Example: - sql>DROP
Employee;
Column details of a table creating:- DESCRIBE COMMAND
This command displays the column names, the data type and the special
attributes connected to the table.

Syntax: - Describe <table name>


Example: - sql> Describe employee;

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 dept is commerce.


Sql>SELECT * FROM EMPLOYEE WHERE DEPT = ‘COMMERCE’;

3) Display rows from a table where salary is less than


5000; Sql>SELECT * FROM EMPLOYEE WHERE sal <
5000;

Boolean Connections: - AND, OR, NOT.


These connections will be used to create compound conditions or to negate a
condition. Between can be used in a comparison of some value with two other first
smaller than the second. If the comparison value can be equal to either the smaller (or)
the larger (or) can be equal to any value in between.
Ex: -
1) Display rows from a table where salary between 1000 and 5000.

SELECT * from employee WHERE SALARY BETWEEN 1000 AND 5000;

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’;

Elimination of duplicates from the SELECT Statement:-


A table could hold duplicate rows. In such a case, to see only unique rows
use DISTINCT COMMAND
syntax : SELECT DISTINCT Column name1, Columnname2… from <table name>;
Ex: - sql>SELECT DISTINCT * from Employee;
Sorting Data in a Table:-
Oracle allows data form a table to be viewed on a sorted order. The
rows retrieved from the table will be sorted in either ascending or descending order
depending On the condition specified in the select sentence.
Syntax: - select * from <table name> ORDER BY column name1, column name2 (sort
order);
Query: -retrieve information of employee sorted on employee number field, in
descending order.
Sql>SELECT * FROM EMPLOYEE ORDER BY Empno desc;
In case there is no mention of the sort order, the oracle engine sorts in
ascending order by default.
Wild Card Characters: - Special symbols that stand for unspecified strings of
characters.
SQL has two wild card characters.
1 ) %( percent)
2) _ (under score). The underscore stands for exactly one specified character, the
percent stands for zero (or) more unspecified character.
The “LIKE” operator is used to compare character variables when wild card characters
are used.
Query: - Find all the employees whose name begins with “A”.
Select * from Employee where Empname like’ A%’;
Data base Change Operations:-
SQL provides three data base change operations
INSERT, UPDATE and DELETE, to allow the addition of rows, the changing of values
in rows, and the deletion of rows, respectively, to or from a table.
INSERT: - Insert operation that causes rows to be added to a table,
Syntax: - INSERT into table name (column1, column2----------n)
VALUES (value1, value2----------value ‘n’);
OR
INSERT into table name values (&column1, &column2, ---------&column n);
Ex: -sql>Insert into Employee (Empno, Empname, Address, Phone no, ----)
VALUES (102, ‘NEELIMA’, ‘UPPAL’, ‘HYD’, 23456, ----);

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’;

AGGREGATE FUNCTIONS (Built – in- Functions):


SQL provides fire statistical functions that examine a set if rows in a relation and
produce a single value. Functions are SUM, AVG, COUNT, MAX, and MIN.
1) MAX and MIN
Query:-What is the highest and lowest salary in employee table?
Sql> SELECT MAX (SALARY), MIN (SALARY) FROM EMP1;
The MAX and MIN functions operate on a single column in a relation. They select
the largest (or) the smallest value, respectively, to be found in that column.
2. COUNT (*):-Returns the number of rows in the table, including duplicates and those
with nulls.
Ex:- sql>SELECT COUNT (*) “TOTAL”FROM EMP;
*Emp1 is table name
3. SUM:-Returns sum of values of
‘n’
Ex:-sql>SELECT SUM (SALARY) ‘TOTAL AMOUNT’FROM Emp1;
OUTPUT:-TOTAL AMOUNT
What is the average salary paying for employees?
4. AVG:-sql>SELECT AVG (SALARY) “AVERAGE “FROM
EMP1; Returns average value of ‘n’ ignoring null values.

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)

In above example, there are two tables i.e. WORKER, ASSIGNMENT.


Now
1 QUERY:-What are the skill types of worker assigned to building 435?
This query is found in two in two tables WORKER and ASSIGNMENT. The
SQL solution requires the listing of both of these relations in the FROM clause.
Sql>SELECT SKILL_TYPE FROM WORKER, ASSIGNMENT WHERE WORKER.
WORKER ID = ASSIGNMENT. WORKER ID AND BLDG_ID=435;
2 QUERY:-Displays WORKER_ID, SUPV_ID, BLDG_ID from worker table and
ASSIGNMENT TABLE.
Sql>SELECT worker .WORKER_ID, SUPV_ID. WORKER_ID, ASSIGNEMNT,
BLDG_ID
FROM WORKER, ASSIGNMENT WHERE worker
.WORKER_ID=ASSIGNEMT, WORKER_ID
In this case, they are two tables in the clause. So, system creates the
Cartesian products of the rows in the tables. This means that one huge table is logically
created, consisting of all the columns form both tables, and pairing up every row in one
table with every two in the other table in the clause.
In our example, since there are five columns in the worker table, and for
columns in ASSIGBEMNT there will be 9 columns in the new table, the total number of
rows in the new table is 133 rows i.e. 7x19=133.suppose. In worker table there are 7 rows
In ASSIGNEMNT table there are 19 rows If one table have “m” rows and
second table have ‘n’ rows, then after joining, new table will have mxn rows.

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

019001 C00006 12-04-97


019002 C00002 25-12-97
019003 C00007 03-10-97
019004 C00005 18-01-97
019005 C00002 20-08-97
019006 C00007 12-01-97
Table: Client-master
Client no Name
C00001 Ashok Mehra 500
C00002 Vishal Parikh 1000
C00003 Ajay Mehta 0
C00004 Rohith Roy 0
C00005 0
Nalini Deewan
C00006 0
Prem Iyer
C00007 0
Rahul Desai
Sql>SELECT * from sales-order WHERE client no= (Select client no from client master
WHERE name= “rahul desai”,

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

SUPERVISES * * IS- ASSIGNED- TO * TYPE


WORKER WORKER BUILDING
QUALITY-
1 ASSIGNMENT LEVEL
* STATUS
HAS-SKILL SKILL-TYPE
1 START- DATE
SKILL BONUS-RATE
NUMBER- OF- DAYS
HOURS-PER- RATE

Recursive Relationship: A relationship that relates an object set to itself.


In the above figure a relationship like SUPERVISES, which relates an object to itself, is recursive.
This data model can be converted into a relation data model. The following figure shows a relation with sample
data values, which represents the WORKER object set, its attributes, and two of its relationships. Each
column in the relation is an attribute of the relation.
Relational attribute: A column in a relation.
Converting Conceptual model to relational model of worker object:

Conceptual model Relation Attribute

WORKER- ID (Attribute) WORKR- ID


NAME (Attribute) NAME
HOURLY- RATE (Attribute) HOURLY- RATE
HAS- SKILL (Relationship) SKILL- TYPE
SUPERVISES (Relationship) SUPV- ID

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

Tuple: A Row in relation.


Degree of a Relation: The number of attributes in a relation (table).
Attribute Domain: The set from which an attribute takes its values.
Null Value: The value given an attribute in a tuple if the attribute is inapplicable or its value is unknown. Null
value is not a blank or zero. Ex. In the worker table some rows don’t have SUPV-ID.

Keys:

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 3321
P. Mason 17.40 Framing 1520
H. Rickover 15.40 Framing

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

312 123 Elm Office 2


435 456 Maple Retail 1
515 789 Oak Residence 3
210 1011 Brich Office 3
111 1213 Aspen Office 4

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

Composite key: A key consisting of more than one attribute.


Ex: In the above example ASSIGNMENT relation, the key consists of the WORKER- ID & the BLDG- ID
attributes. Neither WORKER- ID alone nor BLDG- ID alone uniquely identifies every row, but the two
attributes together do provide the unique identification required for a key.

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.

The relations of above figure are designated as follows.


WORKER (WORKER- ID, NAME, HOURLY- RATE, SKILL- TYPE, SUPV- ID)
ASSIGNMENT (WORKER- ID, BLDG- ID, START- DATE, NUM- DAYS)
BUILDING (BLDG- ID, BLDG-ADDRESS, TYPE, QLTY- LEVEL, STATUS)
SKILL (SKILL- TYPE, BONUS- RATE, HOURS-PER- WEEK)

The key is specified using underlined attribute.

Relational database schema: A listing showing relation names, attribute names, key attributes, and foreign
keys.

ENTITY- RELATIONSHIP MODEL :( E- R MODEL):

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

Object: machine, building


Event: sale, registration
Concept: account, course

Entity is denoted using a rectangle box in the E-R diagram.

Entity STUDENT

Entity type and Entity Instance:

An Entity type is a collection of entities that share common properties or characteristics.


Ex: Employee, Student
An Entity instance is a single occurrence of entity type. Ex: John

Strong Entity and Weak Entity types:

A strong entity type is one that exists independently of other types.


Example: SUDENT, EMPLOYEE, AUTOMOBILE and, COURSE

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:

An attribute is property of an entity type that is of interest to the organization


Ex: STUDENT: student_ID, Student _Name, Address, Phone_Number
EMPLOYEE: Employee_Id, Employee_Name, Address, Skill
Attributes are denoted using ellipse in the E-R Diagram

Student_ID Student_Name

Simple versus Composite Attributes: STUDENT

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

H_No Street_Nam City


STUDENT e
A Simple Attribute is an attribute that cannot be broken down into smaller components.
Ex: age for student entity.
5

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.

Course_I Course_Title Prerequisite


Employee_id Name dD

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.

Ex: Employee Course

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

Course_I Course_Title Prerequisite


Employee_id Name
Ceritficate_name
dD

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

Binary Relationship: the relationship between two entity types.

Student Has Book

Ternary Relationship: the relationship among three entity types.

PART

` `
VENDOR Supply WAREHOUSE
Schedul
e

Shipping_mode Unit_cost

Data models use three types of relationships:

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.

Relationships are bidirectional.


Constraints: A constraint is a restriction placed on the data. Constraints are important because they help to
ensure data integrity. Constraints are normally expressed in the form of rules.
8

E- R Model Notation:

Strong entity Associative entity

Weak entity Attribute

Relationship Multivalued attribute

Identifying
relationship Derived Attribute

Mandatory one
Unary

Binary Mandatory many

Optional one

Optional many
Ternary

Enhanced E-R Model:

Conceptual Data Models:

Objects: object represent things that are important to users in the portion of reality we want to model.

Ex: People, Automobiles, trees and books


Object set: A set of things of the same kind.
Object Instance: a particular member of an object set

The object set PERSON The object set


name
 Person
+
An object instance

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

TWO POINTS REPRESENTING THE SAME INSTANCE

PERSON
George

MAN
George

Relationship: A linking between instances of two object sets.


Cardinality constraints: The number of instances of one object set or entity associated to a single instance of
other object set or entity.
Minimum Cardinality: The minimum number of instances of one object set or entity associated to a single
instance of other object set or entity.
Maximum Cardinality: The maximum number of instances of one object set or entity associated to a single
instance of other object set or entity
If we assume each married person has only one spouse, the cardinality of the IS-MARRIED-TO relationship is
one in each direction.
Here the maximum cardinality is One and Minimum Cardinality is Zero in both directions.
This is represented as follows

MAN 0, 1 IS- 0, 1 WOMAN


MARRIE
D- TO

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.

Cardinality Notation Example

One- one 1:1 or 1-1 A husband has one wife.


A wife has one husband.
(The marriage relationship is one-one)
One – many 1:* or 1-* An employee is in one department
A department has many employees.
(The employment relationship is one- many)
Many- many *:* or *-* A student takes many courses.
A course has many students.
(The enrollment relationship is many- many)

Attribute is functional relationship from an object set to another set.


Key is a value that can always be used to uniquely identify an object instance.
External Key is a set of lexical attributes whose values always identify a single object instance.
An External key is called as Identifier
Inherit: The property of a specialization set that causes it to have all the attributes of its generalization set.

SPECIALIZATION INHERIT ATTRIBUTES

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

PERSON WORKS- COMPANY


FOR

John Doe XYZ Company


MARRIED
PERSON
John Doe

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.

Another ex: QUANTIT


Y

* *
PRODUCT IS-SOLD-IN COUNTRY
12

QUANTIT
USING TWO BINARY RELATIONSHIPS Y

* *
PRODUCT IS-SOLD-IN COUNTRY

*
SOLD-ON
*
DATE

USING THREE WAY RELATIONSHIP


QUANTIT
Y

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

FD: WORKER-ID NAME


FD: WORKER-ID SKILL- TYPE

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:

Normalization: The process of converting a relation to a standard form.


Data redundancy: Repetition of data in a database.
Data Integrity: Consistency of data in a database.
13

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.

Normal forms: Rules for structuring relations that eliminate anomalies.

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:

Emp.no Ename Dept- Name salary Course- Title Date- Completed

100 Venkat Comp. Sc 22000 C++ 08/9/2001


22000 Oracle 10/9/2001

101 Vasu Electronics 50000 Java 19/9/2001


ASP 20/9/2001

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

ASSIGNMENT (WORKER-ID, BLDG-ID, START-DATE, NAME)


In the above table the key consists of WORKER-ID and BLDG-ID together. NAME is determined by
WORKER-ID and so functionally dependent on a part of key. That is, knowing WORKER-ID and so is
functionally dependent on a part of the key. The above relation is not in the second normal form. So it can lead
to the following problems.

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

WORKER-ID SKILL-TYPE BONUS - RATE


1235 ELECTRIC 3.50
1412 PLUMBING 3.00
1311 ELECTRIC 3.50

FD: WORKER-ID SKILL-TYPE


FD: WORKER-ID BONUS- RATE
Are functional dependencies for this relation since WORKER-ID is a key. However

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.

You might also like