Вы находитесь на странице: 1из 13

/EXP NO.

1
CREATE TABLE SALESPERSON
(SNO INT,
SNAME VARCHAR(10),
CITY VARCHAR(10),
COMM DECIMAL)
SELECT * FROM SALESPERSON
DROP TABLE SALESPERSON
CREATE TABLE SALESPERSON
(SNO INT NOT NULL,
SNAME VARCHAR(10),
CITY VARCHAR(10),
COMM DECIMAL)
SELECT * FROM SALESPERSON
DROP TABLE SALESPERSON
CREATE TABLE SALESPERSON
(SNO INT NOT NULL UNIQUE,
SNAME VARCHAR(10),
CITY VARCHAR(10),
COMM DECIMAL,
UNIQUE(SNO,SNAME))
SELECT * FROM SALESPERSON
DROP TABLE SALESPERSON
CREATE TABLE SALESPERSON
(SNO INT PRIMARY KEY,
SNAME VARCHAR(10) NOT NULL,
CITY VARCHAR(10),
COMM DECIMAL)
SELECT * FROM SALESPERSON
DROP TABLE SALESPERSON
CREATE TABLE SALESPERSON
(SNO INT,
SNAME VARCHAR(10),
CITY VARCHAR(10),
COMM DECIMAL,
CHECK(COMM>1))
SELECT * FROM SALESPERSON
CREATE TABLE SALESPERSON
(SNO INT PRIMARY KEY,
SNAME VARCHAR(10) NOT NULL,
CITY VARCHAR(10),
COMM DECIMAL)
CREATE TABLE CUSTOMER
(CNO INT NOT NULL PRIMARY KEY,
CNAME VARCHAR(10),
SNO INT CONSTRAINT FK FOREIGN KEY(SNO) REFERENCES SALESPERSON(SNO))
SELECT * FROM SALESPERSON
SELECT * FROM CUSTOMER
DROP TABLE SALESPERSON
DROP TABLE CUSTOMER

//EXP NO.2
CREATE TABLE SALESPERSON
(SNO INT NOT NULL,
SNAME VARCHAR(10),
CITY VARCHAR(10))
SELECT * FROM SALESPERSON
//ADD COLUMN
ALTER TABLE SALESPERSON
ADD COMM DECIMAL
SELECT * FROM SALESPERSON
//DROPING COLUMN
ALTER TABLE SALESPERSON
DROP COLUMN COMM
SELECT * FROM SALESPERSON
//ADD PRIMARY KEY
ALTER TABLE SALESPERSON
ADD CONSTRAINT PK PRIMARY KEY(SNO)
SELECT * FROM SALESPERSON
//DROP PRIMARY KEY
ALTER TABLE SALESPERSON
DROP CONSTRAINT PK
SELECT * FROM SALESPERSON
//ADD FOREIGN KEY
CREATE TABLE CUSTOMER
(CNO INT NOT NULL PRIMARY KEY,
CNAME VARCHAR(10),
SNO INT NOT NULL)
SELECT * FROM CUSTOMER
ALTER TABLE SALESPERSON
ADD CONSTRAINT PK PRIMARY KEY(SNO)
SELECT * FROM SALESPERSON
ALTER TABLE CUSTOMER
ADD CONSTRAINT FK FOREIGN KEY(SNO) REFERENCES SALESPERSON(SNO)
SELECT * FROM CUSTOMER
//DROP FOREIGN KEY
ALTER TABLE CUSTOMER
DROP CONSTRAINT FK
SELECT * FROM CUSTOMER
//CHANGING THE DEFAULT VALUE FOR COLUMN
CREATE TABLE PRODUCT
(PNUM INT PRIMARY KEY,
PNAME VARCHAR(10),
PADD VARCHAR(10),
PPHONE INT,
PSALES MONEY)
SELECT * FROM PRODUCT
ALTER TABLE PRODUCT
ADD PSEX CHAR(10)
SELECT * FROM PRODUCT
ALTER TABLE PRODUCT
DROP COLUMN PSEX
SELECT * FROM PRODUCT
ALTER TABLE PRODUCT
ADD PSEX CHAR(10) DEFAULT 'M'
SELECT * FROM PRODUCT
INSERT INTO PRODUCT(PNUM,PNAME,PADD,PPHONE,PSALES)
VALUES(103,'ANURAG','DELHI',243001,15000)
SELECT * FROM PRODUCT
DROP TABLE SALESPERSON
DROP TABLE CUSTOMER
//EXP NO.3 DATABASE UPDATES
CREATE TABLE SALESPERSON
(SNO INT NOT NULL PRIMARY KEY,
SNAME VARCHAR(10),
CITY VARCHAR(10),
COMM DECIMAL)
SELECT * FROM SALESPERSON
//1)ADDING DATA TO THE DATABASE
//A)SINGLE ROW INSERT
INSERT INTO SALESPERSON
VALUES(1,'SAPANA','MUMBAI',45.3)
SELECT * FROM SALESPERSON
//B)INSERTING NULL VALUES
INSERT INTO SALESPERSON(SNO,SNAME,COMM)
VALUES(4,'JAY',43.3)
SELECT * FROM SALESPERSON
INSERT INTO SALESPERSON
VALUES(5,'JAYA',NULL,48.3)
//C)INSERTING ALL COLUMN
INSERT INTO SALESPERSON
VALUES(6,'SHREYA','DELHI',40.3)
SELECT * FROM SALESPERSON
//D)MULTIROW INSERT
//DROP TABLE SALES
CREATE TABLE SALES
(SNO INT NOT NULL ,
SNAME VARCHAR(10),
CITY VARCHAR(10),
COMM DECIMAL)
SELECT * FROM SALES
INSERT INTO SALES(SNO,SNAME,CITY,COMM)
SELECT SNO,SNAME,CITY,COMM
FROM SALESPERSON
WHERE CITY='MUMBAI'
SELECT * FROM SALES
//2)DELETING DATA FROM DATABASE
DELETE FROM SALESPERSON
WHERE CITY='DELHI'
SELECT * FROM SALESPERSON
//DELETING ALL ROWS
DELETE FROM SALESPERSON
SELECT * FROM SALESPERSON
INSERT INTO SALESPERSON
VALUES(1,'SAPANA','MUMBAI',45.3)
INSERT INTO SALESPERSON(SNO,SNAME,COMM)
VALUES(4,'JAY',43.3)
INSERT INTO SALESPERSON
VALUES(6,'SHREYA','DELHI',40.3)
INSERT INTO SALESPERSON
VALUES(5,'JAYA',NULL,48.3)
SELECT * FROM SALESPERSON
//3)UPDATING DATA FROM DATABASE
//1)UPDATING SINGLE ROW
UPDATE SALESPERSON
SET CITY='PUNE'
WHERE SNO = 4
SELECT * FROM SALESPERSON
//2)UPDATE ALL ROWS
UPDATE SALESPERSON
SET CITY='MUMBAI'
SELECT * FROM SALESPERSON
//EXP NO.4 SIMPLE QUERIES
//1)SELECT STATEMENT
//A)SELECTING ALL COLUMN
SELECT * FROM SALESPERSON
//B)DISTINCT
SELECT DISTINCT CITY
FROM SALESPERSON
//2)ROW SELECTION(WHERE CLAUSE)
SELECT *
FROM SALESPERSON
WHERE CITY='MUMBAI'
//3)COMPARISION TEST
SELECT *
FROM SALESPERSON
WHERE COMM<45
SELECT *
FROM SALESPERSON
WHERE COMM>45
SELECT *
FROM SALESPERSON
WHERE city='PUNE'
SELECT *
FROM SALESPERSON
WHERE COMM<>40
SELECT *
FROM SALESPERSON
WHERE COMM>=45
SELECT *
FROM SALESPERSON
WHERE COMM<=45
//4)RANGE TEST(BETWEEN)
SELECT SNAME,COMM
FROM SALESPERSON
WHERE SNO BETWEEN 1 AND 3
//5)SET MEMBERSHIP TEST
SELECT *
FROM SALESPERSON
WHERE CITY IN('MUMBAI','PUNE')
SELECT *
FROM SALESPERSON
WHERE CITY NOT IN('MUMBAI','PUNE')
//7)PATTERN MATCHING TEST(LIKE)
SELECT *
FROM SALESPERSON
WHERE SNAME LIKE 'S%'
SELECT *
FROM SALESPERSON
WHERE SNAME LIKE 'S_H%'
//8)ESCAPE CHARACTER
SELECT *
FROM SALESPERSON
WHERE SNAME LIKE 'A$%B' ESCAPE '$'
//9)NULL VALUE TEST(IS NULL)
SELECT *
FROM SALESPERSON
WHERE CITY IS NULL
SELECT *
FROM SALESPERSON
WHERE CITY IS NOT NULL
//10)COMPOUND SERACH CONDITION(AND,OR,NOT)
SELECT *
FROM SALESPERSON
WHERE CITY='MUMBAI' AND COMM<45
SELECT *
FROM SALESPERSON
WHERE SNO IN(2,4,6) OR city='PUNE'
SELECT *
FROM SALESPERSON
WHERE NOT COMM>=45
//11)SORTING QUERY RESULT
SELECT *
FROM SALESPERSON
WHERE COMM > 42
ORDER BY CITY
SELECT *
FROM SALESPERSON
WHERE COMM > 42
ORDER BY CITY DESC
SELECT SNO,SNAME,COMM-1
FROM SALESPERSON
ORDER BY 3 DESC
SELECT SNAME,CITY,COMM-2
FROM SALESPERSON
ORDER BY CITY ASC, 3 DESC
//12)COMBINING QUERY RESULTS(UNION)
SELECT SNO,SNAME
FROM SALESPERSON
WHERE COMM>45
UNION
SELECT SNO,SNAME
FROM SALESPERSON
WHERE CITY='PUNE'
//13)UNION & DUPLICATE ROWS
SELECT SNO,SNAME
FROM SALESPERSON
WHERE COMM>45
UNION ALL
SELECT SNO,SNAME
FROM SALESPERSON
WHERE CITY='PUNE'
//13)UNION & SORTING
SELECT SNO,SNAME
FROM SALESPERSON
WHERE COMM>45
UNION
SELECT SNO,SNAME
FROM SALESPERSON
WHERE CITY='PUNE'
ORDER BY 1,2
//EXP NO 5. SUMMARY QUERIES
//1)SUM( )
SELECT SUM(COMM) AS SUM_COMM
FROM SALESPERSON
SELECT SUM(DISTINCT COMM) AS SUM_COMM
FROM SALESPERSON
//2)AVG( )
SELECT AVG(COMM) AS AVG_COMM
FROM SALESPERSON
SELECT AVG(DISTINCT COMM) AS AVG_COMM
FROM SALESPERSON
//3)MIN( )
SELECT MIN(SNAME) AS MIN_SNAME
FROM SALESPERSON
//4)MAX( )
SELECT MAX(COMM) AS MAX_COMM
FROM SALESPERSON
//5)COUNT( )
SELECT COUNT(COMM) AS COUNT_COMM
FROM SALESPERSON
//6)COUNT(* )
SELECT COUNT(*) AS COUNT_COMM
FROM SALESPERSON
///MULTIPLE GROUPING COLUMN
SELECT SNO,SNAME,SUM(COMM) AS SUM_COMM
FROM SALESPERSON
GROUP BY SNO,SNAME
SELECT SNO,SNAME,SUM(COMM) AS SUM_COMM
FROM SALESPERSON
GROUP BY SNO,SNAME
ORDER BY SNO,SNAME
SELECT SNO,SNAME,COMM
FROM SALESPERSON
ORDER BY SNO
COMPUTE SUM(COMM)BY SNO
COMPUTE AVG(COMM)BY SNO
///HAVING CLAUSE
SELECT SNO,SUM(COMM) AS SUM_COMM
FROM SALESPERSON
GROUP BY SNO
HAVING AVG(COMM)>40
//EXP NO.6 MULTITABLE QUERY
CREATE TABLE SALS
(EMP_NO INT,
SNAME CHAR(10),
MANAGER INT,
QUOTA INT,
SALES INT)
SELECT * FROM SALS
INSERT INTO SALS
VALUES(105,'A',104,350,367)
INSERT INTO SALS
VALUES(109,'B',106,300,396)
INSERT INTO SALS
VALUES(105,'C',NULL,250,267)
INSERT INTO SALS
VALUES(107,'D',103,750,767)
INSERT INTO SALS
VALUES(105,'E',101,250,167)
SELECT * FROM SALS
CREATE TABLE ORDER1
(AMOUNT INT,
ORDER_NO INT,
PRODUCT INT,
AGE INT)
SELECT * FROM ORDER1
INSERT INTO ORDER1
VALUES(100,105,12,13)
INSERT INTO ORDER1
VALUES(111,107,14,12)
INSERT INTO ORDER1
VALUES(111,105,26,12)
SELECT * FROM ORDER1
//1)SIMPLE JOINS(EQUI-JOINS)
SELECT * FROM SALS
SELECT * FROM ORDER1
SELECT SNAME,QUOTA,SALES,AGE,AMOUNT,EMP_NO,ORDER_NO
FROM SALS,ORDER1
WHERE EMP_NO=ORDER_NO
//2)PARENT_CHILD QUERIES
CREATE TABLE SALE1
(SNUM INT NOT NULL PRIMARY KEY,
SNAME CHAR(10) NOT NULL UNIQUE,
CITY CHAR(10) DEFAULT 'BOISAR',
COMM DECIMAL)
SELECT * FROM SALE1
INSERT INTO SALE1(SNUM,SNAME,COMM)
VALUES(10,'ABC1',9)
INSERT INTO SALE1(SNUM,SNAME,COMM)
VALUES(12,'AC1',9)
SELECT * FROM SALE1
CREATE TABLE CUST
(CNUM INT NOT NULL PRIMARY KEY,
CNAME CHAR(10),
SNUM INT ,
FOREIGN KEY(SNUM) REFERENCES SALE1(SNUM))
SELECT * FROM CUST
INSERT INTO CUST(CNUM,CNAME,SNUM)
VALUES(1,'ABC',12)
INSERT INTO CUST(CNUM,CNAME,SNUM)
VALUES(9,'ABC',12)
SELECT * FROM CUST
//QUERIES WITH TWO OR MORE TABLE
//SELECT * FROM SALE1
//SELECT * FROM CUST
SELECT SALE1.SNUM,SNAME,CITY,COMM,CNUM,CUST.SNUM
FROM SALE1,CUST
WHERE CUST.SNUM=SALE1.SNUM AND COMM=CNUM AND CUST.SNUM>10
//OTHER EQUI_JOINS
SELECT SALE1.SNUM,SNAME,CITY,COMM,CNUM,CNAME,CUST.SNUM
FROM SALE1,CUST
WHERE COMM=CNUM
//NON EQUI_JOIN
SELECT SALE1.SNUM,SNAME,CITY,COMM,CNUM,CNAME,CUST.SNUM
FROM SALE1,CUST
WHERE CNUM<COMM
SELECT SALE1.SNUM,SNAME,CITY,COMM,CNUM,CNAME,CUST.SNUM
FROM SALE1,CUST
WHERE CUST.SNUM=SALE1.SNUM
//EX NO.7:SUB QUERY & QUERY EXPRESSION
CREATE TABLE OFFICES
(OFFICE INT NOT NULL UNIQUE,
CITY CHAR(10),
REGION CHAR(15),
TARGET FLOAT,
SALES INT)
SELECT * FROM OFFICES
INSERT INTO OFFICES
VALUES(22,'MUMBAI','WESTERN',300000,186042)
INSERT INTO OFFICES
VALUES(11,'PUNE','WESTERN',575000,735042)
INSERT INTO OFFICES
VALUES(12,'KOLKATA','EASTERN',800000,735042)
INSERT INTO OFFICES
VALUES(13,'DELHI','EASTERN',350000,367911)
INSERT INTO OFFICES
VALUES(21,'SURAT','WESTERN',725000,835913)
SELECT * FROM OFFICES
CREATE TABLE SALESREP
(NAME CHAR(10),
REP_OFFICE INT,
QUOTA FLOAT,
SALES INT)
SELECT * FROM SALESREP
INSERT INTO SALESREP
VALUES('PRIYA',13,350000,367911)
INSERT INTO SALESREP
VALUES('SWAPNALI',11,350000,392725)
INSERT INTO SALESREP
VALUES('KOMAL',21,350000,474050)
INSERT INTO SALESREP
VALUES('SUVARNA',11,275000,299120)
INSERT INTO SALESREP
VALUES('SHWETA',12,200000,142594)
INSERT INTO SALESREP
VALUES('SHEETAL',NULL,NULL,759845)
SELECT * FROM SALESREP
//SUBQUERIES
SELECT CITY
FROM OFFICES
WHERE TARGET>(SELECT SUM(QUOTA)
FROM SALESREP
WHERE REP_OFFICE=OFFICE)
//SUBQUERY SEARCH CONDITION
//1)SUBQUERY COMPARISION TEST(<>,<,>,<=,>=)
SELECT NAME
FROM SALESREP
WHERE QUOTA>=(SELECT TARGET
FROM OFFICES
WHERE CITY='DELHI')
//2)SET MEMBERSHIP TEST(IN)
SELECT NAME
FROM SALESREP
WHERE REP_OFFICE IN(SELECT OFFICE
FROM OFFICES
WHERE SALES>TARGET)
//3)EXISTANCE TEST(EXIST)
SELECT DISTINCT CITY
FROM OFFICES
WHERE EXISTS(SELECT REP_OFFICE
FROM SALESREP
WHERE TARGET=QUOTA AND OFFICES.SALES<=SALESREP.SALES)
//4)QUALIFIFD TEST(ANY,ALL)
////ANY TEST
SELECT NAME
FROM SALESREP
WHERE (0.1*QUOTA)< ANY(SELECT SALES
FROM OFFICES
WHERE OFFICE=REP_OFFICE)
//ALL TEST
SELECT TARGET
FROM OFFICES
WHERE (0.50*TARGET)< ALL(SELECT SALES
FROM SALESREP
WHERE REP_OFFICE=OFFICE)
CREATE TABLE CUSTOMERS
(CUST_NO INT NOT NULL PRIMARY KEY,CUST_NAME CHAR(10),
COMPANY CHAR(20),
CUST_REP INT,
CREDIT_LIMIT FLOAT)
SELECT * FROM CUSTOMERS
INSERT INTO CUSTOMERS
VALUES(2111,'PRIYA','BADSHAH MASALA',103,5000)
INSERT INTO CUSTOMERS
VALUES(2102,'SWAPNALI','CHANDAN TABLE',101,65000)
INSERT INTO CUSTOMERS
VALUES(2103,'DIVYA','UNIFLEX CABLES',105,5000)
INSERT INTO CUSTOMERS
VALUES(2123,'HIRAL','HINDUSTAN PENCIL',102,40000)
INSERT INTO CUSTOMERS
VALUES(2121,'MITALI','VIJAY FIRES',103,45000)
SELECT * FROM CUSTOMERS
SELECT COMPANY
FROM CUSTOMERS
WHERE CUST_NAME IN(SELECT NAME
FROM SALESREP
WHERE REP_OFFICE IN(SELECT OFFICE
FROM OFFICES
WHERE REGION='EAST
ERN'))

////CORRELATED SUBQUERIES
SELECT CITY
FROM OFFICES
WHERE SALES<(SELECT AVG(TARGET)
FROM OFFICES)
///SUBQUERY IN HAVING CLAUSE
SELECT REGION,AVG(CREDIT_LIMIT)
FROM OFFICES,CUSTOMERS
GROUP BY REGION
HAVING AVG(CREDIT_LIMIT)>=(SELECT AVG(CREDIT_LIMIT)
FROM CUSTOMERS)