SCAX1001 Course Name: Computer Applications in Business Chapter Name: Rdbms Subject Coordinator: Mrs - Jancy
SCAX1001 Course Name: Computer Applications in Business Chapter Name: Rdbms Subject Coordinator: Mrs - Jancy
Unit –V
Relational Database Management System (RDBMS)
One of the most popular types of database. It was developed by Dr. EF CODD at IBM in the late
1960s.
In our system keeping computerized records, user can store view, change, and retrieve data in a
standardized way. One of the standardized way is RDBMS.
RDBMS is based on “table” which is also called “relations”.
These tables are made up of rows and columns.
Each table in the database contains only a subset of information.
A different table contains different information.
Terminology (Important Terms)
Data: Raw facts. Collection of information.
Database: Collection of related data stored in standard format, designed to be processed, shared by
different users.
Table: Collection of data arranged in rows and columns.
Row: In datatable, data laid out in rows are horizontal. go from left to right.
Column: In datatable, data laid out in rows are vertical. go from top to bottom.
Attibutes (Fields): Smallest units of information user can access are placed in a column.
Record: Complete set of information. Records are placed in a row.
Cell: In database table, each cell is identified by a combination of “row” and column coordinates to
which it belongs.
Frontend (Client side): Interact with the database by requesting and receiving information from the
database server.
Backend (Server side):
Interface between the server and database.
It is used to manage the database among multiple clients.
Database System
Two parts
1. Database management System
1
DBMS is the program that organizes and maintains information.
2. Database Application
DBA is the program that lets us view, retrieve and update information stored in DBMS.
Data Types
RDBMS supports various data types
NUMBER: The syntax for NUMBER is NUMBER (P, S) p is the precision and s is the scale. P can range from 1
to 38 and s from -84 to 127
Character: CHAR (len) is fixed length character strings
Fixed length character data of length len bytes. This should be used for fixed length data.
Varchar2 (len) Variable length character strings Variable length character string having maximum length len
bytes. We must specify the size.
Date –Dates 12-mar-03.
Numeric: 21, -32, $0.75, 1.2E4.
String: enclosed within ‘…’
Operators
1. Arithmetic Operators (+, -, *, /).
- Used to do Arithmetic operation for addition, subtraction, multiplication and division.
2. Logical Operators (AND, OR, NOT).
- Used to combine one or more conditions.
3. Relational Operators (=, <=, >=, <>).
- used to compare the values.
DDL Commands
Data Definition Language (DDL).
Used to define the database structure or schema.
Commands are
1. CREATE
This command is used to create a table in the database.
Syntax:
create table tablename(Fieldname1 datatype (width),…………… Fieldnamen datatype
(width));
Eg:
create table student(stuname varchar(30),sturno number(10));
Table created.
2
2. ALTER
This command is used to add and modify the existing column definition.
Add – Used to add a new column in existing table.
Modify – Used to modify the datatype and width in existing table.
Syntax:
alter table tablename add(new fieldname datatype(width));
Eg:
alter table student add(sturegno number(10) );
Table Altered.
Syntax:
alter table tablename modify (oldfieldname newdatatype (width));
Eg:
alter table student modify(sturegno number(20) );
Table Altered.
3. DROP
Delete table from the database.
Syntax: drop table tablename;
Eg:
drop table student;
Table Dropped.
4. TRUNCATE
Remove all records from a table including all spaces.
5. COMMENT
Add comment to the data dictionary.
6. RENAME
Rename an Object (Rename the Table).
DML Commands
3
Syntax: Select * from tablename;
b) Selecting specific column
Syntax: Select * from tablename where condition;
Eg:
Select * from student;
Select * from student where sturno=101;
2. INSERT
Insert command is used to add one or more rows to a table.
The values must be entered in the same order as they are defined in the table.
Syntax:
a) Inserting only one record into table
Syntax:
insert into tablename values (value1, value2,…,value n);
b) Inserting more than one record into table
Syntax:
insert into tablename values (&Fieldname1, &Fieldname2,…….&Fieldname);
Eg:
insert into student values(‘joy’,101,301);
insert into student values(&stuname ,&sturno,&sturegno);
3. UPDATE
Updates existing data with in a table.
Update command is used to alter the column values in a table, it consist of ‘set’
clause, used to set each field with some values and also includes optional ‘where’
clause.
Syntax:
update tablename set fieldname=value where condition;
Eg:
update student set sturegno=104 where sturno=101;
4. DELETE
Delete all records from a table, the space for the records remains.
To delete specific rows ‘where’ condition is used.
a) Delete all records
Syntax:
4
Delete from tablename;
b) Deleting selected records
Syntax:
delete from tablename where condition;
Eg:
delete from student;
delete from student where sturegno=301;
TCL Commands
Transaction control language.
Used to manage the changes made by DML statements.
It allows statements to be grouped together into logical transactions.
Commands are
1. Commit
We want to save transaction we can use this command.
Used to save work done.
Syntax:
commit;
2. Rollback
Restore database to original since the last commit.
Its similar to undo commands.
Syntax:
rollback;
3. Save point
It is a way of implementing sub transaction with in a RDBMS.
Save point identify a point in a transaction to which you can later “roll back to” without
affecting any work done in the transaction before the savepoint was created.
Useful for implementing complex error recovery in database applications
Syntax:
savepoint name;
rollback to name;
Eg: Using DDL, DML and TCL Commands
Create a table employee
Query:
5
create table employee (eno number (4), ename varchar2(25), salary number(10,2), designation
varchar2(20),DOJ date);
Table Created
Modify the width value in eno
Query:
alter table employee modify(eno number(8));
- It has modified by width from number (4) to number (8).
Insert a 3 record into employee table
Query:
insert into employee values (1001, ‘Ram’,10000,’programmer’,’10-jun-07’);
insert into employee values (1002, ‘Sam’,20000,’programmer’,’14-jul-08’);
insert into employee values (1003, ‘Jim’,30000,’projectMgr’,’10-jan-06’);
6
Display the employee information
select * from employee;
eno ename salary designation DOJ
1002 Sam 20000 programmer 14-jul-08
1003 Jim 30000 projectMgr 10-jan-06
Rollback Operation
rollback to ta;
After rollback to table employee now user will get original table
Display the employee information
select * from employee;
eno ename salary designation DOJ
1001 Ram 10000 programmer 10-jun-07
1002 Sam 20000 programmer 14-jul-08
1003 Jim 30000 projectMgr 10-jan-06
WHERE, ALL, DISTINCT, BETWEEN, IN, LIKE, ORDERBY
7
select * from tablename where columnname Like ‘a%’;
- % sign can be used to define wildcard (missing letter in the pattern) both before and after the
pattern.
‘a%’ – start with alphabet ’a’
‘%a’ – ends with alphabet ’a’
‘%a%’ – any where (middle).
1. Select name percentage of all students with percentage between 85 & 90.
Query:
Select sname, per from college where per Between 85 And 90;
Output:
sname per
AAA 88
ABC 85
2. Select name, department of all students whose department in MBA and MCA.
Query:
Select sname, dept from college where Dept In(‘MBA’,’MCA’) ;
Output:
sname dept
AAA MBA
8
ABC MCA
ABC MBA
9
Select All (sname) from college;
Output:
sname
AAA
ABC
BBB
ABC
AAA
ABC
BBB
Aggregate Functions
SQL - Aggregate functions
Aggregate functions work on all the rows of the table taken as a group (based on some condition
optionally).
Used when information you want to extract from a table has to do with the data in the entire table
taken as a set.
Aggregate functions are used in place of column names in the SELECT Statement.
The aggregate functions are :
SUM ( ), AVG ( ), MAX ( ), MIN ( ), COUNT ( )
Aggregate function - SUM
Adds up the values in the specified column.
Column must be numeric data type.
Value of the sum must be within the range of that data type.
Syntax:
select sum(columnname)from tablename where condition;
Aggregate function – AVG
10
Returns the average of all the values in the specified column.
Column must be numeric data type.
Syntax:
select avg(columnname)from tablename where condition;
Aggregate function - MAX
Returns the largest value that occurs in the specified column.
Syntax:
select max (columnname)from tablename where condition;
Aggregate function - MIN
Returns the smallest value that occurs in the specified column.
Syntax:
select min(columnname)from tablename where condition;
Aggregate function – COUNT
Returns the number of rows in the table.
Syntax:
select count(columnname)from tablename;
(or)
count(*) returns Number of rows
Using two or more aggregate functions:
Syntax:
select min(columnname), max(columnname) from tablename where condition;
Example:
Library Table
Bname Bcode Price
Clm 101 250
Java 102 300
Dbms 103 200
Oracle 104 500
11
312.5
3. select max(Price)from Library;
Output:
500
4. select min(Price)from Library;
Output:
200
5. select count(*)from Library;
Output:
4
Set Operators
Syntax:
select columnname from tablename1 union select columnname from tablename2;
select columnname from tablename1 unionall select columnname from tablename2;
The results of two independent SELECT statements can be worked with using the SET operation
UNION/ UNIONALL. By default, UNION returns only distinct values. Union is like an “OR” operation. If the tuple
occurs in relation 1 or relation 2, it is selected.
12
An intersection is an AND operation. It retrieves those tuples which are present in both relation A
and B
Syntax:
select columnname from tablename1 intersect select columnname from tablename2;
Retrieval of records using Minus
This is the difference operation. It retrieves tuples which are present in relation 1 but not in relation 2.
Syntax:
select columnname from tablename1 Minus select columnname from tablename2;
Example:
Customercity
Name Id City
AAA 101 Chennai
BBB 102 Mumbai
CCC 103 Bangalore
Suppliercity
Name Id City
XXX 101 Chennai
YYY 102 Delhi
ZZZ 103 Chennai
13
Output:
City
Chennai
Mumbai
Bangalore
Delhi
2. select City from Customercity unionall select City from Suppliercity;
Output:
City
Chennai
Mumbai
Bangalore
Chennai
Delhi
Chennai
ASSIGNMENT QUESTIONS
14
A. DDL OPERATION TO BE PERFORMED
1. In a single SQL statement, create a table called QUESTION. It should have four columns:
--Email. Datatype: varchar2. Width: 35. Cannot be null.
--RecDate. Datatype: date. Cannot be null.
--Name. Datatype: varchar2. Width: 50. Can be null.
--Question. Datatype: varchar2. Width: 500. Cannot be null.
2. Now use an ALTER statement to add an alternate Email address with varchar data type.
3. View the resulting table schema with "desc".
4. Alter statement to modify the Data types for Name field.
5. View the table schema again to show the results of your changes.
B. DML OPERATIONS TO BE PERFORMED:
1. To create employee table with ename, eid, doj, basicpay, age, address fields.
2. Insert five records to the employee database.
3. Increment the basic pay of all the employees by 5% of their basic pay.
4. Delete the third employee record from the table.
C. DQL OPERATIONS TO BE PERFORMED:
Create a student detail database with roll number, name, date of birth, course, department, semester,
percentage. Insert 5 records into the database and perform the following operations.
1. Select all Records.
2. Select name, percentage from the records.
3. Select distinct course.
4. Select details of all students with percentage greater than 75.
5. Select roll number and name of all records sorted in order by percentage.
6. Select name of students belonging to 3rd semester MBA degree.
15
Create a library database with book name, author name, access code, date of access, publisher name,
and price as the fields.
i. Select the average price of all books in the library database.
ii. Select the name of the book with maximum price in the given database.
iii. Select the book with minimum price.
iv. Count the number of records in the library database.
v. Print the total (total cost of all the books) estimate of the library book.
REFERENCE LINK
www.tutorialspoint.com/sql/sql-rdbms-concept.html
www.studytonight.com/dbms/rdbms-concept
16