You are on page 1of 2

PYTHON- MYSQL CONNECTIVITY FOR JOINS

AIM
Create Employee table (Empno, Name, Desig, Deptno, Salary) and Department
table(Deptno,Dname, Location) with appropriate number of records. Execute a query to
display details of all Mangers like Empno, Name, Salary, Dname and Location.

import mysql.connector as ms
mydb=ms.connect(host="localhost",user="root",password="")
mc=mydb.cursor()
#mc.execute("create database emp;")
mc.execute("use emp;")
mc.execute("drop table employee;")
mc.execute("drop table department;")
mc.execute("create table employee(empno int not null primary key,name char(20),desig
char(20),deptno int not null,salary int);")
mc.execute("create table department(deptno int not null primary key,dname
char(20),location char(20));")
mc.execute("insert into employee values(101,'ram','accountant',501,50000);")
mc.execute("insert into employee values(102,'ravi','manager',502,60000);")
mc.execute("insert into employee values(103,'sundar','manager',503,65000);")
mc.execute("insert into employee values(104,'rakesh','salesman',504,30000);")
mc.execute("insert into employee values(105,'nitin','accountant',505,50000);")
mc.execute("insert into department values(501,'finance','bangalore');")
mc.execute("insert into department values(502,'admin','mumbai');")
mc.execute("insert into department values(503,'mkt','bangalore');")
mc.execute("insert into department values(504,'sales','chennai');")
mc.execute("insert into department values(505,'finance','bangalore');")
mydb.commit()
print("The details of employee and department")
mc.execute("select*from employee,department where
employee.deptno=department.deptno;")
#print(mc.fetchall())
for k in mc:
print(k)
print("Details of manager")
mc.execute("select empno,salary,dname,location from employee,department where
employee.deptno=department.deptno and desig='manager';")
for k in mc:
print(k)
OUTPUT

The details of employee and department


(101, 'ram', 'accountant', 501, 50000, 501, 'finance', 'bangalore')
(102, 'ravi', 'manager', 502, 60000, 502, 'admin', 'mumbai')
(103, 'sundar', 'manager', 503, 65000, 503, 'mkt', 'bangalore')
(104, 'rakesh', 'salesman', 504, 30000, 504, 'sales', 'chennai')
(105, 'nitin', 'accountant', 505, 50000, 505, 'finance', 'bangalore')
Details of manager
(102, 60000, 'admin', 'mumbai')
(103, 65000, 'mkt', 'bangalore')
>>>

You might also like