Chapter 4
Relational Database Model
Database Schema and
Sub Schema
Database Schema and Subschema
The Schema is the overall logical structure of a
database.
Itis the global plan that controls the organization
and use of data.
A schema can also be viewed as a logical
representation of the physical database.
It gives a complete description of all records,
fields, and data relationships.
The schema describes the structure of the database
to the DBMS.
It also describes the application processing need
of the organization.
Schemas are generally stored in a data dictionary.
1) Physical Schema
Describes the database design at the physical level.
2) Logical Schema
Describes the database design at the logical level.
3) Subschema
Describes different views of the database.
Database Subschema
A Subschema is a Subset of a Schema.
Itis the functional database or the user’s view of
the database.
When a user requests for a record, this is expressed
in terms of the functional database.
Relational Database and
Relational database Schema
Relational Database and Relational
database Schema
A database is a collection of interrelated data files or
structures.
A relational database is a collection of information
that organizes data points with defined relationships
for easy access.
In a relational database, the schema defines the
tables, where each table has its own rows and
columns. A row represents a record (or tuple), while a
column represents a field (or attribute) and the
relationships between fields and tables.
Example
College Management System
Principal
Staff
Student
Library
Principal
Principal Name Qualification Mobile.no Email_Id
Staff
Staff Name Department Qualification Mobile.no Email_Id
Student
Name Roll_ No Enrollment _No Mobile No. Address
Library
Location HOD No. Of Books
Integrity Constraint
Integrity Constraint
An Integrity Constraint is a mechanism used
to prevent invalid data entry into the table.
OR
Integrity constraints are a set of rules.
It is used to maintain the quality of
information.
Types of Integrity Constraint
Integrity Constraints
Domain Entity Referential
Integrity Constraints Integrity Constraints Integrity Constraints
Primary Foreign
Not Null Check Unique Reference
Key Key
1) Domain Integrity Constraints
It is used to maintain value according to
the user specifications.
Types:
a) NOT NULL
b) Check
a) NOT NULL
Case A: At the time of table creation
create table <table_name>
(column_name1 datatype(size),
column_name2 datatype(size) NOT NULL,
.
.
.
column_namen datatype(size));
Case B: After table Creation
alter table <table_name>
modify <column_name> NOT NULL;
b) Check
The Check Constraint defines a condition that each row must
satisfy.
Case A: At the time of table creation
create table <table_name>
(column_name1 datatype(size)
constraint <constraint_name>
check <condition>,
column_name2 datatype(size),
.
.
.
column_namen datatype(size));
Case B: After table Creation
alter table <table_name>
add constraint <constraint_name>
check <condition>;
2) Entity Integrity Constraints
An entity is any data recorded in a database.
Each database represents a table and each row
of a table represents an instance of that entity.
Types:
a) Primary Key
b) Unique
a) Primary Key
Case A: At the time of table creation
create table <table_name>
(column_name1 datatype(size)
constraint <constraint_name> primary key,
column_name2 datatype(size),
.
.
column_namen datatype(size));
Case B: After table Creation
alter table <table_name>
add constraint <constraint_name>
primary key <column_name>;
b) Unique
Case A: At the time of table creation
create table <table_name>
(column_name1 datatype(size)
constraint <constraint_name> unique,
column_name2 datatype(size),
.
.
column_namen datatype(size));
Case B: After table Creation
alter table <table_name>
add constraint <constraint_name>
unique <column_name>;
3) Referential Integrity Constraints
It is used to establish a ‘Parent-child’
relationship between two tables having a
common column.
Types:
a) Reference Key
b) Foreign Key
a) Reference Key
Case A: At the time of table creation
create table <table_name>
(column_name1 datatype(size),
column_name2 datatype(size)
constraint <constraint_name>
references <parent_table_name>
(parent_table_column_name)
[on delete cascade],
:
:
:
column_name n datatype(size));
b) Foreign Key
Foreign key represents relationship between tables.
A Foreign key is a column(or a group of columns) whose values are
derived from the ‘Primary Key’ of other table.
One or more columns can be defined as Foreign key.
b) Foreign Key
Case A: At the time of table creation
create table <table_name>
(column_name1 datatype(size),
column_name2 datatype(size),
:
:
:
column_name n datatype(size),
constraint <constraint_name>
references <parent_table_name>
(parent_table_column_name)
[on delete cascade]);
Database Keys
Database Keys
Keys are very important part of Relational
database. They are used to establish and identify
relation between tables.
Types:
Super Key
Candidate Key
Primary Key
Foreign Key
Super Key
Super Key is defined as a set of attributes within a
table that uniquely identifies each record within a table.
Candidate Key
A Candidate Key is a set of one or more
fields/columns that can identify a record uniquely in a
table.
Primary Key
Primary Key is a one fields/columns of a table that
uniquely identify a record in database table.
Foreign Key
Primary Key of one table become a attributes of
another table is called Foreign Key.
Super Key
Super Key is defined as a set of attributes within a
table that uniquely identifies each record within a
table.
A combination of one or more columns in a table
which can be used to identify a record in a table
uniquely, a table can have any number of super
keys.
Primary key, Unique key, Alternate key, Candidate
Key are subset of Super Keys.
Cust-ID Cust_name Cust_street Cust_city account_no
Customer
Example:
In the Customer table Cust_ID, Cust_name,Cust_street ,Cust_city and account_no
are Super key.
Customer{ Cust_ID,Cust_name,Cust_street,Cust_city, account_no}
Candidate Key
A Candidate Key is a set of one or more
fields/columns that can identify a record uniquely
in a table.
Candidate keys are defined as the set of fields from
which primary key can be selected. There can be
multiple Candidate Keys in one table.
Candidate Key are subset of Super Keys.
Cust-ID Cust_name Cust_city account_no
Customer
Example:
In Customer table Cust_ID,Cust_name,Cust_city ,account_no are Candidate Key.
Customer{ Cust_ID,Cust_name,Cust_city, account_no}
Primary Key
Primary key is a one fields/columns of a table that
uniquely identify a record in database table.
It can not accept null, duplicate values.
Only one Candidate Key can be Primary Key.
Primary key that uniquely identify each record in a
table.
Cust-ID
Customer
Example:
In Customer table Cust_ID is Primary Key.
Customer{ Cust_ID},
Composite Key / Compound Key
Composite Key that consists of two or more
attributes that uniquely identify an entity is called
Composite key.
Cust-ID account_no
Customer
Example:
In Customer table Cust_ID and account_no are is Composite Key.
Customer{ Cust_ID,account_no.},
Foreign Key
Foreign key represents relationship between tables.
A Foreign key is a column(or a group of columns)
whose values are derived from the ‘Primary Key’
of other table.
One or more columns can be defined as Foreign
key.
Cust-ID Cust_name Cust_city account_no
Customer
Relationship
Account_no Bname Balance
Account
Example:
We can have account_no column in the Customer table which is pointing to
account_no column in a account table where it a primary key.
Codd’s Rules of RDMS
Codd’s Rules of RDMS
Rule 1: Information Rule
Rule 2: Guaranteed Access Rule
Rule 3: Systematic Treatment of NULL Values
Rule 4: Active Online Catalog
Rule 5: Comprehensive Data Sub-Language Rule
Rule 6: View Updating Rule
Rule 7: High-Level Insert, Update, and Delete Rule
Rule 8: Physical Data Independence
Rule 9: Logical Data Independence
Rule 10: Integrity Independence
Rule 11: Distribution Independence
Rule 12: Non-Subversion Rule
Rule 1: Information Rule
Relational Databases should store the data in the form of
relations. The data stored in a database, may it be user data
or metadata, must be a value of some table cell.
Everything in a database must be stored in a table format.
Rule 2: Guaranteed Access Rule
Every single data element (value) is guaranteed to be
accessible logically with a combination of table-name,
primary-key (row value), and attribute-name (column
value). No other means, such as pointers, can be used to
access data.
Rule 3: Systematic Treatment of NULL Values
The NULL values in a database must be given a
systematic and uniform treatment. NULL can be
interpreted as one the following − data is missing, data is
not known, or data is not applicable.
Rule 4: Active Online Catalog
The structure description of the entire database must be
stored in an online catalog, known as data dictionary,
which can be accessed by authorized users.
Rule 5: Comprehensive Data Sub-Language Rule
The system must support a least one relational language
that
(a) has a linear syntax,
(b) can be used both interactively and within application
programs, and
(c) supports data definition operations (including view
definitions), data manipulation operations (update as well as
retrieval), security and integrity constraints, and transaction
management operations (begin, commit, and rollback).
Rule 6: View Updating Rule
All the views of a database, which can theoretically be
updated, must also be updatable by the system.
Rule 7: High-Level Insert, Update, and Delete
Rule
A database must support high-level insertion, updating,
and deletion.
Rule 8: Physical Data Independence
The Physical data in a database must be independent of its
user’s view (application). Any change in logical data must
not affect the applications using it.
Rule 9: Logical Data Independence
The logical data in a database must be independent of its
user’s view (application). Any change in logical data must
not affect the applications using it.
Rule 10: Integrity Independence
A database must be independent of the application that uses
it. All its integrity constraints can be independently
modified without the need of any change in the application.
Rule 11: Distribution Independence
The Data Manipulation Language of the relational system
should not be concerned about the physical data storage
and no alterations should be required if the physical data is
centralized or distributed.
Rule 12: Non-Subversion Rule
Any row should obey the security and integrity constraints
imposed. No special privileges are applicable.
Working with RDMS
Structure Query Language
It is also called ‘Query Language’.
It can define the Structure of the database,
modify data in the database and specify
Security & Constraint.
developed by IBM in 1970’s.
It was first named as ‘Sequal’.
Data Types in SQL
Char
Varchar2
Long
Number
Date
Char
Fixed length character string
store alphanumeric values
column name can vary between 1 to 2000
bytes
Varchar2
Variable length character string
store alphanumeric values
column name can vary between 1 to 4000
bytes
Long
store variable character length
maximum 2GB
only on column in the table can have long data
type
Date
store Date & Time in a table
fixed length of 7 bytes
default date format is ‘dd-mmm-yy’
sysdate()
Number
store positive no. , negative no. , zeroes , fixed point
no. , and floating point no. with a precision of 38.
(P= 38 , S=0)
where P is Precision
Total no. of digit ( 1 to 38)
&
S is Scale
No. of digit to the right of the decimal Point
( -84 to 127)
Eg: SSC Percentage (85.37%)
Limitation
maximum column limit of SQL is 30
characters.
maximum table name is 30 characters.
first letter start with alphabet.
SQL support three standard special
character ( _ , $ , #)
1) Data Definition Language (DDL)
• It is used to create and modify the structure of
database objects in database.
or
• to define the structure or schema of a database
• Commands are
Create
Alter
Truncate
Rename
Drop
Data Definition Language (DDL) Commands
1) Create table
2) Alter table
3) Truncate table
4) Rename table
5) Drop table
1) Create table
Definition: To create new table or database
Syntax:
CREATE TABLE <table_name>
(column_name1 datatype(size),
column_name2 datatype(size),
.
.
column_namen datatype(size)
);
Example of Employee table
ID Name Age Salary
Fig : Employee Table
Example:
2) Alter table
Definition: It is used to add new attributes or
to modify the existing attributes in the table.
Alter Table
Add clause Modify clause
Case 1: ADD is used to add columns into the existing
table.
Syntax:
ALTER TABLE <table_name>
add (column_name1 datatype(size),
column_name2 datatype(size),
.
.
column_namen datatype(size)
);
Example:
Case 2: It is used to modify the existing columns in a
table.
Syntax:
ALTER TABLE <table_name>
modify (column_name1 datatype(size),
column_name2 datatype(size),
.
.
column_namen datatype(size)
);
Example:
DROP COLUMN is used to drop column
in a table. Deleting the unwanted columns
from the table.
Syntax:
ALTER TABLE <table_name>
drop column column_name;
Example:
3) Truncate table
Definition: The TRUNCATE TABLE statement
removes all the rows from a table, but the table
structure and its columns are remain intact.
Syntax:
TRUNCATE TABLE <table_name>;
Example:
4) Rename table
Definition: Rename statement is used to
rename a table.
Syntax:
RENAME <oldtable_name> to <newtable_name> ;
Example:
5) Drop table
Definition: The drop table command removes
the data as well as definition(Structure) of an
oracle table.
Syntax:
DROP table <table_name> ;
Example:
Data Manipulation Language
(DML) Commands
1) Insert
2) Select
3) Update
4) Delete
1) Insert
Definition: insert data in to a table.
OR it creates a record.
Case a: To insert a row into a table by specifying all
attributes and its value:
Syntax:
insert into <table_name>
(attributes_name1, attributes_name2,…., attributes_name n)
values
(expression 1, expression 2,……., expression n));
Example of Employee table
ID Name Age Salary
1 Adam 34 13000
2 Alex 28 15000
3 Smith 20 18000
4 John 42 19020
Fig : Employee Table
Example:
Case b: To insert values for selected attributes in a
table.
Syntax:
insert into <table_name>
(attributes_name1, attributes_name2)
values
(expression 1, expression 2) );
Example:
Case c: To insert the values for all attributes in order
attributes declared in the table.
Syntax:
insert into <table_name>
values
(expression 1, expression 2,……., expression n));
Example:
Case d: To insert more than one row.
Syntax:
insert into <table_name>
values
(&attributes 1, ‘&attributes 2’,…..&attributes n) );
Example:
2) Select
Definition: Retrieve data from the a database.
Case a: To display all attributes from the table
Syntax:
Select *
from <table_name>;
Example:
Case b: To retrieve specific attributes from the table.
Syntax:
Select <attributes_name1,…. attributes_namen>
from <table_name>;
Example:
Case c: To list only those records from the table
which satisfies the given predicate.
Syntax:
Select <attributes_name1,…. attributes_namen>
from <table_name>
where <search condition>
Example:
Case d: To list distinct values in the column.
Syntax:
Select distinct <attributes_name1>
from <table_name>;
Example:
3) Update
Definition: Updates existing data within a table.
Case a: To update all rows from the table.
Syntax:
update <table_name>
set <attributes_name1= expression 1>;
Example:
Case b: To update selective rows from the table
where the specified condition gets satisfied.
Syntax:
update <table_name>
set <attributes_name1= expression 1>
where <search condition>;
Example:
4) Delete
Definition: Deletes all records from a table.
Case a: To delete specific rows from the table.
Syntax:
delete
from <table_name>
where <search condition>;
Example:
Case b: To delete all rows from the table.
Syntax:
delete
from <table_name>;
Example:
Consider the following schemas:
(i) Dept (Dept_no, Dept_name, Dept-loc)
(ii) Staff (Staff_id, Staff_name, Dept_no, Joint_date)
Draw and explain parent-child relationship for above schemas
and find out foreign key with justification.(S-19)
Winter-18
1) Define the term Database Schema.(2M)
2) Define the term Foreign Key. (2M)
3) Explain Integrity constraints with example. (4M)
4) Explain primary key and candidate key with example. (4M)
5) Explain various types of Relational constraints. (4M)
6) Consider ‘student’ database with appropriate details. Write a
procedure to manipulate given database by adding, modifying and
deleting records. (6M)
Summer-19
1) Define Constraint.(2M)
2) Explain any four Codd’s rule. (4M)
3) Explain different operations performed with Data Definition
Language. (4M)
.
4) Explain terms Primary Key and Candidate Key with example.(4M)
5) Explain entity integrity constraint with example . (4M)
6) Consider the following schemas:
(i) Dept (Dept_no, Dept_name, Dept-loc)
(ii) Staff (Staff_id, Staff_name, Dept_no, Joint_date)
Draw and explain parent-child relationship for above schemas
and find out foreign key with justification.(6M)
7) Consider Employee database with appropriate details. Write a procedure to
manipulate given database by adding, modifying and deleting records.
Winter-19
1) Define primary key and candidate key.(2M)
2) Define constraints, list types. (2M)
3) Write Syntax for create table.(2M)
4) Explain Domain constraints with Syntax and example (4M)
.
5) Explain any 4 Codd’s rules. (4M)
6) Consider the following schema
student (R_No, Name, DOB, Percentage, D_No).
Write procedure to manipulate given database by adding,
modifying and deleting records.(6M)
7) Consider the following schemas:
(i) Dept (Dept_No, DName, LOC)
(ii) Emp (Emp_No, Ename, Job, Sal, Dept_No)
Draw and explain parent child relationship for above schemas
and apply referential integrity constraint.