Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Aim:
Procedure:
1. Identify entities
2. Identify relationships
3. Identify cardinalities
4. Draw the E-R diagram for Banking, Employee, Hospital
databases.
Entity-Relationship Diagram:
Lines: It provide the link between attributes to entity sets & entity sets
to relationship sets.
1
Double lines: It represents total participation of an entity in a
relationship set.
2
Foreign Key
Key Definitions:
Primary Key:
One attributes whose value can uniquely identify a
complete record (one row of data) within an entity.
Composite Primary Key
A primary key that consists of two or more attribute within
an entity.
Foreign Key
A copy of a primary key that exists in another entity for the
purpose of forming a relationship between the entities
involved.
Degrees of a Relationship:
One to one (1:1)
1 1 1
MAN WOMEN
1 N M
CUSTOMER ORDER
N M
COURSE SUBJECT
3
ER diagram for banking system
street city
pincod
e
Balanc
address Accno
name e
phoneno
Depositor
Customer Account
D.O.B
Acc Branch
age
Borrower
Branch_cit
y
Branch_Name Assets
Branch
Loan
Loan Branch
Loann Amoun
o t
Result:
4
EX No: 2 Study of Normalization
Aim:
Procedure:
5
• There are a few rules for database normalization. Each rule is
called a "normal form." If the first rule is observed, the database
is said to be in "first normal form." If the first three rules are
observed, the database is considered to be in "third normal
form." Although other levels of normalization are possible, third
normal form is considered the highest level necessary for most
applications.
6
**********************************
Examples of Normalization
**********************************
Un-normalized table:
Registration:
Student# Class#
1022 101-07
1022 143-01
1022 159-02
4123 201-01
4123 211-02
4123 214-01
7
Third Normal Form: ELIMINATE DATA NOT DEPENDENT ON KEY
In the last example, Adv-Room (the advisor's office number) is
functionally dependent on the Advisor attribute. The solution is to
move that attribute from the Students table to the Faculty table, as
shown below:
Students:
Student# Advisor
1022 Jones
4123 Smith
Faculty:
Student# Class#
1022 101-07
1022 143-01
1022 159-02
4123 201-01
4123 211-02
4123 214-01
Students:
Student# Advisor
1022 Jones
4123 Smith
Faculty:
8
Result:
EX. NO: 3
DDL COMMANDS
Aim:
Create Table
Alter Table
Drop Table
Rename
TO CREATE TABLE.
Create table command is used to create a new table.
Syntax for create table:
Create table tablename(field1 datatype1, field2 datatype2, field3
datatype3…….);
9
TO ALTER TABLE
ADD
Alter table with add command is used to add new field in existing table.
Syntax:
Alter table tablename add(fieldname datatype);
SQL> alter table emp add(primary key(eno), addr varchar2(10));
Table altered.
SQL> desc emp;
MODIFY
Alter table with modify command is used to change the existing fields
in table.
Syntax:
Alter table tablename modify(fieldname datatype);
SQL> alter table emp modify(jobid char);
Table altered.
SQL> desc emp;
Name Null? Type
----------------------- -------- --------------------
10
ENO NOT NULL NUMBER(10)
ENAME VARCHAR2(10)
DNO NUMBER(10)
SAL NUMBER(10)
JOBID CHAR(20)
MANAGERID VARCHAR2(10)
ADDR VARCHAR2(10)
PHNO VARCHAR2(10)
11
Result:
12
EX. NO: 4 DML COMMANDS
Aim:
Insert
Update
Delete
Truncate
13
SQL> insert into cust
values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance);
Enter value for cname: Madhan
Enter value for cid: 4
Enter value for caddr: Salem
Enter value for caccno: 1004
Enter value for cacctype: checkings
Enter value for cbalance: 5000
old 1: insert into cust
values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)
new 1: insert into cust
values('Madhan',4,'Salem',1004,'checkings',5000)
1 row created.
14
old 1: insert into cust
values('&cname',&cid,'&caddr',&caccno,'&cacctype',&cbalance)
new 1: insert into cust values('Sridharan',7,'Kanchi',1007,'fd',22000)
1 row created.
7 rows selected.
SQL>update cust set caccno=1111 where cname='Chamundi';
1 row updated
SQL> select * from cust;
CNAME CID CADDR CACCNO CACCTYPE CBALANCE
7 rows selected.
SQL>delete from cust where cacctype='fd’;
3 row deleted
SQL> select * from cust;
CNAME CID CADDR CACCNO CACCTYPE CBALANCE
4 rows selected.
Truncate table
This command is used to delete records from table. It only
remove the table contents but table structure is in database.
Syntax:
Truncate table <table name>;
15
Example
Truncate table stud;
Table deleted.
Result:
16
EX. NO: 5 DCL COMMANDS
Aim:
grant
revoke
You can grant SELECT, INSERT, UPDATE, and DELETE table privileges.If you grant a
privilege to PUBLIC AT ALL LOCATIONS, the grantee is PUBLIC*. Because
PUBLIC* is a special identifier that is used by DB2 internally, you should not use
PUBLIC* as a primary or secondary authorization ID. When a privilege is revoked from
PUBLIC AT ALL LOCATIONS, authorization IDs to which the privilege was
specifically granted still retains the privilege.
Some differences exist in the privileges for a query that uses system-
directed access:
GRANT syntax
17
database_object ::= [ TABLE ] table_name | SCHEMA schema_name
user_list ::= PUBLIC | username1, username2, ...
If you want to connect the user sam, then give the following syntax:
You can grant users various privileges to tables. These privileges can
be any combination of select, insert, update, delete, references,
alter, and index. Below is an explanation of what each privilege
means.
Privilege Description
Select Ability to query the table with a select statement.
Insert Ability to add new rows to the table with the
insert statement.
Update Ability to update rows in the table with the update
statement.
Delete Ability to delete rows from the table with the
delete statement.
Reference Ability to create a constraint that refers to the
s table.
Alter Ability to change the table definition with the
alter table statement.
Index Ability to create an index on the table with the
create index statement.
You can also use the all keyword to indicate that you wish all
permissions to be granted. For example:
18
grant all on student to sam;
If you wanted to grant select access on your table to all users, you
could grant the privileges to the public keyword. For example:
Once you have granted privileges, you may need to revoke some or all
of these privileges. To do this, you can execute a revoke command.
You can revoke any combination of select, insert, update, delete,
references, alter, and index.
If you wanted to revoke all privileges on a table, you could use the all
keyword. For example:
revoke all on student from sam; If you had granted privileges to public
(all users) and you wanted to revoke these privileges, you could
execute the following statement:
Result:
19
EX. NO: 6 USE OF SELECT STATEMENT
Aim:
SELECT MULTICOLUMN:
SQL> select eno,ename,sal from employee;
ENO ENAME SAL
1 sayee 2890
2 rama 5000
3 meera 6000
4 sayee 4536
5 bala 7780
SELECT ALL:
SQL> select * from employee;
ENO ENAME SAL AGE
1 sayee 2890 19
2 rama 5000 20
3 meera 6000 20
4 sayee 4536 20
5 bala 7780 20
20
ENO ENAME SAL+1000
1 sayee 3890
2 rama 6000
3 meera 7000
4 sayee 5536
5 bala 8780
RENAME:
SQL> select sal “salary” from employee;
salary
2890
5000
6000
4536
7780
21
ENO||ENAME
1sayee
2rama
3meera
4sayee
5bala
SELECT DISTINCT:
SQL> select distinct ename from employee;
ENAME
meera
bala
sayee
rama
ENO
1
2
4
5
3
7 rows selected.
22
SQL> select cname,caddr from cust where cbalance between 1000 and
10000;
CNAME CADDR
--------------- ----------
Madhan Salem
Subha Trichy
7 rows selected.
CNAME CBALANCE
--------------- ----------
Madhan 5000
CNAME CBALANCE
--------------- ----------
Hari 15000
Shriram 25000
Chamundi 36200
Jayashree 15000
Sridharan 22000
5 rows selected.
23
SQL> select cname,cacctype,cbalance from cust where
cbalance>20000 and cacctype=’fd’;;vCpe=’fd’;;
Chamundi fd 36200
Sridharan fd 22000
2 rows selected.
Result:
24
EX. NO: 7 JOIN OPERATIONS
Aim:
Table created;
SQL>desc grade;
Name Null? Type
25
LS NUMBER(8)
HS NUMBER(8)
SQL>
1 row created
insert into locn values(&lid,'&city','&area');
enter lid:1
enter city:chennai
enter area:aaa
old 1: insert into locn values(&lid,'&city','&area')
new 1: insert into locn values(1,'chennai','aaa')
SQL>insert into dip values(&dno,'&dname',&lid);
enter dno:1
enter dname:admin
enter lid:2
old 1: insert into dep values(&dno,'&dname',&lid)
new 1: insert into dep values(1,'admin',2)
1 row created
SQL>insert into emp10 values(&eid,'&ename',&dno,&esal,
&jobid,&mgr id);
enter eid:3
enter ename:zzz
enter dno:3
enter esal :3500
enter jobid:2
enter mgr id:2
old 1: insert into emp
values(&eid,'&ename',&dno,&esal,&jobid,&mgrid)
new 1: insert into emp values(1,'zzz',3500,2,2)
1 row created
26
SQL> select * from locn;
1 chennai aaa
2 bombay bbb
3 calcutta ccc
GNO LS HS
1 1000 2000
2 2001 3000
3 3001 4000
4 4001 5000
5 bbc 4700 2
1 xxx 1 4000 1
2 yyy 2 2000 2 1
3 zzz 3 3500 2 2
4 abc 2 4500
EQUI-JOIN
~~~~~~~~~
SQL>select e.ename,d.dname from emp e,dep d where e.dno=d.dno;
ENAME DNAME
xxx admin
yyy finance
zzz hr
abc finance
NON-EQUIJOIN
~~~~~~~~~~~~
SQL> select e.ename,e.esal,g.gno from emp e,grade g where e.esal
between g.ls and g.hs;
ENAME ESAL GNO
---------- ---------- ----------
bbc 4700 4
xxx 4000 3
yyy 2000 1
zzz 3500 3
abc 4500 4
27
LEFTOUT-JOIN
~~~~~~~~~~~~
SQL> select e.ename,d.dname from emp e,dep d where
e.dno(+)=d.dno;
ENAME DNAME
---------- ----------------
xxx admin
yyy finance
abc finance
zzz hr
market
sales
RIGHTOUTER-JOIN
SQL> select e.ename,d.dname from emp e,dep d where
e.dno=d.dno(+);
ENAME DNAME
---------- ---------------
bbc
xxx admin
yyy finance
zzz hr
abc finance
FULLOUTER-JOIN
SQL>select e.ename,d,dname from emp e,dep d where
e.dno(+)=(+)d.dno;
ENAME DNAME
-------- ---------------
bbc
xxx admin
yyy finance
zzz hr
abc finance
market
sales
ENAME ENAME
---------- ----------
bbc yyy
yyy xxx
zzz yyy
28
SELFJOIN----TO DISPLAY MANAGER'S SALARY FOR EVERY EMPLOYEE
SQL> select e.ename,m.esal from emp e,emp m where e.mgrid=m.eid;
ENAME ESAL
---------- ----------------
bbc 2000
yyy 4000
zzz 2000
29
Result:
Aim:
.
1 x 10 100000 MD 0 15
2 y 20 50000 Mgr 1 10
3 z 20 25000 Mgr 2 2
4 a 30 15000 Mgr 2 1
5 b 40 10000 Emp 3 5
SQL> select 'Salary of ' || ename || ' is ' || salary from employee22;
30
'SALARYOF'||ENAME||'IS'||SALARY
--------------------------------------------------------------------------
Salary of X is 100000
Salary of y is 50000
Salary of z is 25000
Salary of a is 15000
Salary of b is 10000
SQL> select 'Salary of ' || ename || ' is ' || salary from employee22;
'SALARYOF'||ENAME||'IS'||SALARY
--------------------------------------------------------------------------------
Salary of X is 100000
Salary of y is 50000
Salary of z is 25000
Salary of a is 15000
Salary of b is 10000
31
SQL> insert into sample values('Hello
world','Lovelyn','Divya',120.856);
1 row created.
SQL> insert into sample values('Welcome
everyone','Dhivyaa','Meens',340.15);
1 row created.
SQL> insert into sample values('Hi
everybody','Harsha','Innanji',280.59);
1 row created.
SQL> select * from sample;
NAME FNAME LNAME FNO
--------------------- ---------- ---------- -------------------
Hello world Lovelyn Divya 120.9
Welcome everyone Dhivyaa Meens 340.2
Hi everybody Harsha Innanji 280.6
SQL> select concat(fname,lname),fno from sample;
CONCAT(FNAME,LNAME) FNO
-------------------- ------------------------
LovelynDivya 120.9
DhivyaaMeens 340.2
HarshaInnanji 280.6
SQL> select upper(name) from sample;
UPPER(NAME)
------------------------------
HELLO WORLD
WELCOME EVERYONE
HI EVERYBODY
SQL> select lower(lname) from sample;
LOWER(LNAME)
----------
divya
meens
innanji
SQL> select substr(fname,1,5) from sample;
SUBST
-------------
Lovel
Dhivy
Harsh
32
SQL> select length(lname) from sample;
LENGTH(LNAME)
-------------
5
5
7
33
hivyaa
SQL> select trim('a' from fname)from sample;
TRIM('A'FR
----------
Lovelyn
Dhivy
Harsh
NUMBER FUNCTIONS:
SQL> select ROUND(45.923,2),ROUND(45.923,0),ROUND(45.923,-1)
from DUAL;
45.92 46 50
1 row created.
SQL> insert into sample values('Hi
chennai','aishwarya','sukumar',170,'30-oct-87');
1 row created.
SQL> select * from sample;
NAME FNAME LNAME FNO HIREDATE
Hello world Lovelyn Divya 120.9
Welcome everyone Dhivyaa Meens 340.2
Hi everybody Harsha Innanji 280.6
REC CSE Anitha Anu 320.7 21-MAR88
Hi chennai aishwarya sukumar 170 30-OCT-87
34
Anu 21-MAR-88
SQL> select sysdate from DUAL;
SYSDATE
-----------------
22-AUG-07
SQL> select
fname,hiredate,add_months(hiredate,6),next_day(hiredate,'friday'),
last_day (hiredate) from sample;
35
Result:
Aim:
Order By
Group By
Aggregate Functions
ORDER BY
========
SQL> select cname from cust order by cname desc;
CNAME
---------------
Subha
Sridharan
Shriram
Madhan
Jayashree
Chamundi
Hari
7 rows selected.
36
Chamundi 1003 36200
Shriram 1002 25000
Sridharan 1007 22000
Hari 1001 15000
Jayashree 1006 15000
Subha 1005 10000
Madhan 1004 5000
7 rows selected.
GROUP BY
=========
MAX(CBALANCE) CACCTYPE
25000 savings
10000 checkings
36200 fd
AGGREGATE FUNCTIONS
=======================
SQL> select count(*) from cust;
COUNT(*)
7
SQL> select count(distinct cname) from cust;
COUNT(CNAME)
7
SQL>select sum(cbalance) as totbal,avg(sum(cbalance)) as average
from cust where cacctype='savings';
TOTBAL AVERAGE
40000 20000
MAX(CBALANCE) MIN(CBALANCE)
36200 5000
37
Result:
Aim:
Procedures:
1. Create a table using create command.
Table created.
38
Procedure created.
3. Execute a procedure
1 80 85 165
2 75 84 0
3 65 80 0
4 90 85 0
1 80 85 165
2 75 84 0
3 65 80 0
4 90 85 0
1 80 85 165
2 75 84 159
3 65 80 145
4 90 85 0
SQL> desc emp17;
Name Null? Type
ENO NOT NULL NUMBER(2)
39
ENAME NOT NULL VARCHAR2(18)
DNO NOT NULL NUMBER(3)
SAL NUMBER(8)
MID NUMBER(3)
ADDITION
40
2 a number;
3 b number;
4 c number;
5 begin
6 a:=&a;
7 b:=&b;
8 c:=a+b;
9 dbms_output.put_line('sum of'||a||'and'||b||'is'||c);
10 end;
41
new 12: c:=10;
enter a:
enter b:
enter c:
C is maximum
PL/SQL procedure successfully completed.
REVERSING THE NO.
42
7 n:=1;
8 for n in 1..endvalue
9 loop
10 if mod(n,2)=1
11 then
12 sum1:=sum1+n;
13 end if;
14 end loop;
15 dbms_output.put_line('sum ='||sum1);
16* end;
SQL>
Enter value for endvalue: 5
old 6: endvalue:=&endvalue;
new 6: endvalue:=5;
sum =9
PL/SQL procedure successfully completed.
SUM OF ODD NO USING WHILE LOOP
SQL> set serveroutput on
SQL> declare
2 n number;
3 sum1 number default 0;
4 endvalue number;
5 begin
6 endvalue:=&endvalue;
7 n:=1;
8 while(n<endvalue)
9 loop
10 sum1:=sum1+n;
11 n:=n+2;
12 end loop;
13 dbms_output.put_line('sum of odd no. bt 1 and' ||endvalue||'is'||
sum1);
14 end;
Enter value for endvalue: 5
old 6: endvalue:=&endvalue;
new 6: endvalue:=5;
sum of odd no. bt 1 and5is4
PL/SQL procedure successfully completed.
SQL> /
Enter value for endvalue: 7
old 6: endvalue:=&endvalue;
new 6: endvalue:=7;
sum of odd no. bt 1 and7is9
PL/SQL procedure successfully completed.
SALARY
SQL> set serveroutput on
SQL> declare
2 ename varchar2(15);
3 basic number;
43
4 da number;
5 hra number;
6 pf number;
7 netsalary number;
8 begin
9 ename:=&ename;
10 basic:=&basic;
11 da:=basic*(41/100);
12 hra:=basic*(15/100);
13 if(basic<3000)
14 then
15 pf:=basic*(5/100);
16 elsif(basic>=3000 and basic<=5000)
17 then
18 pf:=basic*(7/100);
19 elsif(basic>=5000 and basic<=8000)
20 then
21 pf:=basic*(8/100);
22 else
23 pf:=basic*(10/100);
24 end if;
25 netsalary:=basic+da+hra-pf;
26 dbms_output.put_line('employee name:'||ename);
27 dbms_output.put_line('providend fund:'||pf);
28 dbms_output.put_line('net salary:'||netsalary);
29* end;
enter value for ename: 'ice'
old 9: ename:=&ename;
new 9: ename:='ice';
enter value for basic: 1000
old 10: basic:=&basic;
new 10: basic:=1000;
employee name:ice
providend fund:50
net salary:1510
PL/SQL procedure successfully completed.
EXAMPLE FOR LOOP
SQL> set serveroutput on
SQL> declare
2 begin
3 for i in 1..10
4 loop
5 dbms_output.put_line(to_char(i));
6 end loop;
7* end;
1
2
3
44
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.
EXAMPLE FOR WHILE
SQL> set serveroutput on
SQL> declare
2 i number:=0;
3 j number:=0;
4 begin
5 while i<=100 loop
6 j:=j+1;
7 i:=i+2;
8 end loop;
9 dbms_output.put_line(to_char(i));
10 end;
102
PL/SQL procedure successfully completed.
EXAMPLE FOR LOOP USING EXIT
SQL> set serveroutput on
SQL> declare
2 a number:=100;
3 begin
4 loop
5 a:=a+25;
6 exit when a=250;
7 end loop;
8 dbms_output.put_line(to_char(a));
9 end;
250
PL/SQL procedure successfully completed.
PRIME OR NOT
SQL> set serveroutput on
SQL> declare
2 no number(3):=&no;
3 a number(4);
4 b number(2);
5 begin
6 for i in 2..no-1
7 loop
8 a:=no MOD i;
9 if a=0
10 then
11 GOTO out;
45
12 end if;
13 end loop;
14 <<out>>
15 if a=1
16 then
17 dbms_output.put_line(no||'is a prime');
18 else
19 dbms_output.put_line(no||'is not a prime');
20 end if;
21* end;
Enter value for no: 7
old 2: no number(3):=&no;
new 2: no number(3):=7;
7is a prime
PL/SQL procedure successfully completed.
AREA CALCULATION
SQL> set serveroutput on
SQL> declare
2 pi constant number(4,2):=3.14;
3 radius number(5);
4 area number(14,2);
5 begin
6 radius:=3;
7 while radius<=7
8 loop
9 area:=pi*power(radius,2);
10 insert into areas values(radius,area);
11 radius:=radius+1;
12 end loop;
13 end;
PL/SQL procedure successfully completed.
SQL> select* from areas;
RADIUS AREA
---------- ----------
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86
7 154
5 78.54
3 28.26
4 50.24
5 78.5
6 113.04
RADIUS AREA
---------- ----------
7 153.86
46
12 rows selected.
Result:
47
EX. NO: 11 FUNCTIONS
Aim:
Table created.
Function created.
STUDE(2)
----------
159
SQL> create table purchase (icode number(3),iname
varchar2(13),price number(6),quantity number(3),rate
number(8),primary key(icode),unique(iname));
48
Table created.
100 PenSet 20 10 0
101 ParkerPen 60 10 0
102 180pg Note 24 10 0
103 80pg Note 10 25 0
104 StickFile 10 20 0
Function created.
SQL> select pur(102) from dual;
PUR(102)
-----------
240
Result:
49
EX. NO: 12 CURSOR
Aim:
1 row created.
SQL> /
Enter value for cname: sree
Enter value for sal: 12000
old 1: insert into ssss values('&cname',&sal)
new 1: insert into ssss values('sree',12000)
1 row created.
SQL> /
Enter value for cname: santhi
Enter value for sal: 30000
old 1: insert into ssss values('&cname',&sal)
new 1: insert into ssss values('santhi',30000)
1 row created.
SQL> /
Enter value for cname: babu
Enter value for sal: 20000
old 1: insert into ssss values('&cname',&sal)
new 1: insert into ssss values('babu',20000)
1 row created.
SQL> select * from ssss;
CNAME SAL
50
viji 17000
sree 12000
santhi 30000
babu 20000
SQL> set serveroutput on;
SQL> declare
2 cursor emp_cur is select cname,sal from ssss;
3 name ssss.cname%type;
4 salary ssss.sal%type;
5 begin
6 open emp_cur;
7 dbms_output.put_line('name salary');
8 dbms_output.put_line('-----------');
9 loop
10 fetch emp_cur into name,salary;
11 exit when(emp_cur%notfound);
12 dbms_output.put_line(name||' '||salary);
13 end loop;
14 close emp_cur;
15 end;
16 /
name salary
viji 17000
sree 12000
santhi 30000
babu 20000
51
18 /
Function created.
1 cts 99
2 tcs 55
3 ibm 89
Result:
52
EX NO: 13 TRIGGER
Aim:
TABLE
Table created.
INSERT
----------
SQL> insert into orders(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_item);
Enter value for order_id: 1
Enter value for quantity: 4
Enter value for cost_per_item: 20
old 1: insert into orders(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_it
new 1: insert into orders(order_id,quantity,cost_per_item)
values(1,4,20)
1 row created.
SQL> /
Enter value for order_id: 2
Enter value for quantity: 5
Enter value for cost_per_item: 30
old 1: insert into orders(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_it
new 1: insert into orders(order_id,quantity,cost_per_item)
values(2,5,30)
1 row created.
SQL> /
Enter value for order_id: 3
Enter value for quantity: 6
Enter value for cost_per_item: 25
old 1: insert into orders(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_it
new 1: insert into orders(order_id,quantity,cost_per_item)
values(3,6,25)
1 row created.
SQL> select * from orders;
53
1 4 20
2 5 30
3 6 25
TRIGGER SCRIPT
------------------------
Trigger created.
SQL> update orders set total_cost=3000 where order_id=2;
1 row updated.
TRIGGER WITH AFTER UPDATE
TABLE
----------
SQL> create table orders30(order_id number(5),quantity
number(4),cost_per_item number(6,2),total_cost number(8,2));
Table created.
Table created.
1 row created.
SQL> /
54
Enter value for order_id: 101
Enter value for quantity: 4
Enter value for cost_per_item: 20
old 1: insert into orders30(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_
new 1: insert into orders30(order_id,quantity,cost_per_item)
values(101,4,20)
1 row created.
SQL> /
Enter value for order_id: 102
Enter value for quantity: 5
Enter value for cost_per_item: 30
old 1: insert into orders30(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_
new 1: insert into orders30(order_id,quantity,cost_per_item)
values(102,5,30)
1 row created.
Trigger created.
SQL> update orders30 set quantity=25 where order_id=101;
1 row updated.
SQL> select *from orders_audit;
ORDER_ID QUANTITY_BEFORE QUANTITY_AFTER USERNAME
101 4 25 CSE3090
55
Result:
Aim:
Procedure:
SQL precompiler
E d ito r h o st p rogram + em b ed d ed S Q L
P r e c o m p ile r
h o s t p r o g r a m + tr a n s la te d S Q L
C o m p ile r
o b je c t (b in a r y ) p r o g r a m
L in k e r D B M S a n d o th e r lib r a r ie s
e x e c u ta b le p r o g r a m
Variables
Variables to be shared between the embedded SQL code and the 3GL
have to be specified in the program.
56
We also should declare a link to the DBMS so that database status
information can be accessed.
EXEC SQL include sqlca;
This allows access to a structure sqlca, of which the most common
element
sqlca.sqlcode has the value 0 (operation OK), >0 (no data found), and
<0 (an error).
Connecting to the DBMS
Before operations can be performed on the database, a valid
connection has to be established.
EXEC SQL connect :userid identified by :password;
In all SQL statements, variables with the ‘:’ prefix refer to
shared host variables, as opposed to database variables
(e.g. row or column identifiers).
This assumes that userid and password have been properly
declared and initialised.
When the program is finished using the DBMS, it should disconnect
using:
EXEC SQL commit release;
Queries producing a single row
A single piece of data (or row) can be queried from the database so
that the result is accessible from the host program.
EXEC SQL SELECT custname
INTO :cname
FROM customers
WHERE cno = :cno;
Thus the custname with the unique identifier :cno is stored in :cname.
However, a selection query may generate many rows, and a way is
needed for the host program to access results one row at a time.
57
Result:
EX NO: 15 Banking
Aim:
Design Window
Adding Library
Project = > References => Microsoft ActiveX Data Objects 2.0
Library
Coding
Dim Con As New ADODB.Connection
Dim Rs As New ADODB.Recordset
58
End Sub
Sub txtClear()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
59
Text4.Text = ""
End Sub
Sub DisplayText()
Text1.Text = Rs(0).Value
Text2.Text = Rs(1).Value
Text3.Text = Rs(2).Value
Text4.Text = Rs(3).Value
End Sub
Transaction Window
Design
Coding
60
Rs.MoveNext
Loop
End Sub
61
Result:
Aim:
62
rs.Update
MsgBox "Record Updated"
End Sub
Private Sub Command3_Click()
On Error Resume Next
If rs.EOF Then
MsgBox "No Records"
Else
rs.Delete
rs.MoveNext
DisplayText
MsgBox "Record Deleted"
End If
End Sub
Private Sub Form_Load()
Set Con = New ADODB.Connection
Set rs = New ADODB.Recordset
Con.Open "Provider=MSDAORA.1;User
ID=scott;Password=tiger;DataSource=employees;Persist Security
Info=False"
Con.CursorLocation = adUseClient
rs.Open "select * from employees", Con, adOpenKeyset,
adLockPessimistic
End Sub
Sub txtClear()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
End Sub
Sub DisplayText()
Text1.Text = rs(0).Value
Text2.Text = rs(1).Value
Text3.Text = rs(2).Value
Text4.Text = rs(3).Value
Text5.Text = rs(4).Value
Text6.Text = rs(5).Value
Text7.Text = rs(6).Value
End Sub
Private Sub command5_click()
Text3.Text = Val(Text2.Text * 29 / 100)
Text4.Text = Val(Text2.Text * 60 / 100)
Text5.Text = Val(Text2.Text * 6 / 100)
If (Text2.Text >= 1500) Then
Text6.Text = 1600
Else
63
Text6.Text = 780
End If
Text7.Text = Val(Text2.Text) + Val(Text3.Text) + Val(Text4.Text) +
Val(Text5.Text) - Val(Text6.Text)
End Sub
Employees
Design Window
Adding Library
64
Else
Rs.Delete
Rs.MoveNext
DisplayText
MsgBox "Record Deleted"
End If
End Sub
Private Sub CmdMoveFirst_Click()
Rs.MoveFirst
DisplayText
End Sub
Private Sub CmdMoveNext_Click()
Rs.MoveNext
If Not Rs.EOF Then
DisplayText
Else
MsgBox "End Of The Record"
End If
End Sub
Private Sub CmdUpdate_Click()
Rs(0).Value = Val(Text1.Text)
Rs(1).Value = Text2.Text
Rs(2).Value = Text3.Text
Rs(3).Value = Text4.Text
Rs.Update
MsgBox "Record Updated"
End Sub
Private Sub Form_Load()
Con.Open "Provider=MSDAORA.1;User ID=scott;Password=tiger;Data
Source=employees;Persist Security Info=False"
Con.CursorLocation = adUseClient
Rs.Open "select * from employees", Con, adOpenKeyset,
adLockOptimistic
End Sub
Sub txtClear()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
Sub DisplayText()
Text1.Text = Rs(0).Value
Text2.Text = Rs(1).Value
Text3.Text = Rs(2).Value
Text4.Text = Rs(3).Value
End Sub
65
Result:
66
EX NO: 17 Library Information System
Aim:
67
rs(2).Value = Text3.Text
rs(3).Value = Text4.Text
rs(4).Value = Text5.Text
rs.Update
MsgBox "Record Updated"
End Sub
Private Sub Command4_Click()
rs.MoveFirst
DisplayText
End Sub
Private Sub Command5_Click()
rs.MoveNext
If Not rs.EOF Then
DisplayText
Else
MsgBox "End of the Record"
End If
End Sub
Private Sub Form_Load()
Con.Open "Provider = MSDAORA.1;User
Id=scott;Password=tiger;DataSource=lib;Persist Security Info=False"
Con.CursorLocation = adUseClient
rs.Open "select * from lib", Con, adOpenKeyset, adLockPessimistic
End Sub
Sub txtClear()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
End Sub
Sub DisplayText()
Text1.Text = rs(0).Value
Text2.Text = rs(1).Value
Text3.Text = rs(2).Value
Text4.Text = rs(3).Value
Text5.Text = rs(4).Value
End Sub
Result:
68