Mysql Practical Programs
Mysql Practical Programs
STACK – DATASTRUCTURE - 2
Aim: To create a stack and implement push, pop, peek and display operations on the
elements of the stack.
Program:
def check_stack_isEmpty(stk):
if stk==[]:
return True
else:
return False
top = None # This is top pointer for push and pop operation
def main_menu():
while True:
print("Stack Implementation")
print("1 - Push")
print("2 - Pop")
print("3 - Peek")
print("4 - Display")
print("5 - Exit")
if ch==1:
push(s,el)
elif ch==2:
e=pop_stack(s)
if e=="UnderFlow":
print("Stack is underflow!")
else:
print("Element popped:",e)
elif ch==3:
print(peek(s))
elif ch==4:
print(s)
display(s)
elif ch==5:
break
else:
def push(stk,e):
stk.append(e)
top = len(stk)-1
def display(stk):
if check_stack_isEmpty(stk):
print("Stack is Empty")
else:
top = len(stk)-1
print(stk[top],"-Top")
for i in range(top-1,-1,-1):
print(stk[i])
def pop_stack(stk):
if check_stack_isEmpty(stk):
return "UnderFlow"
else:
e = stk.pop()
if len(stk)==0:
top = None
else:
top = len(stk)-1
return e
def peek(stk):
if check_stack_isEmpty(stk):
return "UnderFlow"
else:
top = len(stk)-1
return stk[top]
Output:
27. MYSQL – DDL , DML COMMANDS
Aim: Create an Employee Table with the fields Empno, Empname, Desig, Dept,
• Modify the table structure by adding one more field namely date of joining.
mysql> Insert into employee values(1221, 'Sidharth', 'Officer', 'Accounts', 45, 'Salem');
mysql> Insert into employee values(1222, 'Naveen', 'Manager', 'Admin', 32, 'Erode');
mysql> Insert into employee values(1224, 'Abinaya', 'Manager', 'Admin', 28, 'Anna
Nagar');
mysql> Insert into employee values(1225, 'Rahul', 'Officer', 'Accounts', 31, 'Anna
Nagar');
mysql> Insert into employee values(3226, 'Sona', 'Manager', 'Accounts', 42, 'Erode');
mysql> Insert into employee values(3227, 'Rekha', 'Officer', 'Admin', 34, 'Salem');
desc employee;
(vii) Inserting date of joining to each employee:
28. Create Student table with following fields and enter data as given in the
table below
Data to be entered
Consider the following MOVIE table and write the SQL queries based on it.
movies. Claculate the business done by movie using the sum of productioncost
and businesscost.
f) Display the list of movies which are going to release in February, 2022.
Answers:
30.
hundred.
Answers:
a) select pow(5,3);
b) select round(563.854741,-2);
c) select mid(“Computer”,4,3);
"Date";
e)
select right("Media",3);
h)select right
(businesscost,4)from movie;
desc team;
Inserting data:
Answers:
33 - Connectivity – 1
'AIM: Write a MySQL connectivity program in Python to Create a database 'alaya' and
drop the database. Create a table students with the specifications –
ROLLNO integer,
STNAME character(10)
Perform all the operations with reference to table ‘students’ through MySQL-Python
connectivity under the database smart.
Answers:
import pymysql as ms
def c_database():
try:
dn=input("Enter Database Name=")
c.execute("use {}".format(dn))
except Exception as a:
print("Database Error",a)
def d_database():
try:
except Exception as a:
def c_table():
try:
c.execute('use {}'.format('smart'))
except Exception as a:
def e_data():
try:
while True:
c.execute("use {}".format('smart'))
c.execute("insert into students values({},'{}');".format(rno,name))
db.commit()
if choice in "Nn":
break
except Exception as a:
def d_data():
try:
data=c.fetchall()
for i in data:
print(i)
except Exception as a:
db=ms.connect(host="localhost",user="root",password="password")
c=db.cursor()
while True:
if choice==1:
c_database()
elif choice==2:
d_database()
elif choice==3:
c_table()
elif choice==4:
e_data()
elif choice==5:
d_data()
elif choice==6:
print('Thank you!!!.....')
break
else:
OUTPUT:
34. MYSQL – CONECTIVITY – 2
Aim:
import pymysql as py
con=py.connect(host='localhost',user='root', passwd='password',database='school')
cur=con.cursor()
def main():
while True:
print('Menu....')
print('6. Quit')
ch=int(input('Enter Choice'))
if ch==1:
insertrec()
elif ch==2:
viewrec()
elif ch==3:
updaterec()
elif ch==4:
deleterec()
elif ch==5:
viewtables()
elif ch==6:
print('Thank you!!!...')
con.close()
break
def insertrec():
pid=input('Enter Phy_id')
pname=input('Enter name')
data=(pid,pname,ph,mid)
cur.execute(q,data)
con.commit()
def viewrec():
a=cur.fetchall()
for i in a:
print(i)
def updaterec():
%(mid,pid))
con.commit()
def deleterec():
con.commit()
def viewtables():
q='show tables'
cur.execute(q)
a=cur.fetchall()
for i in a:
print(i)