DBMS LAB
DBMS LAB
LAB MANUAL
FOR
DBMS LAB
CITM CSE
1
DBMS LAB MANUAL
CITM/CSE/DBMS
1.
SQL BASICS
The structure queries language is a language that enable to create and operate on relational
database ,which are sets of related information stored in tables .
SQL has clearly established itself as the standard relational database language.
4. VIEW DEFINITION :
The sql DDL also includes commands for defining views
5. AUTHORIZATION :
The sql DDL includes command for specifying access rights to
relation and views.
6. INTEGRITY:
The sql provides forms of integrity checking. Future products and
standard of sql are likely to include enhanced features for integrity checking .
7. TRANSACTION CONTROL:
Sql includes command for specifying the beginning and
ending of transaction along with commands to have a control over transaction
processing.
2
DBMS LAB MANUAL
CITM/CSE/DBMS
1. SELECT CLAUSE:
select branch-name from loan;
it will select all branch-name from the loan table
2. WHERE CLAUSE:
select loan-number from loan
where amount between 8000 and 9000
it will select all loan-number from loan where amount is between the 8000
and 9000
3. FROM CLAUSE:
select customer-name,borrower.loan-
number,amount
from borrower,loan
where borrower.loan-no=loan.loan-no
3
DBMS LAB MANUAL
CITM/CSE/DBMS
2.
TO CREATE DATABASE
Table created.
4
DBMS LAB MANUAL
CITM/CSE/DBMS
3.
CREATION OF TABLE WITH CONSTRAINTS:
SQL> create table Gaurav
2 ( empid int constraint v1 primary key ,
3 epmnm char(20) constraint v2 unique,
4 desig char(20) default 'clerk',
5 dept char(20) constraint v3
6 check(dept in('edp','fin')),
7 salary int constraint v4 not null);
Table created.
5
DBMS LAB MANUAL
CITM/CSE/DBMS
8.
ALTER TABLE :
(a) Adding column : (ADD clause)
SQL> alter table Gaurav
2 add (marks int);
Table altered.
Table altered.
Table altered.
Table altered
Table altered.
Table altered
6
DBMS LAB MANUAL
CITM/CSE/DBMS
Table altered.
Table altered.
Table altered.
Table altered.
Table altered.
7
DBMS LAB MANUAL
CITM/CSE/DBMS
4.
8
DBMS LAB MANUAL
CITM/CSE/DBMS
1 row created.
1 row created.
SQL> /
Enter value for rollno: 6058
Enter value for name: 'Sudhir'
Enter value for branch: 'CSE'
Enter value for sem: 4
Enter value for marks: 730
Enter value for fav_sub: 'DBMS'
Enter value for stdid: 2
old 2: values(&rollno,&name,&branch,&sem,&marks,&fav_sub,&stdid)
new 2: values(6058,'Sudhir','CSE',4,730,'DBMS',2)
1 row created.
9
DBMS LAB MANUAL
CITM/CSE/DBMS
2 (rollno,name,branch,sem)
3 values(7006,'Amit','cse',2);
1 row created.
10
DBMS LAB MANUAL
CITM/CSE/DBMS
7.
UPDATING TABLES(MODIFY):
(a) Updating without where clause:
SQL> update Gaurav
11
DBMS LAB MANUAL
CITM/CSE/DBMS
2 set name='Goru';
4 rows updated.
1 row updated.
14.
Generating sub query:
SQL> update Gaurav
2 set name=(select name from Gaurav where rollno=6302)
3 where rollno=6058;
12
DBMS LAB MANUAL
CITM/CSE/DBMS
1 row updated.
13
DBMS LAB MANUAL
CITM/CSE/DBMS
6.
DELETING RECORDS:
(a) Delete (SINGLE record)
SQL> delete from Gaurav
2 where rollno=7006;
1 row deleted.
14
DBMS LAB MANUAL
CITM/CSE/DBMS
2 rows deleted.
SQL> select *from Gaurav;
Table truncated.
no rows selected
1 rows deleted.
no rows selected
15
DBMS LAB MANUAL
CITM/CSE/DBMS
1 row deleted.
DROPPING TABLE:
(a) Dropping a table that has a primary key:
SQL> drop table s;
Table dropped.
Table dropped.
RETRIEVING DATA :
(a) Retrieving all records:
SQL> select *from Gaurav;
16
DBMS LAB MANUAL
CITM/CSE/DBMS
17
DBMS LAB MANUAL
CITM/CSE/DBMS
1). percent:
SQL> select * from Gaurav
2 where name like '%h%';
18
DBMS LAB MANUAL
CITM/CSE/DBMS
2). underscore:
SQL> select * from Gaurav
2 where marks like '8 ';
3). IS NULL:
SQL> select * from Gaurav
2 where stdid is null;
19
DBMS LAB MANUAL
CITM/CSE/DBMS
9.
ORDERING RECORDS:
a). ascending:
SQL> select * from Gaurav
2 order by name asc;
20
DBMS LAB MANUAL
CITM/CSE/DBMS
b). descending:
SQL> select * from Gaurav
2 order by name desc;
a). concat:
SQL> select name || ',' || branch from Gaurav;
NAME||','||BRANCH
-------------------------------
Goru ,CSE
Sourabh ,IT
Amit ,CSE
Sudhir ,CSE
21
DBMS LAB MANUAL
CITM/CSE/DBMS
Richa ,CSE
b). initcap:
SQL> select initcap(name) from Gaurav;
INITCAP(NAME)
--------------------
Goru
Sourabh
Amit
Sudhir
Richa
c). lower:
SQL> select lower(name) from Gaurav;
LOWER(NAME)
--------------------
vikas
sourabh
amit
sudhir
richa
d). upper:
SQL> select upper(name) from Gaurav;
UPPER(NAME)
--------------------
VIKAS
SOURABH
AMIT
SUDHIR
RICHA
22
DBMS LAB MANUAL
CITM/CSE/DBMS
11.
ORACLE FUNCTION:
a). add_months:
SQL> select sysdate, add_months('9-april-2005',4) from dual;
SYSDATE ADD_MONTH
--------- ---------
08-APR-05 09-AUG-05
b). last_day:
SQL> select sysdate,last_day('9-april-2005') from dual;
SYSDATE LAST_DAY(
--------- ---------
08-APR-05 30-APR-05
23
DBMS LAB MANUAL
CITM/CSE/DBMS
c). months_between:
SQL> select sysdate, months_between(sysdate,'02-nov-1985')from dual;
SYSDATE MONTHS_BETWEEN(SYSDATE,'02-NOV-1985')
--------- -------------------------------------
08-APR-05 233.22555
d). next_day:
SQL> select sysdate,next_day('9-april-2005','monday')from dual;
SYSDATE NEXT_DAY(
--------- ---------
08-APR-05 11-APR-05
Ceil:
SQL> select ceil(months_between(sysdate,'02-nov-1985'))from dual;
CEIL(MONTHS_BETWEEN(SYSDATE,'02-NOV-1985'))
-------------------------------------------
234
Floor:
SQL> select floor(months_between(sysdate,'02-nov-1985'))from dual;
FLOOR(MONTHS_BETWEEN(SYSDATE,'02-NOV-1985'))
--------------------------------------------
233
24
DBMS LAB MANUAL
CITM/CSE/DBMS
Mod:
SQL> select mod(10,7) from dual;
MOD(10,7)
---------
3
Power:
SQL> select power(2,3) from dual;
POWER(2,3)
----------
8
Sqrt:
SQL> select sqrt(10) from dual;
SQRT(10)
---------
3.1622777
Abs:
SQL> select abs(10)from dual;
ABS(10)
---------
10
AGGREGATE FUNCTIONS:
AVG()
SQL> select avg(marks) from Gaurav;
AVG(MARKS)
----------
766.6
MAX()
SQL> select max(marks) from Gaurav;
25
DBMS LAB MANUAL
CITM/CSE/DBMS
MAX(MARKS)
----------
833
MIN()
SQL> select min(marks) from Gaurav;
MIN(MARKS)
----------
729
SUM()
SUM(MARKS)
----------
3833
COUNT()
SQL> select count(name) from Gaurav;
COUNT(NAME)
COUNT(*)
26
DBMS LAB MANUAL
CITM/CSE/DBMS
10.
GROUPING FUNCION
GROUP BY clause:
SQL> select sum(marks) from Gaurav
2 group by stdid;
SUM(MARKS)
----------
2322
1511
HAVING clause:
SQL> select sum(marks) from Gaurav
2 group by stdid
3 having sum(marks)>2000;
SUM(MARKS)
----------
2322
ALL clause:
SQL> select all name from Gaurav;
27
DBMS LAB MANUAL
CITM/CSE/DBMS
NAME
--------------------
Goru
Sourabh
Amit
Sudhir
Richa
12.
SET OPERATIONS:
(a) Union:
SQL> select name from std union select name from Gaurav;
NAME
--------------------
Amit
Richa
Sourabh
Sudhir
goru
amit
preeti
sourabh
goru
9 rows selected.
(b) Union All:
SQL> select rollno,name,branch,sem from Gaurav union all select *from std;
28
DBMS LAB MANUAL
CITM/CSE/DBMS
9 rows selected.
29
DBMS LAB MANUAL