Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CS2258 DBMS Manual

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

1.

DATA DEFINITION, TABLE CREATION, CONSTRAINTS

AIM:
 CREATE objects like CREATE TABLE, CREATE FUNCTION, CREATE
SYNONYM, CREATE VIEW. Etc.

 ALTER   Objects like ALTER TABLE, ALTER USER, ALTER TABLESPACE,


and ALTER DATABASE. Etc.

 DROP Objects like DROP TABLE, DROP USER, DROP TABLESPACE, and
DROP FUNCTION. Etc.

 TRUNCATE (delete all rows) a table.


 
Create:
To create tables, views, synonyms, sequences, functions, procedures, packages etc.

To create tables: -

Purpose: This query is used to create a table.

Syntax:

Create table <table_name>(“column1” “DataType for


column1”,”column2” “DataType for Column2”,..);

Example:
create table emp (empno number(5) ,
                   name varchar2(20),
                   sal number(10,2),
                   job varchar2(20),
                   comm number(10,2));
Output:-
Table Created.

1
To create view: -

Purpose: Views can be considered as virtual tables. A table has a set of definition, and it
physically stores the data. A view also has a set of definitions, which is build on top of table(s)
or other view(s),and it does not physically store the data.

Syntax:

Create view <view name> AS <SQL Statement>

Example:

CREATE VIEW V_emp As SELECT * FROM EMP;

Output: View created.

SQL> select * from V_emp;

EMPNO NAME SAL JOB COMM


----------------------------------------------------------------------------------
11 xxx 15000 SE 2258965
22 yyy 20000 DE 2245689

To create Primary Key :-

Purpose: A primary key is used to identify each row in a table. When describe that table,
it shows NOT NULL value in NULL column.

Syntax :

Create table <table_name>(“column1” “DataType for column1” ”PRIMARY


KEY”,”column2” “DataType for Column2”,..);

Example:

create table emp (empno number(5) primary key,


                   name varchar2(20),
                   sal number(10,2),
                    job varchar2(20),
                    comm number(10,2));

Output: Desc emp;

2
Name Null? Type

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

EMPNO NOT NULL NUMBER(5)

NAME VARCHAR2(20)

SAL NUMBER(10,2)

JOB VARCHAR2(20)

COMM NUMBER(10,2)

CREATE FOREIGN KEY: -

Purpose:

A foreign key is a field (or fields) that points to the primary key of another table.

Syntax :

Create table <table_name>(“column1” “DataType for column1” REFERENCES


<table_name>(primary_key_column), (“column2” “DataType for column2”,etc.,);

Example:

create table dept (deptno number(5) primary key,


                   dept_name varchar2(20),
employeeno number(5) references
emp(empno));

Output:

SQL> create table dept(deptno number(5) primary key,dept_name varchar2(20), employeeno


number(5) references emp(empno));

Table created.

3
SQL> desc dept;

Name Null? Type

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

DEPTNO NOT NULL NUMBER(5)

DEPT_NAME VARCHAR2(20)

EMPLOYEENO NUMBER(5)

CREATE DEFAULT : -

Purpose:

The DEFAULT constraint is used to insert a default value into a column.

Syntax :

Create table <table_name>(“column1” “Data Type for column1”,


(“column2” “Data Type for column2”default(‘name’),etc.,);

Example:

create table dept_defalt (deptno number(5) primary key,


                   dept_name varchar2(20) default(‘IT’));

Output:

SQL> insert into dept_defalt (deptno) values(11);

1 row created.

SQL> insert into dept_defalt (deptno) values(12);

1 row created.

SQL> insert into dept_defalt (deptno,dept_name) values(13,’MCA’);

1 row created.

4
SQL> select * from dept_defalt;

DEPTNO DEPT_NAME

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

11 IT
12 IT

13 MCA

ALTER:-

Purpose:

Once a table is created in the database ,there are many occasions where one may wish
to change the structure of the table. Typical cases include the following:

 Add a column
 Modify the datatype for a column

ALTER Table – Add a column :

Purpose:

To add a new column to this table.

Syntax :

Alter table <table_name> add(“column1” “Data Type for column1”,


(“column2” “Data Type for column2”,etc.,);

Example:

Alter table emp add(address varchar2(20));

Output:

SQL> Alter table emp add(address varchar2(20));

Table altered.

SQL> Desc emp;

5
Name Null? Type

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

EMPNO NOT NULL NUMBER(5)

NAME VARCHAR2(20)

SAL NUMBER(10,2)

JOB VARCHAR2(20)

COMM NUMBER(10,2)

ADDRESS VARCHAR2(20)

DROP:

Purpose:

To delete a table ( the table structure, attributes and indexes will also be deleted).

Syntax :

Drop table <table_name>

Example:

Drop table dept_defalt;

Output:

SQL> drop table dept2;

Table dropped.

Result :

Thus the DDL program has been successfully executed and verified.

6
2 . INSERT, SELECT COMMANDS, UPDATE & DELETE COMMANDS

AIM:

The Data Manipulation Language (DML) is used to retrieve, insert and modify
database information.

a.INSERT :

Purpose:

The INSERT command in SQL is used to add records to an existing table.


Syntax:
Insert into <table name> values(value list);

Example:
Insert into dept values(11,’IT’,1101);

Output:

SQL> insert into dept values(11,’IT’,1101);

1 row created.

SQL> insert into dept values(12,’IT’,1101);

1 row created.

b.SELECT :

Purpose:
The SELECT allows database users to retrieve the specific information from the
table.

Syntax:
 To retrieve all attributes from the particular table.
select * from <table name>;

SQL> select * from dept;

DEPTNO DEPT_NAME EMPLOYEENO


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

14 IT 1101

7
15 IT 1102

 To retrieve selected attributes from the particular table.

SQL> select deptno,deptname from dept;

DEPTNO DEPT_NAME

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

16 IT
17 IT

c.INSERT using Null Values :

It is used to inserting the null values by creating the table itself.

Syntax:

Insert into <table name> values(NULL,NULL);

Example:

SQL> insert into dept(deptno,dept_name,EmployeeNo) values(15,null,’1103');

Output:

1 row created.

SQL> select * from dept;

DEPTNO DEPT_NAME EMPLOYEENO

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

18 IT 1101
19 IT 1102

20 1103

d.UPDATE :

Purpose:

The update statement used to modify the data in a table.

8
Syntax:

Update <table name> set <column name>=[expression,… [where <condition>]];

Example:

Update dept set dept_name=’CSE’ where deptno=’13’;

Output:

SQL> Update dept set dept_name=’CSE’ where deptno=’13’;

1 row updated.

SQL> select * from dept;

DEPTNO DEPT_NAME EMPLOYEENO

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

13 CSE 1101
14 IT 1102

15 1103

e.DELETE:

Purpose:

The DELETE command with a WHERE clause can be used to remove his record from
the personal_info table.

Syntax:

Delete from <table name> [where <condition>]];

Example:

Delete from emp where deptno=’13’;

Output:

SQL> delete from emp where deptno=’13’;

9
1 row deleted.

SQL> select * from dept;

DEPTNO DEPT_NAME EMPLOYEENO

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

14 IT 1102

15 1103

f. SELECT with Constraints:

Select using WHERE clause:


Purpose:
To conditionally select data from a table, a where clause can be added to the SELECT
statement.

Syntax:
select <column name> from <table name> [ where <condition>];

Example:
select name from dept where Dept_name=’IT’;

Output:
SQL> select * from dept where Dept_name=’IT’;

DEPTNO DEPT_NAME EMPLOYEENO

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

14 IT 1102

15 CSE 1103

16 IT 1104

Select using Order by:

Purpose:

Order by used to select the statement in order of that particular column.

10
Syntax:

Select <column name> from <table name> order by <column name>;

Example:

SQL> select * from dept order by deptno;

Output:

SQL> select deptno, dept_name from dept order by dept_name;

DEPTNO DEPT_NAME

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

15 CSE

14 IT

16 IT

Select using Order by Ascending :

SQL> select deptno, dept_name from dept order by dept_name asc;

DEPTNO DEPT_NAME

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

15 CSE

14 IT

16 IT

Select using Order by Descending:

SQL> select deptno, dept_name from dept order by dept_name desc;

DEPTNO DEPT_NAME

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

11
14 IT

16 IT

15 CSE

Select using LIKE operator :

Purpose:

% (percentage) and _ (underscore) have a special meaning. These two wild card
characters are used with LIKE operator.
 % allows matching any string of any length (including zero length)
 _ allows matching a single character

 start with search using an input/search string containing % or _. The expected


output should return only those strings that actually contain these character(s) at
the specified places and not make use of the “special meaning” attached to
them.

Syntax:

Select * from <table name> where <column name> like ‘<char>%’

SQL> select * from dept where dept_name like ‘%s%’;

DEPTNO DEPT_NAME

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

15 CSE

Result:

Thus the DML program has been successfully executed and verified.

12
3 . NESTED QUERIES & JOIN QUERIES

AIM:

To write nested queries and join queries operations in the tables.

Nested Query:

SQL nested query is SELECT Query that is nested inside a Select, Update, Insert or
Delete query.

Syntax of Nested Query:

SELECT <column name> from <table name> where <condition>


(any query for select/update/insert/delete);

Example of Nested Queries:

SQL> select * from itemfile where qty=(select qty from itemfile1


where max=40);

MAX QTY ITEMAMT


---------- ---------- ----------
40 34 56

SQL> select * from itemfile where qty>all(select qty from itemfile1 where
max=40);

MAX QTY ITEMAMT


---------- ---------- ----------
50 90 70

SQL> select * from itemfile where qty=(select qty from itemfile1 where
max=40) or itemamt in(select itemamt from itemfile);

MAX QTY ITEMAMT

13
---------- ---------- ----------
100 200 300
50 90 70
40 34 56

Join Query :
 A join is a query that combines rows from two or more tables,
views, or materialized views.
 Oracle performs a join whenever multiple tables appear in the
query's FROM clause.
 The query's select list can select any columns from any of these tables.
 If any two of these tables have a column name in common, you must qualify
all references to these columns throughout the query with table names to avoid
ambiguity.

Join Conditions :
 Most join queries contain WHERE clause conditions that compare two columns, each
from a different table.Such a condition is called a join condition.

 To execute a join, Oracle combines pairs of rows, each containing one row from each
table, for which the join condition evaluates to TRUE. The columns in the join conditions
need not also appear in the select list.

Example of Join Queries:

SQL> select * from itemfile;


Output:

MAX QTY ITEMAMT


---------- ---------- ----------
100 200 300
50 90 70
40 34 56

SQL> select * from itemfile1;

Output:

MAX QTY ITEMAMT


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

14
300 200 300
300 200 300
40 34 56

Equi Join :
 An equijoin is a join with a join condition containing an
equality operator ( = ). An equijoin combines rows that have
equivalent values for the specified columns.

SQL> select itemfile.qty,itemfile1.max from itemfile,itemfile1 where


itemfile.max=itemfile1.max;

Output:

QTY MAX
---------- ----------
34 40

Non-Equi join:

 Non equi joins is used to return result from two or more tables
where exact join is not possible.

SQL> select itemfile.qty,itemfile1.max from itemfile,itemfile1 where


itemfile.max>itemfile1.max;

Output:

QTY MAX
---------- ----------
90 300
34 300
90 300
34 300
90 40

Outer join:

An outer join extends the result of a simple join. An outer join returns all
rows that satisfy the join condition and also returns some or all of those rows from one table
for which no rows from the other satisfy the join condition.

15
 To write a query that performs an outer join of tables A and B and returns all rows from
A (a left outer join), use the ANSI LEFT[OUTER] JOIN syntax, or apply the outer
join operator (+) to all columns of B in the join condition. For all rows in A that have
no matching rows in B, Oracle returns null for any select list expressions containing
columns of B.
 To write a query that performs an outer join of tables A and B and returns all rows from
B (a right outer join), use the ANSI RIGHT[OUTER] syntax, or apply the outer join
operator (+) to all columns of A in the join condition. For all rows in B that have no
matching rows in A, Oracle returns null for any select list expressions containing
columns of A.
 To write a query that performs an outer join and and returns all rows from A and B,
extended with nulls if they do not satisfy the join condition (a full outer join), use the
ANSI FULL [OUTER] JOIN syntax.

SQL> select itemfile.qty,itemfile1.max from itemfile,itemfile1 where


itemfile.max=itemfile1.max(+);

Output:

QTY MAX
---------- ----------
200
34 40
90

Union:

SQL> select *from itemfile union select *from itemfile1;

Output:

MAX QTY ITEMAMT


---------- ---------- ----------
100 200 300
300 200 300
40 34 56
50 90 70

Union All:
SQL> select * from itemfile union all select * from itemfile1;

16
Output:

MAX QTY ITEMAMT


---------- ---------- ----------
100 200 300
50 90 70
40 34 56
300 200 300
300 200 300
40 34 56

6 rows selected.

Intersection:

SQL> select * from itemfile intersect select * from itemfile1;

Output:

MAX QTY ITEMAMT


---------- ---------- ----------
40 34 56

Minus:

SQL> select * from itemfile minus select * from itemfile1;

Output:

MAX QTY ITEMAMT


---------- ---------- ----------
100 200 300
50 90 70

Result:

Thus the Nested and Join Queries has been successfully executed and verified.

17
4.VIEWS

AIM:

To create a view for whole table or partial data in that table.

Purpose:

Views can be considered as virtual tables. A table has a set of definition, and it
physically stores the data. A view also has a set of definitions, which is build on top of table(s)
or other view(s),and it does not physically store the data.

Syntax:

Create view <view name> AS <SQL Statement>

Example:

CREATE VIEW V_emp As SELECT * FROM EMP;

Output:

View created.

SQL> select * from V_emp;

EMPNO NAME SAL JOB COMM


--------------------------------------------------------------------------
11 xxx 15000 SE 2258965
22 yyy 20000 DE 2245689

Example:

CREATE VIEW V_ Singlecol As SELECT empno FROM EMP;

18
Output:

View created.

SQL> select * from V_ Singlecol;

EMPNO
------------
11
22

Example:

CREATE VIEW V_colEmp As SELECT empno,name,sal FROM EMP;

Output:
View created.

SQL> select * from V_colEmp;

EMPNO NAME SAL


--------------------------------------
11 xxx 15000
22 yyy 20000

Result:

Thus the view creation program has been successfully executed and verified.

19
5 . HIGH LEVEL PROGRAMMING LANGUAGE EXTENSIONS

(CONTROL STRUCTURES, PROCEDURES AND FUNCTIONS)

AIM :

To write programs a High Level Language extensions with stored procedures and

functions in PL/SQL statements.

Procedures:

The procedure is a program that performs an action and does not return a value (outside of IN
OUT and OUT parameters).

Functions:

The function is a program that might perform an action and does return a value.

 Named PL/SQL programs (procedures and functions) can take


parameters. Parameters are named variables that are available to a program and that modify
program behavior and/or data. Parameters are optional on both procedures and functions.
 Parameters are declared when a procedure or function are declared and are declared
between an open and a close parenthesis (()).
When you create a procedure or function, you may define parameters. There are three
types of parameters that can be declared:
1. IN - The parameter can be referenced by the procedure or function. The value of the
parameter can not be overwritten by the procedure or function.
2. OUT - The parameter can not be referenced by the procedure or function, but the value
of the parameter can be overwritten by the procedure or function.
3. IN OUT - The parameter can be referenced by the procedure or function and the value
of the parameter can be overwritten by the procedure or function.
4.
Procedures VS Functions:

20
Procedures are traditionally the workhorse of the coding world and functions are traditionally
the smaller, more specific pieces of code. In general, if you need to update the chart of
accounts, you would write a procedure. If you need to retrieve the organization code for a
particular GL account, you would write a function.

A few more differences between a procedure and a function:

 A function MUST return a value.


 A procedure cannot return a value.
 Procedures and functions can both return data in OUT and IN OUT
Parameters.
 The return statement in a function returns control to the calling
program and returns the results of the function.
 The return statement of a procedure returns control to the calling
program and cannot return a value.
 Functions can be called from SQL, procedure cannot.
 Functions are considered expressions, procedure are not.

Procedure/Stored Procedure Syntax:

CREATE OR REPLACE PROCEDURE


procedure_name[(parameter [,parameter]) ]
AS
//Variable declaration
BEGIN
//Executable Section
[EXCEPTION
    exception_section]
END; (or) END procedure_name;

Procedure/Stored Procedure Example:

SQL> set serveroutput on;

21
SQL> create or replace procedure addacc(acc_id in
varchar2,name in varchar2,balance in number)
2 as
3 begin
4 insert into accoun111 values(acc_id,name,balance);
5 dbms_output.put_line('value added');
6 end addacc;
7 /

Output:

Procedure created.

SQL> exec addacc('a005','karti',6000);


value added

PL/SQL procedure successfully completed.

SQL> select *from accoun111;

ACC_ID NAME BALANCE


-------------------- -------------------- ---------
a001 nipa 5000
a002 rani 3000
a003 priya 4000
a005 karti 6000

2. Procedure to update the balance when greater than 2500


SQL> create or replace procedure updacc(accno varchar2)
2 as
3 bal number(20);
4 begin
5 select balance into bal from account111 where acc_id=accno;
6 if bal>=2500 then
7 update account111 set balance=balance-2000 where acc_id=accno;
8 dbms_output.put_line('balance updated');
9 else
10 dbms_output.put_line('Updated amount below account limit');

22
11 end if;
12 exception when no_data_found then
13 dbms_output.put_line('No such data found');
14 end;
15 /

Output:
Procedure created.
SQL> select * from account111;
ACC_ID NAME BALANCE
---------- ---------- ---------
a0001 maha 3000
a0002 praveen 1000
a0003 anand 4000

SQL> exec updacc('a0003');


balance updated
PL/SQL procedure successfully completed.

SQL> select * from account111;


ACC_ID NAME BALANCE
---------- ---------- ---------
a0001 maha 3000
a0002 praveen 1000
a0003 anand 2000

Function Syntax :

CREATE OR REPLACE FUNCTION <function_name>


RETURN <variable_type>

IS | AS
<variable declarations>

23
BEGIN
  <code_here>;
END <function_name>;
/

Sample table(account11) data:


SQL> select * from account11;
ACC_ID NAME BALANCE
---------- ---------- ---------
a0001 rani 100
a0002 radha 200
a0003 ravi 600
a0004 dhana 300
a0005 praveena 400
a0006 aaaa 100
a0007 bbbb 100
a0008 cccc 100

Function Example:

SQL> set serveroutput on;

SQL> create or replace function f1(id in varchar2)


RETURN varchar2
IS
2 cus_name account11.name%type;
3 begin
4 select name into cus_name from account11 where acc_id=id;
5 RETURN cus_name;
6 end;
7 /

24
Output:
Function created.

SQL> declare
2 no account11.acc_id%type;
3 name account11.name%type;
4 begin
5 no:='&account_id';
6 name:=f1(no);
7 dbms_output.put_line('account number:'||no);
8 dbms_output.put_line('customer name:'||name);
9 exception when no_data_found then
10 dbms_output.put_line('no such account id exists');
11 end;
12 /

Output:
Enter value for account_id: a0001
old 5: no:='&account_id';
new 5: no:='a0001';
account number:a0001
customer name:rani

PL/SQL procedure successfully completed.


SQL> /
Enter value for account_id: a0009
old 5: no:='&account_id';
new 5: no:='a0009';
no such account id exists

PL/SQL procedure successfully completed.

25
Result:

Thus the High Level Programming Language Extension has been successfully executed
and verified.

6. DESIGN AND IMPLEMENTATION OF PAYROLL PROCESSING SYSTEM

( Front End Tools , Forms & Menu Design )

AIM:

To develop an employee payroll system using Oracle as a back end (database) and
Microsoft Visual Basic6.0 as a front end.

PROCEDURE:

1. Create a employee database with the following fields in oracle namely


Empno,Empname,address,Basicpay and insert some record into the database.

2. Design the corresponding form with labels,text boxes and command buttons.

Form1Employee Payroll

3. Create the back with the front end using DAO method by creating a DSN as
follows,
a.Select administrative tools option from control panel.
Then click on Data Source(ODBC),which displays a dialog box named
ODBC DATASOURCES ADMINISTRATOR in which click Add button.
b.In Create New Data Source dialog box,select “Microsoft ODBC for
ORACLE” and click finish button
.
c.Give a suitable data source name,user name and server name.

4. Perform the required operations like Insert,Delete,Searching,Modify and Exit.

26
5. Calculate the net salary of the employee.

6. Execute the project.

TABLE DESIGN:
Table Name :Employee

NAME TYPE
EMPNO NUMBER(6)
EMPNAME VARCHAR2(30)
DESIGNATION VARCHAR2(20)
DA NUMBER(10)
HRA NUMBER(10)
ADDRESS VARCHAR2(20)
TA NUMBER(10)
PF NUMBER(10)
BASICPAY NUMBER(8,2)
LOAN.Text=adodc1.Recordset.Fields(9)

ADV.Text= adodc1.Recordset.Fields(10)

TD.Text=adodc1.Recordset.Fields(11)

GP.Text=adodc1.Recordset.Fields(12)

NP.Text=adodc1.Recordset.Fields(13)

End Sub

PROGRAM TO VIEW:

Private sub view_Click()


Dim a As Integer
On Error Go To err
a=InputBox(“enter the empno”)
Adodc1.Recordset.MOVEFIRST

27
While Not Adodc1. Recordset.EOF
If Adodc1.Recordset.Fields(0)=a Then
fillt 1
Exit Sub
End If
Adodc1.Recordset.MOVENEXT
Wend
err:
MsgBox(“record doesnot exist”)
End Sub
Private sub MOVENEXT_Click()
Adodc1.Recordset.MOVENEXT
End Sub

Private sub MOVEPREVIOUS_Click()


Adodc1.Recordset.MOVEPREVIOUS
End Sub

PROGRAM TO SAVE:

Private sub save_Click()


Adodc1.Recordset.Update
NAME1.Enabled=False
NO.Enabled=False
DESIG.Enabled=False
B1.Enabled=False
HRA.Enabled=False
DA.Enabled=False
OA.Enabled=False
TA.Enabled=False
LOAN.Enabled=False
ADV.Enabled=False
TD.Enabled=False
GP.Enabled=False
PF.Enabled=False
NP.Enabled=False
add.Enabled= True
del.Enabled= True
BASIC.Enabled= True
edit.Enabled=True
End Sub

Fill function

28
Public sub fillt()
NO.Text=adodc1.Recordset.Fields(0)
NAME.Text= adodc1.Recordset.Fields(1)
DESIG.Text=adodc1.Recordset.Fields(2)
MONYH.Text=adodc1.Recordset.Fields(14)
B1.Text=adodc1.Recordset.Fields(3)

DA.Text=adodc1.Recordset.Fields(4)
HRA.Text= adodc1.Recordset.Fields(5)
OA.Text=adodc1.Recordset.Fields(6)
TA.Text=adodc1.Recordset.Fields(7)
PF.Text=adodc1.Recordset.Fields(8)
LOAN.Text=adodc1.Recordset.Fields(9)
ADV.Text= adodc1.Recordset.Fields(10)

PROGRAM TO ADD:

Private sub add_Click()


NAME1.Enabled= True
NO.Enabled= True
DESIG.Enabled= True
B1.Enabled= True
HRA.Enabled= True
DA.Enabled= True
OA.Enabled= True
TA.Enabled= True
LOAN.Enabled= True
ADV.Enabled= True
TD.Enabled= True
GP.Enabled= True
PF.Enabled= True
NP.Enabled= True
save. Enabled=True
NO.SetFocus
MsgBox”new record is added”
add. Enabled=False
del.Enabled=False
BASIC. Enabled=False
edit. Enabled=False
End Sub

PROGRAM TO CALCULATE ALLOWANCES:

29
Private sub B1_LostFocus()
If Val(B1.Text)<5000 Then
DA.Text=Val(B1)*(20/100)
HRA.Text= Val(B1)*(15/100)
OA.Text= Val(B1)*(10/100)
TA.Text=Val(B1)+Val(HRA)+Val(OA)
PF.Text=Val(B1)*(5/100)
Else
DA.Text=Val(B1)*(25/100)
HRA.Text= Val(B1)*(20/100)
OA.Text= Val(B1)*(15/100)
TA.Text=Val(DA)+Val(HRA)+Val(OA)
PF.Text=Val(B1)*(5/100)
End If
End Sub

Dim a As Integer
a=InputBox(“enter the empno”)
Adodc1.Recordset.MOVEFIRST
While Not Adodc1.Recordset.EOF
If Adodc1.Recordset.Fields(0)=a Then
Fillt
Exit Sub
End If
Adodc1.Recordset.MOVENEXT
Wend
MsgBox”record modify successfully”
add.Enabled=False
del.Enabled=False
BASIC. Enabled=False
edit. Enabled=False
End Sub

Private Sub exit_Click()


Unload Me
End Sub

Private sub Form_Click()


NAME1.Enabled= False
NO.Enabled= False
DESIG.Enabled= False
B1.Enabled= False
HRA.Enabled= False
DA.Enabled= False
OA.Enabled= False
TA.Enabled= False

30
LOAN.Enabled= False
ADV.Enabled= False
TD.Enabled= False
GP.Enabled= False
PF.Enabled= False
NP.Enabled= False
save. Enabled=True
End Sub

Private sub MOVEFIRST_Click()


Adodc1.Recordset.MOVEFIRST
End Sub

Private sub MOVELAST_Click()


Adodc1.Recordset.MOVELAST
End Sub

PROGRAM TO DELETE:

Private Sub del_Click()


Form.Caption=”delete operation”
Dim a As Integer
On Error Go To err
a=InputBox(“enter the empno”)
Adodc1.Recordset.MOVEFIRST
While Not Adodc1.Recordset.EOF
If Adodc1.Recordset.Fields(0)=a Then
Adodc1.Recordset.Delete
Exit Sub
End If

Adodc1.Recordset.MOVENEXT
Wend
MsgBox”record deleted”
err:
MsgBox”record does not exists”
End Sub
Private sub edit_Click()
Form1.Caption=”modify operation”
Adodc1.Recordset.Update
NAME1.Enabled=True
NO.Enabled= True
DESIG.Enabled= True
B1.Enabled= True
HRA.Enabled= True
DA.Enabled= True
OA.Enabled= True

31
TA.Enabled= True
LOAN.Enabled= True
ADV.Enabled= True
TD.Enabled= True
GP.Enabled= True
PF.Enabled= True
NP.Enabled= True
save. Enabled=True
NO.SetFocus

OUTPUT:

RESULT:

32
Thus the employee payroll processing system has been successfully developed and
verified.

7.TRIGGERS

AIM:

To write programs in High Level Language extension with triggers.

Parts of Data Base Trigger:

a. Triggering Event
b. Triggering constraint ( Optional )
c. Triggering Action

Triggering Action:

 Before Option : Fires the trigger only once,before executing the triggering statement.

 After Option: Fires the trigger only once ,after the execution of the triggering
statement.

 Instead of Option: Fires the trigger only once, to do something else instead of
performing the action that executed the trigger.

Syntax:

Create or replace trigger <trigger name>


Before | After
Insert | Delete | Update [of <column>]
ON <table name> } -> Triggering event
[Referencing [old as <old>] [New as <new>]] } ->Trigger constraint
Begin
PL/SQL Block  Trigger body ( Trigger Action)
End;

33
1 . Create trigger to display the message after the data insert / delete/
update operation

SQL> set serveroutput on;


SQL> create or replace trigger tro11
2 after update or insert or delete on acco111
3 for each row
4 begin
5 if updating then
6 dbms_output.put_line(‘Updating’);
7 elsif inserting then
8 dbms_output.put_line(‘Inserting’);
9 elsif deleting then
10 dbms_output.put_line(‘Deleting’);
11 end if;
12 end;
13 /

Output:

Trigger created.

SQL> insert into acco111 values(22,’john’,2300);


Inserting

1 row created.

2 . Create trigger to display the message after the data insert / delete operation

SQL> set serveroutput on;


SQL> create or replace trigger mytrig11
2 before insert or delete or update on account11
3 for each row
4 begin
5 if to_char(sysdate,'dy')='sun'then

34
6 dbms_output.put_line('today is holiday');
7 else
8 dbms_output.put_line('welcome back');
9 end if;
10 end;
11 /

Output:

Trigger created.

SQL> update account11 set balance=100 where balance=500;


welcome back

1 row updated.

3. Trigger to create error message when balance becomes less than 2000

SQL> create or replace trigger t112


2 before update on account11
3 for each row
4 begin
5 if:new.balance<100 then
6 raise_application_error(-20001,'LOW BALANCE');
7 end if;
8 end;
9 /

Output:

Trigger created.

SQL> update account11 set balance='90' where name='aaaa';


update account11 set balance='90' where name='aaaa'
*
ERROR at line 1:
ORA-20001: LOW BALANCE
ORA-06512: at "SCOTT.T112", line 3
ORA-04088: error during execution of trigger 'SCOTT.T112'

4. Create trigger with referencing new as newrow values swapped in another table

SQL> create trigger trig4 after insert on t4 referencing new as newrow

35
2 for each row when(newrow.a>10)
3 begin
4 insert into t5 values(:newrow.b,:newrow.a);
5 end trig4;
6 /

Output:

Trigger created.

SQL> insert into t4 values(2,4);

1 row created.

SQL> select * from t4;

A B
---------- ---------
2 4

SQL> insert into t4 values(12,16);

1 row created.

SQL> select * from t5;

C D
---------- ---------
16 12

SQL> select * from t4;

A B
---------- ---------
2 4
12 16

CREATE TRIGGER TO DELETE ACCOUNT DETAILS WHEN CUSTOMER GETS


DELETED

36
SQL> create table custdet(c_id varchar2(10) primary key,name varchar2(20),street_name
varchar2(20),c
ity varchar2(20),ph_no number(12));

Table created.

SQL> select * from custdet;

C_ID NAME STREET_NAME CITY PH_NO


---------------------------------- -------------------- -------------------- --------------------
c001 dhana k.k.nagar ch-24 243563
c002 praveen mgr nagar ch-43 236534
c003 hema nehru street ch-93 295123

SQL> insert into acco111 values('a0002','c001',4000);

1 row created.

SQL> insert into acco111 values('a0003','c002',5000);

1 row created.

SQL> set serveroutput on;


SQL> create or replace trigger tig112
2 after delete on custdet
3 for each row
4 begin
5 delete from acco111 where acco111.c_id=:old.c_id;
6 end;
7 /

Output:

Trigger created.

SQL> delete from custdet where c_id='c002';

1 row deleted.

SQL> select * from custdet;

C_ID NAME STREET_NAME CITY PH_NO

37
---------- -------------------- -------------------- -------------------- ---------
c001 dhana k.k.nagar ch-24 243563
c003 hema nehru street ch-93 295123

1 row deleted.

SQL> select * from acco111;

ACC_ID C_ID BALANCE


---------- ---------- ---------
a0002 c001 4000

6 . TRIGGER TO INSERT INTO SEPARATE TABLES;

SQL> create table acco_del111(acc_id varchar2(20),c_id varchar2(20),balance


number(10),deldate date);

Table created.

SQL> create table acco_ins111(acc_id varchar2(20),c_id varchar2(20),balance


number(10),insertdate date);

Table created.

Program:

SQL> set serveroutput on;


SQL> create or replace trigger tro11
2 after delete or insert on acco111
3 for each row
4 begin
5 if deleting then
6 insert into acco_del111(acc_id,c_id,balance,deldate)
values(:old.acc_id,:old.c_id,:old.balance,sysdate);
7 elsif inserting then
8 insert into acco_ins111(acc_id,c_id,balance,insertdate)
Values(:new.acc_id,:new.c_id,:new.balance,sysdate);
9 end if;
11 end;
12 /

38
Output:

Trigger created.

SQL> select * from acco111;

ACC_ID C_ID BALANCE


---------- ---------- ---------
a0002 c001 4000
a0003 c002 4500

SQL> insert into acco111 values('a0004','c003',3000);

1 row created.

SQL> select * from acco_ins111;

ACC_ID C_ID BALANCE INSERTDAT


-------------------- -------------------- --------- ---------
a0004 c003 3000 09-MAR-10

SQL> delete from acco111 where acc_id='a0002';

1 row deleted.

SQL> select * from acco_del111;

ACC_ID C_ID BALANCE DELDATE


-------------------- -------------------- --------- ---------
a0002 c001 4000 09-MAR-10

39
Result :

Thus the Triggers has been successfully executed and verified.

8. REPORTS

AIM:
To generate the REPORT using SQL commands.

Commands :

Remark :
 Tells SQL plus that the cords to follow are to be treated as comments & not instructions.
 SETHEADSEP :
The Heading separator identifies the single character that tells sql plus to split into one or
more lines.
 TTITLE :
Sets the top title for each page of a report.
 BTITLE :
Sets the bottom title for each page of a report.
 COLUMN :
Gives sql plus a variety of instructions on the heading format and treatment of a column.
 BREAKON :
Tells the sql plus how the transactions for each kind of job are grouped
together under any field.
 COMPUTESUM :
Makes sql plus ,calculate sub tools
 SETLINESIZE :
Set the maximum number of lines per page.
 SETNEWPAGE :
Set the number of blank lines between pages.

40
 SPOOL :
Moves a report you would normally displayed on the screen into a file.

PROGRAM:

SQL> desc aa1;

Name Null? Type


------------------------------- -------- ----
ACTION_DATE DATE
NAME VARCHAR2(20)
QUANTY NUMBER(10)
STORENAME VARCHAR2(20)
RATE NUMBER(10)
AMOUNT NUMBER(10)

SQL> rem REPORT CREATED BY HARRY


rem description : report for news paper
set headsep|
ttitle report about the news papers in India
column name heading ‘What was sold’
column name format a18
column storename heading ‘to whom sold’ format a18 word_wrapped
column rate format 90.99
column action_da heading ‘date’
column quanty heading ‘quantity’ format 999
column amount1 format 9999.99
compute sum of amount1 on amount
break on storename skip 2
set linesize 82
set pagesize 60
set newpage 0

41
Output:

SQL> select * from aa1;

Report about the news papers in India

ACTION_DA What was sold quantity to whom sold RATE AMOUNT


-------------------------------------------------------------------------------------------------
07-JAN-10 The Hindu 370 HifiStore 8.00 200

08-FEB-10 Economic Times 400 LogStores 6.00 600

08-MAR-10 GlobalNews 500 BloggStore 10.00 100

42
RESULT :

Thus the report has been successfully created and verified.

9. MINI PROJECT(1)

DESIGN AND IMPLEMENTATION OF BANKING SYSTEM

AIM:

To develop a banking system using Oracle as s back end(data base) and Microsoft
Visual basic6.0 as a front end.

PROCEDURE :

1. Create a banking database with the following fields in oracle


namely,acno,name,address,balance and some record into the database.

2. Design the corresponding form with labels,text boxes and command buttons.
Form1customer details
Form2withdraw
Form3deposit

3. Create the back with the front end using DAO method by creating a dsn as follows
a.Select administrative tools option from control panel.
Then click on data source(ODBC),which displays a dialog box named
ODBCDATASOURCESADMINISTRATOR in which click Add button.
b.In Create New Data Source dialog box,select “Microsoft ODBC for
ORACLE” and click finish button
c.Give a suitable data source name,user name and server name.

4. Perform the required operations like Insert,Delete,Searching and Exit.

5. Issue the books according to the no.of copies available.

43
5. Execute the project.

TABLE DESIGN:

Table Name :Banking

NAME TYPE
ACNO NUMBER(6)
NAME VARCHAR2(30)
ADDRESS VARCHAR2(20)
BALANCE NUMBER(8,2)

FORM 1:

Dim DB As Database
Dim RS As recordset

Private Sub Form_Load()


Set DB =OpenDatabase(“BANKDSN”,FALSE,FALSE,”ODBC;UID= ;PWD= ;”)
Set RS =DB.OpenRecordset(“select * from Bank”)
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
End Sub

Private Sub CLEAR_click()


Text1.text=””
Text2.text=””
Text3.text=””
Text4.text=””
End Sub

Private sub DELETE_Click()


RS.DELETE

44
MsgBox”the record is deleted”
RS.MoveNext
IfRS.EOF Then
MsgBox”no more records”
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
End If
End Sub

Private Sub EXIT Click()


End
End Sub

Private Sub FIRST_Click()


RS .MoveFirst
Text1.Text=RS(0)
Text2.Text=RS(1)
Text3.Text=RS(2)
Text4.Text=RS(3)
End Sub

Private Sub INSERT_Click()


RS.MoveLast
RS.AddNew
RS(0)=Text1.Text
RS(1) =Text2.Text
RS(2)=Text3.Text
RS(3)=Text4.Text
MsgBox”record is inserted”
RS.UPDATE
End Sub

Private Sub LAST_Click()


RS. MoveLast
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
End Sub

Private Sub NEXT_Click()


RS.Move Next
If RS.EOF Then
MsgBox”no more recfords”

45
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
EndIf
End Sub
Private Sub PREVIOUS_Click()
RS.Move Previous
If RS.EOF Then
MsgBox”no more recfords”
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
EndIf
End Sub

Private Sub UPDATE_Click()


RS.Edit
RS(0)=Text1.Text
RS(1) =Text2.Text
RS(2)=Text3.Text
RS(3)=Text4.Text
RS.UPDATE
MsgBox”record is inserted”
End Sub

Private Sub FIND_Click()


1=InputBox(“ENTER THE ACNO”,”FIND”)
rs.FindFirst”[ACNO]=”&I
If rs.NoMatch Then
MsgBox”no such records”
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
EndIf
End Sub

Private Sub WITHDRAW_Click()


Form2.Show
End Sub

Private Sub DEPOSIT_Click()

46
Form3.Show
End Sub

FORM 2:

Private Sub HOMEPAGE_Click()


Form1.Show
Form2.Hide
End Sub

Private Sub WITHDRAW_Click()


Text2.Text=val(Form1.Text)-Text1.Text
Form1.Text4=Text2.text
End Sub

End

FORM 3:

Private Sub DEPOSIT_Click()


Text2.Text=val(Form1.Text)+Text1.Text
Form1.Text4=Text2.text
End Sub

Private Sub HOMEPAGE_Click()


Form1.Show
Form3.Hide
End Sub

47
WITHDRAW FORM:

48
DEPOSIT FORM:

49
RESULT:

Thus the banking system has been successfully developed in Visual Basic.

10. MINI PROJECT (2)

DESIGN AND IMPLEMENTATION OF LIBRARY INFORMATION SYSTEM

AIM:

50
To develop a library information system using Oracle as a back end (data
base)and Microsoft Visual Basic as a front end.

PROCEDURE:

1.Create a book and issue database with the following fields in Oracle namely
Bid,Bname,Author,Price,noofcopiesavailable and Bid,Bname,Author,card
number,stuname,dateofreturn respectively and insert some record into the database.

2.Design the corresponding form with labels,text boxes and command buttons.

Form1 bookdetails
Form2 issuedetails

3.Create the back with the front end using DAO method by creating a dsn as follows
A . Select administrative tools option from control panel.
Then click on data source(ODBC),which displays a dialog box named
ODBC DATASOURCES ADMINISTRATOR in which click Add
button.

B . In Create New Data Source dialog box,select “Microsoft ODBC


for ORACLE” and click finish button.

C . Give a suitable data source name,user name and server name.

4.Perform the required operations like Insert,Delete,Searching and Exit.

5.Issue the books according to the no.of copies available.

6.Execute the project.

TABLE DESIGN:

Table Name 1 : Books

51
BID NUMBER(6)
BNAME VARCHAR2(30)
AUTHOR VARCHAR2(20)
CARDNUMBER VARCHAR2(8,2)
STUNAME VARCHAR2(8,2)
DATEOFRETURN NUMBER(6)
Table Name 2 : Issue

NAME TYPE
BID NIMBER(6)
BNAME VARCHAR2(30)
AUTHOR VARCHAR2(20)
PRICE NUMBER(8,2)
NO OF COPIES AVAILABLE NUMBER(3)

BOOK DETAILS FORM:

52
ISSUE DETAILS FORM:

53
FORM 1:

Dim DB As Database
Dim RS As recordset

Private Sub Form_Load()


Set DB =OpenDatabase(“libdsn”,false,false,”ODBC;uid= ;pwd= ;”)
Set RS =DB.OpenRecordset(“select * from Book”)
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
Text5.text=RS(4)
End Sub

Private Sub CLEAR_click()


Text1.text=””
Text2.text=””
Text3.text=””

54
Text4.text=””
Text5.text=””
End Sub

Private sub DELETE_Click()


RS.DELETE
MsgBox”the record is deleted”
RS.MoveNext
IfRS.EOF Then
MsgBox”no more records”
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
Text5.text=RS(4)
End If
End Sub

Private Sub EXIT Click()


End
End Sub

Private Sub INSERT_Click()


RS .MOVELAST
RS.AddNew
RS(0)=Text1.Text
RS(1) =Text2.Text
RS(2)=Text3.Text
RS(3)=Text4.Text
RS(4)=Text5.Text
RS.UPDATE
End Sub

Private Sub MOVEFIRST_Click()


RS.MOVEFIRST
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
Text5.text=RS(4)
End Sub
Private Sub SEARCH_Click()
1=InputBox(“ENTER THE BID”,”FIND”)
RS.FindFirst”[BID]-”&I
If RS.NoMatch Then
MsgBox”no such records”

55
Else
RS.MOVEFIRST
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
Text5.text=RS(4)
End Sub
Private Sub ISSUE_Click()
If Val(Text5.Text)>=1 Then
Form2.Show
Else
MsgBox”NO BOOKS AVAILABLE”
End If
End Sub

FORM2:
Dim DB As Database
Dim RS As recordset

Private Sub Form_Load()


Set DB =OpenDatabase(“libdsn”,false,false,”ODBC;uid= ;pwd= ;”)
Set RS =DB.OpenRecordset(“select * from Book”)
Text1.Ttext=Form1.Text1
Text2.Text=Form1.Text2
Text3.Text=Form1.Text3
End Sub

Private Sub ISSUE_Click()


RS.AddNew
RS(0)=Text1.Text
RS(1) =Text2.Text
RS(2)=Text3.Text
RS(3)=Text4.Text
RS(4)=Text5.Text
RS(5)=Text6.Text
RS.UPDATE
RS.MOVELAST
Form1.Text5=val(Form1.Text5)-1
End Sub
Private Sub MOVELAST_Click()
RS.MOVELAST
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
Text5.text=RS(4)

56
End Sub

Private sub NEXT_Click()


RS.MoveNext
IfRS.EOF Then
MsgBox”no more records”
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
Text5.text=RS(4)
End If
End Sub

Private sub PREVIOUS_Click()


RS.MovePrevious
IfRS.EOF Then
MsgBox”no more records”
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
Text5.text=RS(4)
End If
End Sub

Private Sub UPDATE_Click()


RS.Edit
RS(0)=Text1.Text
RS(1) =Text2.Text
RS(2)=Text3.Text
RS(3)=Text4.Text
RS(4)=Text5.Text
RS.UPDATE
End Sub

Private Sub MAINMENU_Click()


Form1.Show
Form2.Hide
End Sub

RESULT :
Thus the Library Information System has been successfully developed in VB.

57

You might also like