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

XII Practical File (SQL)

The document contains a series of MySQL exercises aimed at creating and manipulating various database tables, including student, employee, stock, and book tables. It provides SQL commands for creating databases, inserting data, querying records, and performing updates and deletions. Additionally, it includes Python scripts for establishing MySQL connectivity and executing queries from a Python environment.

Uploaded by

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

XII Practical File (SQL)

The document contains a series of MySQL exercises aimed at creating and manipulating various database tables, including student, employee, stock, and book tables. It provides SQL commands for creating databases, inserting data, querying records, and performing updates and deletions. Additionally, it includes Python scripts for establishing MySQL connectivity and executing queries from a Python environment.

Uploaded by

A.S.Vishwaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

MYSQL EXERCISE # 1

Aim:- To create a student table and insert data using MySQL.

SQL Command: -

1.To create and open a new database named school;


mysql> create database school;
mysql> use school;

2.Write an SQL command to create a STUDENT TABLE with the following attributes and
fields.

TABLE: STUDENT

Field Name Data type


Stud_id Int Primary Key
Stud_name Varchar(20)
Class Char(2)
Sec Char(2)
stream Varchar(10)

mysql> Create table student (stud_id int primary key, stud_name varchar(20), class
char(2), sec char(2),stream varchar(10));
3.Write an SQL command to display the structure of the table named student.
mysql> Desc student; (or) Describe student;
OUTPUT:

4. Write an SQL command to insert the data.

mysql> insert into student values (1, “Ajay”, “12”, “A”,” Science”);
mysql> insert into student values (2, ”Siva”,”12”,”B”,”Science”);

5. Write a query to display all the records from student table.

mysql> Select * from STUDENT;

1
OUTPUT:

6.Write a query to display all the records order by section.


mysql> Select * from student order by sec;

OUTPUT:

7. Write an SQL command to add a new field MARK to the table.


mysql> Alter table student add Mark float;
8. Write an SQL command to update the marks.
mysql> update student set Mark=100 where stud_id=1;

9. Write an SQL query to display minimum mark group by Stream.

mysql> Select Stream, min(mark) as “Minimum_Mark” from student group by Stream;


OUTPUT:-

10. Write a query to delete the all data.

mysql> Delete from student;

2
MYSQL EXERCISE # 2
Aim: - To create an employee table and execute the following queries.
Table: Employee

Table: Department

1.Write an SQL query to fetch unique values of city from Employee table.
mysql> Select DISTINCT city from Employee;
OUTPUT:

2. Write an SQL query to print all Employee details from the Employee table order by
Employee Name Ascending.
mysql> Select * from Employee order by emp_name;
OUTPUT:

3
3. Write an SQL query to print details for department name with the dept_name as “HR”
and “Admin” from department table.
mysql> Select * from department where dept_name in (“HR”,”Admin”);
OUTPUT:

4. Write an SQL query to print details of Employee with city as “Madurai”.


mysql> Select * from Employee where city=”Madurai”;
OUTPUT:

5. Write an SQL query to print details of the Employee whose Name ends with “a”.
mysql> Select emp_name from employee where emp_name like ‘%a’;
OUTPUT:

6. Write an SQL query to print details of workers whose Name contains four characters.
mysql> Select * from Employee where emp_name like ‘____’;
OUTPUT:

4
7. Write an SQL query to print details of Employee whose salary lies between 45000 and
50000.
mysql> Select * from employee where salary between 45000 and 50000;
OUTPUT:

8. Write an SQL query to print details of Employee name and their department name.
mysql> Select emp_name, dept_name from employee e, department d where e.emp_id =
d.emp_id;
OUTPUT:

5
MYSQL EXERCISE # 3
Aim: - To create a stock table and execute the following queries.
Table: STOCK

1.Compute the number of products with a price larger than or equal to 20.
mysql> Select count(*) from stock where price>=20;
OUTPUT:

2. Select the name and price of all products with a price is less than 50 and sort price by
descending order;
mysql> select stock_name, price from stock where price<50 order by price desc;
OUTPUT:

3. Select the average price of each product date wise.


mysql> select dob,avg(price) from stock group by dob;
OUTPUT:

6
4. Apply a 5% discount to all products with a price larger than 25;
mysql> select stock_name, price, price-price*5/100 as 'Discount price' from stock where
price>25;
OUTPUT:

5. update the name of stock pen to “Ink Pen”


mysql> update stock set stock_name="Ink Pen" where stock_name="Pen";
OUTPUT:

6. Select the stock name and price of the cheapest product.


mysql> select stock_name, price from stock where price=(select min(price) from stock);
OUTPUT:

7. To display the details of all products whose stock is purchased January month.
mysql> select * from stock where dob>="2024-01-01" and dob<="2024-01-31";
OUTPUT:

8. Write an SQL query to delete the record whose stock price is 10.
mysql> delete from stock where price=10;

7
MYSQL EXERCISE # 4
Aim: - To create a book table and execute the following queries.
Table: BOOK

Table: AUTHOR

1.Write an SQL query to display the details of Math book.


mysql> select * from book where book_name="math";
OUTPUT:

2. Write an SQL query to display the details of book name and author name of English
book.
mysql> select book_name, author_name from book b, author a where
b.book_id=a.book_id and book_name="eng";
OUTPUT:

8
3. Write an SQL query to add a primary key of book table.
mysql> alter table book add primary key (book_id);
OUTPUT:

4. Write an SQL query to display the book name and author name whose price is more
than 250 and quantity in not null;
mysql> select book_name, author_name from book b, author a where
b.book_id=a.book_id and price>250 and qty is not null;
OUTPUT:

5. Write an SQL query to display the sum of all price from book table.
mysql> select sum(price) from book;
OUTPUT:

6. Write an SQL query to display the details of book whose price is null;
mysql> select * from book where price is null;
OUTPUT:

9
7. Write an SQL query to display the total price without repetition and null.
mysql> select count(distinct price) from book;

8. Write an SQL query to delete the all records in author table.


mysql> delete from author;
mysql> Select * from author;
OUTPUT:
Empty set (0.00 sec)
9. Write an SQL query to delete the author table permanently.
mysql> drop table author;
mysql> desc author;
OUTPUT:
ERROR 1146 (42S02): Table 'books.author' doesn't exist
10. Write an SQL query to delete the books database permanently.
mysql> drop database books;
mysql> use books;
OUTPUT:
ERROR 1049 (42000): Unknown database 'books'

10
PYTHON – MySQL Connectivity#1

Aim: - To write a python script to establish Python MySQL connectivity.


SOURCE CODE:

import mysql.connector as mysql


def connect_with_mysql():
try:
con=mysql.connect(host="localhost", user="root", password="Vvsn@
123",database="stud")
if con.is_connected():
db_info=con.get_server_info()
print("=====Python- MySQL integration===========")
print()
print("Connection established to MySQLversion",db_info)
except:
print("Connection error")
finally:
if con.is_connected():
con.close()
print("Connection closed")
print("======================================")

connect_with_mysql()

OUTPUT: -

11
PYTHON – MySQL Connectivity#2

Aim: - Write a program to display all record from table‘student’ using MySQL
database connectivity in python.
Source Code: -
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="Vvsn@123"
,database="student")
mycursor=mydb.cursor()
mycursor.execute("Select * from classA")
mydata=mycursor.fetchall()
for i in mydata:
print(i)
print("The total number of rows are fetched:", mycursor.rowcount)

Output:-
(101,'Ajay', (2000, 12, 22),'Anna Nagar', 'Madurai')

(102,'Siva', (2001, 10, 20),'Rajai Road', 'Chennai')

(103, 'Sanjay', (2000, 9, 2), 'JJ Colony', 'Covai')

(104, 'Bilal', (2002, 10, 22), 'KK Nagar', 'Trichy')

(105, 'Saran', (2000, 8, 5), 'SS Nagar', 'Chennai')

The total numbers of rows are fetched: 5

12
PYTHON – MySQL Connectivity#3

Aim: - Write a program to display one record from table‘student’ using MySQL
database connectivity in python.
Source Code:-
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="Vvsn@123"
,database="student")
mycursor=mydb.cursor()
mycursor.execute("Select * from classA")
mydata=mycursor.fetchone()
print(mydata)
print("The total number of rows are fetched:", mycursor.rowcount)

Output:-

(101, 'Ajay', (2000, 12, 22), 'Anna Nagar', 'Madurai')

The total numbers of rows are fetched: 1

13
PYTHON – MySQL Connectivity#4

Aim: - Write a program to display number of record from table ‘student’ using
MySQL database connectivity in python.
Source Code: -
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="Vvsn@123"
,database="student") mycursor=mydb.cursor()
mycursor.execute("Select * from classA")
mydata=mycursor.fetchmany(3)
for i in mydata:
print(i)
print("The total number of rows are fetched:", mycursor.rowcount)

Output:-
(101,'Ajay', (2000, 12, 22),'Anna Nagar', 'Madurai')
(102,'Siva', (2001, 10, 20),'Rajaji Road', 'Chennai')
(103,'Sanjay', (2000, 9, 2),'JJ Colony', 'Covai')
The total numbers of rows are fetched:3

14
PYTHON - MySQL Connectivity#5

Aim: - Write a program to create a new relation ‘std_details’ using MySQL


database connectivity in python.
Source Code: -

import mysql.connector

mydb=mysql.connector.connect(host="localhost",user="root",password="Vvsn
@123",database="school")
mycursor=mydb.cursor()

mycursor.execute("create table std_details(sid int primary key,sname


varchar(20),age int, city varchar(20))")
Output: -

15

You might also like