Relationship between Two Tables:
Database is collection of one or more table which are related to each other. To illustrate the relating two
tables, I consider my3 database which already exist and contains employee table. In the same database
create project table which stores project details. Project table contain three field pid, name and hours.
Pid is primary key. Below python code is used to create project table and insert a records into a table.
#Following Python program will be used to create a table project in my3 database in which already
#employee table exists and insert records into project table
import sqlite3
conn = sqlite3.connect('c:\sqlite3\my3.db')
print ("Opened database successfully")
#create project table
conn.execute('''CREATE TABLE project
(PID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
HOURS REAL NOT NULL
# );''')
print( "Table created successfully")
#insert a records into project table
conn.execute("insert into project (PID,NAME,HOURS) values (888,'Database Application',30)")
conn.execute("insert into project (PID,NAME,HOURS) values (999,'Mobile Application',25)")
conn.execute("insert into project (PID,NAME,HOURS) values (777,'Big data Application',10)")
conn.commit()
print( "records are inserted successfully")
conn.close()
output:
Table created successfully
Records inserted successfully
Type the below at command prompt:
C:\>cd sqlite3
C:\sqlite3>sqlite3 my3.db
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> .schema project
CREATE TABLE project
(PID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
HOURS REAL NOT NULL
);
sqlite> select * from project;
888|Database Application|30.0
999|Mobile Application|25.0
777|Big data Application|10.0
Now create assign table which relates employee and project table. Employees are assigned to project.
Below code create a assign table in my3 database with pid1 and empid attributes. Pid1 is foreign key
refers to project table and empid is foreign key refers to employee table primary key.
#following Python program will be used to create a table assign in my3 database in which already
#employee and project table exists
import sqlite3
conn = sqlite3.connect('c:\sqlite3\my3.db')
print ("Opened database successfully")
#create assignment table in which employee is assigned to particular project
#in assignment table empid and pid both are foreign keys referes to employee and project table
conn.execute("create table assign(pid1 int references project, empid int references employee)")
print( "Table created successfully")
conn.close()
output:
opened database successfully
Table created successfully
Type the following at command prompt
sqlite> .schema assign
CREATE TABLE assign(pid1 int references project, empid int references employee);
Next insert records into assign table. Records must be inserted without violating foreign key constraint.
i.e pid1 and empid in assign table must exist in project and employee respectively. To ensure this first
retrieve all empid and pids from both table and store it in list. When user input empid and pid for
inserting into a assign table check are they in list if yes then insert a record into a table. Below is user to
insert records into assign table.
#Following Python program insert records into assign table
import sqlite3
conn = sqlite3.connect('c:\sqlite3\my3.db')
#inserting records into assign table
r=conn.execute("select empid from employee")
r1=conn.execute("select pid from project")
le=[]
lp=[]
for k in r:
le.append(k[0])
for j in r1:
lp.append(j[0])
print("Existing employee ids in employee table are")
print(le)
print("Existing project ids in project table are")
print(lp)
k1=1
while k1==1:
print("input empid and project id to be inseryted in a table")
e,p=input().split()
e,p=int(e),int(p)
if e in le and p in lp:
conn.execute("insert into assign(pid1,empid)values(?,?)",(p,e))
conn.commit()
print("record inserted sucessfully")
else:
print("can not insert a record")
print("do u want to continue press 1 continue/0 for exit")
k1=input()
conn.close()
output:
Existing employee ids in employee table are
[1, 5, 9, 38, 63, 78, 123, 576, 678, 777, 900, 11111]
Existing project ids in project table are
[777, 888, 999]
input empid and project id to be inseryted in a table
9 888
record inserted sucessfully
do u want to continue press 1 continue/0 for exit
Type at command prompt
sqlite> select * from assign;
999|38
777|63
888|576
999|900
888|9
We retrieve records from these three using natural join condition. In below python program first query
is used to retrieve name of employee assigned to a project and in second one is used to retrieve project
names assigned to a employee.
#join queries
import sqlite3
conn = sqlite3.connect('c:\sqlite3\my3.db')
r=conn.execute("select ename from employee,assign where assign.empid=employee.empid")
print("Employee names who are working on projects")
for j in r:
print(j)
r1=conn.execute("select name from project,assign where project.pid=assign.pid1")
print("project names which are assigned to employee")
for k in r1:
print(k)
conn.close()
output:
Employee names who are working on projects
('KAVERI',)
('shivu',)
('gfh',)
('RAGHU',)
('GAYATHRI',)
project names which are assigned to employee
('Mobile Application',)
('Big data Application',)
('Database Application',)
('Mobile Application',)
('Database Application',)