DBMS Practicak Key to Success
DBMS Practicak Key to Success
-----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.
_______________________
Table created.
_______________________
Table created.
_______________________
Table created.
___________________________
PRACTICAL 2
desc deposit
1 row created.
OR
_________________________________
**HOW TO UPDATE VALUES IN TABLE**
update deposit set amount = 1000 where act_no = 100;
1 row updated.
_________________________________
PRACTICAL 3 [connect system]
Table created.
Table created.
----Add column to table----
Table altered.
Table altered.
Table altered.
Table altered.
Table altered.
Table dropped.
Table truncated.
Table altered.
METHOD 2
Table renamed.
_________________________________
PRACTICAL 4 [connect system]
Table altered.
Table altered.
Table altered.
______________________________
_________________________________
PRACTICAL 5 [connect system]
Table altered.
OR
Table altered.
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
CONSTRAINT_NAME
------------------------------
COLUMN_NAME
--------------------------------------------------------------------------------
SYS_C007149
FIRSTNAME
EM_LASTNAME_FIRSTNAME
LASTNAME
EM_LASTNAME_FIRSTNAME
FIRSTNAME
CONSTRAINT_NAME
------------------------------
COLUMN_NAME
--------------------------------------------------------------------------------
EMPLOYEES_GND_CHK
GENDER
4 rows deleted.
Table altered.
Table created.
______________________________
_________________________________
PRACTICAL 6 [connect system]
----- Foreign Key and inserting data using Script-----
Table altered.
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;
1 row created.
1 row created.
______________________________
_________________________________
PRACTICAL 7 [connect system]
----- DDL & DML commands ------
1) Update
2) Delete
3) Drop
4) Truncate
5) Merge
__________________________
1 row created.
1 row updated.
1 row deleted.
Table altered.
(6)Drop Copy table
drop table copy_emp;
Table dropped.
Table truncated.
3 rows merged.
______________________________
_________________________________
PRACTICAL 8 [connect system]
----- TCL commands ------
1) Commit
2) Savepoint
3) Rollback
__________________________
(1) Commit
CODE : commit;
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
Step 1
insert into department values (103, 'Harshal');
1 row created.
Savepoint created.
1 row deleted.
Savepoint created.
D_ID DEPT_NAME
---------- --------------------
100 Viraj
102 Parth
104 Tom
Rollback complete.
D_ID DEPT_NAME
---------- --------------------
100 Viraj
102 Parth
104 Tom
Rollback complete.
D_ID DEPT_NAME
---------- --------------------
100 Viraj
103 Harshal
102 Parth
104 Tom
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*******
______________________________
1 row created.
STEP 2 insert into employees
values(2,'Va','De','[email protected]','F','123459789','19-Mar-21',104);
1 row created.
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
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
OUTPUT:
1 VIRAJ Patel
OUTPUT:
1 VIRAJ Patel
(b) INITCAP
OUTPUT:
1 VIRAJ Patel
OUTPUT:
1 VIRAJ Patel
OUTPUT:
1 VIRAJ Patel
OUTPUT:
EMPID FIRSTNAME
---------- --------------------
2 Parth
OUTPUT:
EMPID FIRSTNAME
---------- --------------------
5 Vi
OUTPUT:
5 Vi 3
1 Viraj 5
2 Parth 7
5 Vi 3
(ii) CONCAT:
OUTPUT:
EMPID CONCAT(FIRSTNAME,LASTNAME)
---------- ----------------------------------------
1 VirajPatel
2 ParthThakkar
5 ViPal
OUTPUT :
EMPID CONCAT(CONCAT(FIRSTNAME,''),LASTNAME)
---------- -----------------------------------------
1 Viraj Patel
2 Parth Thakkar
5 Vi Pal
(iii)SUBSTR:
OUTPUT:
1 Viraj Pat
2 Parth Tha
5 Vi Pal
OUTPUT:
1 Viraj Pat
2 Parth Tha
5 Vi Pal
OUTPUT:
1 raj Pat
2 rth Tha
5 Pal
OUTPUT:
1 ra Pat
2 rt Tha
5 Pal
OUTPUT:
EMPID RPAD(FIRSTNAME,10,'*')
---------- ----------------------------------------
1 Viraj*****
2 Parth*****
5 Vi********
OUTPUT:
EMPID RPAD(SUBSTR(FIRSTNAME,2,3),10,'*')
---------- ----------------------------------------
1 ira*******
2 art*******
5 i*********
OUTPUT:
EMPID LPAD(RPAD(FIRSTNAME,10,'*'),20,'*')
---------- --------------------------------------------------------------------------------
1 **********Viraj*****
2 **********Parth*****
5 **********Vi********
OUTPUT:
EMPID LPAD(RPAD(FIRSTNAME,10,'*'),10,'*')
---------- ----------------------------------------
1 Viraj*****
2 Parth*****
5 Vi********
2. NUMBER FUNCTION
OUTPUT :
X
(i) MOD:
OUTPUT :
MOD(7,2)
----------
OUTPUT :
MOD(34.3452,2)
--------------
.3452
(ii) ROUND:
OUTPUT :
ROUND(34.3456)
--------------
34
OUTPUT :
ROUND(34.9898)
--------------
35
(iii) TRUNC:
OUTPUT :
TRUNC(34.5656)
--------------
34
OUTPUT :
TRUNC(34.9)
-----------
34
3. DATE FUNCTION
OUTPUT :
SYSDATE
---------
17-MAY-22
OUTPUT :
SYSDATE+5
---------
22-MAY-22
CODE : select sysdate + 31 from dual ;
OUTPUT :
SYSDATE+3
---------
17-JUN-22
OUTPUT :
SYSDATE-5
---------
12-MAY-22
OUTPUT :
SYSDATE+1
---------
18-MAY-22
OUTPUT :
SYSDATE+5
---------
18-MAY-22
OUTPUT :
SYSTIMESTAMP
---------------------------------------------------------------------------
OUTPUT :
TO_DATE('31-DEC-22')-TO_DATE('22-MAY-22')
-----------------------------------------
223
OUTPUT :
TO_DATE('31-DEC-21')-TO_DATE('22-MAY-22')
-----------------------------------------
-142
OUTPUT :
MONTHS_BETWEEN('31-DEC-22','22-MAY-22')
---------------------------------------
7.29032258
OUTPUT :
NEXT_DAY(
---------
27-MAY-22
ADD_MONTH
---------
22-JUL-22
OUTPUT :
ADD_MONTH
---------
22-AUG-23
OUTPUT :
ROUND(MONTHS_BETWEEN('31-DEC-22','22-MAY-22'))
----------------------------------------------
_________________________________
PRACTICAL 10-1 [connect system]
----- use of Null Value Handing ------
__________________________
1. SQL Functions
OUTPUT:
_____________________________________________________
(a) to_char
OUTPUT :
5 Vi Pal TUESDAY
OUTPUT :
5 Vi Pal 3
1 Viraj Patel 4
2 Parth Thakkar 4
OUTPUT :
no rows selected
OUTPUT :
OUTPUT :
TO_CHAR(
--------
17 05 22
OUTPUT :
TO_CHAR(SYSDATE,'DDMONTHYY')
------------------------------------------
17 may 22
OUTPUT :
TO_CHAR(SYSDATE,'DDMONTHYEAR')
----------------------------------------------------------------------------------
OUTPUT :
TO_CHAR(SYSDATE,'DAYMONTHYEAR')
-------------------------------------------------------------------------------------------------
-------------------
OUTPUT :
TO_CHAR(SYSDATE,'DL')
-----------------------------------------------------------------------------------
_________________________________
PRACTICAL 10-2 [connect system]
----- use of Null Value Handing ------
__________________________
● SQL Functions
● SQL Views
● Creating Users
● DCL commands
OUTPUT :
110 Parht
9 rows selected.
—-------------------
CODE : select act_n, c_name, salary, comm, salary + (salary * (comm/100)) as inhand_amt from
deposit;
OUTPUT:
106 sandip 0 0 0
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:
106 sandip 0 0 0
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
106 sandip 0 0 0
110 Parht 5
9 rows selected.
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.
OUTPUT :
5 Vi Pal M
1 Viraj Patel M
2 Parth Thakkar M
(c) Creating User
OUTPUT :
User created.
OUTPUT :
Enter password:
Connected.
OUTPUT :
Grant succeeded.
CODE:conn viraj;
OUTPUT :
Enter password:
Connected.
OUTPUT :
Enter password:
Connected.
OUTPUT :
Grant succeeded.
OUTPUT :
Grant succeeded.
OUTPUT :
Table created.
OUTPUT :
1 row created.
OUTPUT :
1 row created.
OUTPUT :
1 row created.
OUTPUT :
ID FNAME
---------- --------------------
1 parth
2 Viraj
3 Miit
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
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
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
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 ------
__________________________
------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
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
—---------------------------------
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
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;
(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.