0% found this document useful (0 votes)
26 views28 pages

DBMS Lab Exercise 1-3

Uploaded by

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

DBMS Lab Exercise 1-3

Uploaded by

graj200026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EX NO:1 SINGLE LINE AND GROUP BY FUNCTION

DATE:
21-03-2022

AIM:
To create the table and perform single line and group by functions.

1. DDL STATEMENTS

Data Types in SQL:

1. char(n) : Fixed-length character string.


2. varchar(n) : Variable-length string.
3. int : Integer.
4. smallint : Small Integer.
5. numeric(p,d) : The number consists of p digits(plus a sign), d of the p digit are
to the right of the decimal point.
6. real, double precision : Floating-point and double-precision floating-Point
numbers.
7. float(n) : Floating-point number.
8. date : The date containing a date, month and year.
9. time : The time in hours, minutes and seconds.
10. timestamp : The combination of date and time.

Data Definition Language(DDL) : To specify the database schema.


List of DDL Commands
1. Creating a table
2. Deleting a table
3. Altering the contents of the table
4. Truncating a table
5. To view table structure
6. Dropping a table

1. Creation of Table : Creates a table with specified attributes and its data type
Along with constraints.
Syntax:
Create table r(A1D1, A2D2, …………AnDn, (integrity-constraint1),
……,
(integrity_constraintk)); where r-relation name(Table name),Ai-Attribute
name,Di-Data type.
1
Ex: Create a customer table which consists of 4 columns.

i) custID – Customer ID – Integer – Primary Key


ii) customer – customer Name – varchar2 (15)
iii) custstreet – Customer Street – varchar2 (20)
iv) custcity – Customer City – varchar2 (10)
create table customer(custID int,custname varchar2(15),custstreet
varchar2(10), Primary key(custID));

2. Deletion of Table : Deletes the records of the table specified.


Syntax:
delete from r;
Ex:
delete from customer;

3. Altering the Table Schema : Add or delete or change the attribute and
datatype.It also can add or drop the integrity constraints.
Syntax:
1. Alter table r add (Ai Di);
2. Alter table r drop(Ai);
3. Alter table r modify (Ai Di);
4. Alter table r add primary key (Ai);
5. Alter table r enable primary key;
6. Alter table r disable primary key;
Ex:
Alter table customer add(phoneno int(12));
Alter table customer drop(custcity);
Alter table customer modify(custname varchar2(20));
Alter table depositor add primary key(acctno);
Alter table depositor disable primary key;
Alter table depositor enable primary key;

4. Truncating a Table : Deletes the records and not the structure of a table.
Syntax:
Truncating table r;
Ex:
Truncate table customer;
5. To view the table structure;
Syntax:
desc r;
Ex:
desc customer;
2
6. Dropping the Table : Deletes all information and structure about that
table(relation)
Syntax:
Drop table r;

Ex:
Drop table customer;
Create a table Stud with attributes.
rno varchar2(8)
name varchar2(15)
mark1 number(5)
mark2 number(5)
mark3 number(5)

To create a table stud.


SQL> create table student(rno varchar2(8),name varchar2(15),mark1
number(5),mark2 number(5),mark3 number(5));
Table created.

To view the structure of stud table.

SQL> desc stud;


Name Null? Type
------------- --------- -----------
RNO VARCHAR2(8)
NAME VARCHAR2(15)
MARK1 NUMBER(5)
MARK2 NUMBER(5)
MARK3 NUMBER(5)

To add a primary key to stud table and view the structure.

SQL> alter table stud add(primary key(rno));

Table altered.

3
SQL> desc stud;

Name Null? Type


----------- ---------------- ------------
RNO NOT NULL VARCHAR2(8)
NAME VARCHAR2(15)
MARK1 NUMBER(5)
MARK2 NUMBER(5)
MARK3 NUMBER(5)

To remove a primary key from the student table and view the structure.

SQL> alter table stud disable primary key;

Table altered.

SQL> desc stud;


Name Null? Type
------------- ---------------- ------------
RNO VARCHAR2(8)
NAME VARCHAR2(15)
MARK1 NUMBER(5)
MARK2 NUMBER(5)
MARK3 NUMBER(5)

To enable a primary key to stud table and view the structure.

SQL> alter table stud enable primary key;

Table altered.

SQL> desc stud;


Name Null? Type
------------- ------------------ --------------------
RNO NOT NULL VARCHAR2(8)
NAME VARCHAR2(15)
MARK1 NUMBER(5)
MARK2 NUMBER(5)
MARK3 NUMBER(5)

4
To insert tuples into the table stud.
SQL> insert into stud values('&rno','&name','&mark1','&mark2','&mark3');
Enter value for rno: 09mca01
Enter value for name: kalai
Enter value for mark1: 95
Enter value for mark2: 97
Enter value for mark3: 96
old 1: insert into stud values('&rno','&name','&mark1','&mark2','&mark3')
new 1: insert into stud values('09mca01','kalai','95','97','96')

1 row created.

SQL> /
Enter value for rno: 09mca02
Enter value for name: kathir
Enter value for mark1: 99
Enter value for mark2: 96
Enter value for mark3: 90
old 1: insert into stud values('&rno','&name','&mark1','&mark2','&mark3')
new 1: insert into stud values('09mca02','kathir','99','96','90')

1 row created.

SQL> /
Enter value for rno: 09mca03
Enter value for name: arish
Enter value for mark1: 99
Enter value for mark2: 99
Enter value for mark3: 99
old 1: insert into stud values('&rno','&name','&mark1','&mark2','&mark3')
new 1: insert into stud values('09mca03','arish','99','99','99')

1 row created.

5
To view the tuples in the table stud.

SQL> select * from stud;

RNO NAME MARK1 MARK2 MARK3


-------- ------------ ---------- ---------- ----------
09mca01 kalai 95 97 96
09mca02 kathir 99 96 90
09mca03 arish 99 99 99

To alter the size of the name attribute in the stud table and view after
alteration.

SQL> alter table stud modify(name varchar2(20));


Table altered.

SQL> desc stud;


Name Null? Type
------------ ----------------- -----------
RNO NOT NULL VARCHAR2(8)
NAME VARCHAR2(20)
MARK1 NUMBER(5)
MARK2 NUMBER(5)
MARK3 NUMBER(5)

Include the check constraints for the mark3 attribute value to be less than
100.

SQL> alter table stud add check(mark3<100);


Table altered.

To view the record from stud whose rollnumber is 09mca03.

SQL> select * from stud where rno='09mca03';

RNO NAME MARK1 MARK2 MARK3


-------- -------------------- ---------- ---------- ----------
09mca03 arish 99 99 99

6
To view roll number, name, mark1 from stud table.

SQL> select rno,name,mark1 from stud;

RNO NAME MARK1


-------- -------------------- ----------
09mca01 kalai 95
09mca02 kathir 99
09mca03 arish 99

To delete a record whose roll number is 09mca01.

SQL> delete from stud where rno='09mca01';

1 row deleted.

To view the tuples after deletion.

SQL> select * from stud;

RNO NAME MARK1 MARK2 MARK3


-------- ---------- ---------- ---------- ----------
09mca02 kathir 99 96 90
09mca03 arish 99 99 99

To list the name that starts with character ‘a’.

SQL> select * from stud where name like 'a%';

RNO NAME MARK1 MARK2 MARK3


----------- --------- ---------- ---------- ----------
09mca03 arish 99 99 99

To update the stud table of the attribute stud name as ‘udhaya’ whose
rollnumber is 09mca10.

SQL> update stud set name='udhaya' where rno='09mca10';

1 row updated.

7
To view the stud table after updation.

SQL> select * from stud;

RNO NAME MARK1 MARK2 MARK3


-------- ------------ ---------- ----------- ---------
09mca02 kathir 99 96 90
09mca03 arish 99 99 99
09mca10 kavitha 83 82 82

To truncate the table values.


SQL> truncate table stud;

Table truncated.

To drop the table from database.

SQL> drop table stud;

Table dropped.

8
2.Data Manipulation Language

Aim: To perform the following queies using DML statements.

Data Manipulation Language(DML) – to express database queries and updates.

List of DML Commands:


1. Insertion of information
2. Retrieval of information
3. Deleting information
4. Modifying information

Two types of DML:-

1. Procedural DMLs – requires a user to specify what data are needed and
how to get those data.
2. Declarative DMLs(nonprocedural language) – requires a user to specify
what data are needed without specifying how to get those data.

DML Component of SQL language is nonprocedural language.


Query is a statement requesting the retrieval of information.
1. Insertion of information can be done in two ways –
(i)Syntax –
Insert into r values(v1,v2,…,vn);
// where v1 – Attribute Values r – relation name(‘1’able name)

Example –
Insert into customer values(101,’Anu’,’Bharathi Street’,’Chennai’);

(ii)Syntax –
Insert into r values(‘&A1’,’&A2’,…,’&An’); // where Ai – Attribute

Example –

Insert into customer


values(‘&custID’,’&Custname’,’&Custstreet’,’&Custcity’);

2. Retrieval of Information –
(i) Syntax –
Select A1,A2,…,An from r1,r2,…,rn where condition ;
Example –
Select CustID, Custname from customer where Custname=’anu’;
9
3. Deletion of information –
(i) Syntax -
Delete from r where condition;
Example –
Delete from customer where Custname=’anu’;

4. Modifying Information –
(i) Syntax –
Update r set A1=values;
Example –
Update customer set Custname=’babu’ where CustID=101;

Create table Employee with attributes.


eno number(5)
ename varchar2(15)
salary number(8)

To create a table employee.

SQL> create table employee(eno number(5),ename varchar2(15),salary


number(8));
Table created.
To insert the tuples into the table.
SQL> insert into employee values('&eno','&ename','&salary');
Enter value for eno: 101
Enter value for ename: abi
Enter value for salary: 20000
old 1: insert into employee values('&eno','&ename','&salary')
new 1: insert into employee values('101','abi','20000')

1 row created.
SQL> /
Enter value for eno: 102
Enter value for ename: kavi
Enter value for salary: 25000
old 1: insert into employee values('&eno','&ename','&salary')
new 1: insert into employee values('102','kavi','25000')

1 row created.
To view the tuples in the table.

10
SQL> select * from employee;

ENO ENAME SALARY


---------- ------------ ----------
101 abi 20000
102 kavi 25000
103 bhuvana 25000
104 mathu 21000
105 kani 30000

To view the tuples whose salary is 25000.

SQL> select * from employee where salary=25000;

ENO ENAME SALARY


---------- --------------- ----------
102 kavi 25000
103 bhuvana 25000
To view only eno,ename attributes from the table.

SQL> select eno,ename from employee;

ENO ENAME
---------- ---------------
101 abi
102 kavi
103 bhuvana
104 mathu
105 kani

To list the ename starts with character ‘k’.

SQL> select * from employee where ename like 'k%';

ENO ENAME SALARY


---------- --------------- ----------
102 kavi 25000
105 kani 30000

To delete a tuple whose ename is ‘bhuvana’.

11
SQL> delete from employee where ename='bhuvana';

1 row deleted.

To delete a tuple whose employee number is 101.

SQL> delete from employee where eno=101;

1 row deleted.
To view the tuples in table after deletion.

SQL> select * from employee;

ENO ENAME SALARY


---------- --------------- -----------
102 kavi 25000
104 mathu 21000
105 kani 30000

SQL> update employee set ename='santhiya' where eno=102;

1 row updated.

To view the tuples after updation.


SQL> select * from employee;

ENO ENAME SALARY


---------- --------------- ----------
102 santhiya 25000
104 mathu 21000
105 kani 30000

Create table account with attributes.


accno number(5)
amount number(8)
bname varchar2(15)

To create a table account.

12
SQL> create table account(accno number(5),amount number(8),bname
varchar2(15));

Table created.

To insert the tuples into the table.

SQL> insert into account values('&accno','&amount','&bname');


Enter value for accno: 111
Enter value for amount: 20000
Enter value for bname: coimbatore
old 1: insert into account values('&accno','&amount','&bname')
new 1: insert into account values('111','20000','coimbatore')

1 row created.
SQL> /

Enter value for accno: 122


Enter value for amount: 37000
Enter value for bname: chennai
old 1: insert into account values('&accno','&amount','&bname')
new 1: insert into account values('122','37000','chennai')

1 row created.

To view the tuples in the table.


SQL> select * from account;

ACCNO AMOUNT BNAME


---------- -------------- ---------------
111 20000 coimbatore
122 37000 chennai
113 25000 bangalore
118 27000 coimbatore
125 67000 madurai
116 50000 coimbatore
129 90000 chennai
7 rows selected.

13
Additional Built-In Functions:
Group Functions
1. Count command : returns the number of values.
Syntax:
Count(column name)
Example:
Select count(eno)from employee;
Output:
COUNT(ENO)
-------------------
3
2. Sum command : return the sum of entries in the table.
Syntax:
Sum(column name)
Example:
Select sum(salary) from employee;
Output:
SUM(SALARY)
----------------------
76000

3. Avg command : performs the average


Syntax:
Avg(column name)
Example:
Select avg(salary) from employee;
Output:
AVG(SALARY)
----------------------
25333.3333
4. MAX command : returns the maximum value of the specified column.
Syntax:
Max(column name)
Example:
Select max(salary) from employee;
Output:
MAX(SALARY)
----------------------
30000

14
MIN command : returns the minimum value of the specified column.
Syntax:
Min(column name)
Example:
Select min(salary)from employee;
Output:
MIN(SALARY)
---------------------
21000
BUILT-IN FUNCTIONS

To find the average amount from the account table.

SQL> select avg(amount) from account;

AVG(AMOUNT)
-------------
45142.8571

To find the sum of the amount from the account table.

SQL> select sum(amount) as total from account;

TOTAL
----------
316000

To find the number of branch names in the account table.

SQL> select distinct count(bname) from account;

COUNT(BNAME)
------------
7
To find the maximum amount in the account table.

SQL> select max(amount) from account;

MAX (AMOUNT)
----------------------
90000
15
To find the minimum amount in the account table.

SQL> select min(amount) from account;

MIN(AMOUNT)
---------------------
20000

GROUP BY & ORDER BY FUNCTIONS

To find the branch name where total sum is greater than 80000 group by
branchname.

SQL> select sum(amount),bname from account group by bname having


sum(amount)>80000;

SUM(AMOUNT) BNAME
----------- ---------------
127000 chennai
97000 coimbatore

To display account number, amount from account table in ascending order.

SQL> select accno,amount from account order by accno;

ACCNO AMOUNT
---------- ----------
111 20000
113 25000
116 50000
118 27000
122 37000
125 67000
129 90000

7 rows selected.

16
To display account number, amount, branchname from account table in
descending order.

SQL> select accno,amount,bname from account order by amount desc;


ACCNO AMOUNT BNAME
---------- -------------- ---------------
129 90000 chennai
125 67000 madurai
116 50000 coimbatore
122 37000 chennai
118 27000 coimbatore
113 25000 bangalore
111 20000 coimbatore

7 rows selected.

To create a table depositt.

SQL> create table deposit(accno number(5),amount number(10));

Table created.

To insert the tuples into the table.

SQL> insert into depositt values('&accno','&amount');


Enter value for accno: 123
Enter value for amount: 20000
old 1: insert into depositt values('&accno','&amount')
new 1: insert into depositt values('123','20000')

1 row created.

SQL> /
Enter value for accno: 111
Enter value for amount: 2000
old 1: insert into depositt values('&accno','&amount')
new 1: insert into depositt values('111','2000')

1 row created.

17
To view the tuples in the table depositt.

SQL> select * from depositt;

ACCNO AMOUNT
---------- ----------
123 20000
111 2000
230 20900
116 15000
129 27500
122 3400
6 rows selected.
To create a table borroww.

SQL> create table borroww(accno number(5),amount number(8));


Table created.
To insert the tuples into the table.

SQL> insert into borroww values('&accno','&amount');

Enter value for accno: 111


Enter value for amount: 10000
old 1: insert into borroww values('&accno','&amount')
new 1: insert into borroww values('111','10000')
1 row created.
SQL> /
Enter value for accno: 230
Enter value for amount: 17000
old 1: insert into borroww values('&accno','&amount')
new 1: insert into borroww values('230','17000')
1 row created.
To view the tuples in the borroww table.

SQL> select * from borroww;


ACCNO AMOUNT
---------- -------------
111 10000
230 17000
330 3900
116 3870
118 2750
18
SET OPERATIONS
To view the accountnumber of the person who have deposited and borrowed
without duplicates.
SQL> (select accno from depositt) union (select accno from borroww);
ACCNO
----------
111
116
118
122
123
129
230
330
8 rows selected.
To view the accountnumber of the persons who have deposited and borrowed
with duplicates.
SQL> (select accno from depositt) union all (select accno from borroww);
ACCNO
----------
123
111
230
116
129
122
111
230
330
116
118
11 rows selected.

19
To view the accountnumber of the persons who have both deposited and
borrowed without duplicates.
SQL> (select accno from depositt) intersect (select accno from borroww);
ACCNO
----------
111
116
230

To view the accountnumber of the persons who have only deposited amount
but not have loan.
SQL> (select accno from depositt) minus (select accno from borroww);
ACCNO
----------
122
123
129
To view the accountnumber of the persons who have both deposit and borrow
without duplicates.
SQL> select accno from borroww where accno in(select accno from depositt);
ACCNO
----------
111
116
230

To view the accountnumber of the persons who have only deposited amount
but not have loan.

SQL> select accno from borroww where accno not in(select accno from
depositt);

ACCNO
----------
330
118

20
To find the branchname whose amount is greater than the amount which has
accno as 111.
SQL> select bname from account where amount>some(select amount from account
where accno=111);
BNAME
---------------
Chennai
bangalore
coimbatore
madurai
coimbatore
chennai

6 rows selected.

To create the table branch.

SQL> create table branch(amount number(6),bcity varchar2(20),bname


varchar2(20));

Table created.

To insert the tuples into branch table.

SQL> insert into branch values('&amount','&bcity','&bname');

Enter value for amount: 86000


Enter value for bcity: tambaram
Enter value for bname: chennai
old 1: insert into branch values('&amount','&bcity','&bname')
new 1: insert into branch values('86000','tambaram','chennai')

1 row created.
SQL> /
Enter value for amount: 67000
Enter value for bcity: manjur
Enter value for bname: madurai
old 1: insert into branch values('&amount','&bcity','&bname')
new 1: insert into branch values('67000','manjur','madurai')

1 row created.

21
To view the tuples in the table branch.
SQL> select *from branch;
AMOUNT BCITY BNAME
---------- ------------- ---------------
86000 tambaram Chennai
67000 manjur Madurai
15600 rspuram Coimbatore
37000 tnagar Chennai
27000 gandipuram Coimbatore
2000 townhall Coimbatore

6 rows selected.

To view the branchname from branch table whose amount is greater than
amount present in the branchcity=’coimbatore’.

SQL> select bname from branch where amount>some(select amount from


branch where bcity='coimbatore');
no rows selected

To view the from branch table whose amount is greater than amount present
in the branchcity=’chennai’.

SQL> select bname from branch where amount>all(select amount from


branch where bcity='chennai');

BNAME
---------------
chennai
madurai
coimbatore
chennai
coimbatore
coimbatore

6 rows selected.
SQL> commit;
Commit complete.

22
JOINS:

To view the tuples in the stud1 table.

SQL> select * from stud1;


ROLLNO NAME ADDRESS
------------ ------------- ----------------
10042 kalai coimbatore
10028 ganesh erode
10009 aarthi coimbatore
10043 meena salem

To view the tuples in the stud2 table.

SQL> select * from stud2;

ROLLNO NAME TOTAL


------------ -------------- ----------
10033 vinoth 321
10042 kalai 435
10009 aarthi 290

To view the rollno,name,address from stud1 and stud2 table using equip. join.

SQL> select stud2.rollno,stud2.name,stud1.address from stud2,stud1 where


stud2.rollno=stud1.rollno;
ROLLNO NAME ADDRESS
------------ ------------- -----------------
10009 aarthi Coimbatore
10042 kalai Coimbatore

To find the student who have got below 300 of total.

SQL> select a.name,b.rollno from stud2 a,stud2 b where a.total=b.total and


b.total<300;

NAME ROLLNO
------------ ----------
aarthi 10009

23
To view the tuples from stud1 whose name is ‘kalai’

SQL> select * from stud1 where rollno=(select rollno from stud1 where
name='kalai');

ROLLNO NAME ADDRESS


------------- ---------- --------------------
10042 kalai Coimbatore

To view the rollno,name,rollno,name from stud1 and stud2 table using right
outer join.

SQL> select stud2.rollno,stud2.name,stud1.rollno,stud1.name from


stud2,stud1 where stud2.rollno(+)=stud1.rollno;

ROLLNO NAME ROLLNO NAME


------------- -------------
---------- --------------------
10009 aarthi 10009 aarthi
10028 ganesh
10042 kalai 10042 kalai
10043 meena
To view the rollno,name,rollno,name from stud1 and stud2 table using left
outer join.

SQL> select stud2.rollno,stud2.name,stud1.rollno,stud1.name from


stud2,stud1 where stud2.rollno=stud1.rollno(+);

ROLLNO NAME ROLLNO NAME


------------ ------------ ------------ --------
10009 aarthi 10009 aarthi
10033 vinoth
10042 kalai 10042 kalai

24
GROUP FUNCTIONS

SQL> SELECT COUNT(ENO) FROM EMP;


COUNT(ENO)
------------------
4
SQL> SELECT SUM(SALARY) FROM EMP;
SUM(SALARY)
---------------------
200000

SQL> SELECT AVG(SALARY) FROM EMP;


AVG(SALARY)
---------------------
50000
SQL> SELECT MAX(SALARY) FROM EMP;
MAX(SALARY)
---------------------
75000
SQL> SELECT MIN(SALARY) FROM EMP;
MIN(SALARY)
--------------------
35000

SINGLE ROW FUNCTIONS

SQL>create table sankar(id number(2),name varchar(15),m1 number(5),m2


number(5),tot number(5),avg number(4,5));
Table created

SQL> select * from sankar;

ID NAME M1 M2 TOT
------- --------------- --------- --------- --------
1 ram 77 88 165
2 b 66 77 143
3 raj 63 35 98
4 sant 55 50 105

25
SQL> select count(*) from sankar;

COUNT(*)
----------------
4

SQL> select min(avg) from sankar;

MIN(AVG)
----------------
49

SQL> select max(avg) from sankar;

MAX(AVG)
-----------------
83

SQL> select sum(tot) from sankar;

SUM(TOT)
----------------
511

SQL> select avg(tot) from sankar;

AVG(TOT)
----------------
127.75

SQL> select ascii(name) from sankar;

ASCII(NAME)
-------------------
100
53
100
102

SQL> select concat(name,tot)from sankar;

26
CONCAT(NAME,TOT)
-------------------------------
ram165
bb143
raj98
sant105

SQL> select initcap(name) from sankar;

INITCAP(NAME)
------------------------
ram
b
raj
sant

SQL> select length(name) from sankar;

LENGTH(NAME)
-----------------------
4
2
3
5
SQL> select dbtimezone from sankar;

DBTIME
------------
-05:00
-05:00
-05:00
-05:00

SQL> select current_date from sankar;

CURRENT_D
-----------------
13-MAR-10
13-MAR-10
13-MAR-10
13-MAR-10

27
SQL> select round(avg) from sankar;

ROUND(AVG)
-------------------
83
72
49
53

SQL> select cos(avg) from sankar;

COS(AVG)
----------------
.249540118
-.96725059
.300592544
-.91828279

SQL> select sin(avg) from sankar;

SIN(AVG)
----------------
.968364461
.253823363
-.95375265
.39592515

RESULT:
Thus the above query has been executed successfully.

28

You might also like