Practical No 26 - Merged

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Practical no = 27

Aim : Consider the following tables Product and Client. Write SQL
commands for the statements (i) to (iii) and give outputs for SQL
queries (i) to (vi)

Customers

Products
(i) To display the details of those clients whose city is delhi
(ii) To display the details of Products whose
Price is in the range of 50 to 100 (both values
included).

(iii) To display the details of those products whose name


ends with ‘Wash’.

(iv) select distinct City from CLIENT;


(v) select Manufacturer, max(Price), min(Price),
count(*) from PRODUCT group by Manufacturer;

(vi) select Product Name, Price * 4 from PRODUCT;


Practical no = 28

Aim: Consider the following Sports Table

Sports Table

(i) Display the names of the students who have


grade ‘C’ in either Game1 or Game2 or both.

(ii) Display the number of students getting grade


‘A’ in Cricket.
(iii) Display the names of the students who have
the same game for both Game1 and Game2.

(iv) Display the game taken up by the students,


whose name starts with ‘A’.

(v) Add a new column named ‘Marks’.


(f) Assign a value 200 for marks for all those
who are getting grade ‘B’ or grade ‘A’ in both
Game1 and Game2.
Practical no = 29

Aim: Consider the following tables: COMPANY and MODEL:


Comp_ID is the Foreign Key referencing
Comp_ID of Company table.
Write SQL commands for queries (i) to (iv)
and output for (v) and (vi).

Company Table

Modern Table
(i) To display details of all models in the Model table in ascending
order of DateOfManufacture.

(ii) To display details of those models manufactured


in 2011 and whose Cost is below 2000.
(iii) To display the Model_ID, Comp_ID, Cost from the table Model,
CompName and ContactPerson from Company table, with their
corresponding
Comp_ID.

(iv) To decrease the cost of all the models in


Model table by 15%
(v) select count(distinct CompHO) from Company

(vi) select CompName, contact(‘Mr.’,ContactPerson)

from Company where CompName ends with ‘a’


Practical no = 30

Aim : Use MySQL-Python Connectivity and Insert record in Employee


Table entered by the user and display the inserted record in output
windows. Attributes of the employee table: EmpID, EmpName, EmpDept,
EmpDesig, DOJ(date of joining), Salary

➔ Creating Database

➔ Creating Table

import mysql.connector

mydb = mysql.connector.connect (
host="localhost",user="root",password="12345678",database="file")

mycursor = mydb.cursor()

file_sql_query = "CREATE TABLE Employees ( EmpID VARCHAR(4) NOT NULL PRIMARY


KEY,EmpName CHAR (20) NOT NULL, EmpDept CHAR(15),EmpDesig CHAR (25),DOJ DATE,Salary
INT (6))"

mycursor.execute(file_sql_query)
mycursor.execute("CREATE DATABASE employee")
➔ Taking Input from user

➔ Inserting values in table


Practical no = 31

Aim : Use MySQL-Python Connectivity and Insert 5 record in Employee


Table by using executemany() function. Attributes of the employee table:
EmpID, EmpName, EmpDept, EmpDesig, DOJ(date of joining), Salary
PRACTICAL NUMBER:26

Aim: Create a student table and insert 10 row data. Implement the following SQL
commands on the student table: ALTER table to add new attributes/modify
data type/drop attribute; UPDATE table to modify data; ORDER By to
display data in ascending /descending order DELETE to remove tuple(s);
GROUP BY and find the min, max, sum.

● Creating Table

Source Code:
mysql> create table student (ROLLNO INT NOT NULL PRIMARY KEY,NAME
CHAR(10),Gender CHAR(10),DOB date, stream CHAR(10),PhoneNo
varchar(10),Location Char(10));
Query OK, 0 rows affected (0.30 sec)

Output:

Inserting Values Into Table

Source Code:
mysql> insert into student
values(01,"Bhaskar","M","2006-02-05","Science",9711980876, "Delhi"),
(02,"Prince ","M","2007-03-27","Science",9711980877, "Delhi"),
(03,"Keshav","M","2006-03-06","Science",9711980879,
"Delhi"),(04,"Shreya","F","2005-01-29","Science",9711980893, "Delhi"),
(05,"Srishti","F","2005-03-20","Arts",9711975876,
"Delhi"),(06,"Aditya","M","2003-09-29","Science",9711980674, "Delhi"),
(07,"Mantsha","M","2005-12-26","Commerce",9711980353, "Delhi"),
(08,"Roshan","M","2007-12-06","Science",9711982367, "Delhi"),
(09,"Neha","F","2005-02-24","Arts",9743980876, "Delhi"),
(10,"Akansha","F","2003-02-23","Arts",9711980870, "Delhi");
Query OK, 10 rows affected (0.08 sec)
Records: 10 Duplicates: 0 Warnings: 0

Output:
● Altering Table
Source Code:
mysql> alter table student add column Percentage
float(3,2); Query OK, 0 rows affected, 1 warning (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 1

Output:

● Modifying Datatype of Percentage


Source Code:
mysql> alter table student modify column Percentage
float(6,2); Query OK, 0 rows affected, 1 warning (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 1

Output;

● Updating Table
Source Code:
mysql> update student set PhoneNo = 9399999999 where ROLLNO =
10; Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Output:

(Before Updating)

(After Updating)

● Dropping a Attribute

Source Code:
mysql> alter table student drop column Percentage;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

Output:

(Before Dropping)

● Order By

Source Code:
mysql> select * from student order by DOB;

Output:
● Group By

Source Code:
mysql> select min(DOB) as Min_DOB from student;

Output:

Source Code:
mysql> select max(DOB) as Max_DOB from student;

Output:

Source Code:
mysql> select count(DOB) as count_dob from student;
Output:

You might also like