Create Alter Rename Drop

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 76

Expt.

No : 1
Date :

Data Definition Language commands in RDBMS.

Aim:
To implement and execute a query for defining data items in a Oracle
database using Structured Query Language commands.

DDL Commands:
 Create
 Alter
 Rename
 Drop

Syntax:
 Create
Create table <tablename> (columnname1 datatype1, columnname2
datatype2,…, columnnameN datatypeN);
 Alter
- Add
- Modify
- Drop
Alter table <tablename> add (columnname1 datatype1,..);
Alter table <tablename> modify (columnname1 datatype1,..);
Alter table <tablename> drop column (columnname);
 Rename
Rename <tablename> to <newtablename>;
 Drop
Drop table <tablename>;
CREATE AND DESCRIBE THE FOLLOWING TABLES:

A) NAME: branch
FIELDS DATATYPE
branch_name varchar2(30) primary key
branch_city varchar2(30)
assets number(8,2)

B) NAME: account
FIELDS DATATYPE
account_no varchar2(11) primary key
branch_name varchar2(30) references branch(branch_name)
balance number(8)

C) NAME: customer
FIELD DATATYPE
customer_id varchar2(11) primary key
customer_name varchar2(20)
customer_street varchar2(15)
customer_city varchar2(15)

D) NAME: depositor
FIELD DATATYPE
customer_id varchar2(11)references customer (customer_id)
account_no varchar2(11)references account (account_no)

E) NAME: loan
FIELDS DATATYPE
loan_no varchar2(4) primary key
branch_name varchar2(30) references branch(branch_name)
amount number(8,2)

F) NAME: borrower
FIELDS DATATYPE
customer_id varchar2(11) references customer(customer_id)
loan_no varcahr2(4) references loan(loan_no)

INSERT THE FOLLOWING VALUES INTO THE TABLES


1. branch :

BRANCH_NAME BRANCH_CITY ASSETS


Perryridge Rye 50000
Downtown Stamford 100000
Brighton Paloalto 25000
Redwood Harrison 150000
Mianus Pitsfield 450000
Roundhill Princeton 150000

2) account :

ACCOUNT_NO BRANCH_NAME BALANCE


019_28_3746 Perryridge 1500
182_73_6091 Downtown 2300
192_83_7465 Brighton 1800
321_12_3123 Redwood 500
336_66_9999 Mianus 500
963_96_3963 Roundhill 500
376_66_9999 Mianus 900
963_96_3964 Mianus 1300

3) loan :

LOAN BRANCH_NAME AMOUNT


1_11 Roundhill 900
1_14 Downtown 1500
1_15 Perryridge 1500
1_16 Perryridge 1300
1_17 Downtown 1000
1_23 Redwood 2000
1_93 Mianus 500
4) depositor

CUSTOMER_ID ACCOUNT_NO
c_08 182_73_6091
c_03 192_83_7465
c_05 321_12_3123
c_07 336_66_9999
c_08 963_96_3963
c_02 376_66_9999

5) customer

CUSTOMER_ID CUSTOMER_NAME CUSTOMER_STREET


CUSTOMER_CITY
c_01 smith north rye
c_02 turner putnam stamford
c_03 johnson alma palo alto
c_04 curry north rye
c_05 jones main harrisdon
c_06 adoms spring pittsfield
c_07 lindsay park pittsfield
c_08 hayes main harrison
c_09 williams nassau Princeton

6) borrower

CUSTOMER_ID LOAN_NO
c_01 1_11
c_01 1_23
c_03 1_93
c_05 1_17
c_03 1_16
c_05 1_14
CREATE THE FOLLOWING TABLES:
A)NAME: branch
FIELDS DATATYPE
branch_name varchar2(30) primary key
branch_city varchar2(30)
assets number(8,2)

SQL> create table branchz(branch_name varchar2(30)


primary key,branch_city varchar2(30),assets numr(8,2));
Table created.

SQL> insert into branchz values('&branch_name',


'&branch_city',&assets);
Enter value for branch_name: Perryridge
Enter value for branch_city: Rye
Enter value for assets: 50000
old 1: insert into branchz values('&branch_name','&branch_city',&assets)
new 1: insert into branchz values('Perryridge', 'Rye',50000)
1 row created.

SQL> select * from branchz;


BRANCH_NAME BRANCH_CITY ASSETS
------------------------------------ ----------------------------------
Perryridge Rye 50000
Downtown Stamford 100000
Brighton Paloalto 25000
Redwood Harrison 150000
Mianus Pitsfield 450000
Roundhill Princeton 150000

B)NAME:account
FIELDS DATATYPE
account_no varchar2(11) primary key
branch_name varchar2(30) references branchz(branch_name)
balance number(8)
SQL> create table accountz(account_no varchar2(11)
primary key,branch_name varchar2(30) references
branchz(branch_name),balance number(8));
Table created.

SQL> insert into accountz values('&account_no',


'&branch_name',&balance);
Enter value for account_no: 019_28_3746
Enter value for branch_name: Perryridge
Enter value for balance: 1500
old 1: insert into accountz values('&account_no',
'&branch_name',&balance)
new 1: insert into accountz values('019_28_3746',
'Perryridge',1500)
1 row created.

SQL> select * from account;

ACCOUNT_NO BRANCH_NAME BALANCE


----------- ------------------------- ----------------------------------
019_28_3746 Perryridge 1500
182_73_6091 Downtown 2300
192_83_7465 Brighton 1800
321_12_3123 Redwood 500
336_66_9999 Mianus 500
963_96_3963 Roundhill 500
376_66_9999 Mianus 900
963_96_3964 Mianus 1300

8 rows selected.
___________________________________________________
C)NAME:customer

FIELD DATATYPE
customer_id varchar2(11)primary key
customer_name varchar2(20)
customer_street varchar2(15)
customer_city varchar2(15)
SQL> create table customer(customer_id varchar2(11)
primary key,customer_name varchar2(20),customer_street
varchar2(15),customer_city varchar2(15));
Table created.

SQL> insert into customer values('&customer_id',


'&customer_name','&customer_street','&customer_city');
Enter value for customer_id: c_01
Enter value for customer_name: smith
Enter value for customer_street: north
Enter value for customer_city: rye
old 1: insert into cusotmer values('&customer_id',
'&customer_name','&customer_street','&customer_city')
new 1: insert into cusotmer values('c_01','smith', 'north','rye')
1 row created.

SQL> select * from customer;


CUSTOMER_ID CUSTOMER_NAME CUSTOMER_STREET
CUSTOMER_CITY
----------- -------------------- --------------- ---------------
c_01 smith north rye
c_02 turner putnam stamford
c_03 johnson alma palo alto
c_04 curry north rye
c_05 jones main harrisdon
c_06 adoms spring pittsfield
c_07 lindsay park pittsfield
c_08 hayes main harrison
c_09 williams nassau princeton
9 rows selected.

_____________________________________________________________
D)NAME:depositor

FIELD DATATYPEcustomer_id varchar2(11)references


cusotmer(customer_id)account_no varchar2(11)references
accountz(account_no)

SQL> create table depositorz(customer_id varchar2(11)


references customer(customer_id),account_no varchar2(11)
references account(account_no));
Table created.

SQL> insert into depositor values('&customer_id',


'&account_no');
Enter value for customer_id: c_08
Enter value for account_no: 182_73_6091
old 1: insert into depositor values('&customer_id',
'&account_no')
new 1: insert into depositor values('c_08','182_73_6091')
1 row created.

SQL> select * from depositor;


CUSTOMER_ID ACCOUNT_NO
----------- ------------------------------
c_08 182_73_6091
c_03 192_83_7465
c_05 321_12_3123
c_07 336_66_9999
c_08 963_96_3963
c_02 376_66_9999
6 rows selected.
_________________________________________________________
E)NAME: loan

FIELDS DATATYPE
loan_no varchar2(4)primary key
branch_name varchar2(30)references branchz(branch_name)
amount number(8,2)
SQL> create table loan(loan_no varchar2(4) primary key,
branch_name varchar2(30) references branchz(branch_name),
amount number(8,2));
Table created.

SQL> insert into loan values('&loan_no','&branch_name',&amount);


Enter value for loan_no: 1_11
Enter value for branch_name: Roundhill
Enter value for amount: 900
old 1: insert into loan values('&loan_no','&branch_name',&amount)
new 1: insert into loan values('1_11','Roundhill',900)
1 row created.

SQL> select * from loan;


LOAN BRANCH_NAME AMOUNT
---- ------------------------------ ----------
1_11 Roundhill 900
1_14 Downtown 1500
1_15 Perryridge 1500
1_16 Perryridge 1300
1_17 Downtown 1000
1_23 Redwood 2000
1_93 Mianus 500

7 rows selected.
_____________________________________________________________
________
F)NAME:borrower

FIELDS DATATYPE
customer_id varchar2(11)references cusotmer(customer_id)
loan_no references loan(loan_no)

SQL> create table borrower(customer_id varchar2(11)


references cusotmer(customer_id),loan_no references
loan(loan_no));
Table created.

SQL> insert into borrower values('&customer_id','&loan_no');


Enter value for customer_id: c_01
Enter value for loan_no: 1_11
old 1: insert into borrower values('&customer_id','&loan_no')
new 1: insert into borrower values('c_01','1_11')
1 row created.

SQL> select * from borrower;


CUSTOMER_ID LOAN
----------- ----
c_01 1_11
c_01 1_23
c_03 1_93
c_05 1_17
c_03 1_16
c_05 1_14
6 rows selected.

SQL> create table abc (name varchar2(30));


Table created.

SQL> insert into abc values('shalini');


1 row created.

SQL> alter table abc add(class varchar2(30));


Table altered.
SQL> desc abc
Name Type
-------- ---------------
NAME VARCHAR2(30)
CLASS VARCHAR2(30)

SQL> drop table abc;


Table dropped.

RESULT:
Thus the program to create and modify a table using data
defining language commands was executed successfully.
Expt. No : 2 `

Date :

Data Manipulation Language

Aim:
To implement and execute a query for manipulating & storing data
items in a Oracle database using Structured Query Language
commands.

DML Commands:
 Insert
 Select
 Update
 Delete
DCL Commands:
 Commit
 Rollback
 Savepoint
 Privilege

Syntax:
 Insert
Single level insertion
Insert into <tablename> values (Columnname1,…);
Multilevel insertion
Insert into <tablename> values (&columnname1…);
 Select
Single level selection
Select Columnname from <tablename>;
Multilevel selection
Select * from <tablename>;
 Update
Update <tablename> set <columnname>=’valuetobegiven’ where
<condition>;
 Delete
Single row deletion
Delete from <tablename> where <columnname>=’valuegiven’;
Multi row deletion
Delete from <tablename>;
 Commit
Commit;
 Rollback
Rollback <label>;
 Savepoint
Savepoint <label>;
 Privilege
Grant <privilege> on <tablename> to userN;

Exercises :

1. Find the names of all branches in loan relation.


2. Find the names of all branches in loan relation, eliminate
duplicates.
3. Display the loan relation with the attributes amount is multiplied
by 100.
4. Find all loan no's for loan's made at the perryridge branch with
loan amounts greater than 1200
5. Find all loan no's for loan's with loan amount between 900 and
1500.
6. For all customers who have a loan from the bank, find their ids,
loan numbers and loan amount.
7. For all customers who have a loan at perryridge branch, find
their ids, loan numbers and loan amounts.
8. Find the names of all branches that have assets greater than at
least one branch located in Stamford.
9. Find the names of customers whose street address includes the
substring 'ma'.
10.Find the names of customers whose street address starts with 'sp'.
11.Display all customer name in alphabetic order who have a loan at
perryridge branch.
12.Display the entire loan relation in descending order of the
amount. If several loans have the same amount, order them in
ascending order by loan number.
13.Find all customers having a loan, or account or both at the same
bank.
14.In the above query include duplicates.
15.Find all customers having an account and loan at the same bank
16.Find the average account balance at the mianus branch.
17.Find the average account balance at each branch.
18.Find the number of depositors at each branch.
19.Find the branches where the average account balances greater
than 1200.
20.Find the total no of customers.
21.Find the average balance for each customer who lives in Harrison
and has at least two accounts.

22.Find all loan no's that appear in the loan relations with null values
for amount.
23.Find all loan no's that appear in the loan relations with not null
values for amount.
24.Find all customers having both account and loan at the same
bank(subquery).
25.Find all customers who do have a loan at the bank, but who do
not have an account at the bank(subquery).
26.Find the custoemers whose names are neither smith nor Jones.
27.Find the names of all branches that have assets greater than those
at least one branch located in Harrison (sub query).
28.Find those branches for which the average balance is greater than
or equal to all average balances.
29.Find all customers who have both an account and a loan at the
bank (using exists)
30.Create a view called all customer consisting of branch names and
the name of customers who have either an account or a loan at
that branch.
31.From all customer view, find all customers of the perryridge
branch.
32.Delete all account tuples in the perryridge branch.

1)Find the names of all branches in loan relation.


SQL> select branch_name from loanz;
BRANCH_NAME
------------------------------
roundhill
downtown
perryridge
perryridge
downtown
redwood
mianus
7 rows selected.

2)Find the names of all branches in loan relation, eliminate duplicates.

SQL> select distinct branch_name from loanz;

BRANCH_NAME
------------------------------
downtown
mianus
perryridge
redwood
roundhill

3)Display the loan relation with the attributes amount


is multiplied by 100.
SQL> select 100*amount from loanz;

100*AMOUNT
----------
90000
150000
150000
130000
100000
200000
50000
7 rows selected.

4)Find all loan no's for loan's made at the perryridge branch
with loan amounts greater than 1200
SQL> select loan_no from loanz where branch_name='perryridge'
and amount>1200;
LOAN
----
1_15
1_16

5)Find all loan no's for loan's with loan amount between
900 and 1500.
SQL> select loan_no from loanz where amount>900 and amount<1500;
LOAN
----
1_16
1_17
6)For all customers who have a loan from the bank, find their ids,
loan numbers and loan amount.
SQL> select borrowerz.customer_id,loanz.loan_no,loanz.branch_name,
loanz.amount from loanz,borrowerz where
loanz.loan_no=borrowerz.loan_no;
CUSTOMER_ID LOAN BRANCH_NAME AMOUNT
----------- ---- ------------------------------ -------------------------------
c_01 1_11 roundhill 900
c_01 1_23 redwood 2000
c_03 1_93 mianus 500
c_05 1_17 downtown 1000
c_03 1_16 perryridge 1300
c_05 1_14 downtown 1500

6 rows selected.

7)For all custoemers who have a loan at perryridge branch, find their ids,
loan numbers and
loan numbers
SQL> select borrowerz.customer_id,loanz.loan_no,loanz.branch_name,
loanz.amount from loanz,borrowerz where(loanz.branch_name='perryridge'
and loanz.loan_no=borrowerz.loan_no);
CUSTOMER_ID LOAN BRANCH_NAME AMOUNT
----------- ---- ------------------------------ ----------------------------------------
c_03 1_16 perryridge 1300
8)SQL> select branch_name from branchz where assets>any
(select assets from branchz where branch_city='stamford');
BRANCH_NAME
------------------------------
redwood
mianus
roundhill

9)Find the names of customers whose street address includes the


substring 'ma'.
SQL> select customer_name from customerz where customer_street like
'%ma';
CUSTOMER_NAME
--------------------
Johnson

10)Find the names of customers whose street address starts with 'sp'.
SQL> select customer_name from customerz where customer_street like 'sp
%';
CUSTOMER_NAME
Adoms
11)Display all customer name in alphabetic order who have a
loan at perryridge branch.
SQL> select customer_name from customerz where
customerz.customer_id=any
(select borrowerz.customer_id from borrowerz where
borrowerz.loan_no=any
(select loan_no from loanz where loanz.branch_name='perryridge'))
order by customerz.customer_name asc;
CUSTOMER_NAME

12)Display the entire loan relation in descending order of the amount.


If several loans have the same amount order them in ascending
order by loan number.
SQL> select * from loanz order by amount desc,loan_no asc;
LOAN BRANCH_NAME AMOUNT
---- ------------------------------ -------------------------------
1_23 redwood 2000
1_14 downtown 1500
1_15 perryridge 1500
1_16 perryridge 1300
1_17 downtown 1000
1_11 roundhill 900
1_93 mianus 500
7 rows selected.

13)Find all customers having a loan, or account or both at the same bank.
SQL> select distinct customer_id from borrowerz union select
customer_id from depositorz;
CUSTOMER_ID
-----------
c_01
c_02
c_03
c_05
c_07
c_08
6 rows selected.
14)In the above query include duplicates.
SQL> select customer_id from borrowerz union select customer_id
from depositorz;
CUSTOMER_ID
-----------
c_01
c_02
c_03
c_05
c_07
c_08
6 rows selected.

15)Find all customers having an account no loan at the same bank.


SQL> select customer_id from depositorz minus (select customer_id
from borrowerz);
CUSTOMER_ID
-----------
c_02
c_07
c_08

16)Find all customers having both account and loan at the same bank.
SQL> select customer_id from depositorz intersect select customer_id
from borrowerz;
CUSTOMER_ID
-----------
c_03
c_05

17)Find the average account balance at the mianus branch.


SQL> select avg(balance) from accountz where branch_name='mianus';
AVG(BALANCE)
------------
900

18)Find the average account balance at each branch.


SQL> select branch_name,avg(balance) from accountz group by
branch_name;
BRANCH_NAME AVG(BALANCE)
------------------------------ ------------
brighton 1800
downtown 2300
mianus 900
perryridge 1500
redwood 500
roundhill 200
6 rows selected.

19)Find the number of depositors at each branch.


SQL> select count(customer_id),accountz.branch_name from
accountz,depositorz where depositorz.account_no=accountz.account_no
group by accountz.branch_name
COUNT(CUSTOMER_ID) BRANCH_NAME
------------------ ------------------------------
1 brighton
1 downtown
2 mianus
1 redwood
1 roundhil

20)Find the branches where the average account balance greater than 1200.
SQL> select avg(balance) from accountz having avg(balance)>1200
group by branch_name;
AVG(BALANCE)
------------
1800
2300
1500

21)Find the total no of customers.


SQL> select count(customer_id) from customerz;
COUNT(CUSTOMER_ID)
------------------
9
22)Find the average balance for each customer who lives in harrison
and has atleast two accounts.
SQL>
1 select avg(balance) from accountz,customerz,depositorz
2 where customerz.customer_id=depositorz.customer_id
3 and depositorz.account_no=accountz.account_no
4 and customer_city='harrison'
5 group by customerz.customer_id having
count(customerz.customer_id)>=2;
SQL> /
AVG(BALANCE)
------------
1250

23)Find all loan no's that appear in the loan relations with null
values for amount.
SQL> select loan_no from loanz where amount is null;
no rows selected

24)Find all loan no's that appear in the loan relations with not
null values for amount.
SQL> select loan_no from loanz where amount is not null;
LOAN
----
1_11
1_14
1_15
1_16
1_17
1_23
1_93
7 rows selected.

25)Find all customers having both account and loan at the


same bank(subquery).
SQL> select customer_id from depositorz where customer_id in
(select customer_id from borrowerz);
CUSTOMER_ID
-----------
c_03
c_05
26)Find all customers who do have a loan at the bank, but who do not
have an account at the bank(subquery).
SQL> select customer_id from borrowerz where customer_id not in
(select customer_id from depositorz);
CUSTOMER_ID
-----------
c_01
c_01

27)Find the custoemers whose names are neither smith nor jones.
SQL> select customer_name from customerz where
not(customer_name='smith' or customer_name='jones');
CUSTOMER_NAME
--------------------
turner
johnson
curry
adoms
lindsay
hayes
williams
7 rows selected.

28)Find the names of all branches that have assets greater than
those atleast one branch located in harrison(subquery).
SQL> select branch_name from branchz where assets>any
(select assets from branchz where branch_city='harrison');
BRANCH_NAME
Mianus
29)Find those branches for which the average balance is greater
than or equal to all average balances.
SQL> select branch_name,avg(balance) from accountz group by
branch_name having avg(balance)>=all(select
avg(balance) from accountz group by branch_name);
BRANCH_NAME AVG(BALANCE)
------------------------------ ------------
downtown 2300

30)Find all customers who have both an account and a loan at the
bank (using exists)
SQL> select customer_id from borrowerz where
exists(select * from depositorz where
depositorz.customer_id=borrowerz.customer_id);
CUSTOMER_ID
-----------
c_03
c_05
c_03
c_05

31)Create a view called all_customer consisting of branch names


and the name of customers who have either an account or a
loan at that branch.
SQL> create view all_customer as
(select accountz.branch_name,depositorz.customer_id from depositorz,
accountz where depositorz.account_no=accountz.account_no)union
(select loanz.branch_name,borrowerz.customer_id from loanz,borrowerz
where
borrowerz.loan_no=loanz.loan_no);
View created.

32)From all_customer view, find all customers of the perryridge branch.


SQL> select customer_id from all_customer where
branch_name='perryridge';
CUSTOMER_ID
-----------
c_03

RESULT:
Thus the program to perform manipulation on created table
using data manipulation language commands was executed successfully.
Expt. No : 3 `
Date :

DATA CONTROL LANGUAGE COMMANDS

AIM:
To perform the transaction control of database using data control
language commands.

LIST OF COMMANDS:

 Commit
 Roll back
 Save point

SYNTAX AND SEMANTICS:

 COMMIT:
Commit ends the current transaction and makes
permanent changes made during the transaction.

SYNTAX:
SQL>COMMIT;
>ROLL BACK:

ROLL BACK statement is the exact opposite of commit


statement.
It ends the transaction but unless any changes made during the
transaction.
SYNTAX:
SQL>ROLL BACK;
SQL>ROLL BACK TO<save point name>;
>SAVE POINT:
SAVE POINT is created during the transaction and it is
used to do the partial transaction.

SYNTAX:
SQL>SAVE POINT<name>
SQL> commit;
Commit complete.

SQL> select * from bankz;

NAME ACCNO CITY


-------------------- ---------- --------------------
anitha 101 coimbatore
balu 102 bombay
prem 103 banglore

SQL> update bankz set accno=105 where city='bombay';


1 row updated.

SQL> insert into bankz values('saleema','106','chennai');


1 row created.

SQL> select * from bankz;

NAME ACCNO CITY


-------------------- ---------- --------------------
anitha 101 coimbatore
balu 105 bombay
prem 103 banglore
saleema 106 chennai

SQL> rollback;
Rollback complete.

SQL> select * from bankz;

NAME ACCNO CITY


-------------------- ---------- --------------------
anitha 101 coimbatore
balu 102 bombay
prem 103 banglore
SQL> savepoint ss;
Savepoint created.

SQL> delete from bankz where accno=101;


1 row deleted.

SQL> delete from bankz where accno=101;


1 row deleted.

SQL> select * from bankz;

NAME ACCNO CITY


-------------------- ---------- --------------------
balu 102 bombay
prem 103 banglore

SQL> savepoint rr;


Savepoint created.

SQL> delete from bankz where accno=102;


1 row deleted.

SQL> select * from bankz;

NAME ACCNO CITY


-------------------- ---------- --------------------
prem 103 banglore

SQL> savepoint aa;


Savepoint created.

SQL> delete from bankz where accno=102;

1 rows deleted.
SQL> select * from bankz;

NAME ACCNO CITY


-------------------- ---------- --------------------
prem 103 banglore
SQL> roll back rr;
Rollback complete.
SQL> select * from bankz;

NAME ACCNO CITY


-------------------- ---------- --------------------
balu 102 bombay
prem 103 banglore

SQL> roll back ss;


Rollback complete.

SQL> select * from bankz;

NAME ACCNO CITY


-------------------- ---------- --------------------
anitha 101 coimbatore
balu 102 bombay
prem 103 banglore

RESULT:
Thus the program to perform the transaction control of database
using data control language commands was executed successfully.
Expt. No : 4 `
Date :

BUILT IN FUNCTIONS
AIM:

To execute the built in functions . Built in functions are predefined


function.

LIST OF COMMANDS:
The list of built in functions in SQL are

>Numeric
>character
>Aggregate

SYNTAX AND SEMANTICS:

>NUMERIC:
 ABSOLUTE:
Used to find the absolute value of a given number.

SYNTAX:
SQL>Select abs(n) from dual;

 POWER:
Used to find the power value of the given number.
Power(m,n)means m^n.

SYNTAX:
SQL>Select power(m,n) from dual;

 SQRT:
It will give the square root of the given number

SYNTAX:
SQL>Select sqrt(n) from dual;
 TRIGONOMETRIC VALUES:
To find trigonometric value of the given
given number.

SYNTAX:
SQL>Select sin (n) from dual;
SQL>Select cos (n) from dual;
SQL>Select tan (n) from dual;

 CHARACTER VALUE:
It will give the corresponding ASCII values of a
given character.

SYNTAX:
SQL>Select char (n) from dual;

 MODULUS VALUE:
It will give the modulus value of a given number(i.e)it
will give the remainder of a division(m%n).

SYNTAX:
SQL>Select mod(m,n) from dual;

 ROUND VALUE:
It will give the round off value of a given number.

SYNTAX:
SQL>Select round(m,n) from dual;

 TRUNCATE:
It will roud off the value and gives the value with one
decimal point.

SYNTAX:
SQL>Select trunk (m,n) from dual;

 LOG:
It will give the logarithmic value of a given number log
m^n.

SYNTAX:
SQL>Select log (m,n) from dual;

 NATURAL LOGARITHM:
It wil give the natural logarithm of a number
ln(n)

SYNTAX:
SQL>Select ln(n) from dual;

 EXPONENT VALUE:
It will give the exponent value of a given number(i.e )
e^n.

SYNTAX:
SQL>Select exp(n) from dual;

 CEIL:
It is also a type of round off value.

SYNTAX:
SQL>Select ceil(n) from dual;

 FLOOR:
It will be give the integer value for the given number.

SYNTAX:
SQL>Select floor(n) from dual;

>CHARACTER FUNCTIONS:

 UPPER:
To convert lower case into upper case character.

SYNTAX:
SQL>Select upper(‘CHAR’) from dual;

 LOWER:
To convert upper case into lower case character.

SYNTAX:
SQL>Select lower(‘CHAR’)from dual;

 INITCAP:
To convert the first character of the string into upper case
character.

SYNTAX:
SQL>Select initcap(‘STRING’)from dual;

 CONCATINATION:
To join any two words where C1 and C2 are characters.

SYNTAX:
SQL>Select concat(‘C1’,’C2’)from dual;

 REPLACE:
To replace a character in a string by another character.

SYNTAX:
SQL>Select replace(‘TEXT’,’C1’,’C2’)from dual;

 SUBSTRING:
To remove a number of characters from a string
where n is the starting position and m is the total number of
characters fetched from that position.

SYNTAX:
SQL>Select substr(‘s’,n,m)from dual;

 LENGTH:
To find the length of the given string.

SYNTAX:
SQL>Select length(‘STRING’);

 LPAD:
To add symbols to left side of the string where n is the
number of characters and ‘char’ is the character that
should be added to left side.

SYNTAX:
SQL>Select lpad(‘string’,n,’CHA’)from dual;

 RPAD:
To add symbols to right side of the string where n is the number
of characters and ‘char’ is the character that should be added to
right side.

SYNTAX:
SQL>Select rpad(‘STRING’,n,’CHAR’)from dual;

 LTRIM:
Write string by neglecting the matching character from left
where ‘char’ is the character to be neglected from left side.

SYNTAX:
SQL>Select ltrim(‘STRING’,”CHAR’)from dual;

 RTRIM:
Write string by neglecting the matching character from right
where ‘char’ is the character to be neglected from right side.

SYNTAX:
SQL>Select rtrim(‘STRING’,”CHAR’)from dual;

>AGGREGATE FUNCTIONS:

 MINIMUM:
To find maximum value of the given field.
SYNTAX:
SQL>Select min (Cn) from <table name>;

 MAXIMUM:
To find minimum value of the given field.
SYNTAX:
SQL>Select max (Cn) from <table name>;

 AVERAGE:
To find the average value of a given field.

SYNTAX:
SQL>Select avg (Cn) from <table name>;

 STANDARD DEVIATION:
To find the standard deviation of a given field.

SYNTAX:
SQL>Select stddev(Cn) from <table name>;

 COUNT:
To give the number of elements in the given field.

SYNTAX:
SQL>Select count(Cn) from <table name>;

 COUNT(*)
To give the maximum number of records in the given
table.

SYNTAX:
SQL>Select count(*) from <table name>;

>DATE FUNCTIONS:

 SYSTEM TABLE:
This command displays the current date of the system.
SYNTAX:
SQL>Select sysdate from dual;

 NEXT DAY:
To give the same day of the next week.

SYNTAX:
SQL>Select
next_day(‘DD_MM_YYYY’,’day’)from dual;

 LAST DAY:
To give the previous day of the current day from
previous week.

SYNTAX:
SQL>Select last_day(‘DD_MM_YYYY’)from
dual;

 ADD MONTHS:
This command adds the number of specified months to
the given month.

SYNTAX:
SQL>Select
add_months(‘DD_MM_YYYY’,n)from dual;

 MONTHS BETWEEN:
It calculates and gives the number of months between to
the given dates.

SYNTAX:
SQL>Select
months_between(‘DD_MM_YYYY’,’DD_MM_YYYY’) from dual;

 TO DATE:
This command is used to give the month in character
when it is given as numeric.
SYNTAX:
SQL>Select
to_date(‘DD_NO_YYYY’,’DD_MM_YYYY’)from
dual;

 TO NUMBER:
It gives the number to the given character.

SYNTAX:
SQL>Select to_number(‘number’)from dual;

BUILT IN FUNCTIONS

SQL> select abs(-5.3)from dual;


ABS(-5.3)
----------
5.3

SQL> select power(3,4)from dual;


POWER(3,4)
----------
81

SQL> select sqrt(16)from dual;


SQRT(16)
----------
4

SQL> select sin(60) from dual;


SIN(60)
----------
-.30481062

SQL> select cos(90)from dual;


COS(90)
----------
-.44807362

SQL> select tan(45)from dual;


TAN(45)
----------
1.61977519

SQL> select chr(54)from dual;


C
----
6
SQL> select mod(5,3)from dual;
MOD(5,3)
----------
2

SQL> select round(123.55647)from dual;


ROUND(123.55647)
----------------
124

SQL> select trunc(145.4563)from dual;


TRUNC(145.4563)
---------------
145

SQL> select log(3,10)from dual;


LOG(3,10)
----------
2.09590327

SQL> select ln(5)from dual;


LN(5)
----------
1.60943791

SQL> select ceil(45.6223)from dual;


CEIL(45.6223)
-------------
46

SQL> select floor(56.9323)from dual;


FLOOR(56.9323)
--------------
56
SQL> select upper('abc')from dual;
UPP
---
ABC

SQL> select lower('ABC')from dual;


LOW
---
abc

SQL> select initcap('siva')from dual;


INIT
----
Siva

SQL> select concat('SIVA','RANJANI')from dual;


CONCAT('SIV
-----------
SIVARANJANI

SQL> select replace('ROSHINI','S','H')from dual;


REPLACE
-------
ROHHINI

SQL> select substr('RANJANI',3,4)from dual;


SUBS
----
NJAN

SQL> select length('ADBC')from dual;


LENGTH('ADBC')
--------------
4
SQL> select lpad('ABC',2,'Z')from dual;
LPAD(‘ABC’
-----------------
ZZZZZZZABC

SQL> select rpad('ABC',10,'Z')from dual;


RPAD('ABC'
----------
ABCZZZZZZZ

SQL> select ltrim('RANJANI','R')from dual;


LTRIM(
------
ANJANI

SQL> select rtrim('RANJANI','I')from dual;


RTRIM(
------
RANJAN

SQL>select * from NUM;


VALUE
-----------
12
34
67
45
67
12
6 rows selected.

SQL>select min(value)from num;


MIN(VALUE)
--------
12

SQL>select max(VALUE)from num;


MAX(VALUE)
---------
67

SQL>select avg(value)from num;


AVG(VALUE)
--------
39.5

SQL>select stddev(value)from num;


STDDEV(VALUE)
---------
24.841495

SQL> select * from ac;


NAME ACC_NO CITY
-------------------------------------
Abc 101 coimbatore
Abe 102 bombay
Ace 103 bangalore

SQL>select count(ACC_NO)from ac;


COUNT(ACC_NO)
----------------------
3
SQL>select count(*)from ac;
Count(*)
---------------
3

SQL> select sysdate from dual;

SYSDATE
---------
09-SEP-09

SQL> select next_day('22-AUG-09','SATURDAY')from


dual;
NEXT_DAY(
---------
29-AUG-09

SQL> select last_day('22-AUG-09')from dual;

LAST_DAY(
---------
31-AUG-09

SQL> select add_months('22-aug-09',8)from dual;


ADD_MONTH
---------
22-APR-10

SQL> select months_between('19-dec-2003','28-mar-


2005')from dual;
MONTHS_BETWEEN('19-DEC-2003','28-MAR-2005')
-------------------------------------------
-15.290323

SQL> select to_number(455)from dual;


TO_NUMBER(455)
--------------
455

RESULT:
Thus the program to execute the built in function
was executed successfully.
Expt. No : 5 `
Date :

High-level language extension with Cursors

Aim:
To implement and execute Cursor for storing data items in a same area
or disk in Oracle database using Procedural Language concepts.

Procedural Language
 Declaration Section – It is a part in which all the PL / SQL
variable constants and expression are declared.
 Execution Commands – PL / SQL program codes are placed in
this section.
 Exception Handling – Errors are handled in the execution part.
This part is optional.
Syntax for Procedural Language:
Declare
Declaration 1;
.......
Declaration N;
Begin
Execution 1;
……
Execution N;
Exception
Exception handlers;
End;

Simple Loops
– Sequence of statement between the loop will be executed repeatedly
forever.
Syntax:
Loop
……
Exit when < Condition > Area > 100;
End Loop;

While loop – used to execute a set of PL / SQL statement whenever the


condition is True.
Syntax:
While < Some Condition >
Loop
……
End loop;

For Loop – used if a given sequence of statement is to be executed a


number of times.
Syntax:
For variables in[reverse] start_no .. end_no
Loop
…..
End Loop;

Cursor Control
 Open – executes the query and identifies the active set & position
of the pointer before the first row.
Syntax:
Open <cursorname>;
 Fetch
– Retrieves the current row and places the pointer to the next row.
– % rowtype – Provides a record type represents a row in a table.
Syntax:
Fetch <cursorname> into <columnname>;
 Close – After processing the last row in the active set, the cursor is
closed using close command
Syntax:
Close <cursorname>;

Cursor attributes
 % notfound
 % found
 % rowcount
 % isopen

EXCEPTION HANDLING
- User defined exceptions should be declared in the declarative part
and raised explicit by a ‘raise’ statement
Syntax:
Exception_name exception;
Raise exception_name;

////////////////////////////////////////////////////ELECTRIC
BILL\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

DECLARE
CURSOR V1 IS SELECT * FROM AVIEBILL WHERE CNO=&CNO;
CCNO AVIEBILL.CNO%TYPE;
CCNAME AVIEBILL.CNAME%TYPE;
CUNITS AVIEBILL.UNIT%TYPE;
COST NUMBER(10);
BEGIN
OPEN V1;
IF(V1%ISOPEN) THEN
LOOP
FETCH V1 INTO CCNO,CCNAME,CUNITS;
EXIT WHEN(V1%NOTFOUND);
IF(CUNITS < 200)THEN
COST:=2*CUNITS;
ELSIF(CUNITS < 500 AND CUNITS > 200)THEN
COST:=(200*2)+((500-CUNITS)*4);
ELSIF(CUNITS > 500)THEN
COST:=1600+((CUNITS-500)*6);
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('THE BILL IS :'||COST);
END IF;
CLOSE V1;
END;

AVIEBILL TABLE

CNO CNAME UNIT


----- ---------------- ----------
1 A 156
2 B 124
3 C 252

OUT PUT:

Enter value for cno: 2


old 2: CURSOR V1 IS SELECT * FROM AVIEBILL WHERE
CNO=&CNO;
new 2: CURSOR V1 IS SELECT * FROM AVIEBILL WHERE CNO=2;
THE BILL IS :248
RESULT:
Thus the program for electricity bill calculation was created and
executedsuccessfully. .
Expt. No : 6 `
Date :

High-level language extension with Triggers

Aim:
To implement and execute trigger in Oracle database using Procedural
Language concepts

Triggers
- Trigger is a special type of procedure that the oracle executes
when an insert, modify or delete operation is performed against a
given table
- It is a stored sub program associated with a table
- It is used to keep an audit trial of a table, to prevent invalid
transaction, enforce complex security authorization, to generate
data automatically

Syntax:
Create or replace trigger <trigger_name>
{ before / after / instead of }
{ insert / update / delete [of column] }
on <tablename / viewname>
referencing { old as old / new as new }
[for each statement / for each row [ when <condition>]]
Declare
Variable declaration
Constant declaration
Begin
PL / SQL Sub program body
Exception
Exception PL / SQL Block (or) user defined exception
End
Problem specification :

Create a table ‘stud’ that consist of student’s roll no, name date
of birth. Also it should give the details about student’s marks and
individual grades in various subjects.

Create table product(product_code varchar2(10) primary key, part_no


varchar2(10) not null, price number(7,2) not null, stock number(5) not null);
Create table audit_product(product_code varchar2(10) not null, operation
varchar2(10) not null, part_no varchar2(10) not null, oper_date date not
null);
Sql>create or replace trigger tgr
After update or delete or insert on product
For each row
Declare
Oper varchar2(10);
Prd_code varchar2(10);
Prd_name varchar2(20);
Begin
If updating then
Oper:=’update’;
Prd_code:=:old.product_code;
Prd_name:=:old.part_no;
End if;
If deleting then
Oper:=’delete’;
Prd_code:=:old.product_code;
Prd_name:=:old.part_no;
End if;
If inserting then
Oper:=’insert’;
Prd_code:=:new.product_code;
Prd_name:=:new.part_no;
End if;
Insert into audit_product values(prd_code, oper, prd_name, sysdate);
End;
/
Insert into product values(101,’abc’,100.2,20);
Insert into product values(102,’acd’,250.75,220);
Insert into product values(101,’ade’,200.5,250);

Commit;
Select * from product;
Select * from audit_product;
Update product set price =101 where product_code=’101’;
Select * from audit_product;
Rollback;
Select * from audit_product;
Delete product where product_code=’101’;
Select * from audit_product;
Commit;
Select * from product;
Select * from audit_product;

Sql>create or replace trigger tgr1


Before insert or update or delete on product
Declare
Sys_dt varchar2(10);
Begin
Sys_dt:= to_char(sysdate, ‘dy’);
If sys_dt in (‘tue’, ‘wed’)then
Raise_application_error(-20001,’try on days except tue or wed’);
End if;
End;
/

Select * from product;


Delete product where product_code=’103’;
Update product set part_no=’abc’ where product_code=’102’;
Insert into product values(104,’asd’,200,100);

SQL>create table newemp(empno numner(4),ename varchar2(20),job


varchar2(20));
Table created.
SQL>desc newemp;
Name NULL? Type
EMPNO NUMBER(4)
ENAME VARCHA2(20)
JOB VARCHAR2(20)

SQL>insert into newemp values(‘empno’,’&ename’,’&job’);


Enter the value for empno:1
Enter the value for ename:sdsd
Enter the value for job:gm
Old 1:Insert into newemp values(‘&empno’,’&ename’,’&job’)
New 1:insert into newemp values(‘1’,’sdsd’,’gm’)
1 row created.

SQL>insert into newemp values(‘empno’,’&ename’,’&job’);


Enter the value for empno:2
Enter the value for ename:sd
Enter the value for job:mgr
Old 1:Insert into newemp values(‘&empno’,’&ename’,’&job’)
New 1:insert into newemp values(‘1’,’sd’,’mgr’)
1 row created.

SQL>insert into newemp values(‘empno’,’&ename’,’&job’);


Enter the value for empno:2
Enter the value for ename:dsd
Enter the value for job:ed
Old 1:Insert into newemp values(‘&empno’,’&ename’,’&job’)
New 1:insert into newemp values(‘’,’dsd’,’ed’)
1 row created.

SQL>select * from newemp;


EMPNO ENAME JOB
----------------------------------------------------------------
1 sdsd gm
2 sd mgr
2 dsd ed
SQL>create table oldemp(empno number(4), ename varchar2(20),job
varchar2(20));
Table created.
SQL>CREATE OR REPLACE TRIGGER TR_AD_EMPS
2 AFTER DELETE ON NEWEMP
3 FOR EACH ROW
4 BEGIN
5 INSERT INTO OLDEMP
VALUES(:OLD.EMP,:OLD.ENAME,:OLD.JOB);
6 END;
7 /
Trigger created.
SQL>DELETE FROM NEWEMP WHERE EMPNO=2;
2 rows deleted.
SQL>SELECT * FROM OLDEMP;
EMPNO ENAME JOB
----------------------------------------------------------------
2 sd mgr
2 dsd ed

SQL>SELECT * FROM NEWEMP;

EMPNO ENAME JOB


----------------------------------------------------------------
1 sdsd gm

RESULT:
Thus the commands were wriiten and successfully executed using
PL/SQL.
Expt. No : 7 `
Date :

Procedures and Functions

Aim:
To implement and execute procedures and functions in Oracle database
using Procedural Language concepts.

Procedure & Function


- PL / SQL has 2 types of sub program
- Procedure and Function
- Procedure is used to perform an action
- Function is used to compute a value

Procedure:
- Procedure is a sub program used to perform an action
- Replace – recreates the procedure if it already exists

3 Modes:
 In – Means you must specify a value for the argument at the time execution
of the procedure
 Out – Passes back a value to its calling program
 In Out – When calling the procedure, you must specify the value and that
procedure passes the value back to the calling procedure

Procedure Syntax:
Create or replace procedure < procedure_name > (argument { in, out,
in out } data type) { is, as }
Variable declaration
Begin
PL / SQL Subprogram body
Exception
Exception PL / SQL Block
End;

FUNCTION:
- A function is a sub program that accepts argument and returns a
unique value to the caller.
-
Function Syntax:
Create or replace function <functionname> [(parameter[,parameter,…])] return
<datatype> is
Begin
Executable statement;
Exception
Exception handlers;
End [functionname];

Return statement – It immediately completes the execution of a subprogram and


return control to the caller

Exercise :
1. Write the PLSQL program to add two numbers?
2. Write the PLSQL program to find the amount from loan
branch?
3. Write the PLSQL program to find the value of i&j?
4. Write the PLSQL program to find value of i in the loop?
1)Write the PLSQL program to add two numbers?
SQL> set serveroutput on
SQL>
1 declare
2 a integer;
3 b integer;
4 c integer;
5 begin
6 a:=10;
7 b:=12;
8 c:=a+b;
9 dbms_output.put_line('c is'||c);
10 end;
SQL> /
c is22
PL/SQL procedure successfully completed.

2)Write the PLSQL program to find the amount from loan branch?
SQL>declare
2 amt integer;
3 begin
4 select amount into amt from loanz where loan_no='&loan_number';
5 dbms_output.put_line('amount is'||amt);
6 end;
7 /
Enter value for loan_number: 1_11
old 4: select amount into amt from loanz where loan_no='&loan_number';
new 4: select amount into amt from loanz where loan_no='1_11';
amount is900
PL/SQL procedure successfully completed.

3)Write the PLSQL program to find the value of i&j?


SQL> set serveroutput on
SQL> declare
2 i number:=0;
3 j number:=0;
4 begin
5 while i<=100 loop
6 j:=j+i;
7 i:=i+2;
8 end loop;
9 dbms_output.put_line('the value is'||j);
10 end;
11 /
the value is2550
PL/SQL procedure successfully completed.

4)Write the PLSQL program to find value of i in the loop?


SQL> ed
Wrote file afiedt.buf
1 begin
2 for i in 1..2
3 loop
4 dbms_output.put_line('i is'||i);
5 end loop;
6* end;
SQL> /
i is1
i is2
PL/SQL procedure successfully completed.

FUNCTIONS

CREATE OR REPLACE FUNCTION WITHDRAW(ACNO


NUMBER,AMT NUMBER) RETURN NUMBER IS BAL NUMBER(10);
BEGIN
SELECT BAL_AMT INTO BAL FROM AVIBNK WHERE
ACCNO=ACNO;
IF(AMT < BAL)THEN
BAL:=BAL - AMT;
END IF;
RETURN (BAL);
END;
Function created.

CREATE OR REPLACE FUNCTION DEPOSIT(ACNO NUMBER,AMT


NUMBER) RETURN NUMBER IS BAL NUMBER(10);
BEGIN
SELECT BAL_AMT INTO BAL FROM AVIBNK WHERE
ACCNO=ACNO;
BAL:=BAL+AMT;
RETURN(BAL);
END;

Function created.

SET SERVEROUTPUT ON
DECLARE
NO NUMBER(6);
C CHAR(2);
AM NUMBER(10);
BB NUMBER(10);
BEGIN
NO:=&NO;
C:='&C';
AM:=&AMOUNT;
IF C='W' THEN
BB:=WITHDRAW(NO,AM);
UPDATE AVIBNK SET BAL_AMT=BB WHERE ACCNO=NO;
ELSIF C='D' THEN
BB:=DEPOSIT(NO,AM);
UPDATE AVIBNK SET BAL_AMT=BB WHERE ACCNO=NO;
ELSE
DBMS_OUTPUT.PUT_LINE('NO CAHR FOUND');
END IF;
END;
OUT PUT:

Enter value for no: 101


old 7: NO:=&NO;
new 7: NO:=101;
Enter value for c: W
old 8: C:='&C';
new 8: C:='W';
Enter value for amount: 123
old 9: AM:=&AMOUNT;
new 9: AM:=123;

PL/SQL procedure successfully completed.

SQL> SELECT * FROM AVIBNK;

ACCNO CUSTOMER_N BAL_AMT


---------- ---------- -------- ----------
101 AVI 9877
102 NAS 5000

SQL> create or replace function fitem(name varchar2,q number)return


number is
2 qy item.qty%type;
3 co item.cost%type;
4 amt item.cost%type;
5 begin
6 select qty,cost into qy,co from item where iname=name;
7 if(qy>q)then
8 amt:=q*co;
9 update item set qty=qy-q where iname=name;
10 end if;
11 return(amt);
12 end;
13 declare
14 name item.iname%type;
15 q item.qty%type;
16 amt item.cost%type;
17 i number(5);
18 n number(5);
19 begin
20 name:='&name';
21 q:=&qty;
22 amt:=fitem(name,q);
23 dbms_output.put_line(amt);
24 end;
25 /
Enter value for name: book
old 20: name:='&name';
new 20: name:='book';
Enter value for qty: 10
old 21: q:=&qty;
new 21: q:=10;
200

PL/SQL procedure successfully completed.

SQL> select * from item;

NAME ICODE QTY COST


----------------------------------------------------------
Book 101 40 20
Heropen 102 10 25

RESULT:
Thus the program for implementing procedures and functions in
oracle database using procedural langage concepts was executed
successfully.
Expt . No: 8
Date:
RAILWAY RESERVATION

AIM:
To write a PL/SQL to perform reservation in railway using
procedures.

ALGORITHM:

 Start the program.


 Write the program and main program and compile them
separately.
 After compiling give the inputs for needed reservation of
ticket using seats which are free.
 The Name,Address,Destination are given and output is
obtained.
 Stop the program.

PL/SQL:

DECLARE
<declaration>;
BEGIN
<executable statement>;
EXCEPTION
<exception handling>;
END;

SYNTAX FOR CREATING STORED PROCEDURE:

CREATE OR REPLACE
PROCEDURE{Schema}procedure name
(argument(IN,OUT,INOUT)datatypes…….)
{.S,AS}
Variable decleration;
Constant decleration;
BEGIN
PL/SQL subprogram body;
EXCEPTION
Exception PL/SQL block;
END;

SQL> create table train1(tno number(10),tname varchar2(20),origin


varchar2(20),dest varchar2(15),dat date,nos number(10));
Table created.

SQL> insert into train1


values('&tno','&tname','&origin','&dest','&dat','&nos');
Enter value for tno: 8100
Enter value for tname: cheran
Enter value for origin: erode
Enter value for dest:
Enter value for dat:
Enter value for nos:
old 1: insert into train1
values('&tno','&tname','&origin','&dest','&dat','&nos')
new 1: insert into train1 values('8100','cheran','erode','','','')

1 row created.

SQL> insert into train1


values('&tno','&tname','&origin','&dest','&dat','&nos');
Enter value for tno: 8102
Enter value for tname: mail
Enter value for origin: mangalore
Enter value for dest: mail
Enter value for dat: 20-oct-03
Enter value for nos: 899
old 1: insert into train1
values('&tno','&tname','&origin','&dest','&dat','&nos')
new 1: insert into train1 values('8102','mail','mangalore','mail','20-oct-
03','899')
1 row created.

SQL> insert into train1


values('&tno','&tname','&origin','&dest','&dat','&nos');
Enter value for tno: 8103
Enter value for tname: malabar
Enter value for origin: chennai
Enter value for dest: mangalore
Enter value for dat: 16-oct-03
Enter value for nos: 999
old 1: insert into train1
values('&tno','&tname','&origin','&dest','&dat','&nos')
new 1: insert into train1 values('8103','malabar','chennai','mangalore','16-
oct-03','999')

1 row created.

SQL> create table passeng(name varchar2(20),tname varchar2(20),tno


number(10),origin varchar2(20),dest varchar2(20),dat date,nos number(10));
Table created.

SQL> insert into passeng


values('&name','&tname','&tno','&origin','&dest','&dat','&nos');
Enter value for name: satya
Enter value for tname: mail
Enter value for tno: 8101
Enter value for origin: chennai
Enter value for dest: tvm
Enter value for dat: 20-oct-03
Enter value for nos: 3
old 1: insert into passeng
values('&name','&tname','&tno','&origin','&dest','&dat','&nos')
new 1: insert into passeng values('satya','mail','8101','chennai','tvm','20-oct-
03','3')

1 row created.

SQL> insert into passeng


values('&name','&tname','&tno','&origin','&dest','&dat','&nos');
Enter value for name: ashwin
Enter value for tname: cheran
Enter value for tno: 8102
Enter value for origin: chennai
Enter value for dest: erode
Enter value for dat: 23-oct-03
Enter value for nos: 1
old 1: insert into passeng
values('&name','&tname','&tno','&origin','&dest','&dat','&nos')
new 1: insert into passeng
values('ashwin','cheran','8102','chennai','erode','23-oct-03','1')

1 row created.

select * from train1;

TNO TNAME ORIGIN DEST DAT NOS


--------------------------------------------------------------------------------------------
-

8100 cheran chennai erode 23-OCT-03 1000

8102 mail mangalore tvm 20-OCT-03 899

8103 malabar chennai mangalore 16-OCT-03 999

SQL> select * from passeng;


NAME TNAME TNO ORIGIN DEST DAT
NOS
-------------------- ---------
------------------------------------------------------------------------
satya mail 8101 chennai tvm 20-OCT-03
3
ashwin cheran 8102 chennai erode 23-OCT-03
1

create or replace procedure rail(oplace in char,dplace in char,dat in date,nose


in number)is
no number(5);
trno number(6);
trna char(10);
pname char(10);
begin
select tno,tname,nos into trno,trna,no from train1 where origin=oplace;
dbms_output.put_line(trno||trna||no);
if (nose<no) then
no:=no-nose;
update train1 set nos=no where origin=oplace;
else
dbms_output.put_line('no seats available');
end if;
end;
/

declare
oo varchar2(10);
ddd varchar2(10);
pname varchar2(20);
dd date;
nn number(5);
trn number(5);
tna varchar2(10);
begin
oo:='&oo';
ddd:='&ddd';
dd:='&dd';
nn:='&nn';
select tno,tname into trn,tna from train1 where origin=oo;
rail(oo,ddd,dd,nn);
pname:='&pname';
insert into passeng values(pname,tna,trn,oo,ddd,dd,nn);
end;
/

OUTPUT:

SQL>Select * from train1;

TNO TNAME ORIGIN DEST DAT NOS


--------------------------------------------------------------------------------------------
-

8100 cheran chennai erode 23-OCT-03 996

8102 mail mangalore tvm 20-OCT-03 899

8103 malabar chennai mangalore 16-OCT-03 999

SQL>select * from passeng;


NAME TNAME TNO ORIGIN DEST DAT
NOS
-------------------- ---------
------------------------------------------------------------------------
satya mail 8101 chennai tvm 20-OCT-03
3

ashwin cheran 8102 chennai erode 23-OCT-03


1

durga cheran 8100 chennai tvm 23-OCT-03


4

RESULT:
Thus the program to perform railway reservation using procedure was
executed successfully.
Expt.No: 9
Date:

DESIGN AND IMPLEMENTATION OF PAYROLL PROCESSING


SYSTEM

AIM:
To design and implement payroll processing system using visual basic
6.0 as front end and oracle 8.0 as back end.

VISUAL BASIC AND ORACLE:

Visual basic(a Microsoft product) can access oracle database using


ADODC connectivity.
Visual basic has an advantage over oracle forms based on the fact that
it is a more mature.
Product and therefore offers the availability of many more add-on
components.
The primary disadvantage of visual basic is its lack of support for data
integrity issues.
For connecting oracle in VB,oracle lient service needs to be
configures on your machine.You can use ADO to connect oracle in VB.

Following is connection string for aracle in VB.

“Provider= ‘ ‘;Password= ‘ ‘;User ID= ‘ ‘;Data Source= ‘ ‘;Persist Security


info-True”
FORM1:

Private Sub Command1_Click()


Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields(0) = Text1.Text
Adodc1.Recordset.Fields(1) = Text2.Text
Adodc1.Recordset.Fields(2) = Text3.Text
Adodc1.Recordset.Fields(3) = Text4.Text
Adodc1.Recordset.Fields(4) = Text5.Text
Adodc1.Recordset.Fields(5) = Text6.Text
Adodc1.Recordset.Fields(6) = Text7.Text
Adodc1.Recordset.Update
MsgBox "record updated", vbInformation, "update"
End Sub

Private Sub Command2_Click()


Form2.Show
Form1.Hide
End Sub

FORM2:

Private Sub Command1_Click()


Dim s As Integer
Adodc1.Recordset.MoveFirst
Do While Adodc1.Recordset.EOF = False
s=0
If (Adodc1.Recordset.Fields(0) = Text1.Text) Then
s=1
Exit Do
Else
Adodc1.Recordset.MoveNext
End If
Loop
If s = 1 Then
Text2.Text = Adodc1.Recordset.Fields(1)
Text3.Text = Adodc1.Recordset.Fields(6)
Else
MsgBox "the empid does not exist", vbCritical, "error"
End If
End Sub

Private Sub Command2_Click()


Text4.Text = Val(Text1.Text)
Text9.Text = Text2.Text
Text10.Text = Val(Text3.Text)
If Val(Text3.Text <= 5000) Then
Text5.Text = Val(Text3.Text) * 0.1
Text6.Text = Val(Text3.Text) * 0.08
Text7.Text = Val(Text3.Text) * 0.08
Text8.Text = Val(Text3.Text) * 0.07
Text11.Text = Val(Text3.Text) + Val(Text5.Text) + Val(Text6.Text) + Val(Text7.Text)
Text12.Text = Val(Text11.Text) - Val(Text8.Text)
Else
Text5.Text = Val(Text3.Text) * 0.12
Text6.Text = Val(Text3.Text) * 0.01
Text7.Text = Val(Text3.Text) * 0.09
Text8.Text = Val(Text3.Text) * 0.09
Text11.Text = Val(Text3.Text) + Val(Text5.Text) + Val(Text6.Text) + Val(Text7.Text)
Text12.Text = Val(Text11.Text) - Val(Text8.Text)
End If
Adodc2.Recordset.AddNew
Adodc2.Recordset.Fields(0) = Text4.Text
Adodc2.Recordset.Fields(1) = Text2.Text
Adodc2.Recordset.Fields(2) = Text10.Text
Adodc2.Recordset.Fields(3) = Text5.Text
Adodc2.Recordset.Fields(4) = Text6.Text
Adodc2.Recordset.Fields(5) = Text7.Text
Adodc2.Recordset.Fields(6) = Text8.Text
Adodc2.Recordset.Fields(7) = Text11.Text
Adodc2.Recordset.Fields(8) = Text12.Text
Adodc2.Recordset.Update
MsgBox "RECORD UPDATED", vbInformation, "update"
End Sub
OUTPUT:
FORM1:

SQL> select * from employee1;

EID ENAME DNO DNAME


---------- -------------------- ---------- --------------------
DESIGNATION ADDRESS SALARY
-------------------- -------------------- ----------
2000 aparna 59 tl
banker chennai 6000
FORM2:

SQL> select * from salary1;

EID ENAME BASICPAY HRA DA TA


---------- -------------------- ---------- ---------- ---------- ----------
DED GROSSPAY NETPAY
---------- ---------- ----------
2000 devi 6000 720 60 540
540 7320 6780
RESULT:
Thus implementation of payroll processing system using Visual
basic 6.0 and Oracle *.0 has been executed successfully.
Expt.No: 10
Date:

NORMALISATION

AIM:
To create a database(table) and implement
the normalization concept to the SQL server
environment.

ALGORITHM:

1. Start the program


2. The prompted login-id and
password(oracle)once given,
SQLprompt appears and connected to the
DBA Now ready to access to
PL/SQL commands and
statement to DBA.
3.Create table insert data into the table.
4.Create view table for implementing
normalization concept.
5.Show the result of 1NF,2NF,3NF of the
given database.
6.print out with necessary details.
0NF:
SQL> select * from custord;

CUSTNAME PRODNO PRODDESC QTY CUSTADDR


DATEORD ORDNO
---------------- ----------------- ------------------ ------- ------------------
---------------- -----------
Shobana 5 Chocolate 200 Ambur 29-
MAR-83 5
Abi 459 Shirts 5 Vellore 16-
JUN-05 15
Karan 256 Pen 2 Chennai 03-
MAY-06 70

1NF-remove multi valued attributes:

SQL> create view custtable as select custname, custaddr from custord;


View created.
SQL> select * from custtable;

CUSTNAME CUSTADDR
------------------------ ----------------------
Shobana Ambur
Abi Vellore
Karan Chennai

SQL> create view customer_order as select custname, custaddr from


custord;
View created.
SQL> select * from customer_order;

CUSTNAME PRODNO PRODDESC QTY DATEORD


ORDNO
--------------------- --------------- ------------------- ----------- -----------------
----------
Shobana 5 Chocolate 200 29-MAR-83
5
Abi 459 Shirts 5 16-JUN-05
15
Karan 256 Pen 2 03-MAY-06
70

2NF-remove partial dependencies:

SQL> create view custtable as select custname, custaddr from custord;


View created.
SQL> select * from custtable;

CUSTNAME CUSTADDR
------------------------ ----------------------
Shobana Ambur
Abi Vellore
Karan Chennai

SQL> create view c_order as select custname, ordno from custord;


View created.
SQL> select * from c_order;

CUSTNAME ORDNO
------------------------------ ----------
Shobana 5
Abi 15
Karan 70

SQL> create view cust_order as select ordno, prodno, proddesc, qty, dateord
from custord;
View created.
SQL> select * from cust_order;

ORDNO PRODNO PRODDESC QTY DATEORD


------------- ------------- -------------------- ---------- ------------------
5 5 Chocolate 200 29-MAR-83
15 459 Shirts 5 16-JUN-05
70 256 Pen 2 03-MAY-06

3NF-remove transitive dependencies:

SQL> create view custtable as select custname, custaddr from custord;


View created.
SQL> select * from custtable;

CUSTNAME CUSTADDR
------------------------ ----------------------
Shobana Ambur
Abi Vellore
Karan Chennai

SQL> create view c_order as select custname, ordno from custord;


View created.
SQL> select * from c_order;

CUSTNAME ORDNO
------------------------------ ----------
Shobana 5
Abi 15
Karan 70

SQL> create view ord_only as select ordno, prodno, qty, dateord from
custord;
View created.
SQL> select * from ord_only;

ORDNO PRODNO QTY DATEORD


------------ -------------- ---------- -----------------
5 5 200 29-MAR-83
15 459 5 16-JUN-05
70 256 2 03-MAY-06
SQL> create view cust_product as select prodno, proddesc from custord;
View created.
SQL> select * from cust_product;

PRODNO PRODDESC
---------------- ------------------
5 Chocolate
459 Shirts
256 Pen

SQL> Commit;
Commit complete.

RESULT:
Thus a database table was created and
the normalization concept to the database in
SQL server environment was implemented.

You might also like