0% found this document useful (0 votes)
34 views

Dbms Practical 3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Dbms Practical 3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

DBMS Practicals With Solution

By Bhavik Thummar

From GEC, Modasa 3rd Sem I.T


Email : [email protected]
Facebook : https://www.facebook.com/Bhavik.Thummar.97
Twitter : https://twitter.com/ThummarBhavik
Google Plus : https://plus.google.com/+BhavikThummar9797
Our Partner Sites :
To Download Games, Softwares for Pc & Android,
GTU Ebooks, Safari Magazine

http://softonicks.blogspot.in/

To Download Study Materials For I.T Engineering &


Computer Engineering

http://bhavikthummar.weebly.com/

Happy To Help You !


Practical - 3
(SQL)
Create Table : ACCOUNT

Column name Data Type Size Attributes


Acc_no Varchar2 5 Primary key / first letter must start
with ‘A’
Name Varchar2 30 NOT NULL
City Varchar2 20 NOT NULL
Balance Number 10,2 Balance >=500
Loan_taken Varchar2 3 Values(‘NO’,’YES’)

Query :
create table ACCOUNT(acc_no varchar2(5) primary key check
(acc_no like 'A%'),Name varchar2(30),City
varchar2(20),Balance number(10,2),Loan_taken varchar2(3));

1. Insert the following records


Acc_no Name City Balance Loan_taken
A001 Patel Jigar Mehsana 50000 YES
A002 Patel Ramesh Mehsana 50000 Yes
A003 Dave Hardik Ahmedabad 75000 NO
A004 Soni Hetal Ahmedabad 100000 NO
A005 Soni Atul Vadodara 100000 YES
Queries :

(1). insert into ACCOUNT values (‘A001’,


’Patel Jigar’,’Mehsana’,50000,’YES’);
(2). insert into ACCOUNT values (‘A002’,
’Patel Ramesh’,’Mehsana’,50000,’YES’);
(3). insert into ACCOUNT values (‘A003’,
’Dave Hardik,’Ahmedabad’,75000,’NO’);
(4). insert into ACCOUNT values (‘A004’,
’Soni Hetal’,’ Ahmedabad’,100000,’NO’);
(5). insert into ACCOUNT values (‘A005’,
’Soni Atul’,’Vadodara’,100000,’YES’);
Create Table : LOAN

Column Name Data Type Size Attributes


Loan_no Varchar2 5 Primary Key / first letter must
start with ‘L’
Acc_no Varchar2 5 Foreign key References Acc_no
of account table
Loan_amt Number 10,2 NOT NULL
Interest_rate Number 5,2 NOT NULL
Loan_date Date
Remaining_loan Number 10,2 Remaining loan<loan amount

Query :
create table LOAN (loan_no varchar2(5) primary key check
(loan_no like 'L%'),acc_no varchar2(5) references
ACCOUNT,loan_amt number(10,2) not null,interest_rate
number(5,2) not null,loan_date date,remaining_loan
number(10,2),constraint check_loan check
(remaining_loan<loan_amt));
1. Insert the following Records

Loan_no Acc_no Loan_amt Interest_rate Loan_date Remaining_loan


L001 A001 100000 7 1-jan-04 75000
L002 A002 300000 9 18-may-04 150000
L003 A005 500000 11 15-june-04 300000

Queries:
(1). insert into LOAN values (‘L001’,
’A001’,100000,7,’01-JAN-04’,75000);
(2). insert into LOAN values (‘L002’,
’A002’,300000,9,’18-JAN-04’,150000);
(3). insert into LOAN values (‘L003’,
’A003’,500000,11,’15-JUN-04’,300000);
Create Table : INSTALLMENT
Column Name Data Type Size Attributes
Loan_no Varchar2 5 Foreign key References Loan_no of
Loan table
Inst_no Varchar2 5 first letter must start with ‘I’
IDate Date NOT NULL
Amount Number 10,2 NOT NULL

Query :
create table INSTALLMENT (loan_no varchar2(5) references
loan,inst_no varchar2(5) check (inst_no like 'I%'),idate date not
null,amount number(10,2) not null);

1. Insert the following Records


Loan_no Inst_no Inst_Date Amount
L001 I001 2-Feb-04 15000
L002 I002 18-June-04 20000
L003 I003 15-July-04 20000
Queries
(1). insert into INSTALLMENT values (‘L001’,
’I001’,’02-FEB-04’,15000);
(2). insert into INSTALLMENT values (‘L002’,
’I002’,’18-JUN-04’,20000);
(3). insert into INSTALLMENT values (‘L003’,
’I003’,’15-JUL-04’,20000);

Create Table : TRANSACTION

Column Name Data Type Size Attributes


Acc_no Varchar2 5 Foreign key References Acc_no
of account table
Trans_Date Date NOT NULL
Amt Number 10,2 NOT NULL
Type_of_tr Char 1 Values in (‘D’,’W’)
Mode_of_pay Varchar2 10 Values in (‘cash’,’check’)
Query :
create table TRANSACTION (acc_no varchar2(5) references
account,trans_date date not null,amt number(10,2) not
null,type_of_tr char(1) check (type_of_tr in
('D','W')),mode_of_pay varchar2(10) check (mode_of_pay in
('cash','check')));

1. Insert the following Records

Acc_no Trans_Date Amt Type_of_tr Mode_of_pay


A001 3-may-04 10000 D Cash
A002 5-july-04 5000 W Check
A003 12-Aug-04 25000 D Check
A004 15-may-04 30000 D Check
A005 22-oct-04 15000 W Cash
Queries

(1). insert into TRANSACTION values (‘A001’,


’03-MAY-04’,10000,’D’,’Cash’);
(2). insert into TRANSACTION values (‘A002’,
’05-JUL-04’,5000,’W’,’Check’);
(3). insert into TRANSACTION values (‘A003’,
’12-AUG-04’,25000,’D’,’ Check’);
(4). insert into TRANSACTION values (‘A004’,
’15-MAY-04’,30000,’D’,’ Check’);
(5). insert into TRANSACTION values (‘A005’,
’12-OCT-04’,15000,’W’,’Cash’);
 Using Operator: NOT,BETWEEN,NOT
BETWEEN,IN,NOT IN
1. Retrieve specified information for the account holder who
are not in ‘Ahmedabad’.
Query : select * from account where city not in
('Ahmedabad');
2. Retrieve specified information for the account holder who
are not in ‘Ahmedabad’ or ‘Vadodara’.
Query : select * from account where city not in
('Ahmedabad','Vadodara');
3. Retrieve those records of Account holder whose balance
between is 50000 and 100000.
Query : select * from account where balance between 50000
and 100000;
4. Retrieve those records of Account holder whose balance
not between is 50000 and 100000.
Query : select * from account where balance not between
50000 and 100000;
5. Display only those records whose amount is 5000, 25000,
30000.
Query : select * from account where balance in
(5000,25000,30000);
6. Display only those records whose amount not in 5000,
25000, 30000.
Query : select * from account where balance not in
(5000,25000,30000);
7. Display System date.
Query : select sysdate from dual;
8. Find the date,15 days after today’s date.
Query : select sysdate+15 from dual;
9. Perform following operation using DUAL table. (1) 5*5,(2)
34+34,(3) 1000/300,(4) Find length of ‘uvpce’,(5) display
only month of systemdate
Queries :
1. select 5*5 from dual;
2. select 34+34 from dual;
3. select 1000/300 from dual;
4. select length('uvpce') from dual;
5. select to_char(sysdate,'Month')"Month" from dual;
10. Find the date,20 days before today’s date.
Query : select sysdate-20 from dual;
 Function Based Queries.
1. Find the total transaction amount of account holder from
transaction table.
Query : select sum(amt) "Total Transaction Amount" from
transaction;
2. Find minimum amount of transaction.
Query : select min(amt) "Minimum Amount" from
transaction;
3. Find maximum amount of transaction.
Query : select max(amt) "Maximum Amount" from
transaction;
4. Count the total account holders.
Query : select count(acc_no) "Total Amount Holders" from
transaction;
5. Count only those records whose made of payment is ‘cash’.
Query : select count(acc_no) "Mode of Pay" from transaction
where mode_of_pay='cash’;
6. Count only those records whose transaction made in the
month of ‘MAY’.
Query : ----------Coming Soon----------
7. Find the average value of transaction.
Query : select avg(amt) "Average Transaction" from
transaction;
8. Display the result of 4 rest to 4.
Query : select power(4,4) from dual;
9. Find the square root of 25.
Query : select sqrt(25) from dual;
10. Write the query for the following Function. (1) LOWER,(2)
INITCAP,(3) UPPER,(4) SUBSTR,(5) LENGTH,(6) LTRIM,(7)
RTRIM,(8) LPAD,(9) RPAD.
Query : 1. select lower('BHAVIK') from dual;
Output : LOWER(
------
bhavik
Query : 2. select initcap('bhavik') from dual;
Output : INITCA
------
Bhavik
Query : 3. select upper('bhavik') from dual;
Output : UPPER(
------
BHAVIK
Query : 4. select substr('bhavik',3) from dual;
Output : SUBS
----
avik
Query : 5. select length('bhavik') from dual;
Output : LENGTH('BHAVIK')
----------------
6
Query : 6. select ltrim('BHAVIKPATEL','BHAVIK') from dual;
Output : LTRIM
-----
PATEL
Query : 7. select rtrim('BHAVIKPATEL','PATEL') from dual;
Output :
RTRIM(
------
BHAVIK
Query : 8. select lpad('BHAVIK','20','*^') from dual;
Output :
LPAD('BHAVIK','20','
--------------------
*^*^*^*^*^*^*^BHAVIK
Query : 9. select rpad('BHAVIK','20','*^') from dual;
Output :
RPAD('BHAVIK','20','
--------------------
BHAVIK*^*^*^*^*^*^*^
 CONSTRAINTS Based queries.

Create Table : STUDENT


Name of column Type and Size
Rollno Varchar2(6)
Name Varchar2(20)
Branch Varchar2(6)
Address Varchar2(20)

Query :
create table STUDENT (Rollno varchar2(6),Name
varchar2(20),Branch varchar2(6),Address varchar2(20));

1. Add PRIMARY KEY (roll no) and provide constraint name


PRIM_rollno.
Query : alter table student add constraint PRIM_rollno
primary key (rollno);
2. Add NOT NULL constraint to name,branch for student table.
Query : alter table student modify (name not null,branch not
null);
3. Add check constraint and check name is in capital letter.
Query : ----------Coming Soon----------
4. Drop the primary key.
Query : alter table student drop constraint PRIM_rollno;
5. Drop the constraint.
Query : 1. alter table student modify name null;
2. alter table student modify branch null;

Create Table : REGISTER


Name of column Type and Size
Rollno Varchar2(6)
Name Varchar2(20)

Query :
create table REGISTER (Rollno varchar2(6),Name
varchar2(20));
1. Provide foreign key references rollno of student table.
Query : alter table student add constraint fore_roll foreign key
(Rollno) references student (rollno);
2. Add check constraint to check name’s first letter is always
capital.
Query : ----------Coming Soon----------
3. Add NOT NULL constraint to name of register table.
Query : alter table register modify name not null;
4. Drop foreign key of REGISTER table.
Query : alter table register drop constraint fore_roll;
5. Drop NOT NULL constraint.
Query : alter table register modify name null;

- Bhavik Thummar

You might also like