Professional Documents
Culture Documents
DBMS Services
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.
Constraints
Constraints are rules that are used to control the invalid data entry in a column. Constraints
are
Primary key
Foreign key
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;
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');
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';
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.
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.
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.
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;
/
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;
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