




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Introduction to RDBMS / 1
1.0 Objectives
1.1 Introduction
1.2 What is RDBMS?
1.3 Difference between DBMS & RDBMS
1.4 Summary
1.5 Check your Progress – Answers
1.6 Questions for Self – Study
1.7 Suggested Readings
After reading this chapter you will be able to,
Most of the problems faced at the time of implementation of any system are
outcome of a poor database design. In many cases it happens that system has to
be continuously modified in multiple respects due to changing requirements of
users. It is very important that a proper planning has to be done.
A relation in a relational database is based on a relational schema, which consists
of number of attributes.
A relational database is made up of a number of relations and corresponding
relational database schema.
The goal of a relational database design is to generate a set of relation schema
that allows us to store information without unnecessary redundancy and also to
retrieve information easily.
One approach to design schemas that are in an appropriate normal form. The
normal forms are used to ensure that various types of anomalies and
inconsistencies are not introduced into the database.
RDBMS stands for Relational Database Management System. RDBMS data is
structured in database tables, fields and records. Each RDBMS table consists of
database table rows. Each database table row consists of one or more database table
fields.
RDBMS store the data into collection of tables, which might be related by common
fields (database table columns). RDBMS also provide relational operators to
manipulate the data stored into the database tables. Most RDBMS use SQL as
database querylanguage.
The most popular RDBMS are MS SQL Server, DB2, Oracle and MySQL.
The relational model is an example of record-based model. Record based models are
so named because the database is structured in fixed format records of several types.
Each table contains records of a particular type. Each record type defines a fixed
number of fields, or attributes. The columns of the table correspond to the attributes of
the record types. The relational data model is the most widely used data model, and a
vast majority of current database systems are based on the relational model.
The relational model was designed by the IBM research scientist and mathematician,
Dr. E.F.Codd. Many modern DBMS do not conform to the Codd’s definition of a
RDBMS, but nonetheless they are still considered to be RDBMS.
Two of Dr.Codd’s main focal points when designing the relational model were to
further reduce data redundancy and to improve data integrity within database systems.
Oracle / 2
The relational model originated from a paper authored by Dr.codd entitled “A
Relational Model of Data for Large Shared Data Banks”, written in 1970. This paper
included the following concepts that apply to database management systems for
relational databases.
The relation is the only data structure used in the relational data model to represent
both entities and relationships between them.
Rows of the relation are referred to as tuples of the relation and columns are its
attributes. Each attribute of the column are drawn from the set of values known as
domain. The domain of an attribute contains the set of values that the attribute may
assume.
From the historical perspective, the relational data model is relatively new .The first
database systems were based on either network or hierarchical models .The relational
data model has established itself as the primary data model for commercial data
processing applications. Its success in this domain has led to its applications outside
data processing in systems for computer aided design and other environments.
A DBMS has to be persistent, that is it should be accessible when the program
created the data ceases to exist or even the application that created the data restarted.
A DBMS also has to provide some uniform methods independent of a specific
application for accessing the information that is stored.
RDBMS is a Relational Data Base Management System Relational DBMS. This adds
the additional condition that the system supports a tabular structure for the data, with
enforced relationships between the tables. This excludes the databases that don't
support a tabular structure or don't enforce relationships between tables.
You can say DBMS does not impose any constraints or security with regard to data
manipulation it is user or the programmer responsibility to ensure the ACID
PROPERTY of the database whereas the RDBMS is more with this regard because
RDBMS define the integrity constraint for the purpose of holding ACID PROPERTY.
1.1,1.2, and 1.3 Check your progress
Fill in the blanks
represent both entities and relationships between them.
State true or false
The normal forms never removes anomalies.
Each attribute of the column are drawn from the set of values known as domain.
The first database systems were based on either network or hierarchical models.
Most RDBMS use SQL as database query language.
Relational database design makes data retrieval difficult.
The goal of a relational database design is to generate a set of relation schema that
allows us to store information without unnecessary redundancy and also to retrieve
information easily.
A database system is an integrated collection of related files, along with details of
interpretation of the data contained therein. DBMS is a s/w system that allows access
to data contained in a database. The objective of the DBMS is to provide a convenient
and effective method of defining, storing and retrieving the information contained in the
database.
Oracle / 4
Data Manipulation & Control / 5
2 .0 Objectives
2 .1 Introduction
2 .2 Subdivisions of SQL
2 .3 Data Definition Language
2 .4 Data Manipulation Language Commands
2 .5 Data Control Language
2 .6 Select Query and Clauses
2 .7 Select Statement with Order by Clause
2 .8 Group by Clause
2 .9 Having Clause
2 .10 String Operation
2 .11 Distinct Rows
2 .12 Rename Operation
2 .13 Set Operations
2 .14 Aggregate Functions
2 .15 Nested Sub Queries
2 .16 Embedded SQL
2 .17 Dynamic SQL
2 .18 Summary
2 .19 Check Your Progress - Answers
2 .20 Questions for Self – Study
2.21 Suggested Readings
After reading this chapter you will able to
In this chapter we study the query language : Structured Query Language (SQL)
which uses a combination of Relational algebra and Relational calculus.
It is a data sub language used to organize, manage and retrieve data from
relational database, which is managed by Relational Database Management System
Vendors of DBMS like Oracle, IBM, DB2, Sybase, and Ingress use SQL as
programming language for their database.
SQL originated with the system R project in 1974 at IBM's San Jose Research
Centre.
Original version of SQL was SEQUEL which was an Application Program Interface
(API) to the system R project.
Data Manipulation & Control / 7
The SQL DDL provides commands for defining relation schemas, deleting
relations, creating indices, and modifying relation schemas.
The SQL DDL allows the specification of not only a set of relations but also
information about each relation including :
Domain/Data Types in SQL :
The SQL - 92 standard supports a variety of built-in domain types, including the
following :
(1) Numeric data types include
INT or INTEGER
FLOAT (n)
DECIMAL (i, j) or
DEC (i, j)
NUMERIC (i, j) or NUMBER (i, j)
where, i - the precision, is the total number of decimal digits
and j - the scale, is the number of digits after the decimal point.
The default for scale is zero and the default for precision is implementation
defined.
(2) Character string data types - are either fixed - length or varying - length.
CHAR (n) or CHARACTER (n) - is fixed length character string with user
specified length n.
VARCHAR (n) - is a variable length character string, with user - specified
maximum length n. The full form of CHARACTER VARYING (n), is
equivalent.
(3) Date and Time data types :
There are new data types for date and time in SQL-92.
DATE - It is a calendar date containing year, month and day typically in
the form
yyyy : mm : dd
TIME - It is the time of day, in hours, minutes and seconds, typically in the
form
Varying length character strings, date and time were not part of the SQL -
89 standard.
In this section we will study the three Data Definition Language Commands :
1. CREATE TABLE Command :
The CREATE TABLE COMMAND is used to specify a new relation by giving it a
name and specifying its attributes and constraints.
Oracle / 8
The attributes are specified first, and each attribute is given a name, a data type to
specify its domain of values and any attribute constraints such as NOT NULL. The key,
entity integrity and referential integrity constraints can be specified within the CREATE
TABLE statement, after the attributes are declared.
Syntax of create table command :
CREATE TABLE table_name (
Column_name 1 data type [NOT NULL],
Column_name n data_type [NOT NULL]);
The variables are defined as follows :
If NOT NULL is not specified, the column can have NULL values.
table_name - is the name for the table.
column_name 1 to column_name n - are the valid column names or attributes.
NOT NULL – It specifies that column is mandatory. This feature allows you to
prevent data from being entered into table without certain columns having data in
them.
Examples of CREATE TABLE Command :
(1) Create Table Employee
(E_name varchar2 (20) NOT NULL,
B_Date Date,
Salary Decimal (10, 12)
Address Varchar2 (50);
(2) Create Table Student
(Student_id Varchar2 (20) Not Null,
Last_Name Varchar2 (20) Not Null,
First_name Varchar2 (20),
BDate Date,
State Varchar2 (20),
City Varchar2 (20));
(3) Create Table Course
(Course_id Varchar2 (5),
Department_id Varchar2 (20),
Title Varchar2 (20),
Description Varchar2 (20));
Constraints in CREATE TABLE Command :
CREATE TABLE Command lets you enforce several kinds of constraints on a
table : primary key, foreign key and check condition, unique condition.
A constraint clause can constrain a single column or group of columns in a table.
There are two ways to specify constraints :
Clauses that constrain several columns are the table constraints.
The Primary Key :
A table's primary key is the set of columns that uniquely identifies each row in the
table. CREATE TABLE command specifies the primary key as follows :
create table table_name (
Column_name 1 data_type [not null],
Column_name n data type [NOT NULL],
[Constraint constraint_name]
Oracle / 10
Column_name N data type [NOT NULL],
[constraint constraint_name
Foreign key (column_name F1 … Column_name FN) references referenced-
table (column_name P1, … column_name P N
table_name - is the name for the table.
Column_name 1 through column_name N are the valid columns.
constraint_name is the name given to foreign key.
referenced_table - is the name of the table referenced by the foreign key declaration.
column_name F
1
through column_name F
N
are the columns that compose the foreign
key.
Column_name P 1
through column_name P N
are the columns that compose the primary
key in referenced-table.
Examples :
Create table_department
(Department_id Varchar2 (20),
Department_name Varchar2 (20),
Constraint PK_Department
Primary key (Department_id));
Create table_course
(Course_id Varchar2 (20),
Department_id Varchar2 (20),
Title Varchar2 (20),
Description Varchar2 (20),
Constraint PK_course
Primary key (Course_id, Department_id),
Constraint FK - course
Foreign key (Department_id) references Department (Department_id));
Thus, primary key of course table is (Course_id, Department_id).
The primary key of Department table is (Department_id).
Foreign key of course table is (Department_id) which references the department
table.
When you define a foreign key, the DBMS verifies the following :
(1) A primary key has been defined for table referenced by the foreign key.
(2) The number of columns composing the foreign key matches the number of
primary key columns in the referenced table.
(3) The datatype and width of each foreign key columns matches the datatype
and width of each primary key column in the referenced table.
Unique Constraint or Candidate key :
A candidate key is a combination of one or more columns, the values of which
uniquely identify each row of the table. Create table command specifies the unique
constraint as follows :
CREATE TABLE table_name
(column_name 1 data_type [NOT NULL],
column_name n data_type [NOT NULL],
[constraint constraint_name
Unique (Column_name A,……… Column_nameX)]);
Example :
Data Manipulation & Control / 11
Create table student
(Student_id Varchar2 (20),
Last_name Varchar2 (20), NOT NULL,
First_name Varchar2 (20), NOT NULL,
BDate Date,
State Varchar2 (20),
City Varchar2 (20),
Constraint UK-student
Unique (last_name, first_name),
Constraint PK-student
Primary key (Student_id));
A unique constraint is not a substitute for a primary key. Two differences between
primary key and unique constraints are :
(1) A table can have only one primary key, but it can have many unique
constraints.
(2) When a primary key is defined, the columns that compose the primary key are
automatically mandatory. When a unique constraint is declared, the columns
that compose the unique constraint are not automatically defined to be
mandatory, you must also specify that the column is NOT NULL.
Check Constraint :
Using CHECK constraint SQL can specify the data validation for column during
table creation. CHECK clause is a Boolean condition that is either TRUE or FALSE. If
the condition evaluates to TRUE, the column value is accepted by database, if the
condition evaluates to FALSE, database will return an error code.
The check constraint is declared in CREATE TABLE statement using the syntax :
Column_name datatype [constraint constraint_name] [CHECK (Condition)]
The variables are defined as follows :
Column_name - is the column name
data_type - is the column's data type
constraint_name - is the name given to check constraint condition is the legal
Condition that returns a Boolean value.
Examples :
Create table_worker
(NameVarchar2 (25) NOT NULL,
Age Number Constraint CK_worker
CHECK (Age Between 18 AND 65) );
Create table_instructor
(Instructor_id Varchar2 (20),
Department_id Varchar2 (20) NOT NULL,
Name Varchar2 (25),
Position Varchar2 (25)
Constraint CK_instructor
CHECK (Position in ('ASSISTANT PROFESSOR', 'ASSOCIATE PROFESSOR', 'PROFESSOR')),
Address Varchar2 (25),
Constraint PK_instructor
Primary key (Instructor_id));
If the position of the instructor is not one of the three legal values, DBMS will
return an error code indicating that a check constraint has been violated.
More than one column can have check constraint.
Create table_Patient
Data Manipulation & Control / 13
No_of_student Number (3));
SQL> Describe Department;
Name Null Type
Department_id Varachar2 (20)
Department_Name Varachar2 (20)
University Varachar2 (20)
No_of_student Varachar2 (20)
(2) Modify an existing column in the existing table.
ALTER TABLE table_name
MODIFY (Column_name datatype : constraint,
… Column_name datatype : constraint,);
A column in the table can be modified in following ways -
(i) Changing a column definition from NOT NULL to NULL i.e. from
mandatory to optional
Consider a table ex_table.
SQL> describe ex_table;
Name NULL? Type
Record_no NOTNULL Numbers (38)
Description Varchar2 (40)
Current_value NOT NULL Number
SQL> Alter Table ex_table;
modify (current_value number Null);
Table altered
SQL> Describe ex_table;
Name NULL? Type
Record_No NOT NULL Number (38)
Description Varchar2 (40)
Current_value Number
(ii) Changing a column definition from NULL to NOT NULL.
If a table is empty, you can define a column to be NOT NULL. However, if
table is not empty, you cannot change a column to NOT NULL unless every row in the
table has a value for that particular column.
(iii) Increasing and Decreasing a Column's Width :
You can increase a character column's width and can increase the number of
digits in a number column at any time.
Example :
SQL> Describe ex_table;
Name NULL? Type
Record_No NOT NULL Number (38)
Description Varchar2 (40)
Current_value NOT NULL Number
SQL> Alter table ex_table
modify (Description Varchar2 (50));
Table altered
SQL> Describe ex_table;
Name NULL? Type
Record_No NOT NULL Number (38)
Description Varchar2 (50)
Current_value NOT NULL Number
You can decrease a column's width only if the table is empty or if that column
is NULL for every row of table.
(3) Adding a constraint to an existing table :
Any constraint i.e. a primary key, foreign key, unique key or check
constraint can be added to an existing table using ALTER TABLE command.
ALTER TABLE table_name
Oracle / 14
ADD (constraint)
Example :
SQL> Create Table ex_table
(Record_No Number (38),
Description Varchar2 (40),
Current_value Number);
Table created
SQL> Alter Table ex_table add
(Constraint PK_ex_table primary key (Record-No));
Table Altered.
(4) Dropping the constraints
ALTER TABLE table_name
DROP Primary key
Using this you can drop primary key of table.
ALTER TABLE Table_name
DROP constraint constraint_name
Using this you can drop any constraint of the table.
Rules for adding or modifying a column :
Following are the rules for adding column to a table :
(1) You may add a column at any time if NOT NULL is not specified.
(2) You may add a NOT NULL column in three steps :
(i) Add a column without NOT NULL specified,
(ii) Fill every row in that column with data,
(iii) Modify the column to be NOT NULL.
Following are the rules to modify a column.
(1) You can increase a character column's width at any time.
(2) You can increase the number of digits in a NUMBER column at any time.
(3) You can increase or decrease the number of places in a NUMBER column
at any time.
If a column is NULL for every row of the table, you can make following
changes.
(i) You can change its data type
(ii) You can decrease a character column's width
(iii) You can decrease the number of digits in a NUMBER column.
3. DROP TABLE Command :
Dropping a table means to remove the table's definition from the database.
DROP TABLE command is used to drop the table as follows :
DROP TABLE table_name;
Example :
(1)SQL > Drop table_student;
Table dropped
(2)SQL > Drop table instructor;
Table dropped.
You drop a table only when you no longer need it.
Note : The truncate command in ORACLE can also be used to remove only
the rows or data in the table and not the table definition.
Example :
Truncate student
Table truncated
Truncating cannot be rolled back.
The SQL DML includes commands to insert tuples into database, to delete tuples from
database and to modify tuples in the database.
Oracle / 16
increase, and the INSERT fails as a result.
the table.
2. DELETE Command :
The syntax of delete statement is :
DELETE FROM table_name
[WHERE condition]
The variables are defined as follows :
table_name - is the table to be updated.
condition - is a valid SQL condition.
DELETE Command without WHERE clause will empty the table
completely.
Example :
SQL> Delete from Student
Where Student_id = 'SE 201';
1 row deleted.
SQL> Detete from student
Where first_name = 'Sachin' and
Student_id ='SE 202';
1 row deleted.
3. UPDATE Command :
If you want to modify existing data in the database, UPDATE command can be
used to do that. With this statement you can update zero or more rows in a table.
The syntax of UPDATE command is :
UPDATE table_name
SET column_name : : expression
[, column_name : : expression]
[, column_name : : expression]
[where condition]
The variables are defined as follows :
table_name is the table to be updated
column_name is a column in the table being updated.
expression is a valid SQL expression.
condition is a valid SQL condition.
The UPDATE statement references a single table and assigns an expression to at
least one column. The WHERE clause is optional; if an UPDATE statement does not
contain a WHERE clause, the assignment of a value to a column will be applied to all
rows in the table.
Example :
SQL> Update Student
Set
City = 'Pune',
State = 'Maharashtra';
SQL> Update Instructor
Set
Position = 'Professor'
where
Instructor_id = 'P3021';
SQL Grammar :
Here, are some grammatical requirements to keep in mind when you are working
with SQL.
Data Manipulation & Control / 17
clarity.
referencing SQL keywords (Such as SELECT and INSERT), table names, and column
names.
However, case does matter when referencing to the contents of a column.
For Example : If you ask for all customers whose last names begin with 'a' and all
customer names are stored in uppercase, you won't receive any rows at all.
4. SELECT Command :
The basic structure of an SQL expression consists of three clauses :
select, from and where
algebra.
It is used to list the attributes desired in the result of a query.
relational algebra. It lists the relations to be scanned in the elevation of the
expression.
algebra.
It consists of predicate involving attributes of the relations that appear in the
from clause.
Simple SQL query i.e. select statement has the form :
select A
1
2
n
from r
1
, r
2
, ……, r
m
where P.
The variables are defined as follows :
1
2
n
represent the attributes.
r 1
, r 2
, …, r m
represent the relations from which the attributes are selected.
P - is the predicate.
This query is equivalent to the relational algebra expression
1
2
n
(s p
(r
1
× r
2
× r
3
… × r
m
where clause is optional. If the where clause is omitted, the predicate P is true.
Select clause forms the cartesian product of relations named in the from clause,
performs a relational algebra selection using the where clause and then projects the
results onto the attributes of the select clause.
A simple select statement :
At a minimum, select statement contains the following two elements.
Example : Consider the student database table.
(1) A simple select statement - a query that retrieves only student_id from the
student table is given
SQL> select student_id
from student;
student_id
Data Manipulation & Control / 19
SQL> Select Ename, Salary + 10000
from Employee;
E_name Salary + 10000
Sachin 20,
Rahul 30,
Ajay 20,
Anil 30,
4 rows selected.
Select statement using where clause :
select and from clauses provide you with either some columns and all rows or all
columns and all rows. But if you want only certain rows, you need to add another
clause, the where clause.
where clause consists of one or more conditions that must be satisfied before a
row is retrieved by the query.
It searches for a condition and narrows your selection of data.
For example, consider select statement with where clause given below :
SQL> Select Student_id, First_name
from Student
where Student_id = 'S10234';
Student_id First_name
S10234 Sunil
1 row selected
SQL> Select E_name Salary
from Employee
where Salary > 10000;
E_name Salary
Rahul 20000
Anil 20000
2 row selected
where uses the logical connectives : and, or and not.
where clause uses the comparison operators
Description Operator
Less than
Less than or equal to
Greater than
Greater than or equal to
Equal to
Not equal to
!= or < >
SQL> Select E_name, Salary
from Employee
where Salary > 10000 and E_name = Anil
Ename Salary
Anil 20000
1 row selected.
5. Views in SQL :
A view in SQL terminology is a single table that is derived from other tables. These
other tables could be base tables or previously defined views. A view does not
necessarily exist in physical form; it is considered a virtual table in contrast to base
tables whose tuples are actually stored in the database. This limits the possible update
operations that can be applied to views but does not provide any limitations on
querying a view. We can think of view as a way specifying a table that we need not
exist physically.
Oracle / 20
Specification of Views in SQL :
The command to specify a view is CREATE VIEW. 'We give the view a table
name, a list of attribute names, and a query to specify the contents of view. If none of
the view attributes result from applying functions or arithmetic operations, we do not
have to specify attribute names for the view as they will be the same as the names of
the attributes of the defining tables.
Example :
Consider the following relation scheme and corresponding relation.
employee_schema (emp_name, street, city)
works_schema (emp_name, comp_name, salary
company_schema (comp_name, city)
emp_name street city
Sachin
Rahul
Raj
Ajay
Anil
Sunil
Pune
Bombay
Pune
Bombay
Delhi
Bombay
emp_name Comp_name salary
Sachin
Rahul
Raj
Ajay
Anil
Sunil
Comp_name city
Delhi
Bombay
Pune
Create view emp_detail (emp, comp, street, city)
As select C.emp_name, C.comp_name, E.street, E.city
from Employee E.company C
where E.emp_name = C.emp_name;
A view is always up date; if we modify the base tables on which the view is
defined, the view automatically reflects these changes. Hence, the view is not
realized at the time of view definition but rather at the time we specify a query on
the view. It is the responsibility of the DBMS and not the user to make sure that
the view is up to date.
If we do not need a view any more, we can use the DROP VIEW command to
dispose of it.
Drop View emp_detail;
Updating of views :
(1) A view with a single defining table is up datable if the view attributes
contain the primary key or some other candidate key of the base relation,
because this maps each view tuple to a single base tuple.
(2) Views defined on multiple tables using joins are generally not updatable.
(3) Views defined using grouping and aggregate functions are not updatable.