You are on page 1of 12

MYSQL

QUESTION:
Database: School

SET 1
Q1.Write the query to create the above table and set the constraints for the attributes.
EXAMNO-PRIMARY KEY, NAME- NOT NULL ,MARK CHECK-MARK SHOULD NOT EXCEED 500.
Query:
CREATE TABLE STUDENTS(
EXAMNO INT PRIMARY KEY,
NAME CHAR(20) NOT NULL,
CLASS VARCHAR(3),
SEC VARCHAR(3),
MARK INT CHECK(MARK<=500));
OUTPUT:

Q2: Write the query to insert the mentioned records into the table.
Query:
INSERT INTO STUDENTS VALUES (1201,'PAVINDHAN','XII','A1',489);
INSERT INTO STUDENTS VALUES (1202,'ESHWAR','XII','A1',465);
INSERT INTO STUDENTS VALUES (1203,'BHARKAVI','XII','A2',498);
INSERT INTO STUDENTS VALUES (1204,'HARIPRIYA','XII','A2',400);
INSERT INTO STUDENTS VALUES(1205,'JAIPRADHAP','XII',NULL,398);
INSERT INTO STUDENTS VALUES(1206,'KAVIPRIYA','XII','A2',NULL);

Q3.Write the query to display all the student records who is


studying in A1 SECTION.
QUERY:
SELECT * FROM STUDENTS WHERE SEC='A1';
Q4.Write the query to display the records whose mark is not assigned.
QUERY:
SELECT * FROM STUDENTS WHERE MARK IS NULL;
Q5.Write the query to display whose marks in the range 400 to 450 (both values are inclusive).
QUERY:
SELECT * FROM STUDENTS WHERE MARK BETWEEN 400 AND 450;
OUTPUT:

SET 2. .
Q1.Write the query to display the student name,marks who secured more than 400 and less than 487.
QUERY:
SELECT NAME,MARK FROM STUDENTS WHERE MARK>400 AND MARK<487;

Q2.Write the query to display the details whose name is starts with ‘P’ or ‘B’.
QUERY:
SELECT * FROM STUDENTS WHERE NAME LIKE 'P%' OR NAME LIKE 'B%';

Q3. Write the query to display the student name and section whose name contain ‘priya’.
QUERY:
SELECT NAME,SEC FROM STUDENTS WHERE NAME LIKE '%PRIYA%';

Q4. Write the query to display all the details sort by name in descending order.
QUERY:
SELECT * FROM STUDENTS ORDER BY NAME DESC;

Q5.Write the query to add 10 marks with the existing marks as ‘Updated marks’ and display the details.
QUERY:
SELECT EXAMNO,NAME,CLASS,SEC,MARK+10 AS 'Updated marks' FROM STUDENTS;

OUTPUT:

SET 3
Q1. Write the query to add a new column named as CITY with the data type VARCHAR(30) and apply the default constraint
‘NOT MENTIONED’ in the students table.
QUERY:
ALTER TABLE STUDENTS ADD COLUMN CITY VARCHAR(30) DEFAULT ("NOT MENTIONED");

Q2.Write the query to change the order of the column in the students table as : EXAMNO,NAME,CITY,CLASS,SEC,MARK
QUERY:
ALTER TABLE STUDENTS MODIFY CITY VARCHAR(30) AFTER NAME;

Q3.Write the query to redefine the NAME field size into VARCHAR(40) in the students table.
QUERY:
ALTER TABLE STUDENTS MODIFY NAME VARCHAR(40);
Q4.Write the query to update the mark as 350 whose marks is null and update the section is A3 whose section is null.
QUERY:
UPDATE STUDENTS SET MARK=350 WHERE MARK IS NULL;
UPDATE STUDENTS SET SEC="A3" WHERE SEC IS NULL;
Q5. Write the query to update the city of all records with the following cities [CHENNAI,BENGALURU]
QUERY:
UPDATE STUDENTS SET CITY='CHENNAI' WHERE EXAMNO IN (1201,1202,1203);
UPDATE STUDENTS SET CITY='BENGALURU' WHERE EXAMNO IN (1204,1205,1206) ;
OUTPUT:

SET 4
DATABASE: HOSPITAL
Q1.Write the query to create DOCTOR TABLE and the PATIENT TABLE and keep DOCID in patient table to be the foreign key
with update and delete cascade.
QUERY:
CREATE DATABASE HOSPITAL;

USE HOSPITAL;

CREATE TABLE DOCTOR(


DOCID VARCHAR(3) PRIMARY KEY,
DNAME CHAR(20),
DEPT CHAR(25),
CITY CHAR(15),
SALARY INT);

INSERT INTO DOCTOR VALUES(‘D01’,’ASHWATHAN’,’ONCOLOGY’,’CHENNAI’,150000);


INSERT INTO DOCTOR VALUES(‘D02’,’RAMYA’,’GYNAECOLOGY’,’COIMBATORE’,140000);
INSERT INTO DOCTOR VALUES(‘D03’,’SRIRAM’,’DENTAL’,’BHOPAL’,180000);
INSERT INTO DOCTOR VALUES(‘D04’,’SANJAY’,’CARDIOLOGY’,’HYDERABAD’,160000);
INSERT INTO DOCTOR VALUES(‘D05’,’SRINITHI’,’PEDIATRICS’,’DELHI’,120000);
INSERT INTO DOCTOR VALUES(‘D06’,’SANTHOSH’,’PEDIATICS’,’BENGALURU’,140000);
INSERT INTO DOCTOR VALUES(‘D07’,’KARTHICK’,’CARDIOLOGY’,’JAIPUR’,180000);
INSERT INTO DOCTOR VALUES(‘D08’,’YASHIK’,’GYNAECOLOGY’,’AHMEDABAD’,195000);

CREATE TABLE PATIENT(


PATID VARCHAR(3) PRIMARY KEY,
PNAME VARCHAR(20),
APMDATE VARCHAR(12),
GENDER VARCHAR(7),
AGE INT,
DOCID VARCHAR(3),
FOREIGN KEY(DOCID) REFERENCES DOCTOR (DOCID) ON UPDATE CASCADE ON DELETE CASCADE);

INSERT INTO PATIENT VALUES(‘P01’,’SATHISH’,’2022/08/19’,’MALE’,45,’D01’);


INSERT INTO PATIENT VALUES(‘P02’,’SUPRIYA’,’2022/09/21’,’FEMALE’,23,’D02’);
INSERT INTO PATIENT VALUES(‘P03’,’ARUNA’,’2022/10/20’,’ FEMALE’,43,’D08);
INSERT INTO PATIENT VALUES(‘P04’,’RAMESH’,’2022/08/25’,’MALE’,84,’D04’);
INSERT INTO PATIENT VALUES(‘P05’,’KUMAR’,’2022/09/23’,’MALE’,12,’D02’);
INSERT INTO PATIENT VALUES(‘P06’,’PRIYA’,’2022/09/14’,’FEMALE’,10,’D06’);
INSERT INTO PATIENT VALUES(‘P07’,’ROOPA’,’2022/08/13’,’FEMALE’,66,’D03’);
INSERT INTO PATIENT VALUES(‘P08’,’CHARLES’,’2022/10/12’,’MALE’,24,’D07’);

Q2.Write the query to display the count of male and female patients in the age between 40 and 50.
QUERY:
SELECT GENDER,COUNT(GENDER) FROM PATIENT WHERE AGE BETWEEN 40 AND 50 GROUP BY GENDER;

Q3.Write the query to display the patient id,name and age who fixed the appointment on September month.
QUERY:
SELECT PATID,PNAME,AGE FROM PATIENT WHERE MONTH(APMDATE)=09;

Q4.Write the query to display the number of doctors in each dept.


QUERY:
SELECT DEPT,COUNT(DOCID) FROM DOCTOR GROUP BY DEPT;
Q5.write the query to display the sum of the salary of the doctors department wise.
QUERY:
SELECT DEPT,SUM(SALARY) FROM DOCTOR GROUP BY DEPT;

OUTPUT:
SET 5

Q1. Write the query to display patient name,doctor name,patient age and their appointment date from the tables.
QUERY:
SELECT PNAME,DNAME,AGE,APMDATE FROM DOCTOR,PATIENT WHERE DOCTOR.DOCID=PATIENT.DOCID;

Q2.Write the query to display the doctor id,doctor name and number of patients need to visit.
QUERY:
SELECT D.DOCID,DNAME,COUNT(P.DOCID) AS 'NO.PATIENT' FROM DOCTOR D,PATIENT P WHERE D.DOCID=P.DOCID GROUP BY
P.DOCID;

Q3.Write the query to display the average salary of each dept from doctor table.
QUERY:
SELECT DEPT,TRUNCATE(AVG(SALARY),2) AS 'AVERAGE SALARY' FROM DOCTOR GROUP BY DEPT;

Q4. Write the query to display the maximum and minimum salary of each department.
QUERY:
SELECT DEPT,MAX(SALARY),MIN(SALARY) FROM DOCTOR GROUP BY DEPT;

Q5.Write the query to delete record of the doctor id ‘D08’ and ‘D06’ from the table .
QUERY:
DELETE FROM DOCTOR WHERE DOCID IN('D06','D08');
OUTPUT:
PYTHON – MYSQL CONNECTIVITY
1. Write a python program to create the table and insert the records into the table by getting the data from the user and to
store in the MySQL database.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT
ATTRIBUTES : EXAMNO,NAME,CLASS,SECTION,MARK
AIM:
To write a python program to create the table and insert the records into the table by getting the data from the user
and to store in the MySQL database.
PROGRAM:
import mysql.connector as mycon
a=mycon.connect(host='localhost',user='root',password='12345',database='MYSCHOOL')
if a.is_connected():
print("Database connected")
else:
print("Database not connected")
b=a.cursor()
q1="drop table if exists student"
b.execute(q1)
q="create table student(EXAMNO INT NOT NULL PRIMARY KEY,NAME VARCHAR(20),CLASS INT,SECTION VARCHAR(2),MARK
INT)"
b.execute(q)
ans='y'
while ans.lower()=='y':
ex=int(input("ENTER EXAMNO:"))
na=input("ENTER NAME:")
cl=int(input("ENTER CLASS:"))
s=input("ENTER SECTION:")
m=int(input("ENTER MARK:"))
q2="insert into student values({},'{}',{},'{}',{})".format(ex,na,cl,s,m)
b.execute(q2)
print("Record successfully stored in the student table")
ans=input("Do you want to add another record(y/n):")
a.commit()
a.close()
OUTPUT:
RESULT:
The above given program is executed successfully and the output is shown.

2. Write a python program to search the records from the table based on the examno and display the details of the student.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT
AIM:
To write a python program to search the records from the table based on the examno and display the details of the student.
PROGRAM:
import mysql.connector as mycon
fh=mycon.connect(host='localhost',user='root',password='12345',database='MYSCHOOL')
if fh.is_connected():
print("Database connected")
else:
print("Database not connected")

ans='y'
while ans.lower()=='y':
a=fh.cursor()
r=int(input("ENTER THE EXAMNO YOU WANT TO SEARCH:"))
q1="select * from student where examno={}".format(r)
a.execute(q1)
rec=a.fetchall()
cnt=a.rowcount
if cnt!=0:
for i in rec:
print('EXAM NO:',i[0])
print('NAME :',i[1])
print('CLASS :',i[2])
print('SECTION :',i[3])
print('MARKS :',i[4])
else:
print('RECORD NOT FOUND')
ans=input('DO YOU WANT TO SEARCH ANOTHER RECORD (Y/N):')
fh.close()
OUTPUT:

RESULT:
The above given program is executed successfully and the output is shown.

3. Write a program to Integrate SQL with Python by importing the MySQL module to search a student using exam no update
the mark.
AIM:
To write a program to Integrate SQL with Python by importing the MySQL module to search a student using exam no update
the mark.
PROGRAM:
import mysql.connector as mycon
fh=mycon.connect(host='localhost',user='root',password='12345',database='MYSCHOOL')
if fh.is_connected():
print("Database connected")
else:
print("Database not connected")
a=fh.cursor()
examno=int(input("Enter exam number:"))
mark=int(input("Enter new mark:"))

print("Before updating record:")


q1="select * from student where examno={}".format(examno)
a.execute(q1)
record=a.fetchone()
print(record)

q2="update student set mark={} where examno={}".format(mark,examno)


a.execute(q2)
fh.commit()
record=a.fetchone()

print("After updating record:")


q3="select * from student where examno={}".format(examno)
a.execute(q3)
record=a.fetchone()
print(record)

fh.close()
OUTPUT
RESULT:
The above given program is executed successfully and the output is shown.

4. Write a python program to delete the particular record from the table based on the examno given by the user . If record
not found display the appropriate message.
DATABASE : MYSCHOOL
TABLE NAME : STUDENT
AIM:
To write a python program to delete the particular record from the table based on the examno given by the user and if record
not found display the appropriate message.
PROGRAM:
import mysql.connector as mycon
fh=mycon.connect(host='localhost',user='root',password='12345',database='MYSCHOOL')
if fh.is_connected():
print("Database connected")
else:
print("Database not connected")
ans='y'
while ans.lower()=='y':
a=fh.cursor()
r=int(input("ENTER THE EXAMNO YOU WANT TO DELETE:"))
q1="select * from student where examno={}".format(r)
a.execute(q1)
rec=a.fetchall()
cnt=a.rowcount
if cnt!=0:
q2="DELETE FROM STUDENT WHERE EXAMNO={}".format(r)
a.execute(q2)
fh.commit()
print('RECORD DELETED SUCCESSFULLY')
else:
print('RECORD NOT FOUND')
ans=input('DO YOU WANT TO DELETE ANOTHER RECORD (Y/N):')
fh.close()
OUTPUT:

RESULT:
The above given program is executed successfully and the output is shown.

You might also like