RDBMS
RDBMS
Register Number:191805
Program Number:01
AIM : TESTING ALL THE DDL (CREATE, ALTER) AND DML (INSERT, UPDATE, DELETE)
AND RENAME COMMANDS
**********************************************************************************
1> Create a table CUSTOMER with following columns:
C_ID INTEGER,CNAME VARCHAR(20),CITY CHAR(15),DOB DATE
SQL> CREATE TABLE CUSTOMER(C_ID INTEGER,CNAME VARCHAR(20),CITY
CHAR(15),DOB DATE);
Table created.
B. Add extra column name cell phone CHAR(10) and E-mail ID CHAR(35)
SQL> ALTER TABLE CUSTOMER ADD(CELLPHONE VARCHAR(10),EMAIL_ID
CHAR(35));
Table altered
SQL> DESC CUSTOMER;
NAME NULL? TYPE
--------------------------------- ------- -------------
C_ID NUMBER(38)
CNAME VARCHAR(20)
CITY CHAR(15)
DOB DATE
CELLPHONE VARCHAR2(10)
EMAIL_ID CHAR(35)
10 rows selected.
10 rows selected
**********************************************************************************
Register Number:191805
Program Number:02
AIM : ADDING AND ALTERING CONSTRAINTS (PRIMARY KEY, UNIQUE KEY AND
FOREIGN KEY)
**********************************************************************************
Sales(SID Integer Not Null, SName Varchar(25) Not Null,DOB date Not Null, CID
Integer, City Char(20) Default ‘Mangaluru’, EmailID varchar(20) Primary Key)
I. Change Primary Key from EmailID to SID
SQL> CREATE TABLE SALES(SID INTEGER NOT NULL,SNAME VARCHAR(25)
NOT NULL,
2 DOB DATE NOT NULL,CID INTEGER,CITY CHAR(20) DEFAULT
’MANGALORE’,
3 EMAIL_ID VARCHAR(20) PRIMARY KEY);
Table created.
10 rows selected.
**********************************************************************************
Register Number:191805
Program Number:03
AIM : ADDING AND ALTERING BUSINESS RULE, NAMING CONSTRAINTS, DROPPING
CONSTRAINTS AND TESTING SIMPLE QUERIES
**********************************************************************************
1> Create the following relations
A> BANK_CUSTOMER(CID INTEGER, CNAME NN, DOB NN, CITY NN CITY
NAME IN CAPITAL LETTERS)
SQL> CREATE TABLE BANK_CUSTOMER(CID INT,CNAME VARCHAR(20)
NOT NULL,DOB DATE NOT NULL,CITY VARCHAR(20) NOT NULL
CHECK(CITY=UPPER(CITY)));
table created
C> Add constraints to loans make status to accept only the values ‘A’,’C’,’N’
SQL> ALTER TABLE LOANS ADD CHECK(STATUS IN(‘A’,’C’,’N’));
table altered.
3>Insert few records to both the table(3 records to bank_customer and 9 records to loans)
SQL> INSERT INTO BANK_CUSTOMER
VALUES(&CID,’&CNAME’,’&DOB’,’&CITY’);
ENTER VALUE FOR CID: 1000
ENTER VALUE FOR CNAME: SHRAV
ENTER VALUE FOR DOB: 26-MAY-1998
ENTER VALUE FOR CITY: GOA
OLD 1: INSERT INTO BANK_CUSTOMER
VALUES(&CID,’&CNAME’,’&DOB’,’&CITY’)
NEW 1:INSERT INTO BANK_CUSTOMER VALUES(1000,’SHRAV’,’26-MAY-
1998’,’GOA’)
1 row created.
1 row created.
SQL> SELECT * FROM LOANS;
LOANID CID LDATE LAMOUNT LCLOSEDATE STATUS
--------------- ------- --------------------- ------------------- ------------------------- -----------------
1000 100 25-APR-2018 100000 05-APR-2019 A
1001 101 15-MAY-2018 90000 15-JAN-2019 B
1002 102 02-APR-2018 10000 25-FEB-2019 A
1003 103 30-DEC-2018 300000 30-APR-2019 B
1004 104 01-NOV-2018 50000 10-MAR-2019 A
1005 105 25-JAN-2019 1000 26-DEC-2019 A
1006 106 03-APR-2018 12000 03-MAY-2019 C
1007 107 17-NOV-2018 1200000 21-JUN-2019 A
1008 108 19-FEB-2019 100000 13-APR-2019 B
1009 109 22-APR-2018 100000 26-JUL-2019 A
10 records selected.
B> Display all the customer id, name, city for those customers whose city=’mangaluru’ in the
descending order of city and ascending order of customer name
SQL> SELECT CID,CNAME,CITY FROM BANK_CUSTOMER WHERE
CITY=’MANGALORE’ ORDER BY CITY DESC, CNAME ASC;
CID CNAME CITY
------------ ------------------- ----------------------------
101 CLINT MANGALORE
C> Display all the customer id, name, city for those customers whose city is either
‘mangaluru’,’mysore’ or ‘bengaluru’ in the ascending order of city
SQL> SELECT CID,CNAME,CITY FROM BANK_CUSTOMER WHERE CITY
IN(‘MANGALORE’,’MYSORE’,’BANGALORE’) ORDER BY CITY ASC;
CID CNAME CITY
------------ ------------------- ----------------------------
103 OSHIN MYSORE
104 SHRAVAN MANGALORE
D> Display all the name, city for those customers whose name begins with ‘o’ or ‘c’ and dob>1st
JAN 1990
SQL> SELECT CNAME,CITY FROM BANK_CUSTOMER WHERE CNAME LIKE
‘O%’ OR CNAME LIKE ‘C%’ AND DOB>’01-JAN-1990’;
CNAME CITY
------------------- ----------------------------
OSHIN MYSORE
E> Display all the customer id, name, city for those customers whose name ends with letter ‘a’
and city is not ‘mangaluru’,’mysore’ or ‘bengaluru’ or ‘coorg’ in the ascending order of city
SQL> SELECT CID,CNAME,CITY FROM BANK_CUSTOMER WHERE CNAME LIKE
‘%A’ AND CITY NOT IN(‘MANGALORE’,’MYSORE’,’BANGALORE’,’COORG’)
ORDER BY CITY ASC;
CID CNAME CITY
------------ ------------------- ----------------------------
103 OSHIN MYSORE
Display all the customer id, loanid, ldate, lamount for those customers whose loan amount is >50,000
and less than 1,00,000 and loan date is before 31st march 2016 in the descending order of lamount
SQL> SELECT CID,LOANID,LDATE,LAMOUNT FROM LOANS WHERE LAMOUNT
BETWEEN 50000 AND 100000 AND LDATE<’31-MAR-2016’ ORDER BY LAMOUNT DESC;
CID LOANID LDATE CITY
------------ ---------------- -------------------- --------------------------
101 1001 10-JAN-16 MANGALORE
105 1005 25-JAN-19 MANGALORE
107 1007 17-NOV-2018 MANGALORE
**********************************************************************************
Register Number:191805
Program Number:04
AIM : I)NAMING ALL THE CONSTRAINTS DURING CREATION OF TABLE
II)TESTING AGGREGATE FUNCTIONS
**********************************************************************************
PART – I
Create a table named student_bio(regno int primary key, sname varchar(20) not null,dob date not
null,city varchar(15), category varchar(3) should accept only ‘GEN’,’OBC’,’SC’,’ST’,total_marks
number(3) total_marks>0)
SQL> CREATE TABLE STUDENT_NIO(REGNO INT CONSTRAINT
STUDBIO_REGNO_PK PRIMARY KEY,SNAME VARCHAR(20) CONSTRAINT
STUDBIO_SNAME_NN NOT NULL,DOB DATE CONSTRAINT STUDBIO_DOB_NN NOT
NULL,CITY VARCHAR(15), CATEGORY VARCHAR(3) CONSTRAINT
STUDBIO_CAT_CCHECK(CATEGORY IN(‘GEN’,’OBC’,’SC’,’ST’)),TOTAL_MARKS
NUMBER(3) CONSTRAINT STUDBIO_TOT_CCHECK(TOTAL_MARKS>0));
table created
1 row created.
10 records selected.
NOTE: ADD CONSTRAINTS WHILE CREATING TABLES AND ALSO NAME THE
CONSTRAINTS
i) Display all the regno, city, sname, total_marks in the ascending order of city descending order
of total_marks
SQL> SELECT REGNO,CITY,SNAME,TOTAL_MARKS FROM STUDENT_BIO ORDER BY
CITY ASC,TOTAL_MARKS DESC;
REGNO CITY SNAME TOTAL_MARKS
-------------- ------------------ ------------------- --------------------------
1001 COORG OSHIN 300
1002 COORG ROHAN 600
1003 GOA ABHI 450
1004 DELHI KIM 430
1005 BOMBAY BELLA 100
1006 MYSORE RAJ 500
1007 DELHI ALLEN 345
1008 GOA CLINT 210
1009 COORG IVAN 390
1010 COORG JASON 270
10 rows selected.
ii) Display all the regno, city, sname,totalmarks for category =”sc” or “st” in the descending
order of marks and descending order of sname.
SQL> SELECT REGNO,CITY,SNAME,TOTAL_MARKS FROM STUDENT_BIO WHERE
CATEGORY IN(‘SC’,’ST’) ORDER BY TOTAL_MARKS DESC,SNAME ASC;
REGNO CITY SNAME TOTAL_MARKS
-------------- ------------------ ------------------- --------------------------
1004 DELHI KIM 430
1009 COORG IVAN 390
iii) Display all students who do not belong to city ‘mangalore’,’kasaragod’ and ‘coorg’ and
whose category is not ‘sc’ and dob is in between 1st jan 1990 and jun 30th 1999
SQL> SELECT * FROM STUDENT_BIO WHERE CITY NOT
IN(‘MANGALORE’,’KASARAGOD’,’COORG’) AND CATEGORY!=’SC’ AND DOB
BETWEEN ’01-JAN-1990’ AND ’30-JUN-1999’;
REGNO SNAME DOB CITY CAT TOTAL_MARKS
-------------- ------------------ --------------- ------------------- --------------------- -----------------------------
1005 BELLA 25-JUL-98 BOMBAY GEN 100
iv) Display all the student name, city name, dob whose name begins with letter ‘a’ or letter ‘m’ in
the ascending order of city.
SQL> SELECT SNAME,CITY,DOB FROM STUDENT_BIO WHERE SNAME LIKE ‘A%’ OR
SNAME LIKE ‘M%’ ORDER BY CITY ASC;
SNAME DOB CITY
-------------- ------------------ ---------------
ALLEN 20-APR-97 DELHI
ABHI 02-APR-98 GOA
v) Display all the regno and marks for those whose marks are in between 300 and 500 and who do not
belong to city ‘mysore’,’bangalore’,’mangalore’
SQL> SELECT REGNO,TOTAL_MARKS FROM STUDENT_BIO WHERE TOTAL_MARKS
BETWEEN 300 AND 500 AND CITY NOT IN(‘MYSORE’,’BANGALORE’,’MANGALORE’);
REGNO TOTAL_MARKS
----------------------- ------------------------------
1001 300
1003 450
1004 430
1007 345
1009 390
5 rows selected
vi) Display all the regno and marks for those whose marks are in between 500 and 550 and city is
either ‘mangalore’ or ‘udupi’ and category is either ‘obc’ or ‘gen’
SQL> SELECT REGNO,TOTAL_MARKS FROM STUDENT_BIO WHERE TOTAL_MARKS
BETWEEN 500 AND 550 AND CITY IN(‘MANGALORE’,’UPUPI’)AND CATEGORY IN
(‘OBC’,’GEN’);
REGNO TOTAL_MARKS
----------------------- ------------------------------
1001 550
1006 500
vii) Display all the regno and marks for those whose marks are in between 300 and 550 and
category is either ‘obc’ or ‘gen’ and city name ends with letter ‘e’ in the descending order of
total_marks
SQL> SELECT REGNO,TOTAL_MARKS FROM STUDENT_BIO WHERE TOTAL_MARKS
BETWEEN 300 AND 550 AND CATEGORY IN(‘OBC’,’GEN’) AND CITY LIKE ‘%E’ ORDER
BY TOTAL_MARKS DESC;
REGNO TOTAL_MARKS
----------------------- ------------------------------
1001 550
1006 500
PART - II
Consider the following tables and execute the given queries
a. Bank_customer(cid integer,cname nn, dob nn, city nn city name in capital letters)
SQL> CREATE TABLE BANK_CUSTOMER(CID INT,CNAME VARCHAR(20) NOT
NULL, DOB DATE NOT NULL,CITY VARCHAR(20) NOT NULL
CHECK(CITY=UPPER(CITY)));
table created.
COUNT(*)
--------------
3
b. Display max and min loan amount
SQL> SELECT MIN(LAMOUNT),MAX(LAMOUNT) FROM LOANS;
MIN(LAMOUNT) MAX(LAMOUNT)
-------------------------- ----------------------------
30000 350000
LDATE SUM(LAMOUNT)
--------------------- -----------------------------------------
26-MAR-15 150000
30-JUN-18 9000
13-FEB-16 15000
03-DEC-10 50000
4 rows selected.
d. Display total loan and average loan issued date wise in the ascending order of date
SQL> SELECT SUM(LAMOUNT),AVG(LAMOUNT),LDATE FROM LOANS
GROUP BY LDATE;
6 rows selected
e. Display status and total loan and average loan issued status wise for dates between 1st jan
2015 to 31st march 2017
SQL> SELECT STATUS,SUM(LAMOUNT),AVG(LAMOUNT) FROM LOANS
WHERE LDATE BETWEEN ’01-JAN-2015’ AND ’31-MAR-2017’ GROUP BY
STATUS;
S SUM(LAMOUNT) AVG(LAMOUNT)
------ ----------------------------- ----------------------------
A 290000 96666.667
f. Display the total loan amount status wise(only for personal loan)
SQL> SELECT SUM(LAMOUNT),STATUS FROM LOANS WHERE
LTYPE=’PERONAL’ GROUP BY STATUS;
SUM(LAMOUNT) S
---------------------------- ------------
100000 C
430000 A
g. Display total loan amount based on type and status not ‘c’ and amount<50000
SQL> SELECT SUM(LAMOUNT),LTYPE FROM LOANS WHERE STATUS!=’C’
GROUP BY LTYPE HAVING SUM(LAMOUNT)<50000;
SUM(LAMOUNT) LTYPE
--------------------------- -------------------
25000 PERSONAL
h. Display total loan amount based on type and for the total amount in between 3 lacs to 5
lacs and date <31st march 2016
SQL> SELECT LTYPE,SUM(LAMOUNT) FROM LOANS WHERE LDATE<’31-
MAR-2016’ GROUP BY LTYPE HAVING SUM(LAMOUNT) BETWEEN 100000
AND 500000;
LTYPE SUM(LAMOUNT)
------------------ -----------------------------
CONSUMER 240000
CONSUMER 380000
i. Display the total count of loans status wise in each category in the descending order of
type
SQL> SELECT COUNT(LOANID),STATUS,LTYPE FROM LOANS GROUP BY
STATUS,LTYPE ORDER BY LTYPE;
COUNT(LOANID) S LTYPE
--------------------------- ---------- -----------------
2 A CONSUMER
5 C CONSUMER
2 rows selected.
**********************************************************************************
Register Number:191805
Program Number:05
AIM : Testing Aggregate functions
**********************************************************************************
Create the following relations
a. Persons(PID Int Primary Key, Pname Varchar(20) in Proper case Letters, DOB Date Not
Null, City Varchar(15) Not Null, Category char(3))
b. Deposits(DID Int PK, PID Int FK, Date1 Date Not Null, CDate Date Cdate>Date1,AcBal
Number(10,2) AcBal>0, DepType Varchar(15) accept only ‘Savings’,’Recurring’,or
‘Current’, Status Char(1) accept only ‘A’, ‘S’,’C’)
Table created.
b> Deposits(DID Int PK, PID Int FK, Date1 Date Not Null, CDate Date Cdate>Date1,AcBal
Number(10,2) AcBal>0, DepType Varchar(15) accept only ‘Savings’,’Recurring’,’Fixed’, or
‘Current’ , Status Char(1) accept only ‘A’,’S’,’C’)
SQL> CREATE TABLE DEPOSITS(DID INT PRIMARY KEY,
PID INT REFERENCES PERSONS(PID),
DATE1 DATE NOT NULL,
CDATE DATE,
CONSTRAINT DEP_CDATE_CHK CHECK(CDATE>DATE1),
ACBAL NUMBER(10,2) CONSTRAINT DEP_BAL_CHK CHECK(ACBAL>0),
DEPTYPE VARCHAR(15) CONSTRAINT DEP_TYPE_CHK
CHECK(DEPTYPE=ANY(‘SAVINGS,’RECURRING’,’FIXED’,’CURRENT’)),
8 STATUS CHAR(1) CONSTRAINT DEP_STATUS_CHK
CHECK(STATUS=ANY(‘A’,’S’,’C’)));
Note : All the constraints should be added while creating the tables (name all the
constraints)
COUNT(*)
---------------
1
b> Count the number of records in Deposits for savings account and Current Account
SQL> SELECT COUNT(*) FROM DEPOSITS WHERE DEPTYPE =
ANY(‘SAVINGS’,’CURRENT’);
COUNT(*)
----------------
4
c> Display Status and Total Loan issued Status wise for dates between 1st Apr 2016 to 31st
March 2017
SQL> SELECT STATUS,SUM(ACBAL),AVG(ACBAL) FROM DEPOSITS WHERE
DATE1 BETWEEN’01-APR-2016’ AND ’31-MAR-2017’ GROUP BY STATUS;
S SUM(ACBAL) AVG(ACBAL)
------------- ------------------------- ------------------------
A 90000 90000
MAX(ACBAL) MIN(ACBAL)
-------------------- ------------------------
90000 40000
e> Display Total Deposit and Average Deposit amount DepType-wise in the ascending order
of DepType
SQL> SELECT DEPTYPE, SUM(ACBAL) FROM DEPOSITS GROUP BY DEPTYPE
ORDER BY DEPTYPE;
DEPTYPE SUM(ACBAL)
----------------- --------------------------
CURRENT 40000
FIXED 80000
RECURRING 50000
SAVINGS 220000
f> Display Total Deposit amount DepType-wise for sum of amount > 75000 in the
descending order TotalAmount
SQL> SELECT DEPTYPE,SUM(ACBAL) AS TOTAL_AMOUNT FROM DEPOSITS
GROUP BY DEPTYPE HAVING SUM(ACBAL)>75000 ORDER BY
TOTAL_AMOUNT DESC;
DEPTYPE TOTAL_AMOUNT
--------------- --------------------------------
SAVINGS 220000
FIXED 80000
g> Display Deposit Type, Total Deposit amount DepType and Status wise for only
AcBal>25,000
SQL> SELECT DEPTYPE ,SUM(ACBAL) FROM DEPOSITS WHERE
ACBAL>25000 GROUP BY DEPTYPE, STATUS;
DEPTYPE SUM(ACBAL)
----------------- --------------------------
CURRENT 40000
FIXED 80000
RECURRING 50000
SAVINGS 145000
SAVINGS 75000
h> Display Date1.DepositType,TotalDeposit amount Datewise and Deposit Type wise for
only Status = ‘A’ or ‘S’ and sum of amount <80,000 in the Ascending order of Total
DATE1 DEPTYPE SUM(ACBAL)
------------------- --------------------- ---------------------------
28-FEB-15 CURRENT 40000
21-JUN-14 RECURRING 40000
26-JUL-14 CURRENT 55000
PART – II
Aim: i) Learning simple joins (Inner Join)
ii) Testing Simple joins with aggregate function
i) Display pid, pname, city, did, acbal, deptype in the descending order of pname
SQL> SELECT P.PID,P.PNAME,P.CITY,D.DID,D.ACBAL,D.DEPTYPE FROM
DEPOSITS D,PERSONS P WHERE P.PID ORDER BY PNAME DESC;
6 rows selected
ii) Display Pid, Pname, City, DID, AcBal, DepType in the descending order of city
name and descending order of Pname ;
SQL> SELECT P.PID,P.PNAME,P.CITY,D.DID,D.ACBAL,D.DEPTYPE FROM
DEPOSITS D,PERSONS P WHERE P.PID=D.PID ORDER BY P.CITY DESC
,P.PNAME DESC;
6 rows selected
iii) Display PID, Pname, City, DID, AcBal, DepType, Status for City
Name=’Mangaluru’ or ‘Coorg’
SQL> SELECT
P.PID,P.PNAME,P.CITY,D.DID,D.ACBAL,D.DEPTYPE,D.STATUS FROM
DEPOSITS D,PERSONS P WHERE P.PID=D.PID AND P.CITY
IN(‘MANGALORE’,’COORG’);
iv) Display PID, Pname, City, DID, AcBal, DepType, Status for City
Name=’Mangaluru’ or ‘Coorg’ and Date1 in between 1st Jan 2016 to 31st Dec 2016
SQL> SELECT
P.PID,P.PNAME,P.CITY,D.DID,D.ACBAL,D.DEPTYPE,D.STATUS FROM
DEPOSITS D,PERSONS P WHERE P.PID=D.PID AND P.CITY
IN(‘MANGALORE’,’COORG’) AND D.DATE1 BETWEEN ‘1-JAN-2016’ AND
’31-DEC-2016’;
v) Display DepType, Total Deposit and Average Deposit amount DepType-wise in the
ascending order of DepType for city=’Mangaluru’
SQL> SELECT DEPTYPE,SUM(ACBAL),AVG(ACBAL) FROM DEPOSITS
D,PERSONS P WHERE P.PID=D.PID AND P.CITY =ANY(‘MANGALURU’)
GROUP BY D.DEPTYPE ORDER BY D DEPTYPE;
no rows selected
vi) Display City, Total Deposit and Average Deposit amount Citywise, DepType-wise in
the ascending order of city, DepType
SQL> SELECT CITY,SUM(ACBAL),AVG(ACBAL) FROM DEPOSITS
D,PERSONS P WHERE P.ID=D.PID GROUP BY P.CITY,D.DEPTYPE ORDER
BY P.CITY,D.DEPTYPE;
6 rows selected.
vii) Display City, Total Deposit City-wise for AcBal>10,000 in the ascending order of
City
SQL> SELECT CITY,SUM(ACBAL),AVG(ACBAL) FROM DEPOSITS
D,PERSONS P WHERE P.PID=D.PID AND ACBAL>10000 GROUP BY P.CITY
ORDER BY P.CITY;
6 rows selected.
viii) Display City, DepType, Total Deposit Citywise, DepType-wise for TotalDepo
amount>50,000
SQL> SELECT CITY, DEPTYPE,SUM(ACBAL) FROM DEPOSITS D,PERSONS
P WHERE P.ID=D.PID GROUP BY D. DEPTYPE,P.CITY HAVING
SUM(ACBAL)>50000;