Professional Documents
Culture Documents
Practical Cs Xii Mysql 2022-23 Final
Practical Cs Xii Mysql 2022-23 Final
CBSE SYLLABUS
Database Management
● Create a student table and insert data. Implement the following SQL commands on the student table:
o ALTER table to add new attributes / modify data type / drop attribute
o UPDATE table to modify data
o ORDER By to display data in ascending / descending order
o DELETE to remove tuple(s)
o GROUP BY and find the min, max, sum, count and average
● Similar exercise may be framed for other cases.
● Integrate SQL with Python by importing suitable module.
EMPLOYEE DATABSE
TABLE: DEPT
TABLE: EMP
WOODEN DATABASE
TABLE: FURNITURE
FNO ITEMNAME TYPE DATEOFSTOCK PRICE DISCOUNT
1 WHITE LOTUS DOUBLE BED 2003-02-09 30000 25
2 PINK FEATHER BABY COAT 2000-01-01 7000 20
3 DOLPHIN BABY COAT 2019-02-02 9500 20
4 DECENT OFFICE TABLE 2001-01-02 25000 30
5 COMFORT ZONE DOUBLE BED 2012-01-02 25000 25
6 DONALD BABY COAT 1994-02-02 6500 15
7 ROYAL FINISH OFFICE TABLE 2020-02-02 18000 30
8 ROYAL TIGER SOFA 2022-02-02 31000 30
9 ECONO SITTING SOFA 2013-11-01 9500 25
10 EATING PARADISE DINING TABLE 1998-02-02 11500 25
TABLE: CLIENT
CNO CLIENTNAME CITY DATEOFSALE QUANTITY FNO
C1 WOOD COMFORT MUMBAI 2022-03-03 5 10
C2 DECO FURNITURE KOLKATA 2021-02-03 2 5
C3 MICKY CLUB BANGALORE 2021-02-03 4 3
C4 TECHNO SAVVY MUMBAI 2019-02-02 6 10
C5 HEALTH N CARE DELHI 2013-12-01 4 6
Page 2 of 9
DATE: Q8. : Create the above tables with records.
EMPLOYEE DATABASE
DROP database if exists EMPLOYEE;
CREATE database EMPLOYEE;
USE EMPLOYEE;
CREATE DEPT TABLE
CREATE TABLE DEPT (DEPTNO int NOT NULL, DNAME varchar(14), LOC varchar(13), PRIMARY KEY (DEPTNO));
CREATE EMPL TABLE
CREATE TABLE EMP (EMPNO int NOT NULL PRIMARY KEY, ENAME char(10), JOB char(9),
MGR int, DOJ date, SAL float(7, 2), COMM float (7, 2), DEPTNO int , FOREIGN KEY (DEPTNO) REFERENCES
DEPT(DEPTNO));
INSERT DATA TO DEPT TABLE
INSERT INTO DEPT VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO DEPT VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO DEPT VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO DEPT VALUES (40, 'OPERATIONS', 'BOSTON');
INSERT DATA TO EMP TABLE
INSERT INTO EMP VALUES (7369, 'SMITH', 'CLERK', 7902, '1990-12-17', 800, NULL, 20);
INSERT INTO EMP VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
INSERT INTO EMP VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22',1250, 500, 30);
INSERT INTO EMP VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20);
INSERT INTO EMP VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
INSERT INTO EMP VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30);
INSERT INTO EMP VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10);
INSERT INTO EMP VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, NULL, 20);
INSERT INTO EMP VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10);
INSERT INTO EMP VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
INSERT INTO EMP VALUES (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, NULL, 20);
INSERT INTO EMP VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30);
INSERT INTO EMP VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20);
INSERT INTO EMP VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);
WOODEN DATABASE
DROP database if exists WOODEN;
CREATE database WOODEN;
USE WOODEN;
FURNITURE TABLE CREATION
CREATE TABLE FURNITURE(FNO int NOT NULL, ITEMNAME char(15), TYPE char(14),
DATEOFSTOCK date, PRICE float(8,2), DISCOUNT int, PRIMARY KEY (FNO));
CLIENT TABLE CREATION
CREATE TABLE CLIENT(CNO char(2) NOT NULL PRIMARY KEY, CLIENTNAME char(15),
CITY char(14), DATEOFSALE date, QUANTITY int, FNO int, FOREIGN KEY (FNO)
REFERENCES FURNITURE(FNO));
INSERT IN TO FURNITURE TABLE
Insert into furniture values (1, 'WHITE LOTUS', 'DOUBLE BED','2002-02-23', 30000, 25);
Insert into furniture values(2, 'PINK FEATHER', 'BABY COAT', '2001-01-20', 7000, 20);
Insert into furniture values(3, 'DOLPHIN', 'BABY COAT', '2002-02-19', 9500, 20);
Insert into furniture values(4, 'DECENT', 'OFFICE TABLE', '2002-01-01', 25000, 30);
Insert into furniture values(5, 'COMFORT ZONE', 'DOUBLE BED', '2002-01-12',25000, 25);
Insert into furniture values(6, 'DONALD', 'BABY COAT', '2002-02-24', 6500, 15);
Insert into furniture values(7, 'ROYAL FINISH', 'OFFICE TABLE', '2002-02-20', 18000, 30);
Insert into furniture values(8, 'ROYAL TIGER', 'SOFA', '2002-02-22', 31000, 30);
Insert into furniture values(9, 'ECONO SITTING', 'SOFA', '2001-02-22', 9500, 25);
Insert into furniture values(10,'EATING PARADISE', 'DINING TABLE', '2002-02-19', 11500, 25);
INSERT INTO CLIENT TABLE
Insert into CLIENT values('C1', 'WOOD COMFORT', 'MUMBAI', '2003-03-23', 5, 10);
Insert into CLIENT values('C2', 'DECO FURNITURE', 'KOLKATA', '2003-02-20', 2, 5);
Insert into CLIENT values('C3', 'MICKY CLUB', 'BANGALORE', '2003-02-21', 4, 3);
Insert into CLIENT values('C4', 'TECHNO SAVVY', 'MUMBAI', '2002-02-19', 6, 10);
Insert into CLIENT values('C5', 'HEALTH N CARE', 'DELHI', '2001-12-13', 4, 6);
Page 3 of 9
DATE:
Q9.Alter/ modify the structure of the table using ALTER:
(a) Write the syntax of ALTER command.
ALTER TABLE <table_name>
ADD <col_name> <column_definition>
MODIFY <col_name> <column_definition>
DROP <col_name>
CHANGE <old_col_name> <new_col_name> <column_definition>
RENAME [TO] <new_tbl_name>
(b) Add new attributes or column as PHONE to store 10 digit phone number as a number
in CLIENT table.
ALTER TABLE CLIENT ADD PHONE INT(10);
(c) Now Modify the data type of PHONE to character type to store 10 digits.
ALTER TABLE CLIENT MODIFY PHONE CHAR(10);
(d) Rename the PHONE column name to MOBILE.
ALTER TABLE CLIENT CHANGE PHONE MOBILE CHAR(10);
(e) Now delete / drop the field/Column/attribute MOBILE.
ALTER TABLE CLIENT DROP MOBILE;
(f) Rename the CLIENT table to CUSTOMER.
ALTER TABLE CLIENT RENAME CUSTOMER;
(g) Modify the field ITEMNAME of FURNITURE table to 15 character.
• ALTER TABLE FURNITURE MODIFY ITEMNAME VARCHAR(15);
DATE:
Q.10. Modify or Update the data in a table.
(a) Write the syntax of UPDATE command.
Syntax: UPDATE <table_name> SET <Field_Name> = <Expression>
WHERE <Condition>
(d) Replace all the CITY having name MUMBAI as BOMBAY in CLIENT table of FURNITURE
Database.
UPDATE CLIENT SET CITY = ‘BOMBAY’ WHERE CITY=’MUMBAI’;
(e) Replace COMM field values with 1000 if COMM = NULL in EMP table.
UPDATE EMP SET COMM=1000 WHERE COMM IS NULL;
(f) Increase PRICE by 10% more of all furniture where DATEOFSTOCK before 01.01.2000.
UPDATE FURNITURE SET PRICE=PRICE *1.1;
DATE:
Q.11. DELETE records from a table.
(a) Write the syntax of DELETE command.
Syntax: DELETE FROM table_name [WHERE Clause]
(b) Delete all the records of EMP table.
DELETE FROM EMP;
(c) Delete the record from FURNITURE table with FNO = 10;
DELETE FROM FURNITURE WHERE FNO=10;
(d) Delete all the records from FURNITURE table having furniture TYPE begins with “BABY”.
DELETE FROM FURNITURE WHERE TYPE LIKE “BABY%”;
Page 4 of 9
DATE:
Q.12. SQL Queries using SELECT command.
(a) Write the syntax of SELECT statement.
Conditional operators: =, >, <, <=, >=, <>, IS (used to compare NULL values)
Logical operator to be used in condition: AND, OR, NOT
** LIKE as an operator to be used with wildcards % and _ (underscore)
% - Any no. of character or No character. _ - Any one character.
(b) List all the details of DEPT table in ascending order of LOC (Location).
SELECT * FROM DEPT ORDER BY LOC ASC;
(c) List all the ITEM DETAILS in FURNITURE table in sorted order of ITEM TYPE in
ascending and PRICE in descending.
SELECT * FROM FURNITURE ORDER BY TYPE ASC, PRICE DESC;
(d) Display all the ITERMNAMES, PRICE in FURNITURE table in descending order of table
with PRICE more than or equal to11500.
(e) What is the total sale price of the (as total sale price = sum of all items (sale quantity
*Price of item).
SELECT SUM((PRICE * QUANTITY)) 'TOTAL PRICE' FROM FURNITURE, CLIENT
WHERE CLIENT.FNO = FURNITURE.FNO;
Or
SELECT SUM((PRICE * QUANTITY)) AS “TOTAL PRICE” FROM FURNITURE, CLIENT
WHERE CLIENT.FNO = FURNITURE.FNO;
Or
SELECT SUM((PRICE * QUANTITY)) AS “TOTAL PRICE” FROM FURNITURE, CLIENT
WHERE CLIENT NATURAL JOIN FURNITURE;
Page 5 of 9
(f)List all the distinct CITY from CLIENT.
SELECT CITY FROM CLIENT GROUP BY CITY; OR SELECT DISTINCT(CITY) FROM CLIENT;
(g) Display furniture TYPE, No. of Furnitures of each TYPE as per the FURNITURE table.
SELECT TYPE, COUNT(*) FROM FURNITURE GROUP BY TYPE;
(h) Display JOB and No. of employees in each JOB as per EMP table.
SELECT JOB, COUNT(*) FROM EMP GROUP BY JOB HAVING COUNT(*)>2;
(i) Display DEPTNO, No. of employee in each DEPTNO, total salary paid department-wise
who are getting more than 2000;
SELECT DEPTNO, COUNT(*), SUM(SAL) FROM EMP
WHERE SAL>2000 GROUP BY DEPTNO;
(j) Display average Salary of all the employees in Department 10 as per the EMP table.
SELECT AVG(SAL) FROM EMP WHERE DEPTNO=10;
(k) Display the date of joining (DOJ) of the employees who have joined at last and as per
the EMP table.
SELECT MAX(DOJ) FROM EMP;
(l) Display the employees’ oldest date of joining (DOJ) in EMP table.
SELECT MIN(DOJ) FROM EMP;
Page 6 of 9
DATE:
Q13. Write the OUTPUT of the following SQL Queries:
Page 7 of 9
Date:
Q.14. Connecting MYSQL and Python
(a) Connect to DATABASE EMPLOYEE and Display EMP & DEPT TABLE RECORDS
import mysql.connector
mycon=mysql.connector.connect(host="localhost", user="root",passwd="admin", database="EMPLOYEE")
mycursor=mycon.cursor()
mycon.close()
(b) Connect to DATABASE EMPLOYEE and Display EMP & DEPT TABLE Records
import mysql.connector
mycon=mysql.connector.connect(host="localhost", user="root",passwd="admin", database="EMPLOYEE")
mycursor=mycon.cursor()
mycursor.execute("INSERT INTO EMP VALUES (7499, 'AYUSH', 'ANALYST', 7698, '1981-02-20', 4600, 300, 30)")
mycon.commit() # To save above DML tansactions to the databases otherwise will not save permanently
mycon.close()
import mysql.connector
mycon=mysql.connector.connect(host="localhost", user="root",passwd="admin", database=”kdml”)
if mycon.is_connected()==True:
print("Connection to MySQLdatabase is successful")
mycursor=mycon.cursor()
Page 8 of 9
(c) Connect to DATABASE EMPLOYEE and update the record of an employee as per the input of
empno (employee number).
import mysql.connector
mycon=mysql.connector.connect(host="localhost", user="root",passwd="admin", database=”kdml”)
if mycon.is_connected()==True:
print("Connection to MySQLdatabase is successful")
mycursor=mycon.cursor()
Page 9 of 9