DBMS Lab Exercise 1-3
DBMS Lab Exercise 1-3
DATE:
21-03-2022
AIM:
To create the table and perform single line and group by functions.
1. DDL STATEMENTS
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.
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)
Table altered.
3
SQL> desc stud;
To remove a primary key from the student table and view the structure.
Table altered.
Table altered.
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.
To alter the size of the name attribute in the stud table and view after
alteration.
Include the check constraints for the mark3 attribute value to be less than
100.
6
To view roll number, name, mark1 from stud table.
1 row deleted.
To update the stud table of the attribute stud name as ‘udhaya’ whose
rollnumber is 09mca10.
1 row updated.
7
To view the stud table after updation.
Table truncated.
Table dropped.
8
2.Data Manipulation Language
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.
Example –
Insert into customer values(101,’Anu’,’Bharathi Street’,’Chennai’);
(ii)Syntax –
Insert into r values(‘&A1’,’&A2’,…,’&An’); // where Ai – Attribute
Example –
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;
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
---------- ---------------
101 abi
102 kavi
103 bhuvana
104 mathu
105 kani
11
SQL> delete from employee where ename='bhuvana';
1 row deleted.
1 row deleted.
To view the tuples in table after deletion.
1 row updated.
12
SQL> create table account(accno number(5),amount number(8),bname
varchar2(15));
Table created.
1 row created.
SQL> /
1 row created.
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
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
AVG(AMOUNT)
-------------
45142.8571
TOTAL
----------
316000
COUNT(BNAME)
------------
7
To find the maximum amount in the account table.
MAX (AMOUNT)
----------------------
90000
15
To find the minimum amount in the account table.
MIN(AMOUNT)
---------------------
20000
To find the branch name where total sum is greater than 80000 group by
branchname.
SUM(AMOUNT) BNAME
----------- ---------------
127000 chennai
97000 coimbatore
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.
7 rows selected.
Table created.
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.
ACCNO AMOUNT
---------- ----------
123 20000
111 2000
230 20900
116 15000
129 27500
122 3400
6 rows selected.
To create a table borroww.
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.
Table created.
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’.
To view the from branch table whose amount is greater than amount present
in the branchcity=’chennai’.
BNAME
---------------
chennai
madurai
coimbatore
chennai
coimbatore
coimbatore
6 rows selected.
SQL> commit;
Commit complete.
22
JOINS:
To view the rollno,name,address from stud1 and stud2 table using equip. join.
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');
To view the rollno,name,rollno,name from stud1 and stud2 table using right
outer join.
24
GROUP FUNCTIONS
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
MIN(AVG)
----------------
49
MAX(AVG)
-----------------
83
SUM(TOT)
----------------
511
AVG(TOT)
----------------
127.75
ASCII(NAME)
-------------------
100
53
100
102
26
CONCAT(NAME,TOT)
-------------------------------
ram165
bb143
raj98
sant105
INITCAP(NAME)
------------------------
ram
b
raj
sant
LENGTH(NAME)
-----------------------
4
2
3
5
SQL> select dbtimezone from sankar;
DBTIME
------------
-05:00
-05:00
-05:00
-05:00
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
COS(AVG)
----------------
.249540118
-.96725059
.300592544
-.91828279
SIN(AVG)
----------------
.968364461
.253823363
-.95375265
.39592515
RESULT:
Thus the above query has been executed successfully.
28