You are on page 1of 17

DBMS Defintion, Objective And Applications

Database: Collection of interrelated data.


example: Mark sheet and student records
DBMS: A Database Management system is a collection of interrelated data and set of
programs to access the data.
Objective of DBMS: To provide a convenient environment to retrieve and store database
information. Database systems can support single user and multiuser environment.
Database Application: Database application is the program that is used to view, retrieve and
update information stored in the database. The following are the few applications of
Database.
* Bank database
* University database
* Railway database etc.,

DBMS Services

DBMS offers following services


Data Definition: It is a method of defining data and storage.
Data Maintaenance: It checks whether each record has field containing information for a
particular record or not.
example: In an employee table information about the employee like employee id, name,
designation, salary recorded.
Data Manipulation: It allows data in the database to be inserted, updated, deleted and
sorted.
Data Display: It helps in viewing data.
Data Integrity: It ensures the accuracy of the data.
Relational Database Management System[RDBMS]:An RDBMS is a computer program, which
provides the user the facility to store and retrieve data in a manner consistant with a
defined model called relational model.
Relation -> table;
tuple -> Row, Record;
Cardinality -> No.of rows; Attribute -> column, field
Degree -> No.of columns; Primary Key -> unique identifier
Domain -> Set of legal values;
Introduction to oracle9i:
* Oracle9i is an Object Relational Database Management System (ORDBMS).
* It offers capabilities of relational and object oriented database system.
* Oracle products are based on client/server technology.
* In this technology server performsall activities related to the database and the client
performs activities that help the user to interact with the application.
Features of oracle9i:
* Database performance
* Ease of Management
* Scalability
* Security
* Availability
* Internet content Management
* e-commerce Integration
* Packaged Applications
* Business Intelligence

Tools Of Oracle
*
*
*
*

SQL *Plus
PL/SQL
Forms
Reports

SQL *Plus: SQL *Plus is structured query language supported by Oracle. Through SQL *Plus
we can store, retrieve, edit and run SQL commands and PL/SQL blocks. Using SQL *Plus we
can perform calculations, list column definitions for any table and can also format query
results in the form of a report.
PL/SQL: PL/SQL is an extension of SQL. PL/SQL block can contain any number of SQL
statements integrated with flow of control statements. PL/SQL is a procedural language for
the oracle server and its client. In a PL/SQL program,SQL statements are used for data
manipulation and transaction processing.
Forms: Form is a graphical tool used for generating and executing forms based applications.
A form basically consists of block and fields. Multiple tables can be accesed over a single
form. Oracle form builder is the design component of oracle forms.
Reports: It is an application development tool of oracle for developing, executing, displaying
and printing reports. We can create wide variety of reports, which have various modes.
Oracle reports are powerful and easy to use.

Oracle Data types


NUMBER:
NUMBER datatype is used to store fixed-point or floating-point numbers
We can specify precision, which is the total number of digits and scale which is the number
of digits to the right of the decimal point.
syntax: NUMBER(precision,scale)
NUMBER subtypes
DEC
DECIMAL
INTEGER
NUMERIC
CHAR:
Character types allow us to store alphanumeric data, represent words and text and
manipulate character strings.
CHAR: CHAR datatype is used to store fixed-length character data. represented internally
depends on the database character set. The CHAR datatype an optional parameter that lets
us specify a maximum length upto 32767 bytes.
syntax:char(max_length);
VARCHAR2: VARCHAR2 datatype is used to store variable-length character data.
Data represented internally depends on the database character set. The varchar2
datatype takes a required parameter that specifies a maximum length up to 32767
bytes.
syntax:varchar2(max_length);
DATE:
DATE:DATE datatype is used to store fixed-length date/time values. DATE values
include the time of day in seconds since midnight. The date portion defaults to
the first day of the current month, the time portion defaults to midnight. THe
date function SYSDATE returns the current date and time.
Data Definition Language(DDL)
The Data Definition Language is used to create, remove and alter the structure of database
objects. The DDL is used for table definition and can be classified into 4 categories.
(i) create table command
(ii) alter table command
(iii) truncate table command
(iv) drop table command
Data Manipulation Language(DML)
Data Manipulation commands are used to query and manipulate existing database objects.
Following are some DML commands(i) Insert
(ii) select
(iii) update
(iv) delete

Data Control Language(DCL)


Data Control Language is used to control the kind of a data access to the database and
transaction control. The owner can allow other database users access to the database
objects as per his/her direction.
Eg:

grant, revoke are used for data access control.


commit, rollback and save point are transaction control commands.

Creating table in Oracle


Rules for
* Names
* Names
* Names
* Names
* Names
* Names

table and column names


can be upto 30 characters.
must begin with an alphabet.
cannot contain quotes.
are not case sensitive.
can contain characters a-z, 0-9, _, $ and #.
cannot be reserve words.

Create table statement:


The create table statement can be used to create a new table.
Syntax:
CREATE TABLE tablename (column1 datatype(width),
column2 datatype(width),
column3 datatype(width),
.
.
column-n datatype(width));
Example:
create a table 'student' with the following fields
'Roll_no', 'Name', 'class', 'DOB' and 'Address'
CREATE TABLE student (Roll_no varchar2(20),
Name varchar2(20),
class varchar2(20),
DOB date,
Address varchar2(100));

Inserting Rows in a table


Insert into statement
The insert into statement is used to insert new rows into a table. we can use insert into
statement in three ways.
* We can insert values into the table directly by using the following syntax.
Syntax:
INSERT INTO tablename VALUES(value1,value2,....value-n);
Example:
insert into student values(101,'abc','BCom','03-03-90','Hyderabad');
* We can also specify the columns for which we want to insert the data.
Syntax:
INSERT INTO tablename (column1,column2) values(value1,value2);
Example:
insert into student(Roll_no, name) values(102,'def');
* We can add fields interactively by using the following syntax.
Syntax:
insert into < values('&Roll_no','&Name','&class','&DOB','&Address');

Selecting Rows from the table


Select statement:
Select statement can be used to view the data from the table.
We can use the select statement in the following situations.
* To view the data of only some columns in the table.
* To view the data of all columns in the table.
* To view the distinct values in the table.
* select statement with where clause
* syntax To view some columns:
select column-1,column-2 from <tablename>
Example:
select name, dob, address from student;
* Syntax to view all columns:
select * from <tablename>;
Example:
select * from student; [here * means all]
* Syntax to view distinct values:
The distinct keyword is used to select different values from the table.
Means the distinct keyword doesnot display the repeated data in the table.
select distinct from <tablename>;
Example:
select distinct name from student;
Here, repeated names wont be displayed.
* select statement with where clause
The optional where clause has the following syntax
where <boolean-exp>

Here, the <boolean-exp> has two forms

1. exp operator exp


2. Logical Operator exp
Here, operators are =,<,<=,>,>=,<>
logical operators are And,Or,Not
Example:
select * from student where name='abc';

Constraints
Constraints are rules that are used to control the invalid data entry in a column. Constraints
are

Primary key

Foreign key

Null and NotNull

Unique

Check

Default

Primary Key
The primary key has two features
1. It defines the column as manadatory i.e column cannot be left blank.
2. The data held across the column must be unique i.e values should not be repeated.
* A single column primary key is known as simple primary key.
* Multi column primary key is known as composite primary key.
Note: We cannot define more than one primary key in a single table. We can define primary
key for multi columns by using composite primary key.
Example on simple primary key:
create a table 'customer' with the following fields
cust_no as primary key, name, address, city, state, pin.
create table customer(cust_no number(10) primary key,
name varchar2(50),
address varchar2(50),
city varchar2(50),
state varchar2(50),
pin number(6));
Example on composite primary key:
create table product(order_no number(10),
prod_no varchar2(20),
qty number(5),
price number(8,3)
primary key(order_no,prod_no));

Null:
If in a record any field that is created as nullable(not having value) then oracle will replace a
null value in that column. Null value is not equivalent to zero or blank. Null values can be
inserted into the columns of any datatype.
Not Null:
The not null constraint ensures that the users always type the value for that column
becomes a manadatory column. Not null constraint can be applied at column level only.

Unique Key

* unique key constraint ensures that information in the columns is unique i.e
in unique column must not be repeated across the column(s).

value entered

* A table may have more than one unique key. Unique key may be null at the time
entry. So, value is not manadatory in unique key column.
* We can define a single column as unique and not null both then the constraint
as primary key.

of data
will work

Check
Check constraint ensure that when data is entered, the data in the column is limited to
specific values.
eg: category varchar2(5) check(category in ('sc','st'));
Default Constraint:
At the time of table creation a default value can be assigned to a column. When the user is
entering the values and leaves this column empty the oracle will auotmatically load this
column with the default value. The datatype of the default value should match the datatype
of the column.
eg: age number(3) default 21;

Update Statement
The update statement is used to update or change records that match a specified criteria.
This is accomplished by carefully constructing a where clause.
syntax: update <tablename> set columnname=newvalue, columnname1=newvalue1,...
where <condition>
eg: update employee set phoneno='9884532245' where ename='abc';

Alter
The ALTER command is used to modify the structure of a table. Using ALTER command, we
can add new columns, modify existing columns, and add or drop integrity constraints.
ALTER can be used to make the following changes to any tables
* Add new columns
* Add new integrity constraints
* Modifying existing columns
-Expand length
-Change default
-Decrease length -All values in column must be null
-Change data type -All values in column must be null
* Drop integrity Constraints
Alter Syntaxes
To add a new column
syntax: alter table <tablename> add(new_colname datatype(width));
example: alter table employee add(address varchar2(10));
To change the width of the exisiting field
syntax: alter table <table_name> modify(col_name datatype(width));
example: alter table employee modify(address varchar2(50));
To rename the column
syntax: alter table <tablename> rename column old_col to new_col;

example: alter table employee rename empname to employename;


Adding Many columns in a single alter statement
syntax: alter table <table_name> add(col1 datatype(width),
col2 datatype(width)..
coln datatype(width));
example: alter table employee add(address varchar2(20), city varchar2(20),
dob date);
To add a primary key
syntax: alter table <table_name> add primary key(field);
example: alter table employee add primary key(empno);
To remove a primary key
syntax:alter table <table_name> drop primary key;
example:alter table employee drop primary key;

employee table
1. Create a table 'employee' with the following fields
Empno
varchar2
6(primary key)
Ename
varchar2
20(not null)
Job
char 10
Hiredate
date
basic_sal
number
(9,2)
comm
number
(7,2)
dept_no
varchar2
2
Q: create table employee(empno varchar2(6) primary key,
ename varchar2(20) not null,
job char(10),
hiredate date,
basic_sal number(9,2),
comm number(7,2),
dept_no varchar2(2));
2. Describe the table 'employee'
Q: desc employee
3. Insert the following data into the above 'employee' table
empno

ename

job

hiredate

basic_sal

comm

dept_no

E0001

kim

Manager

15-dec-02

5000

500

D001

E0002

Bruce

Analyst

24-Apr-99

4000

400

D002

E0003

Arnold

Clerk

10-jan-01

2500

250

D004

E0004

Holyfield

Tester

10-oct-01

3000

300

D002

E0005

kelly

Admin

11-apr-99

2000

200

D003

Q:
insert
insert
insert
insert
insert

into
into
into
into
into

employee
employee
employee
employee
employee

values('E0001','Kim','Manager', '15-Dec-02','5000.00','500','D001');
values('E0002','Bruce','Analyst', '24-Apr-99','4000','400','D002');
values('E0003','Arnold','Clerk', '10-jan-01','2500','250','D004');
values('E0004','Holyfield','Tester', '10-oct-01','3000','300','D002');
values('E0005','Kelly','Admin', '11-Apr-99','2000','200','D003');

Select Command And Like Operator


I. employee table
1. List the names of the employee who have a salary less than Rs.3000 from employee table
Q. select ename from employee where basic_sal < 3000
3. List all the employees whose name starts with the letter 'K' from employee table.
Q. select ename from employee where ename like 'K%';
4. List the employee name working in department D002, D003 from employee table.
Q. select ename from employee where dept_no='D002' or dept_no='D003';
5. List all employee whose name start with 'A' and end with 'd' from employee table.
Q. select ename from employee where ename like 'A%d';
6. List all managers and salesman with salary over 2500 from employee table.
Q. select ename from employee where (job='Manager' or job='Sales man') and
basic_sal>2500;
7. Display all the employee names in the ascending order of their date of joining from
employee table.
Q. select ename from employee order by hiredate asc;
8. Display all the employees in alphabetical order from employee table.
Q. select ename from employee order by ename;
9. List all employee who where hired during 1999 from employee table.
Q. select ename from employee where to_char(hiredate,'yy')=99;
10. List all employees whose commission is more than Rs.300 from employee table.
Q. select ename from employee where comm>300;
II. client_mast table
1. Find out the names of all the clients from client_mast table.
Q. select name from client_mast;
2. Retrieve all the records from client_mast table
Q. select * from client_mast;
3. Retrieve all the list of names, address and city of all the clients from client_mast
Q. select name,address,city from client_mast;
4. List all the clients who are staying in Florida from client_mast table
Q. select name from client_mast where state="Florida";
III. Dept table
1. List the department name which is located in Noida and Rockey Creek from Dept table.
Q. select dname from depttable where loc='Noida' or loc='Rocky Creek';

Update Command
I. employee table
1. Change the basic salary Rs.3000 where basic salary less than 2500 from employee table
Q. update employee set basic_sal=3000 where basic_sal<2500;
2. Change the basic_sal=3000 where job in clerk from employee table.
Q. update employee set basic_sal=3000 where job='Clerk';
3. Change the basic_salary of employee number E004 to Rs. 3500 from employee table.
Q. update employee set basic_sal=3500 where empno='E0004';

Delete, Commit, Rollback and Renaming a Table


1. Delete all salesman from the salesman_mast whose salaries are equal to Rs.2000.
Q. delete from salesman_mast where sal_amount=2000;
2. Write a query to undo the above delete query.
Q. rollback;
3. Delete all products from product_mast where the Qty_Available is equal to 100.
Q. delete from product_mast where qty_available=100 ;
4. Write a query such that the above query cannot be undone.
Q. commit;
5. Delete from client_mast where the state venue is "Illinois".
Q. delete from client_mast where state='Illinois';
6. Delete all employees from employee where basic_sal is less than 2000.
Q. delete from employee where basic_sal<2000;
7. Write a query to mark the above two queries such that the first delete operation is undo
Q. savepoint d1;
delete from client_mast where state='Illinois';
rollback to d1;
8. Delete employee from the employee table where basic_sal is equal to Rs.3000 and
job='clerk'.
Q. delete from employee where basic_sal=3000 and job='Clerk';
9. Delete employee from the employee table where job='clerk' or manager.
Q. delete from employee where job='Clerk' or job='Manager';
10. Delete all records from depttable table.
Q. delete from depttable;
11. Delete all managers and salesman with salary over 1500 from employee table.
Q. delete from employee where job='Manager' or job='salesman' and basic_sal>1500;

13. Write a query so the the following statement can be delivered 'Kim is
working as manager since 15-12-2002' where employee number is E0001.
Q. select ename || ' is working as ' || job ||'since'
|| hiredate from employee where ename='Kim';
14. Rename the table name from client_mast to T_client_mast.

Q. rename client_mast to T_client_mast;


15. Write a query the column name 'name' should be displayed as employee name
from the employee table.
Q. select ename as "Name" from employee;

Alter Command
1. Add a column "Telephone_no" of data type 'number' and size ='10' to the employee table.
Q. alter table employee add(telephone_no number(10));
2. Add a column "country" of datatype 'char' and size='15' to the client_mast table.
Q. alter table client_mast add (country char(15));
3. Increase the size of "description" 20 to 25 in product_mast table.
Q. alter table product_mast modify (description varchar2(25));
4. Modify the "product_no" key as a primary key from product_mast table.
Q. alter table product_mast add primary key(product_no);
5. Drop the primary key of client_mast table.
Q. alter table client_mast drop primary key;
6. Define the "deptno" as not null constraint from dept table.
Q. alter table depttable modify deptno not null;
7. Display empno, ename, and annual salary of each employee from employee
table with a column name "Annual Salary" on the basis of basic_sal.
Q. select empno as "Employee Number", ename as "Employee
Name", basic_sal*12 as "Annual salary" from employee;
8. Add two columns"HOD" and "Strength" of datatype varchar2 and size=15 to
the dept table.
Q. alter table depttable add (hod varchar2(15),strength varchar2(15));

What is a database?
Database: data base is a collection of interrelated data organized such a way to get efficient
retrievals. The database is organized by fields, records and files.
File: A file is a collection of records.
Record: A record is a collection of fields.
Field: Field is single piece of information
What is the definition of DBMS?
A Database Management system is a collection of interrelated data and set of programs to
access this data. The objective of DBMS is to provide a convenient environment to retrieve
and store database information. Database systems can support single user and multiuser
environment.

What are the Types of Relations?


*
*
*
*

One-To-One Relationship
One-To-Many Relationship
Many-To-One Relationship
Many-To-Many Relationship

What is SQL?
SQL stands for structured query language. SQL is used to communicate with a database.
According to ANSI, it is the standard language for relational database management
systems. This language is nonprocedural. SQL was introduced by IBM corporation and
standardized by ANSI and ISO in 1986. Oracle was the first company to release a product
that used SQL.
SQL statements are used to perform tasks such as update data on a database or retrieve
data from the database. Some common relational database management systems that use
SQL are:Oracle, sybase, Microsoft SQL Server, Access.
What is SQL *Plus?
SQL *Plus is a oracle specific program, which accepts and executes SQL commands and
PL/SQL blocks. SQL *Plus allow us to manipulate SQL commands and PL/SQL blocks. It also
performs many additional tasks as given below.
*
*
*
*
*

enter, edit, retrieve, store and run SQL commands and PL/SQL blocks
foramt, store, calculations and print query results in the form or reports.
list column definition for any table.
access and copy data between SQL databases.
send and retrieve messages from the users.

What is the difference between drop and delete?


delete: The delete command used to delete the particular row in a table.
drop: The drop command is used to delete the entire table.
What is a sequence?
A sequence is a data base object that generates unique, sequential integer values. It can be
used to automatically generate primary key or unique key column. A sequence can be either
in an ascending or descending order.

Write a pl - sql to find the greater no among three numbers.

SQL> edit
Declare
a number;
b number;
c number;
Begin
a:=&a;
b:=&b;
c:=&c;
if a>b and a>c then
dbms_output.put_line(a||'is greater');
elsif b>a and b>c then
dbms_output.put_line(b||'is greater');
elsif c>a and c>b then
dbms_output.put_line(c||'is greater');
end if;
end;
SQL> /
Program to calculate Factorial of number.
declare
v_num number :=#
fact number :=1;
begin
for i in 1..v_num
loop
fact:=fact*i;
end loop;
dbms_output.put_line('fact of '||v_num||' is '||fact);
end;
/

Program to calculate Even or odd Number


declare
n number;
r number :=1;
begin
n:=&number;
for r in 1..n
loop
if mod(r,2)=0 then
dbms_output.put_line('Even No :' || r);
end if;
end loop;
end;

Multiplication Table
DECLARE
n number := &n;
prod number;
BEGIN
for i in 1..10 loop
prod := n * i;
dbms_output.put_line(n||' * '||lpad(i,2,' ')
||' = '||lpad(prod,3,' '));
end loop;
END;

Types of Table Relationships


A relationship works by matching data in columns usually columns with the same name in
both tables. In most cases, the relationship matches the primary key from one table, which
provides a unique identifier for each row, with an entry in the foreign key in the other table.

There are three types of relationships between tables.

One-to-Many Relationship

Many-to-Many Relationships

One-to-One Relationships

One-to-Many Relationships
A one-to-many relationship is the most common type of relationship, a row in table A can
have many matching rows in table B, but a row in table B can have only one matching row in
table A. For example, the publishers and titles tables have a one-to-many relationship: each
publisher produces many titles, but each title comes from only one publisher.

Many-to-Many Relationships
In a many-to-many relationship, a row in table A can have many matching rows in table B,
and vice versa. You create such a relationship by defining a third table, called a junction
table, whose primary key consists of the foreign keys from both table A and table B.

One-to-One Relationships
In a one-to-one relationship, a row in table A can have no more than one matching row in
table B, and vice versa. A one-to-one relationship is created if both of the related columns
are primary keys or have unique constraints.

One-to-one relationships:

This relationship exists where a record in Table A can only have one related record
in Table B, and a record in Table B can only have a single matching record in Table A
For example, an MP can have only one constituency, and a constituency can have
only one MP

One-to-many relationships:

This relationship exists where a record in Table A can have no, one or more
matching record in Table B, but a record in Table B can only have one matching
records in Table A
For example, a mother can have more than one child, but a child can have only
one biological mother

Many-to-many relationships:

This relationship exists where a record in Table A can have no, one or many
matching records in Table B, and a record in Table B can have no, one or more than
one matching record in Table A
For example, an author can write more than one book and a book can be written
by more than one author

You might also like