0% found this document useful (0 votes)
15 views41 pages

DBMS Practicak Key to Success

Uploaded by

hamiscam
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)
15 views41 pages

DBMS Practicak Key to Success

Uploaded by

hamiscam
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/ 41

PRACTICAL 1

-----CREATE TABLE--------

create table deposit (act_no varchar2(5), c_name varchar2(18), b_name varchar2(18), amount
number(8,2), adate date);

Table created.
_______________________

create table branch(b_name varchar2(18), city varchar2(18));

Table created.

_______________________

create table customers(c_name varchar(19) , city varchar(18));

Table created.

_______________________

create table borrow(loan_no varchar(5), c_name varchar(18), b_name varchar(18), amount


number(8,2));

Table created.

___________________________
PRACTICAL 2

-----INSERT DATA INTO TABLE--------


STEP 1 See the following data using

desc deposit

Name Null? Type


----------------------------------------- -------- ----------------------------
ACT_NO VARCHAR2(5)
C_NAME VARCHAR2(18)
B_NAME VARCHAR2(18)
AMOUNT NUMBER(8,2)
ADATE DATE

STEP 2 INSERT DATA INTO TABLE


insert into deposit values (100,'anil','vrce',100.00,'1-MAR-95');

1 row created.

OR

STEP 2 ENTER DATA USING SCRIPT

insert into deposit values (&A,'&cname' ,'&bname', '&amount','&adata');


Enter value for a: 101
Enter value for cname: sunil
Enter value for bname: ajni
Enter value for amount: 5000
Enter value for adata: 4-JAN-96
old 1: insert into deposit values (&A,'&cname' ,'&bname', '&amount','&adata')
new 1: insert into deposit values (101,'sunil' ,'ajni', '5000','4-JAN-96')

_________________________________
**HOW TO UPDATE VALUES IN TABLE**
update deposit set amount = 1000 where act_no = 100;
1 row updated.
_________________________________
PRACTICAL 3 [connect system]

-----USE DDL COMMANDS--------

STEP 1 CREATE TABLE

create table employees(empid number, firstname varchar2(20), lastname varchar2(20), email


varchar(20), gender char(1), mobileno char(10), doj date default sysdate, deptid int);

---Create table emp with same structure of employees---

create table emp as select * from employees;

Table created.

---Create table emp1 with few columns of employees---

create table emp1 as select empid, firstname from employees;

Table created.
----Add column to table----

alter table emp add blood_group int;

Table altered.

-----Remove column from table----- {POSSIBLE ONLY IN SYSTEM}

alter table emp drop column blood_group;

Table altered.

-------alter database of single and multiple columns in table ----------


(1) Single Column

alter table emp modify firstname int;

Table altered.

(2) Multiple Columns

alter table emp modify (firstname int, lastname int);

Table altered.

--------Delete column in table [ Similar to drop ] --------{POSSIBLE ONLY IN SYSTEM}

alter table emp set unused column blood_group;

Table altered.

------DROP / DELETE TABLE-------

drop table emp1;

Table dropped.

------Truncate - Used to delete all rows from the table -------

truncate table emp;

Table truncated.

------Rename table ------


METHOD 1

alter table emp rename to emp2;

Table altered.

METHOD 2

rename emp2 to emp;

Table renamed.

_________________________________
PRACTICAL 4 [connect system]

-----USE SQL CONSTRAIN Unique and Not Null--------

(1) Add Not Null Constrain in table

alter table employees modify firstname varchar2(20) not null;

Table altered.

----WHEN WE TRY TO ENTER NULL DATA INSERT QUERY----


USING:
insert into employees values(10,NULL,'Parmar','Krishna','M',103336547,'19-Mar-03',100);
IT SHOWS/DISPLAY:
insert into employees values(10,NULL,'Parmar','Krishna','M',103336547,'19-Mar-03',100)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."EMPLOYEES"."FIRSTNAME")

(2) Add Unique Constrain in table

alter table employees add constraint em_lastname unique(lastname);

Table altered.

----WHEN WE TRY TO VIOLATE CONSTRAINT BY ENTERING INSERT QUERY----


USING:
insert into employees values(2,'Cool','Tejas','[email protected]','F',198836547,'30-Mar-04',500);
IT SHOWS/DISPLAY:
insert into employees values(2,'Cool','Tejas','[email protected]','F',198836547,'30-Mar-04',500)
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.EM_LASTNAME) violated

(3) Add Multiple Unique Constraint in table


alter table employees add constraint em_lastname_firstname unique(lastname,firstname);

(4) Drop Constraint in table


alter table employees drop constraint em_lastname;

Table altered.

*****NOTE : IF SINGLE CONSTRAIN IS ADDED PRIOR TO MULTIPLE CONSTRAIN APPLY


ON COMMON COLUMN , IN ORDER TO SHOW ERROR FOR MULTIPLE CONSTRAIN YOU
NEED TO DROP SINGLE CONSTRAIN FIRST. *****

______________________________
_________________________________
PRACTICAL 5 [connect system]

-----USE SQL CONSTRAINT Check and Primary Key--------

(1) Add Check Constrain in table


alter table employees add constraint employees_gnd_chk check(gender in('F','M'));

Table altered.

OR

alter table employees add constraint employees_gnd_chk check(gender='M' or gender='F');

Table altered.

----WHEN WE TRY TO VIOLATE CONSTRAINT BY ENTERING INSERT QUERY----


USING:
insert into employees values(2,'aaCool','aajas','[email protected]','S',198836547,'30-Mar-04',500);
IT SHOWS/DISPLAY:
insert into employees values(2,'aaCool','aajas','[email protected]','S',198836547,'30-Mar-04',500)
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.EMPLOYEES_GND_CHK) violated

(2) Display all the constraints applied on the table


select constraint_name, constraint_type from user_constraints where table_name =
'EMPLOYEES';

CONSTRAINT_NAME C
------------------------------ -
SYS_C007149 C
EMPLOYEES_GND_CHK C
EM_LASTNAME_FIRSTNAME U

(3) Display all the constraints with column name applied on the table

select constraint_name, column_name from user_cons_columns where table_name =


'EMPLOYEES';

CONSTRAINT_NAME
------------------------------
COLUMN_NAME
--------------------------------------------------------------------------------
SYS_C007149
FIRSTNAME

EM_LASTNAME_FIRSTNAME
LASTNAME

EM_LASTNAME_FIRSTNAME
FIRSTNAME

CONSTRAINT_NAME
------------------------------
COLUMN_NAME
--------------------------------------------------------------------------------
EMPLOYEES_GND_CHK
GENDER

(4) Delete Rows from table without deleting the data

delete from employees;

4 rows deleted.

(5) Add Primary Key Constrain in table


alter table employees add constraint primary_emp primary key(empid);

Table altered.

(6) Add Primary Key while creating table

create table department(d_id int primary key, dept_name varchar2(20));

Table created.
______________________________
_________________________________
PRACTICAL 6 [connect system]
----- Foreign Key and inserting data using Script-----

(1) Create Foreign Key

Step 1 Enter values in Department (Parent )


Step 2 Enter value in Employee (Child)
Step 3
alter table employees add constraint emp_dept_id_fk foreign key (deptid) references
department (d_id);

Table altered.

(2) Deleting a row using a value

delete from department where d_id = 102;

1 row deleted.

(3) MEANS
alter table employees add constraint emp_dept_id_fk foreign key (deptid) references
department (d_id) on delete set null;

(4) MEANS
alter table employees add constraint emp_dept_id_fk foreign key (deptid) references
department (d_id) on delete cascade;

(5) Enter Data in Table using Script


insert into department values(&d_id , '&dept_name');
Enter value for d_id: 102
Enter value for dept_name: Parth
old 1: insert into department values(&d_id , '&dept_name')
new 1: insert into department values(102 , 'Parth')

1 row created.

(6) Enter Particular data in Table


insert into employees (empid,firstname,lastname,deptid) values (5,'Ben','Rock',104);

1 row created.

______________________________
_________________________________
PRACTICAL 7 [connect system]
----- DDL & DML commands ------
1) Update
2) Delete
3) Drop
4) Truncate
5) Merge
__________________________

(1) Creating copy of exisiting table using its attributes


create table copy_emp(e_id, e_fname, e_lname, e_mbno, d_id) as select empid, firstname,
lastname, mobileno, deptid from employees where empid=0;

(2)Inserting values into Copy


insert into copy_emp values(1, 'Pa','Tha',81036944,100);

1 row created.

(3)Updating Values in table using attribute


update copy_emp set e_fname = 'Viraj',e_lname = 'Patel' where e_id = 1;

1 row updated.

(4)Deleting Values in table using attribute


delete from copy_emp where e_id = 1;

1 row deleted.

(5)Deleting Columns from table


alter table copy_emp drop column e_mbno;

Table altered.
(6)Drop Copy table
drop table copy_emp;

Table dropped.

(7) Truncate table


truncate table copy_emp;

Table truncated.

(8) Merger Two rows into one by giving specific Condition


Step 1
select * from copy_emp;
Step 2
select * from employees;
Step 3
merge into copy_emp c using employees e on (e.empid = c.e_id) when matched then update
set c.e_fname = e.firstname, c.e_lname = e.lastname, c.d_id = e.deptid when not matched then
insert values(e.empid, e.firstname, e.lastname, e.mobileno, e.deptid);

3 rows merged.

(9)DOUBT - Not Working


merge into copy_emp2 c using copy_emp3 e on (e.empid = c.empid) when matched then
update set c.firstname = e.firstname, c.lastname = e.lastname, c.mobileno = e.mobile when not
matched then insert values(e.empid, e.firstname, e.lastname, e.mobileno);

______________________________
_________________________________
PRACTICAL 8 [connect system]
----- TCL commands ------
1) Commit

2) Savepoint

3) Rollback

__________________________
(1) Commit
CODE : commit;

OUTPUT : Commit complete.


(2) Deleting particular data using given data
CODE : delete from department where d_id = 103;
OUTPUT : 1 row deleted.
(3)RollBack
CODE : rollback;
OUTPUT : Rollback complete.

(4)Rollback to xyz step


Step 1
select * from department;

D_ID DEPT_NAME
---------- --------------------
100 Viraj
102 Parth
104 Tom

Step 2
savepoint deletion;

Savepoint created.

Step 3
insert into department values (103, 'Harshal');

1 row created.

Step 4
savepoint insertion;

Savepoint created.

Step 5
select * from department;

D_ID DEPT_NAME
---------- --------------------
100 Viraj
103 Harshal
102 Parth
104 Tom

Step 6
rollback to deletion;

Rollback complete.
Step 7
select * from department;

D_ID DEPT_NAME
---------- --------------------
100 Viraj
102 Parth
104 Tom

(5) Insertion and deletion Savepoint and roll back

Step 1
insert into department values (103, 'Harshal');

1 row created.

Step 2 savepoint insertion;

Savepoint created.

Step 3 delete from department where d_id = 103;

1 row deleted.

Step 4 savepoint deletion;

Savepoint created.

Step 5 select * from department;

D_ID DEPT_NAME
---------- --------------------
100 Viraj
102 Parth
104 Tom

Step 6 rollback to deletion;

Rollback complete.

Step 7 select * from department;

D_ID DEPT_NAME
---------- --------------------
100 Viraj
102 Parth
104 Tom

Step 8 rollback to insertion;

Rollback complete.

Step 9 select * from department;

D_ID DEPT_NAME
---------- --------------------
100 Viraj
103 Harshal
102 Parth
104 Tom

(6) Select data from system while using sysdba


Step 1

conn sys as sysdba


Enter password:
Connected.

Step 2
CODE : select * from system.department;
OUTPUT :
D_ID DEPT_NAME
---------- --------------------
100 Viraj
103 Harshal
102 Parth
104 Tom

______________________________
****FOR THE NEXY FEW QUERIES ENTER DATA INTO EMPLOYEES*******
______________________________

STEP 1 insert into employees


values(1,'Pa','Ta','[email protected]','M','123456789','19-Mar-20',103);

1 row created.
STEP 2 insert into employees
values(2,'Va','De','[email protected]','F','123459789','19-Mar-21',104);

1 row created.

STEP 3 insert into employees


values(3,'Viraj','Patel','[email protected]','M','127899789','19-Mar-23',105);

1 row created.
______________________________

(7) Join Two Columns of table into another table [conn sys as sysdba]
CODE :
select firstname || ' ' || lastname "Full Name", doj from employees order by firstname;

OUTPUT :
Full Name DOJ
----------------------------------------- ---------
Pa Ta 19-MAR-20
Va De 19-MAR-21
Viraj Patel 19-MAR-23

OR
CODE :
select firstname || ' ' || lastname "Full Name", doj from employees order by mobileno;

OUTPUT :
Full Name DOJ
----------------------------------------- ---------
Pa Ta 19-MAR-20
Va De 19-MAR-21
Viraj Patel 19-MAR-23

(8) SORTING DATA


(a) CODE :
select firstname, lastname,email from employees order by lastname desc, firstname;

OUTPUT :
FIRSTNAME LASTNAME EMAIL
-------------------- -------------------- --------------------
Pa Ta [email protected]
Viraj Patel [email protected]
Va De [email protected]
(b) CODE :
select firstname, lastname,email from employees order by lastname desc, firstname
desc;

OUTPUT :
FIRSTNAME LASTNAME EMAIL
-------------------- -------------------- --------------------
Pa Ta [email protected]
Viraj Patel [email protected]
Va De [email protected]

(c) CODE :
select firstname, lastname,email from employees order by lastname, firstname desc;

OUTPUT :
FIRSTNAME LASTNAME EMAIL
-------------------- -------------------- --------------------
Va De [email protected]
Viraj Patel [email protected]
Pa Ta [email protected]

______________________________
PRACTICAL 9 [connect system]
----- SQL Functions ------
__________________________
1. Character Functions

2. Number Functions

3. Date Functions

________________________
******* TO COMPARE TABLE ******
select * from employees;
EMPID FIRSTNAME LASTNAME EMAIL G MOBILENO DOJ
DEPTID
---------- -------------------- -------------------- -------------------- - ---------- --------- ----------
5 Vi Pal [email protected] M 123456789 19-MAR-02 104
1 Viraj Patel virajp526 M 8140390836 20-MAR-02
2 Parth Thakkar parth526 M 9120390836 19-MAR-03

(0) Display value for particular value in table


CODE : select empid, firstname, lastname from employees where lastname = 'Patel';
OUTPUT :
EMPID FIRSTNAME LASTNAME
---------- -------------------- --------------------
1 Viraj Patel

(1) Character Functions

(a) UPPER & LOWER

CODE : select empid, upper(firstname), lastname from employees where


lastname = 'Patel';

OUTPUT:

EMPID UPPER(FIRSTNAME) LASTNAME

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

1 VIRAJ Patel

CODE : select empid, upper(firstname) as Fname, lastname from employees


where lastname = 'Patel';

OUTPUT:

EMPID FNAME LASTNAME

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

1 VIRAJ Patel

CODE : select empid, upper(firstname) as Fname, lastname from employees


where lastname = lower('Patel');

OUTPUT: no rows selected

CODE : select empid, upper(firstname) as Fname, lastname from employees


where lower(lastname) = 'Patel';
OUTPUT: no rows selected

(b) INITCAP

CODE : select empid, upper(firstname) as Fname, lastname from employees


where initcap(lastname) = 'Patel';

OUTPUT:

EMPID FNAME LASTNAME

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

1 VIRAJ Patel

CODE : select empid, upper(firstname) as Fname, initcap(lastname) from


employees where initcap(lastname) = 'Patel';

OUTPUT:

EMPID FNAME INITCAP(LASTNAME)

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

1 VIRAJ Patel

CODE : select empid, upper(firstname) as Fname, initcap(lastname) as Lname


from employees where initcap(lastname) = 'Patel';

OUTPUT:

EMPID FNAME LNAME

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

1 VIRAJ Patel

(c) Character-Manipulation Functions:


(i) LENGTH:

CODE : select empid,firstname from employees where length(lastname) = 7;

OUTPUT:

EMPID FIRSTNAME

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

2 Parth

CODE : select empid,firstname from employees where length(lastname) = 3;

OUTPUT:

EMPID FIRSTNAME

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

5 Vi

CODE : select empid,firstname, length(lastname) from employees where


length(lastname) = 3;

OUTPUT:

EMPID FIRSTNAME LENGTH(LASTNAME)

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

5 Vi 3

CODE : select empid,firstname, length(lastname) from employees;


OUTPUT:

EMPID FIRSTNAME LENGTH(LASTNAME)

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

1 Viraj 5

2 Parth 7

5 Vi 3

(ii) CONCAT:

CODE : select empid, concat(firstname, lastname) from employees;

OUTPUT:

EMPID CONCAT(FIRSTNAME,LASTNAME)

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

1 VirajPatel

2 ParthThakkar

5 ViPal

CODE : select empid, concat(concat(firstname,' '),lastname) from employees;

OUTPUT :

EMPID CONCAT(CONCAT(FIRSTNAME,''),LASTNAME)

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

1 Viraj Patel

2 Parth Thakkar

5 Vi Pal
(iii)SUBSTR:

CODE : select empid, firstname, substr(lastname,0,3) from employees;

OUTPUT:

EMPID FIRSTNAME SUBSTR(LASTN

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

1 Viraj Pat

2 Parth Tha

5 Vi Pal

CODE : select empid, firstname, substr(lastname,1,3) from employees;

OUTPUT:

EMPID FIRSTNAME SUBSTR(LASTN

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

1 Viraj Pat

2 Parth Tha

5 Vi Pal

CODE : select empid, substr(firstname,3,5), substr(lastname,1,3) from


employees;

OUTPUT:

EMPID SUBSTR(FIRSTNAME,3,5 SUBSTR(LASTN

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

1 raj Pat

2 rth Tha
5 Pal

CODE : select empid, substr(firstname,3,2), substr(lastname,1,3) from


employees;

OUTPUT:

EMPID SUBSTR(F SUBSTR(LASTN

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

1 ra Pat

2 rt Tha

5 Pal

(iv)LPAD & RPAD:

CODE: select empid, rpad(firstname,10,'*') from employees;

OUTPUT:

EMPID RPAD(FIRSTNAME,10,'*')

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

1 Viraj*****

2 Parth*****

5 Vi********

CODE: select empid, rpad(substr(firstname,2,3),10,'*') from employees;

OUTPUT:

EMPID RPAD(SUBSTR(FIRSTNAME,2,3),10,'*')

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

1 ira*******

2 art*******
5 i*********

CODE: select empid, lpad(rpad(firstname,10,'*'),20,'*') from employees;

OUTPUT:

EMPID LPAD(RPAD(FIRSTNAME,10,'*'),20,'*')

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

1 **********Viraj*****

2 **********Parth*****

5 **********Vi********

CODE: select empid, lpad(rpad(firstname,10,'*'),10,'*') from employees;

OUTPUT:

EMPID LPAD(RPAD(FIRSTNAME,10,'*'),10,'*')

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

1 Viraj*****

2 Parth*****

5 Vi********

2. NUMBER FUNCTION

CODE : select * from dual;

OUTPUT :

X
(i) MOD:

CODE :select mod(7,2) from dual;

OUTPUT :

MOD(7,2)

----------

CODE :select mod(34.3452,2) from dual;

OUTPUT :

MOD(34.3452,2)

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

.3452

(ii) ROUND:

CODE : select round(34.3456) from dual;

OUTPUT :

ROUND(34.3456)

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

34

CODE: select round(34.9898) from dual;

OUTPUT :

ROUND(34.9898)

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

35
(iii) TRUNC:

CODE :select trunc(34.5656) from dual;

OUTPUT :

TRUNC(34.5656)

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

34

CODE : select trunc(34.9) from dual;

OUTPUT :

TRUNC(34.9)

-----------

34

3. DATE FUNCTION

CODE : select sysdate from dual;

OUTPUT :

SYSDATE

---------

17-MAY-22

CODE : select sysdate + 5 from dual ;

OUTPUT :

SYSDATE+5

---------

22-MAY-22
CODE : select sysdate + 31 from dual ;

OUTPUT :

SYSDATE+3

---------

17-JUN-22

CODE : select sysdate - 5 from dual;

OUTPUT :

SYSDATE-5

---------

12-MAY-22

CODE :select sysdate + 14/24 from dual;

OUTPUT :

SYSDATE+1

---------

18-MAY-22

CODE : select sysdate + 5/24 from dual;

OUTPUT :

SYSDATE+5

---------

18-MAY-22

CODE : select systimestamp from dual;

OUTPUT :

SYSTIMESTAMP
---------------------------------------------------------------------------

17-MAY-22 09.34.34.313000 PM +05:30

CODE : select to_date('31-Dec-22') - to_date('22-May-22') from dual;

OUTPUT :

TO_DATE('31-DEC-22')-TO_DATE('22-MAY-22')

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

223

CODE : select to_date('31-Dec-21') - to_date('22-May-22') from dual;

OUTPUT :

TO_DATE('31-DEC-21')-TO_DATE('22-MAY-22')

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

-142

CODE : select months_between('31-Dec-22','22-May-22') from dual;

OUTPUT :

MONTHS_BETWEEN('31-DEC-22','22-MAY-22')

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

7.29032258

CODE : select next_day('22-May-22','Friday') from dual;

OUTPUT :

NEXT_DAY(

---------

27-MAY-22

CODE : select add_months('22-May-22',2) from dual;


OUTPUT :

ADD_MONTH

---------

22-JUL-22

CODE : select add_months('22-May-22',15) from dual;

OUTPUT :

ADD_MONTH

---------

22-AUG-23

CODE :select round(months_between('31-Dec-22','22-May-22')) from dual;

OUTPUT :

ROUND(MONTHS_BETWEEN('31-DEC-22','22-MAY-22'))

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

_________________________________
PRACTICAL 10-1 [connect system]
----- use of Null Value Handing ------
__________________________
1. SQL Functions

********** INORDER TO COMPARE OTHER TABLE WITH MAIN*******

CODE: select * from employees;

OUTPUT:

EMPID FIRSTNAME LASTNAME EMAIL G MOBILENO DOJ


DEPTID

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

5 Vi Pal [email protected] M 123456789 19-MAR-02 104

1 Viraj Patel virajp526 M 8140390836 20-MAR-02


2 Parth Thakkar parth526 M 9120390836 19-MAR-03

_____________________________________________________

(a) to_char

CODE : select empid, firstname, lastname, to_char(doj,'DAY') as day_of_join from


employees;

OUTPUT :

EMPID FIRSTNAME LASTNAME DAY_OF_JOIN

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

5 Vi Pal TUESDAY

1 Viraj Patel WEDNESDAY

2 Parth Thakkar WEDNESDAY

CODE : select empid, firstname, lastname, to_char(doj,'D') as day_of_join from


employees;

OUTPUT :

EMPID FIRSTNAME LASTNAME D

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

5 Vi Pal 3

1 Viraj Patel 4

2 Parth Thakkar 4

CODE : select empid, firstname, lastname, to_char(doj,'Day') as day_of_join from


employees where to_char(doj, 'fmDay') = 'TUESDAY';

OUTPUT :

no rows selected

CODE : select empid, firstname, lastname, to_char(doj,'Day') as day_of_join from


employees where to_char(doj, 'fmDay') = 'Tuesday';

OUTPUT :

EMPID FIRSTNAME LASTNAME DAY_OF_JOIN

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


5 Vi Pal Tuesday

CODE : select to_char(sysdate,'dd mm yy') from dual;

OUTPUT :

TO_CHAR(

--------

17 05 22

CODE : select to_char(sysdate, 'dd month yy') from dual;

OUTPUT :

TO_CHAR(SYSDATE,'DDMONTHYY')

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

17 may 22

CODE : select to_char(sysdate, 'dd month year') from dual;

OUTPUT :

TO_CHAR(SYSDATE,'DDMONTHYEAR')

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

17 may twenty twenty-two

CODE : select to_char(sysdate, 'day month year') from dual;

OUTPUT :

TO_CHAR(SYSDATE,'DAYMONTHYEAR')

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

tuesday may twenty twenty-two

CODE : select to_char(sysdate, 'DL') from dual;

OUTPUT :

TO_CHAR(SYSDATE,'DL')
-----------------------------------------------------------------------------------

Tuesday, May 17, 2022

_________________________________
PRACTICAL 10-2 [connect system]
----- use of Null Value Handing ------
__________________________
● SQL Functions
● SQL Views
● Creating Users
● DCL commands

*****INORDER TO COMPARE IT*******

CODE: select * from deposit;

OUTPUT :

ACT_N C_NAME B_NAME SALARY ADATE COMM

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

100 anil vrce 1000 01-MAR-95 100

102 mehul karolbaugh 3500 17-NOV-95

104 Madhuri chandi 1200 17-DEC-95 0

105 Prmod M.G.road 3000 27-MAR-96 500

106 sandip andheri 0 31-MAR-96 0

107 Shiavni Virar 1000 05-SEP-95 0

108 Kranti Nehru Place 5000 02-JUL-95 108

109 Minu Powai 7000 10-AUG-95 140

110 Parht

9 rows selected.

—-------------------

(a) SQL FUNCTION

CODE : select act_n, c_name, salary, comm, salary + (salary * (comm/100)) as inhand_amt from
deposit;

OUTPUT:

ACT_N C_NAME SALARY COMM INHAND_AMT

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


100 anil 1000 100 2000

102 mehul 3500

104 Madhuri 1200 0 1200

105 Prmod 3000 500 18000

106 sandip 0 0 0

107 Shiavni 1000 0 1000

108 Kranti 5000 108 10400

109 Minu 7000 140 16800

110 Parht

9 rows selected.

CODE: select act_n, c_name, salary, comm, salary + (salary * nvl((comm/100),0)) as inhand_amt
from deposit;

OUTPUT:

ACT_N C_NAME SALARY COMM INHAND_AMT

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

100 anil 1000 100 2000

102 mehul 3500 3500

104 Madhuri 1200 0 1200

105 Prmod 3000 500 18000

106 sandip 0 0 0

107 Shiavni 1000 0 1000

108 Kranti 5000 108 10400

109 Minu 7000 140 16800

110 Parht

9 rows selected.

CODE: select act_n, c_name, salary, comm, coalesce(comm, salary, 5) as new from deposit;

OUTPUT:
ACT_N C_NAME SALARY COMM NEW

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

100 anil 1000 100 100

102 mehul 3500 3500

104 Madhuri 1200 0 0

105 Prmod 3000 500 500

106 sandip 0 0 0

107 Shiavni 1000 0 0

108 Kranti 5000 108 108

109 Minu 7000 140 140

110 Parht 5

9 rows selected.

(B) SQL VIEW

CODE : create view parth as select empid, firstname, lastname , gender from employees;

OUTPUT :

View created.

CODE : create view viraj as select empid, firstname, lastname , gender from employees;

OUTPUT :

View created.

CODE : select * from viraj;

OUTPUT :

EMPID FIRSTNAME LASTNAME G

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

5 Vi Pal M

1 Viraj Patel M

2 Parth Thakkar M
(c) Creating User

CODE : create user viraj identified by root;

OUTPUT :

User created.

CODE: conn system;

OUTPUT :

Enter password:

Connected.

CODE: grant create session to viraj;

OUTPUT :

Grant succeeded.

CODE:conn viraj;

OUTPUT :

Enter password:

Connected.

CODE: conn system;

OUTPUT :

Enter password:

Connected.

CODE: grant create table, create view to viraj;

OUTPUT :

Grant succeeded.

CODE: grant unlimited tablespace to viraj;

OUTPUT :
Grant succeeded.

CODE: create table temp_1(id number, fname varchar2(20));

OUTPUT :

Table created.

CODE: insert into temp_1 values (1,'parth');

OUTPUT :

1 row created.

CODE: insert into temp_1 values (2,'Viraj');

OUTPUT :

1 row created.

CODE: insert into temp_1 values (3,'Miit');

OUTPUT :

1 row created.

CODE: select * from temp_1;

OUTPUT :

ID FNAME

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

1 parth

2 Viraj

3 Miit

CODE: grant select on department to viraj;

Grant succeeded.
CODE: select * from system.department;

OUTPUT :

D_ID DEPT_NAME

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

100 Viraj

103 Harshal

102 Parth

104 Tom

_________________________________
PRACTICAL 11 [connect system]
-----Joints in SQL ------
__________________________
1. INNER JOIN

2. LEFT JOIN

3. RIGHT JOIN

4. FULL JOIN

5. SELF JOIN

*********INORDER TO COMPARE DATA************


STEP 1
select * from employees;
OUTPUT
EMPID FIRSTNAME LASTNAME EMAIL G MOBILENO DOJ
DEPTID
---------- -------------------- -------------------- -------------------- - ---------- --------- ----------
5 Vi Pal [email protected] M 123456789 19-MAR-02 104
1 Viraj Patel virajp526 M 8140390836 20-MAR-02
2 Parth Thakkar parth526 M 9120390836 19-MAR-03

STEP 2
select * from department;
OUTPUT
D_ID DEPT_NAME
---------- --------------------
100 Viraj
103 Harshal
102 Parth
104 Tom
—--------------
(1)Inner joint
CODE:
select firstname, lastname , dept_name from employees inner join department on
employees.deptid = department.d_id;
OUTPUT:
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Vi Pal Tom

(2) Left Joint


CODE:
select firstname, lastname , dept_name from employees left join department on
employees.deptid = department.d_id;
OUTPUT:
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Vi Pal Tom
Viraj Patel
Parth Thakkar

CODE:
select firstname, lastname , dept_name from department left join employees on department.d_id
= employees.deptid;
OUTPUT:
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Vi Pal Tom
Harshal
Viraj
Parth

(3) DOUBT
CODE
select firstname, lastname , dept_name from employees, department where deptid = d_id(+);
OUPUT
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Vi Pal Tom
Viraj Patel
Parth Thakkar
CODE
select firstname, lastname , dept_name from employees, department where deptid(+) = d_id;
OUTPUT
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Vi Pal Tom
Harshal
Viraj
Parth
(4)RIGHT JOINT
CODE
select firstname, lastname, dept_name from employees right join department on
employees.deptid = department.d_id;
OUTPUT
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Vi Pal Tom
Harshal
Viraj
Parth
CODE
select firstname, lastname, dept_name from department right join employees on
department.d_id = employees.deptid;
OUTPUT
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Vi Pal Tom
Viraj Patel
Parth Thakkar

(5)Full outer joint


CODE: select firstname, lastname, dept_name from employees full outer join department on
employees.deptid = department.d_id;
OUTPUT
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Viraj
Harshal
Parth
Vi Pal Tom
Parth Thakkar
Viraj Patel

6 rows selected.
CODE: select firstname, lastname, dept_name from department full outer join employees on
department.d_id = employees.deptid;
OUTPUT
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Viraj
Harshal
Parth
Vi Pal Tom
Parth Thakkar
Viraj Patel

6 rows selected.

CODE: select firstname, lastname, dept_name from department full outer join employees on
department.d_id = employees.deptid where employees.deptid>103;
OUTPUTL:
FIRSTNAME LASTNAME DEPT_NAME
-------------------- -------------------- --------------------
Vi Pal Tom

(7)DOUBT
CODE
select m.firstname as manager, w.firstname as worker from employees m, employees w where
w.managerid = m.empid and w.empid>1;

CODE
select m.firstname as manager, w.firstname as worker from employees m, employees w where
w.managerid = m.empid and m.empid>1;

CODE
select m.firstname as manager, w.firstname as worker from employees m, employees w where
w.managerid = m.empid and w.managerid>1;
______________________________
PRACTICAL 12 [connect system]
----- Subqueries in SQL ------
__________________________

select * from deposit;


select salary from deposit where c_name = 'Madhuri'; - Give salary of madhuri

------SubQuery-----
select act_n, c_name from deposit where salary > (select salary from deposit where c_name =
'Madhuri'); - display account no. , cname and salary of those whose salary is greater than salary
of madhuri

------add salary columns-----


alter table employees add salary number(10);

------add values in salary columns [USING SCRIPT]----


update employees set salary = &s where empid = &e;
#Enter value for s:# 10000
#Enter value for e:# 3

/ -used in script to enter data of next row

----Enter values in Table -----


insert into employees
values(6,'zalak','patel','[email protected]','f',9123456781,'05-may-22',103,15000);

-----Update salary of 10% increment to employes who are in department of zeal------

Step1 Find Department of Zeal


select deptid from employees where firstname = 'zalak'; - give deptid of zeal

Step2 Subquery
update employees set salary = (salary + (salary * 0.1)) where deptid = (select deptid from
employees where firstname = 'zalak'); --will do increment of salary of those who are in
department of zeal

----Sort salary of those whose salary than zalak where there are multiple entries of zalak----
Step 1
select empid,firstname,lastname, salary from employees where (firstname = 'zalak'); - give
details of all firstname named zalak
Step 2 [using all where it gives data greater than all zalak]
select empid,firstname,lastname, salary from employees where salary > all( select salary from
employees where firstname = 'zalak'); - when there are multiple zalak then it gives output of
salary greater than all zalak
OR
Step 2 [using any where it gives data greater than each zalak]
select empid,firstname,lastname, salary from employees where salary > any( select salary from
employees where firstname = 'zalak'); - when there are multiple zalak then it gives output of
salary greater than any zalak

----Display Employees salary greater than min. salary ----


select empid,firstname,lastname,salary from employees where salary > (select min(salary) from
employees);

----[CAN COME IN EXAM] Display Employees salary 2nd max ----


Step 1
select max(salary) from employees where salary < (select max(salary) from employees); - give
2nd highest sALARY
Step 2 [ Using 2 SubQueries]
select empid,firstname,lastname, salary from employees where salary = (select max(salary)
from employees where salary < (select max(salary) from employees)); - display all details of
employee with 2nd highest salary

—---------------------------------
select * from employees where salary = any(select max(salary) from employees group by
deptid);
OUTPUT :
EMPID FIRSTNAME LASTNAME EMAIL G MOBILENO DOJ
DEPTID SALARY
---------- -------------------- -------------------- -------------------- - ---------- --------- ---------- ----------
6 zalak atel [email protected] F 9123456781 05-MAY-22 103
16500
1 Viraj Patel virajp526 M 8140390836 20-MAR-02
1000

__________________________________________
______________________________
PRACTICAL 13 [connect system]
----- Procedural language SQL ------
__________________________
(1)WAC to compare which number is smaller
Step 1
set serverout on;
Step 2
1 DECLARE
2 a number;
3 b number;
4 c number;
5 PROCEDURE findMin(x IN number , y IN number , z out number) is
6 BEGIN
7 if x<y THEN
8 z:= x;
9 ELSE
10 z:= y;
11 END IF;
12 END;
13 BEGIN
14 a:= 23;
15 b:= 45;
16 findMin(a,b,c);
17 dbms_output.put_line('Minimum of (23,45) : ' || c);
18* END;
Step 3 Execute Using
CODE : /
Output
Minimum of (23,45) : 23

PL/SQL procedure successfully completed.

(2) To edit the written PL SQL code


CODE : ed
OUTPUT : Wrote file afiedt.buf
Opens the given file in NotePad

(3)WAC to compare which number is bigger

Step 1 (if Not)


set serverout on;
Step 2

1 DECLARE
2 a number;
3 b number;
4 c number;
5 function findmax(x IN number , y IN number)
6 return number
7 is
8 z number;
9 BEGIN
10 if x>y then
11 z:= x;
12 ELSE
13 z:= y;
14 END IF;
15 return z;
16 END;
17 BEGIN
18 a:= 23;
19 b:= 45;
20 c:= findmax(a,b);
21 dbms_output.put_line('Minimum of (23,45) : ' || c);
22* END;

Step 3 Execute Using


CODE : /
Output
Maximum of (23,45) : 45

PL/SQL procedure successfully completed.

(4)Trigger
Step 1
create or replace trigger display_deptid_changes
2 before delete or insert or update on employees
3 for each row
4 when(new.empid > 0)
5 declare
6 deptid_diff number;
7 begin
8 deptid_diff := :new.deptid - :old.deptid;
9 dbms_output.put_line('old deptid:' || :old.deptid);
10 dbms_output.put_line('new deptid:' || :new.deptid);
11 dbms_output.put_line('Dept Difference:' || deptid_diff);
12* end;
13
14 /

Trigger created.
Step 2
insert into employees values(5,'Vi','Pal','[email protected]','M',123456789,'19-Mar-02',104);
old deptid:
new deptid:104
Dept Difference:

1 row created.

You might also like