0% found this document useful (0 votes)
12 views11 pages

Practical Record

The question asks to write SQL queries to list students by computer science stream, display unique stream names, and sort records by name. It also asks for the output of queries to count
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Practical Record

The question asks to write SQL queries to list students by computer science stream, display unique stream names, and sort records by name. It also asks for the output of queries to count
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

REG NO:20652614

NAME:Hariharan.S

Q1.
Source code

import mysql.connector

def input_values():
L=[]
empno=int(input("Enter the employee number:"))
ename=input("Enter the name of the employee :")
dep=input("Enter the department id of the employee :")
L.append(empno)
L.append(ename)
L.append(dep)
emp=(L)
sql="insert into employee (empno,ename,deptid) values (%s,%s,%s)"
cur.execute(sql,emp)
c.commit()

def view_values():
print("Select the search criteria : ")
print("1. Emp no")
print("2. Name")
print("3. Department")
print("4. Display all records")
ch=int(input("Enter the choice : "))
if ch==1:
s=int(input("Enter employee no : "))
rl=(s,)
sql="select * from employee where empno=%s"
cur.execute(sql,rl)
elif ch==2:
s=input("Enter Name : ")
rl=(s,)
sql="select * from employee where ename=%s"
cur.execute(sql,rl)
elif ch==3:
s=int(input("Enter department id : "))
rl=(s,)
sql="select * from employee where deptid=%s"
cur.execute(sql,rl)
elif ch==4:
sql="select * from employee"
cur.execute(sql)
else:
print("Invalid option")
res=cur.fetchall()
print("The employee details are as follows : ")
print("(Empno, Name, deptid)")
for x in res:
print(x)
def MenuSet():
print("Enter 1 : To Add employee")
print("Enter 2 : To View employee details")

userInput = int(input("Please Select An Above Option: "))


if(userInput == 1):
input_values()
elif(userInput == 2):
view_values()
else:
print("Invalid operation")

p=[]
c=mysql.connector.connect(host="localhost",user="root",passwd="Tvsahosur$123")
cur=c.cursor()
db=input("Enter the name of the database :")
p.append(db)
st="create database if not exists "+db
cur.execute(st)

if c.is_connected():
print("Database connected successfully")

st="use "+db
cur.execute(st)
st="create table IF NOT EXISTS employee(empno int,ename varchar(20),deptid int)"
cur.execute(st)
print("employee table created successfully")
print()

ch='y'
while(ch=='y'):
MenuSet()
print()
ch=input("Do you want to continue?y/n")
if ch.lower()=='y':
continue
else:
break
OUTPUT:
Enter the name of the database :employee
Database connected successfully
employee table created successfully

Enter 1 : To Add employee


Enter 2 : To View employee details
Please Select An Above Option: 1
Enter the employee number:200412
Enter the name of the employee :dhevanth
Enter the department id of the employee :20652615

Do you want to continue?y/ny


Enter 1 : To Add employee
Enter 2 : To View employee details
Please Select An Above Option: 2
Select the search criteria :
1. Emp no
2. Name
3. Department
4. Display all records
Enter the choice : 4
The employee details are as follows :
(Empno, Name, deptid)
(200411, 'Hariharan', 20652614)
(200412, 'dhevanth', 20652615)

Do you want to continue?y/ny


Enter 1 : To Add employee
Enter 2 : To View employee details
Please Select An Above Option: 2
Select the search criteria :
1. Emp no
2. Name
3. Department
4. Display all records
Enter the choice : 1
Enter employee no : 200412
The employee details are as follows :
(Empno, Name, deptid)
(200412, 'dhevanth', 20652615)

Do you want to continue?y/nn


Q2.
Write queries for 1) to 3) and write the output for 4) and 5)

#a) List the name of all students who have taken stream as COMPUTER SCIENCE
select * from student where stream='Computer Science';

#b)To Display the unique STREAM names


select distinct(stream) from student;

#c)To display all the records in sorted order by name


select * from student order by name;
#d)SELECT STREAM,COUNT(*) FROM STUDENT GROUP BY STREAM

#e)SELECT NAME,STREAM FROM STUDENT WHERE NAME LIKE 'D%'

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy