0% found this document useful (0 votes)
5 views

SQL QUERIES 05

The document contains a series of SQL queries demonstrating the creation, alteration, and manipulation of database tables. It includes examples of creating tables, inserting data, updating records, and managing views and sequences. Key operations include creating primary keys, unique constraints, and performing data integrity checks.

Uploaded by

selvapraveen1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL QUERIES 05

The document contains a series of SQL queries demonstrating the creation, alteration, and manipulation of database tables. It includes examples of creating tables, inserting data, updating records, and managing views and sequences. Key operations include creating primary keys, unique constraints, and performing data integrity checks.

Uploaded by

selvapraveen1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

1.

SQL QUERIES
SQL> create table ddt(dno number(10),dname varchar2(20),primary key(dno ));

Table created.

SQL> desc ddt;

Name Null? Type

DNO NOT NULL NUMBER(10)

DNAME VARCHAR2(20)

SQL> create table exxx10(eno number(10),ename varchar2(10),dno number(5),sql num

Ber(10),jobid varchar2(10),mergid varchar2(10));

Table created.

SQL> desc exxx10;

Name Null? Type

ENO NUMBER(10)

ENAME VARCHAR2(10)

DNO NUMBER(5)

SQL NUMBER(10)

JOBID VARCHAR2(10)

MERGID VARCHAR2(10)

SQL> alter table exxx10 drop(dno);

Table altered.

SQL> desc exxx10;

Name Null? Type

ENO NUMBER(10)

ENAME VARCHAR2(10)
SQL NUMBER(10)

JOBID VARCHAR2(10)

MERGID VARCHAR2(10)

SQL> alter table exxx10 drop(sql);

Table altered.

SQL> desc exxx10;

Name Null? Type

ENO NUMBER(10)

ENAME VARCHAR2(10)

JOBID VARCHAR2(10)

MERGID VARCHAR2(10)

SQL> drop table exxx10;

Table dropped.
2.
SQL QUERIES
SQL> create table custm02(cname varchar2(10),cid number(6),caddr char(10),caccno

number(5), cacctype varchar2(10),cbalance float,primary key(cid),unique(caccno)

,check(cbalance>=1000));

Table created.

SQL> desc custm02;

Name Null? Type

CNAME VARCHAR2(10)

CID NOT NULL NUMBER(6)

CADDR CHAR(10)

CACCNO NUMBER(5)

CACCTYPE VARCHAR2(10)

CBALANCE FLOAT(126)

SQL> insert into custm02 values('&cname','&cid','&caddr','&caccno','&cacctype','

&cbalance');

Enter value for cname: shiva

Enter value for cid: 05

Enter value for caddr: thanjore

Enter value for caccno: 1006

Enter value for cacctype: savings

Enter value for cbalance: 5000

old 1: insert into custm02 values('&cname','&cid','&caddr','&caccno','&cacctyp

e','&cbalance')

new 1: insert into custm02 values('shiva','05','thanjore','1006','savings','50

00')

1 row created.
SQL> /
Enter value for cname: guyu

Enter value for cid: 06

Enter value for caddr: trichy

Enter value for caccno: 1007

Enter value for cacctype: 5000

Enter value for cbalance: 6000

old 1: insert into custm02 values('&cname','&cid','&caddr','&caccno','&cacctyp

e','&cbalance')

new 1: insert into custm02 values('guyu','06','trichy','1007','5000','6000')

1 row created.

SQL> /

Enter value for cname: surya

Enter value for cid: 07

Enter value for caddr: chennai

Enter value for caccno: 1008

Enter value for cacctype: 7000

Enter value for cbalance: 8000

old 1: insert into custm02 values('&cname','&cid','&caddr','&caccno','&cacctyp

e','&cbalance')

new 1: insert into custm02 values('surya','07','chennai','1008','7000','8000')


1 row created.

SQL> /

Enter value for cname: radha

Enter value for cid: 08

Enter value for caddr: madurai

Enter value for caccno: 1009

Enter value for cacctype: 9000


Enter value for cbalance: 10000

old 1: insert into custm02 values('&cname','&cid','&caddr','&caccno','&cacctyp

e','&cbalance')

new 1: insert into custm02 values('radha','08','madurai','1009','9000','10000'

1 row created

SQL> select * from custm02;

CNAME CID CADDR CACCNO CACCTYPE CBALANCE

shiva 5 thanjore 1006 savings 5000

guyu 6 trichy 1007 savings 600


0
surya 7 chennai 1008 savings 8000

radha 8 madurai 1009 savings 100


00

SQL> insert into custm02 values('&cname','&cid','&caddr','&caccno','&cacctype','

&cbalance');

Enter value for cname: nithi

Enter value for cid: 09

Enter value for caddr: salem

Enter value for caccno: 9000

Enter value for cacctype: 9000

Enter value for cbalance: 11000

old 1: insert into custm02 values('&cname','&cid','&caddr','&caccno','&cacctyp

e','&cbalance')

new 1: insert into custm02 values('nithi','09','salem','9000','9000','11000')

1 row created.

SQL> /

Enter value for cname: murugan


Enter value for cid: 10

Enter value for caddr: trichy

Enter value for caccno: 6000

Enter value for cacctype: 5000

Enter value for cbalance: 7000

old 1: insert into custm02 values('&cname','&cid','&caddr','&caccno','&cacctyp

e','&cbalance')

new 1: insert into custm02 values('murugan','10','trichy','6000','5000','7000'

1 row created.

SQL> select *from custm02;

CNAME CID CADDR CACCNO CACCTYPE CBALANCE

shiva 5 thanjore 1006 savings 5000

guyu 6 trichy 1007 5000 6000

surya 7 chennai 1008 7000 8000

radha 8 madurai 1009 9000 10000

nithi 9 salem 9000 9000 11000

murugan 10 6000 70
trichy 5000 00
6 rows selected.

SQL> update custm02 set cname='surya' where cid=7;

1 row updated.

SQL> select *from custm02;

CNAME CID CADDR CACCNO CACCTYPE CBALANCE

shiva 5 thanjore 1006 500


savings 0
guyu 6 trichy 1007 5000 6000

surya 7 chennai 1008 7000 800


0
radha 8 madurai 1009 9000 10000

nithi 9 salem 9000 9000 11000

murugan 10 6000 5000 7000


trichy
6 rows selected.

SQL> delete from custm02 where cacctype='savings';

1 row deleted.
SQL> select *from custm02;

CNAME CID CADDR CACCNO CACCTYPE CBALANCE

guyu 6 trichy 1007 5000 6000

surya 7 chennai 1008 7000 800


0
radha 8 madurai 1009 9000 10000

nithi 9 salem 9000 9000 11000

murugan 10 trichy 6000 7000


5000

SQL> select *from custm02;

CNAME CID CADDR CACCNO CACCTYPE CBALANCE

guyu 6 trichy 1007 5000 6000

surya 7 chennai 1008 7000 800


0
radha 8 madurai 1009 9000 10000

nithi 9 salem 9000 9000 11000

murugan 10 6000 7000


trichy 5000
5 rows selected
3.
SQL QUERIES
SQL> create table sri01(personid number(6), fname varchar2(20),lname varchar2(2),title1
varchar2(20),title2 varchar2(10),socsecno varchar2(10));

Table created.

SQL> desc sri01;

Name Null? Type

PERSONID NUMBER(6)

FNAME VARCHAR2(20)

LNAME VARCHAR2(20)

TITLE1 VARCHAR2(20)

TITLE2 VARCHAR2(10)

SOCSECNO VARCHAR2(10)

SQL> alter table sri01 drop(socsecno);

Table altered.

SQL> desc sri01;

Name Null? Type

PERSONID NUMBER(6)

FNAME VARCHAR2(20)

LNAME VARCHAR2(20)

TITLE1 VARCHAR2(20)

TITLE2 VARCHAR2(10)

SQL> alter table sri01 add(socsecno varchar2(11));

Table altered.

SQL> desc sri01;

Name Null? Type


PERSONID NUMBER(6)

FNAME VARCHAR2(20)

LNAME VARCHAR2(20)

TITLE1 VARCHAR2(20)

TITLE2 VARCHAR2(10)

SOCSECNO VARCHAR2(11)

SQL> insert into sri01 values('&personid','&fname','&lname','&title1','&title2'

'&socsecno');

Enter value for personid: 03

Enter value for fname: dan

Enter value for lname:kowsi

Enter value for title1: bs

Enter value for title2: phd

Enter value for socsecno: 345

old 1: insert into sri01 values('&personid','&fname','&lname','&title1','&tit

e2','&socsecno')

new 1: insert into sri01 values('03','dan','kowsi','bs','phd','345')

1 row created.

SQL> /

Enter value for personid: 04

Enter value for fname: helen

Enter value for lname: lofstrom

Enter value for title1: ba

Enter value for title2: ma

Enter value for socsecno: 987

old 1: insert into sri01 values('&personid','&fname','&lname','&title1','&tit

e2','&socsecno')
new 1: insert into sri01 values('04','helen','lofstrom','ba','ma','987')

1 row created.

SQL> /

Enter value for personid: 05

Enter value for fname: valli

Enter value for lname: patricia

Enter value for title1: ba

Enter value for title2: null

Enter value for socsecno: 402

old 1: insert into sri01 values('&personid','&fname','&lname','&title1','&tit

e2','&socsecno')

new 1: insert into sri01 values('05','valli','patricia','ba','null','402')

1 row created.

SQL> COMMIT;

Commit complete.

SQL> set linesize 141

SQL> select *from sri01;

SQL>create or replace view sri01_view as select first_name as fname, last_name


lname,socsecno from sri01;

View created

SQL>create or replace view sri01_two_titles_view as select


first_name,last_name,socsecno from sri01 where title_1 is not null and title_2 is not null;

View created

SQL>select *from sri01_two_titles_view;

FIRST_NAM LAST_NAM SOCSECNO


E E

Dan kowsi 345

Helen lofstrom 987


Valli patricia 402

SQL>create or replace view sri01_security_view as select first_name||’


‘||last_name,’******_**_’||substr(socsecno,8)ssn from sri01;

SQL>create or replace view sri01_security_view as select


first_name,last_name,’*****_**_’||substr(socsecno,8)ssn from sri01;

View created

SQL>

SQL>select *from sri01;

PERSON_ID FIRST_NAME LAST_NAME TITLE_1 TITLE_2 SOCSECNO

1 dan kowsi bs phd 345

1 helen lofstrom ba ma 987

1 valli patricia ba null 402

SQL>select *from sri01_security_view;

FIRST_NAME LAST_NAME SSN

Dan kowsi *****_**_0987

helen lofstrom *****_**_4793

valli patricia *****_**_1005

SQL>alter table sri01 add (create_by varchar2(30),created_dt date);

Table altered

SQL>update sri01 set create_by=’ids’,created_dt=SYSDATE+2 where


last_name=’lofstrom’;

1 row updated

SQL>select *from sri01;

PERSON_ID FIRST_NAME LAST_NAME TITLE_1 TITLE_2 SOCSECNO


CREATE_BY

1 dan kowsi bs phd 345

1 helen lofstrom ba ma 987 20-sep

1 valli patricia ba null 402


SQL>select view_name from user _views;

VIEW_NAME

CS_VIEW

DATA_VIEW

NONUPD_VIEW

PERSON_SECURITY_VIEW

PERSON_VIEW
PER_VIEW

SRI_SECURITY_VIEW
SRI_TWO_TITLES_VIEW
SRI_VIEW
UPD_VIEW

VIEW_NAME

USER_VIEW

12 rows selected

SQL>drop view date_view;

View dropped
4.
SQL QUERIES
SQL> create table per21(person1id number(6),firstname varchar2(20),lastname varc

har2(20),title1 varchar2(10),title2 varchar2(20));

Table created.

SQL> insert into per21 values('&person1id','&firstname','&lastname','&title1','&

title2');

Enter value for person1id: 1

Enter value for firstname: dan

Enter value for lastname: morgon

Enter value for title1: bs

Enter value for title2: phd

old 1: insert into per21 values('&person1id','&firstname','&lastname','&title1

','&title2')

new 1: insert into per21 values('1','dan','morgon','bs','phd')

1 row created.

SQL> /

Enter value for person1id: 2

Enter value for firstname: valli

Enter value for lastname: patricia

Enter value for title1: ba

Enter value for title2: ma

old 1: insert into per21 values('&person1id','&firstname','&lastname','&title1

','&title2')

new 1: insert into per21 values('2','valli','patricia','ba','ma')

1 row created.

SQL>create synonym per21_syn for per21;

Synonym created

SQL>crea synony per21_syn per2


te m for 1;
Synonym created
SQL>drop synonym per21_syn;

Synonym droppe
5.
SQL QUERIES
SQL>create table my_numbers(my_number number not null primary key);

Table created

SQL>insert into my_numbers(select rownum from user_objects);

101 rows selected

SQL>select max (my_number)from my_numbers;

MAX(MY_NUMBER)

101

SQL>create sequence my_number_sn start with 101;

Sequence created

SQL>drop sequence my_number_sn;

Sequence dropped

SQL>create sequence my_number_sn start with 102,

Sequence created

SQL>insert into my_numbers(my_numbers)values(my_number_sn NEXTVAL);

1 rows created

SQL>select *from my_numbers;

MY_NUMBER

10
11

MY_NUMBER

12

13

14

15

16

17

18

19

20

21

22

MY_NUMBER

23

24
25

26

27

28

29

30

31

32

33

MY_NUMBER

34

35

36

37

38

39

40

41

42

43

44

MY_NUMBER

45

46

47

48
49

50

51

52

53

54

55

MY_NUMBER

56

57

58

59

60

61

62

63

64

65

66

MY_NUMBER

67

68

69

70

71

72

73
74

75

76

77

MY_NUMBER

78

79

80

81

82

83

84

85

86

87

88

MY_NUMBER

89

90

91

92

93

94

95

96

97

98
99

MY_NUMBER

100

101

101rows selected

SQL>drop sequence my_number_sn;

Sequence dropped
6.
SQL QUERIES

SQL>create table emppp(

2 id varchar2(4 byte)not null,


3 first_name varchar2(10 byte),
4 last_name varchar2(10 byte),
5 start_date date,
6 end_date date,
7 salary number(8,2),
8 city varchar2(10 byte),
9 description varchar2(15 byte)
10 )
11 /

Table created

SQL>--prepare data

SQL>insert into emppp(id,first_name,last_name,start_date,


end_date,salary,city,description)

2 values(‘01’,’jason’,’martin’,to_date(‘19960725’,
‘yyyymmdd’),to_date(‘20060725’,’yyyymmdd’),1234.56,’toronto’,’programmer’)

3/

1 row created

SQL>insert into emppp(id,first_name,last_name,start_date,


end_date,salary,city,description)

2values(‘02’,’celia’,’rice’,to_date(‘19781212’,’yyyymmdd’),6544.78,’vancouver’,’manag
er’)

3/

SQL>--display data in the table

SQL>select *from emppp;

ID FIRST_NAME LAST_NAME START_DATE END_DATE SALARY CITY

DESCRIPTION

01 jason martin 25-jul-96 25-jul-06 1234.54 toronto

Programmer

02 celia rice 12-dec-78 15-mar-90 6544.78 vancouver


manager

SQL>--select first_name,city,from emppp;

SQL>select first_name,city,from emppp;

FIRST_NAME CITY

Jason Toronto

celia Vancouver

SQL>drop index emppp_idx;

SQL>--clean the table

2/

Table dropped
7.
SQL QUERIES

SQL>create table empl(empno number(4) not null,

2 ename varchar2(10),

3 job varchar2(9),

4 mgr number(4),

5 hiredate date,

6 sal number(7,2),

7 comm number(7,2),

8 deptno number(2));

Table created

SQL>insert into emp values(1234,’smith’,’clerk’,7902,to_date(‘17_dec_1980’

‘dd-mon-yyyy’),1600,300,30);

1 rows created

SQL>insert into emp values(7541,’ward’,’salesman’,7502,to_date(‘15_dec

_1980’,’dd-mon-yyyy’),1680,500,30);

1 rows created

SQL>--after you issue a ROLLBACK TO statement,it is possible to add


subsequent—statements to the

SQL>--transcation

SQL>delete from empl where empno=7521;

1 rows deleted

SQL>savepoint id 7521del;

Savepoint created

SQL>update empl set job=’coder’ where empno=1234;

1 rows updated

SQL>select *from empl;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7234 smith programmer 7502 11-feb-89 6000 700


EX.NO:4 creating an employee database to set various constraints

SQL>create table em(empno number(4) primary key,ename varchar2(10),

Job varchar2(6),sal number(5),deptno number(7));

Table created

SQL>insert into em values (‘1’,’radhi’,’gm’,’20000’,’45’);

1 row created

SQL>insert into em values(‘2’,’uma’,’pa’,’10000’,’56’);

1 row created

SQL>insert into em values(‘3’,’janshi’,’md’,’56000’,’83’);

1 row created

SQL>desc em

Name Null? Type

EMPNO NOTNULL NUMBER(4)

ENAME VARCHAR2(10)

JOB VARCHAR2(6)

SAL NUMBER(5)

DEPTNO NUMBER(7)
SQL>select *from em;

EMPNO ENAME JOB SAL DEPTNO

1 radhi gm 20000 45

2 uma pa 10000 56

3 janshi md 56000 89

SQL>create table emp1(empno number(4) constraint emp_empno_pk primary


key,ename varchar2(10),job varchar2(6),sal number(5),deptno number(7));

Table created

SQL>insert into emp1 values(‘1’,’shanthi’,’md’,’10000’,’1’);

1 row created

SQL>insert into emp1 values(‘2’,’durga’,’gm’,’12000’,’2’);

1 row created

SQL>select *from emp1;


EMPNO ENAME JOB SAL DEPTNO

1 shanthi md 10000 1

2 durga gm 12000 2

SQL>create table emp15(empno number(4),ename varchar2(20) constraint

Emp38_ename_nn null,design varchar2(20),sal number(3));

Table created

SQL>insert into emp15 values(‘1’,’kavi’,’sy,’59’);

1 row created

SQL>select *from emp15;

EMPNO ENAME DESIGN SAL

1 kavi sy 59

SQL>alter table emp9 disable constraint emp9_sal_ck;


Table created
8.
SQL QUERIES

SQL> set serveroutput on

declare

begin

for i in 1..100

loop

dbms_output.put_line(to_char(i));

end loop;

end;
/

10

PL/SQL successfull complet


procedure y ed

SQL>set serveroutput on

SQL>declare

a number;

b number;

c number;
begin

a:=&a;

b:=&b;

c:=a+b;

dbms_output.put_line('sumof'||a||'and'||'b'||'is'||c);

end;

/
Enter value for a:5

Old 6:a:=&a;

New 6:a:=5;

Enter value for b:6

Old 7:b:=&b;

New 7:b:=6;

Sum of a is5 and bis 11

PL/SQL procedure successfully completed


SQL>set serveroutput on

SQL>declare

a number;

b number;

c number;

d number;

begin

dbms_output.put_line('enter a');

a:=&a;

dbms_output.put_line('enter b');

b:=&b;

dbms_output.put_line('enter c');

c:=&c;

if(a>b) and (a>c) then

dbms_output.put_line('a is maximum');

elsif(b>a) and (a>c) then

dbms_output.put_line(' b ismaximum');

else

dbms_output.put_line('c ismaximum');

end if;

end;

Enter value for a:6

old 8: a:=&a;

new 8: a:=;

Enter value for b:5

old 10: b:=&b;


new 10: b:=5;

Enter value for c: 8

old 12: c:=&c;

new 12: c:=8;

enter a:

enter a:

enter c:

c is maximum

PL/SQL procedure successfully completed

SQL> set serveroutput on

SQL> declare

2 given_number varchar(5):='1234';

3 str_length number(2);

4 inverted_number varchar(5);

5 begin

6 str_length:=length(given_number);

7 for cntr in reverse 1..str_length

8 loop

9 inverted_number:=inverted_number||substr(given_number,cntr,1);

10 end loop;

11 dbms_output.put_line('the given number is'||given_number);

12 dbms_output.put_line('the given number is'||inverted_number);

13 end;

14 /

the given number is1234

the given number is4321

PL/SQL procedure successfully completed.


SQL> set serveroutput on

SQL> declare

2 a number;

3 s1 number default 0;

4 begin

5 a:=1;

6 loop

7 s1:=s1+a;

8 exit when(a=100);

9 a:=a+1;

10 end loop;

11 dbms_output.put_line('sum bt 1 to 100 is'||s1);

12 end;

13 /

sum bt 1 to 100 is5050

PL/SQL procedure successfully completed.

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;

12 end if;

13 end loop;

14 <<out>>

15 if a=1

16 then

17 dbms_output.put_line(no ||'is prime');

18 else

19 dbms_output.put_line(no||'is not prime');

20 end if;

21 end;

22 /

Enter value for no: 7

old 2: no number(3):=&no;

new 2: no number(3):=7;

7 is prime

PL/SQL procedure successfully completed


9.
SQL QUERIES
SQL>create table phbook(phno number(5) primary key,username
varchar2(10),doorno varchar2(20),street varchar2(20),place varchar2(20),pincode
number(6));
Table created
SQL> insert into phbook values('&phno','&username','&doorno','&street','&place',
'&pincode');
Enter value for phno: 20312
Enter value for username: vijay
Enter value for doorno: 126/5d
Enter value for street: nk street
Enter value for place: ngo colony
Enter value for pincode: 629002
old 1: insert into phbook values('&phno','&username','&doorno','&street','&pla
ce','&pincode')
new 1: insert into phbook values('20312','vijay','126/5d','nk street','ngo col
ony','629002')

1 row created.

SQL> insert into phbook values('&phno','&username','&doorno','&street','&place',


'&pincode');
Enter value for phno: 29476
Enter value for username: surya
Enter value for doorno: 39/4d
Enter value for street: rk street
Enter value for place: pto colony
Enter value for pincode: 629800
old 1: insert into phbook values('&phno','&username','&doorno','&street','&pla
ce','&pincode')
new 1: insert into phbook values('29476','surya','39/4d','rk street','pto colo
ny','629800')
1 row created.
SQL>select *from phbook;
PHONE_NO USERNAME DOORNO STREET PLACE PINCODE
20312 vijay 126/5d nkstreet ngo 629002
colony
29476 surya 39/4d rkstreet pto 629800
colony

SQL>create or replace function findaddress10(phone in number) return varchar2 as


address varchar2(20);
2begin

3select username||'-' ||doorno||'-'||street||'-'||place||'-'||pincode||into address from phbook


where phno=phone;
4return address;
5exception when no_data_found the return 'address' not found';
6end;
7/
Function created
SQL>declare
2 address varchar2(100);
3 begin
4 address:=find address(20312);
5 dbms_output.put_line(address);
6 end;
7 /

Vijay ,120/5d,nk street,ngo colony,629002

SQL>declare

2 address varchar2(100);
3 begin
4 address:=find address(23556);
5 dbms_output.put_line(address);
6 end;
7 /

Address not found

PL/SQL procedure successfully completed


10.
SQL QUERIES

SQL>create table ode(order_id number(5),quantity number(4),cost_per_item


number(6,2),total_cost number(8,2),updated_date date,updated_by varchar2(10));

Table created

SQL>desc created

SQL>desc ode

Name Null? Type

ORDER_ID NUMBER(5)

QUANTITY NUMBER(4)

COST_PER_ITEM NUMBER(6,2)

TOTAL_COST NUMBER(8,2)

UPDATED_DATE DATE

UPDATED_BY DATE

SQL>insert into ode(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 ode(order_id,quantity,cost_per_item)values(&order_id,

&quantity,&cost_per_item);

New 1:insert into ode(order_id,quantity,cost_per_item)value(1,4,20)

1 rows created

SQL>/

Enter value for order_id:2

Enter value for quantity:6

Enter value for cost_per_item:30

Old 1:insert into ode(order_id,quantity,cost_per_item)values(&order_id,

&quantity,&cost_per_item)

New 1:insert into ode(order_id,quantity,cost_per_item)values(2,6,30)


1 row created

SQL>select *from ode;

ORDER_ID QUANTITY COST_PER_ITEM TOTAL_COST UPDATED_D


UPDATED_BY

1 4 20

2 6 30

SQL>create or replace trigger ode_before_update

2 before update

3 on orders

4 for each row

5declare

6v_username varchar2(10);

7begin
8 select user into v_username from dual;

9:new.updated_date:=sysdate;

10:new.updated_by:=v_username;

11end;

12 /

Trigger created

SQL>update ode set total_cost=3000 where order_id:2;

1 rows updated

SQL>select *from ode;

ORDER_ID QUANTITY COST_PER_ITEM TOTAL_COST UPDATED_D


UPDATED_BY

1 4 2
0
2 6 3 30
0 00

SQL>create or replace trigger ode_after_update

2 after update
3 on ode30
4 for each row
5 declare
6 v_username varchar2(10);
7 begin
8 select user into v_username
9 from dual;
10 insert into ode_audit
11 (order_id,
12 Quantity_before,
13 Quantity_after,
14 Username
15 Values
16 (:new.order_id,
17 :old.quantity,
18 :new.quantity,v_username);
19 End;
20 /
Trigger created

SQL>select *from

ode_audit; No rows

selected

You might also like