Kunal
Kunal
EXAMINATION 2025
PRACTICAL RECORD
SUB:COMPUTER SCIENCE
[083]
1
TABLE OF CONTENTS
[Link] Program Description Pg Remarks
no.
1 Program to accept inputs from 4
user and handle the possible
exceptions using
try...except...else...finally
construct
2 Demonstration of built in
exceptions in python
3 Demonstration of creations and
display of text file in python-
Program1
4 Creating a text file and displaying
the number of words present in it
5 Display the numbers of uppercase
letters,lowercase letters and digits
present in a text file
6 Program to replace specified text
in text file
7 Demonstration of seek() and tell()
functions
8 Csv file management-Program 1
2
14 Binary Management program-2
16 Stack operations-2:PUSH
function
17 Checking for underflow error
18 Stack operations-3:PEEK
function
19
20 DemonstartionofDDLcommands-
CREATE,ALTER,DROP
21 Demonstartion of DML
commands:INSERT DELETE
UPDATE
22 Demonstration of DQL-SELECT
23 Demonstration of MySQL
operations-
UNION,INTERSECT,CARTESI
ANPRODUCT,EQUIJOIN&NAT
URAL JOIN
3
'''Practical 1:Program to accept inputs from user and handle the possible
exceptions using try...except...else...finally construct
====================================================='''
try:
x=int(input('Enter the numerator:'))
y=int(input('Enter the denominator:'))
result=x/y
print('The the quotient of the operation:',result)
except(ValueError,ZeroDivisionError):
print('Input should be numbers...')
print('Denominator should not be zero...')
else:
print('The program runs successfully...')
finally:
print('The program is over...')
OUTPUT:
Enter the numerator:25
Enter the denominator:3
The the quotient of the operation: 8.333333333333334
The program runs successfully...
The program is over...
4
The program is over...
try:
x=int(input('Enter the numerator:'))
y=int(input('Enter the denominator:'))
result=x/y
print('The the quotient of the operation:',result)
except(ValueError):
print('Input should be numbers...')
except(ZeroDivisionError):
print('Denominator should not be zero...')
else:
print('The program runs successfully...')
finally:
print('The program is over...')
---------------------------------------------------------------------------------------------
OUTPUT
Enter the numerator:-10.
Input should be numbers...
The program is over...
---------------------------------------------------------------------------------------------
Enter the numerator:10
Enter the denominator:0
Denominator should not be zero...
The program is over...
5
'''Practical-2:Demonstration of exception handling in python
=========================================='''
nameTuple=('india','japan','canada')
print(nameTuple[3])
6
def f1(x):
print(x*x)
f1()
Traceback (most recent call last):
File "C:/Users/acer/Desktop/[Link]", line 3, in <module>
f1()
#TypeError: f1() missing 1 required positional argument: 'x'
---------------------------------------------------------------------------------------------
def f1():
print('Done')
F1()
7
Traceback (most recent call last):
File "C:/Users/acer/Desktop/[Link]", line 2, in <module>
[Link]()
#AttributeError: 'tuple' object has no attribute 'sort'
x={'india':'delhi','japan':'tokyo'}
print(x['India'])
8
'''Program3:Program to create a text file and display its [Link] file
should be decided by the user at run time
====================================================='''
data=[]
ans='yes'
with open('[Link]','a')as F1:
while True:
line=input('enter the line:')
[Link](line)
[Link]('\n')
ans=input('do you want more lines')
if [Link]() in['no','n']:
break
[Link](data)
OUTPUT:
enter the line:when program contains error the python interpreter will stop
the program abrubtly
do you want more linesy
enter the line:exception handling make sure that any possible error is handled
properly by the program during its execution
do you want more lines no
--------------------------------------------------------------------
when program contains error the python interpreter will stop the program
abrubtly
exception handling make sure that any possible error is handled properly
9
'''Program-4:Program to display the number of words present in
the created file'''
================================================
with open('[Link]','r')as o:
a=[Link]()
for i in a:
words=[Link]()
print(len(words))
OUTPUT
12
11
10
'''Program-5:program to display the number of uppercase lowercase and
digits present in a text file'''
======================================================
lower=0
upper=0
numeric=0
with open('[Link]','r')as o:
a=[Link]()
for i in a:
if([Link]()):
lower+=1
elif([Link]()):
upper+=1
elif [Link]():
numeric+=1
print('the number of lower characters:',lower)
print('the number of upper characters:',upper)
print('the number of numeric characters:',numeric)
OUTPUT
the number of lower characters: 20
the number of upper characters: 0
the number of numeric characters: 0
11
'''Program-6:Program to replace specified text in text file
====================================================='''
OUTPUT
python is an open source
python is based on HLL
python is portable
java is an open source
java is based on HLL
java is portable
12
'''Program-7: to demonstarte seek() and tell() functions within a text file-
both in text and binary mode
======================================================'''
with open('[Link]','r') as F:
print('The postion of the file pointer at opening is',[Link]())
OUTPUT
The postion of the file pointer at opening is 0
with open('[Link]','r') as F:
data=[Link]()
print('The postion of the file pointer after reading is',[Link]())
OUTPUT
The postion of the file pointer after reading is 32
with open('[Link]','r') as F:
data=[Link](10)
print('The postion of the file pointer is',[Link]())
data=[Link]()
print('Display after using seek(10) function:')
print(data)
OUTPUT
The postion of the file pointer is 10
Display after using seek(10) function:
two
13
one
two
three
with open('[Link]','r') as F:
data=[Link](10,2)
print('The postion of the file pointer is',[Link]())
OUTPUT
Traceback (most recent call last):
File "C:/Users/acer/Desktop/[Link]", line 5, in <module>
data=[Link](10,2)
[Link]: can't do nonzero end-relative seeks
with open('[Link]','rb') as F:
data=[Link](-10,2)
print('The postion of the file pointer is',[Link]())
data=[Link]()
print(data)
OUTPUT
The postion of the file pointer is 22
b'o\r\nthree\r\n'
14
'''Program-8:Program to store details of marks of a class in a csv file and
to display its contents.
======================================================'''
import csv
ans='y'
record=[]
print('OPERATIONS WITH A CSV FILE')
print('========================')
with open('[Link]','w')as F:
w=[Link](F)
while True:
rollno=input('Enter the roll number:')
name=input('Enter the name of the student:')
marks=input('Enter the total marks obtained:')
record=[rollno,name,marks]
[Link](record)
ans=input('Do you want to add more records?')
if [Link]() in ['no','n']:
break
OUTPUT
15
'''Program-9:Program to
import csv
from tabulate import tabulate
ans='y'
record=[]
header=['roll no','name of student','marks']
print('OPERATIONS WITH A CSV FILE')
print('========================')
with open('[Link]','w',newline='')as F:
w=[Link](F)
[Link](header)
while True:
rollno=input('Enter the roll number:')
name=input('Enter the name of the student:')
marks=input('Enter the total marks obtained:')
record=[rollno,name,marks]
[Link](record)
ans=input('Do you want to add more records?')
if [Link]() in ['no','n']:
break
data=[]
with open('[Link]','r')as F:
r=[Link](F)
head=next(r)
for i in r:
[Link](i)
16
print(tabulate(data,headers=head,tablefmt='grid'))
OUTPUT
OPERATIONS WITH A CSV FILE
========================
Enter the roll number:3
Enter the name of the student:VAIBHAV
Enter the total marks obtained:00(FAIL)
Do you want to add more records?y
Enter the roll number:16
Enter the name of the student:Jibin
Enter the total marks obtained:99.9
Do you want to add more records?n
+-----------+-------------------+----------+
| roll no | name of student | marks |
+===========+===================+==========+
| 3 | VAIBHAV | 00(FAIL) |
+-----------+-------------------+----------+
| 16 | Jibin | 99.9 |
+-----------+-------------------+----------+
17
#Program 10:Program to accept Item code,Item discription,price and
quatity of items in a [Link] accepted along with total price should be
stored in csv [Link] the contents in tabular format
====================================================='''
import csv
from tabulate import tabulate
ans='y'
record=[]
header=['Item Code','Item Discription','Price','Quantity of Items','Total Price']
print('OPERATIONS WITH A CSV FILE')
print('========================')
with open('[Link]','w',newline='')as F:
w=[Link](F)
[Link](header)
while True:
ItemCode=input('Enter the item code:')
ItemDiscription=input('Enter the item discription:')
Price=int(input('Enter the price of the item:'))
QuantityofItems=int(input('Enter the quantity of the items:'))
totalprice=QuantityofItems*Price
record=[ItemCode,ItemDiscription,Price,QuantityofItems,totalprice]
[Link](record)
ans=input('Do you want to add more records?')
if [Link]() in ['no','n']:
break
18
data=[]
with open('[Link]','r')as F:
r=[Link](F)
head=next(r)
for i in r:
[Link](i)
print(tabulate(data,headers=head,tablefmt='grid'))
OUTPUT
OPERATIONS WITH A CSV FILE
========================
Enter the item code:569
Enter the item discription:pen
Enter the price of the item:10
Enter the quantity of the items:100
Do you want to add more records?y
Enter the item code:6593
Enter the item discription:shirt
Enter the price of the item:1499
Enter the quantity of the items:2
Do you want to add more records?n
+-------------+--------------------+---------+---------------------+---------------+
| Item Code | Item Discription | Price | Quantity of Items | Total Price |
+=============+====================+=========+=========
| 569 | pen | 10 | 100 | 1000 |
+-------------+--------------------+---------+---------------------+---------------+
| 6593 | shirt | 1499 | 2| 2998 |
+-------------+--------------------+---------+---------------------+---------------+
19
# Program 11:Program to accept following data from the
user:Rollnumber,name,coursecode and CGPA of students in
[Link] should be accepted as dictionary and contents of the
file'University_Records.csv'should be displayed in proper tabular format
======================================================
f='University_Records.csv'
fields=['roll number','name','course code','CGPA']
ans='y'
data={}
record=[]
with open(f,'w',newline='')as l:
w=[Link](l,fieldnames=fields)
[Link]()
while True:
rollnumber=input('Enter the roll number:')
name=input('Enter the name of the candidate:')
coursecode=input('Enter the course code:')
CGPA=input('Enter the CGPA:')
data['roll number']=rollnumber
data['name']=name
20
data['course code']=coursecode
data['CGPA']=CGPA
[Link](data)
[Link](record)
contents=[]
with open(f,'r') as l:
r=[Link](l)
heading=next(r)
for i in r:
[Link](i)
print(tabulate(contents,headers=heading,tablefmt='grid'))
21
# Program 12:Program to accept following details of employees in an
organization:EmpCode,EmpName,Designation and Basic_Salary.Data
should be taken as dictionaries and stored in the csv file Employee
[Link]. Display the contents in proper tabular format.
====================================================='''
import csv
from tabulate import tabulate
f='Employee_Records.csv'
fields=['Emp Code','Emp Code','Designation','Emp salary']
ans='y'
data={}
record=[]
with open(f,'w',newline='')as l:
w=[Link](l,fieldnames=fields)
[Link]()
while True:
EC=input('Enter the Emp Code:')
EN=input('Enter the name of the employee:')
D=input('Enter the Designation:')
ES=input('Enter the Emp salary :')
data['Emp Code']=EC
data['Emp Code']=EN
data['Designation']=D
data['Emp salary']= ES
[Link](data)
22
[Link](record)
record=[]
content=[]
with open(f,'r') as l:
r=[Link](l)
heading=next(r)
for i in r:
[Link](i)
print(tabulate(content,headers=heading,tablefmt='grid'))
Enter the Emp Code:694
Enter the name of the employee:vaibhav
Enter the Designation:teacher
Enter the Emp salary :50000
Want to add any more record??y.
Enter the Emp Code:6
Enter the name of the employee:jibin
Enter the Designation:vice principal
Enter the Emp salary :100000
Want to add any more record??y
Enter the Emp Code:654
Enter the name of the employee:kunal
Enter the Designation:chairman
Enter the Emp salary :10000000
Want to add any more record??n
+------------+------------+----------------+--------------+
| Emp Code | Emp Name | Designation | Emp salary |
+============+============+================+==========+
| 694 | vaibhav | teacher | 50000 |
23
+------------+------------+----------------+--------------+
| 6 | jibin | vice principal | 100000 |
+------------+------------+----------------+--------------+
| 654 | kunal | chairman | 10000000 |
+------------+------------+----------------+--------------+
24
#Program13:Following program accepts the students
details(roll,number,name and marks) of n number of students of a class
and store the records in a binary [Link] displays the contents after the
storing process
======================================================
import pickle as p
with open('[Link]','wb') as f:
n=int(input('Enter the number of students:'))
record=1
rec=[]
while record<=n:
rollno=input('Enter the roll number:')
name=input('Enter the name of the student:')
marks=input('Enter the marks:')
rec=[rollno,name,marks]
[Link](record,f)
record+=1
try:
with open('[Link]','rb') as f:
while True :
data=[Link](f)
print(data)
except:
print('No more records end of file reached')
25
26
# Program 14:Program to accept following details of employees of an
organization:(emp code,emp name,designation and monthly salary and
annual income(should be calculated)) from users for n number of
[Link] data has to be stored in a binary file and should be
displayed in a tabular format
======================================================
27
#Program 15:PROGRAM TO DEMONSTRATE LIFO OPERATIONS
IN STACK
=====================================================
stack=[]
[Link]('a')
[Link]('b')
[Link]('c')
OUTPUT
28
THE FINAL STACK IS:
[]
def PUSH(nameStack):
while True:
name=input('Enter the name of the student:')
if len(name)!=0:
[Link](name)
ans=input('Do you want to add more names?')
if [Link]()in ['no','n']:
break
return nameStack
Stack=PUSH(nameStack)
print('The stack after PUSH operations is:')
print(Stack)
OUTPUT
Enter the name of the student:vaibhav
Do you want to add more names?y
Enter the name of the student:jibin
Do you want to add more names?n
The stack after PUSH operations is:['vaibhav', 'jibin']
#Program to check the status(empty/non-empty)of a stack and to display
the 'Underflow Error' message
======================================================
n=[]
29
def PUSH(s):
while True:
data=input('Enter the data want to be stored in stack:')
if len(data)!=0:
[Link](data)
def CHECK(s):
if len(s)==0:
return 'Underflow Error-Your stack is empty'
else:
return len(s)
Stack=PUSH(n)
print('Stack after the push operation:')
print(Stack)
status=CHECK(Stack)
print('Current status of your stack:',status)
30
Enter the data want to be stored in stack:
do you want to add more data?y
Enter the data want to be stored in stack:
do you want to add more data?n
Stack after the push operation:
[]
Current status of your stack: Underflow Error-Your stack is empty
def PUSH(s):
while True:
data=input('Enter the data want to be stored in stack:')
if len(data)!=0:
[Link](data)
31
def CHECK(s):
return len(s)
def POP(s):
status=CHECK(s)
if status==0:
print('Underflow Error-empty stack:')
else:
print('Elements popped are:')
for i in range(status):
print([Link]())
Stack=PUSH(n)
print('Stack after the push operation:')
print(Stack)
POP(Stack)
OUTPUT
Enter the data want to be stored in stack:564
do you want to add more data?y
Enter the data want to be stored in stack:jibin
do you want to add more data?n
Stack after the push operation:
['564', 'jibin']
Elements popped are:
jibin
564
32
Program to create stack and demonstrate PEEK operation
k=[]
33
def PUSH(p):
while True:
data=input('Enter the data want to be stored in stack: ')
if len(data)!=0:
[Link](data)
def CHECK(p):
return len(p)
def PEEK(p):
status=CHECK(p)
if status==0:
print('Underflow Error-empty stack:')
else:
print('The topmost element of the stack: ', p[status-1])
status=PUSH(k)
print('The status of the stack after PUSH operation is: ')
print(k)
PEEK(status)
OUTPUT
Enter the data want to be stored in stack: jibin
do you want to add more data? y
Enter the data want to be stored in stack: vaibhav
do you want to add more data? n
The status of the stack after PUSH operation is:
34
['jibin', 'vaibhav']
The topmost element of the stack: vaibhav
35
Prepare following table under the database SALES.
Table name:EMPLOYEE
Attributes:
[Link] code (should be a number)
[Link] name(maximum size-35)
[Link](maximum size-25)
[Link] (maximum size-50)
[Link] of Birth
36
Make the attribute of Employee Code as the Primary Key of the table
37
Insert three records into the file EMPLOYEE table
38
Change the designation of a paticular employee to'Supervisor';
39
Display the contents of the table employee
40
mysql> select*from employee where Employeecode>1001 AND
DateOfBirth>2007-10-03;
+--------------+--------------+------------+-------------+-------------+
| EmployeeCode | Employeename | Department | Designation | DateOfBirth |
+--------------+--------------+------------+-------------+-------------+
| 1002 | vaibhav | Supervisor | accountant | 2007-12-04 |
+--------------+--------------+------------+-------------+-------------+
1 row in set, 1 warning (0.03 sec)
41
*23 Demonstration of mySQL commands.
mysql> use showroom;
Database changed
[Link] FID as the primary key.
mysql> desc furniture;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| FID | varchar(5) | NO | PRI | NULL | |
| NAME | varchar(25) | NO | UNI | NULL | |
| DOP | date | YES | | NULL | |
| COST | int | YES | | NULL | |
| DISCOUNT | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
[Link] NAME as the unique key.
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| FID | varchar(5) | NO | PRI | NULL | |
| NAME | varchar(25) | NO | UNI | NULL | |
| DOP | date | YES | | NULL | |
| COST | int | YES | | NULL | |
| DISCOUNT | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
[Link] the attribute Name a non-NULL column.
mysql> ALTER TABLE FURNITURE MODIFY NAME VARCHAR (25)
NOT NULL;
Query OK, 0 rows affected (0.27 sec)
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| FID | varchar(5) | NO | PRI | NULL | |
| NAME | varchar(25) | NO | UNI | NULL | |
| DOP | date | YES | | NULL | |
42
| COST | int | YES | | NULL | |
| DISCOUNT | int | YES | | NULL | |
Q. Display the data for items which has cost between 20000 to 50000.
mysql> SELECT*FROM FURNITURE WHERE COST BETWEEN 20000
AND 50000;
+------+------------+------------+-------+----------+--------+
| FID | NAME | DOP | COST | DISCOUNT | RATING |
+------+------------+------------+-------+----------+--------+
| B001 | Double Bed | 2018-01-03 | 45000 | 10 | 3|
| B004 | Single Bed | 2021-07-19 | 22000 | 0| 3|
| B006 | Bunk Bed | 2021-01-01 | 28000 | 14 | 3|
+------+------------+------------+-------+----------+--------+
[Link] the name and selling price of the items with appropriate
headings.
mysql> SELECT NAME AS' FURNITURE NAME',COST-
(COST*DISCOUNT/100) AS 'SELLING PRICE'FROM FURNITURE;
+-----------------+---------------+
| FURNITURE NAME | SELLING PRICE |
+-----------------+---------------+
| Double Bed | 40500.0000 |
| Single Bed | 22000.0000 |
| Bunk Bed | 24080.0000 |
| Long Back Chair | 11640.0000 |
| Console Table | 13200.0000 |
| Dining Table | 48450.0000 |
+-----------------+---------------+
[Link] the name and date of purchase of all types of beds in the table.
mysql> SELECT NAME,DOP FROM FURNITURE WHERE NAME LIKE
'%Bed';
+------------+------------+
| NAME | DOP |
+------------+------------+
| Double Bed | 2018-01-03 |
| Single Bed | 2021-07-19 |
| Bunk Bed | 2021-01-01 |
43
+------------+------------+
44
| T010 | Dining Table | 2020-03-10 | 51000 | 5 | NULL |
+------+-----------------+------------+-------+----------+--------+
[Link] the rating of tables to 3 and the rating of beds to 4.
mysql> update furniture set rating=4 where name like'%bed';
Query OK, 3 rows affected (0.06 sec)
Rows matched: 3 Changed: 3 Warnings: 0
45
+------+-----------------+------------+-------+----------+--------+
| C003 | Long Back Chair | 2016-12-30 | 12000 | 3 | NULL |
+------+-----------------+------------+-------+----------+--------+
[Link] the name of item with discount 10,12 and13.
mysql> select name from furniture where discount in(10,12,13);
+---------------+
| name |
+---------------+
| Double Bed |
| Console Table |
+---------------+
[Link] the records in descending order of their price.
mysql> select*from furniture order by cost desc;
+------+-----------------+------------+-------+----------+--------+
| FID | NAME | DOP | COST | DISCOUNT | RATING |
+------+-----------------+------------+-------+----------+--------+
| T010 | Dining Table | 2020-03-10 | 51000 | 5| 3|
| B001 | Double Bed | 2018-01-03 | 45000 | 10 | 4|
| B006 | Bunk Bed | 2021-01-01 | 28000 | 14 | 4|
| B004 | Single Bed | 2021-07-19 | 22000 | 0| 4|
| T006 | Console Table | 2019-11-17 | 15000 | 12 | 3|
| C003 | Long Back Chair | 2016-12-30 | 12000 | 3 | NULL |
+------+-----------------+------------+-------+----------+--------+
[Link] the count of unique rating in table.
mysql> select count(rating) from furniture;
+---------------+
| count(rating) |
+---------------+
| 5|
46
[Link] the data where count of rating is more than 2.
mysql> select*from furniture where rating>2;
+------+---------------+------------+-------+----------+--------+
| FID | NAME | DOP | COST | DISCOUNT | RATING |
+------+---------------+------------+-------+----------+--------+
| B001 | Double Bed | 2018-01-03 | 45000 | 10 | 4|
| B004 | Single Bed | 2021-07-19 | 22000 | 0| 4|
| B006 | Bunk Bed | 2021-01-01 | 28000 | 14 | 4|
| T006 | Console Table | 2019-11-17 | 15000 | 12 | 3|
| T010 | Dining Table | 2020-03-10 | 51000 | 5| 3|
+------+---------------+------------+-------+----------+--------+
[Link] the attribute rating from the table.
mysql> alter table furniture drop rating;
Query OK, 0 rows affected (1.56 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select*from furniture;
+------+-----------------+------------+-------+----------+
| FID | NAME | DOP | COST | DISCOUNT |
+------+-----------------+------------+-------+----------+
| B001 | Double Bed | 2018-01-03 | 45000 | 10 |
| B004 | Single Bed | 2021-07-19 | 22000 | 0|
| B006 | Bunk Bed | 2021-01-01 | 28000 | 14 |
| C003 | Long Back Chair | 2016-12-30 | 12000 | 3|
| T006 | Console Table | 2019-11-17 | 15000 | 12 |
| T010 | Dining Table | 2020-03-10 | 51000 | 5|
+------+-----------------+------------+-------+----------+
[Link] the table Furniture.
mysql> drop table furniture;
Query OK, 0 rows affected (0.66 sec)
[Link] the database.
mysql> drop database showroom;
Query OK, 0 rows affected (0.11 sec)
mysql> use showroom;
47
ERROR 1049 (42000): Unknown database 'showroom'
48
| 1004 | JOBIN | 168480 |
+---------------+---------------+---------------------+
[Link] the code and names of the employee with maximum basic
salary:
mysql> select empname from emp where basicsalary=(select
max(basicsalary)from emp);
+---------+
| empname |
+---------+
| KUNAL |
+---------+
[Link] the code and names of the employee with minimum
deductions.
mysql> select empcode,empname from emp where deductions=(select
min(deductions) from emp);
+---------+---------+
| empcode | empname |
+---------+---------+
| 1001 | KUNAL |
| 1004 | JOBIN |
+---------+---------+
Q. Display the sum of HRA payable to all employees in a year.
mysql> select (sum(hra))*12 from emp;
+---------------+
| (sum(hra))*12 |
+---------------+
| 720 |
+---------------+
Q. Show the average DA permissible for all employees.
mysql> select avg(da)from emp;
+--------------------+
| avg(da) |
+--------------------+
| 15.574999809265137 |
49
Q. Display the names of all employees in descending order of their basic
salary.
mysql> select empname from emp order by basicsalary desc;
+---------+
| empname |
+---------+
| KUNAL |
| ADITI |
| VAIBHAV |
| JOBIN |
+---------+
Q. Display the unique HRA rate of the table.
mysql> select distinct(hra)from emp;
+------+
| hra |
+------+
| 20 |
| 5|
| 15 |
+------+
50
*25 Demonstration of MySQL commands UNION,INTERSECT,
CARTESIAN PRODUCT,EQUIJOIN &NATURAL JOIN
======================================================
Database:SALES
Table1:ITEM(ICODE,INAME,IMAN)
Table2:INVENTORY (ICODE,NOI,PRICE,DOM)
mysql> use sales1;
Database changed
mysql> desc item;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ICODE | int | YES | | NULL | |
| INAME | varchar(25) | YES | | NULL | |
| IMAN | varchar(25) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> select*from item;
+-------+-----------+--------+
| ICODE | INAME | IMAN |
+-------+-----------+--------+
| 1001 | pc | dell |
| 1002 | mouse | razor |
| 1003 | keyboard | acer |
| 1004 | processor | razen7 |
| 1005 | pendrive | hp |
+-------+-----------+--------+
mysql> SELECT*FROM INVENTORY;
+-------+------+-------+------------+
| ICODE | NOI | PRICE | DOM |
+-------+------+-------+------------+
| 1001 | 5 | 50000 | 2007-06-10 |
| 1002 | 2 | 1000 | 2007-10-11 |
| 1003 | 2 | 1500 | 2013-11-11 |
51
| 1004 | 7 | 2000 | 2009-10-03 |
| 1005 | 1 | 500 | 2015-12-02 |
+-------+------+-------+------------+
UNION:
mysql> SELECT*FROM ITEM UNION SELECT*FROM INVENTORY;
ERROR 1222 (21000): The used SELECT statements have a different
number of columns
CARTESIAN PRODUCT:
mysql> SELECT*FROM ITEM,INVENTORY;
+-------+-----------+--------+-------+------+-------+------------+
| ICODE | INAME | IMAN | ICODE | NOI | PRICE | DOM |
+-------+-----------+--------+-------+------+-------+------------+
| 1005 | pendrive | hp | 1001 | 5 | 50000 | 2007-06-10 |
| 1004 | processor | razen7 | 1001 | 5 | 50000 | 2007-06-10 |
| 1003 | keyboard | acer | 1001 | 5 | 50000 | 2007-06-10 |
| 1002 | mouse | razor | 1001 | 5 | 50000 | 2007-06-10 |
| 1001 | pc | dell | 1001 | 5 | 50000 | 2007-06-10 |
| 1005 | pendrive | hp | 1002 | 2 | 1000 | 2007-10-11 |
| 1004 | processor | razen7 | 1002 | 2 | 1000 | 2007-10-11 |
| 1003 | keyboard | acer | 1002 | 2 | 1000 | 2007-10-11 |
| 1002 | mouse | razor | 1002 | 2 | 1000 | 2007-10-11 |
| 1001 | pc | dell | 1002 | 2 | 1000 | 2007-10-11 |
| 1005 | pendrive | hp | 1003 | 2 | 1500 | 2013-11-11 |
| 1004 | processor | razen7 | 1003 | 2 | 1500 | 2013-11-11 |
| 1003 | keyboard | acer | 1003 | 2 | 1500 | 2013-11-11 |
| 1002 | mouse | razor | 1003 | 2 | 1500 | 2013-11-11 |
| 1001 | pc | dell | 1003 | 2 | 1500 | 2013-11-11 |
| 1005 | pendrive | hp | 1004 | 7 | 2000 | 2009-10-03 |
| 1004 | processor | razen7 | 1004 | 7 | 2000 | 2009-10-03 |
| 1003 | keyboard | acer | 1004 | 7 | 2000 | 2009-10-03 |
| 1002 | mouse | razor | 1004 | 7 | 2000 | 2009-10-03 |
| 1001 | pc | dell | 1004 | 7 | 2000 | 2009-10-03 |
| 1005 | pendrive | hp | 1005 | 1 | 500 | 2015-12-02 |
| 1004 | processor | razen7 | 1005 | 1 | 500 | 2015-12-02 |
| 1003 | keyboard | acer | 1005 | 1 | 500 | 2015-12-02 |
52
| 1002 | mouse | razor | 1005 | 1 | 500 | 2015-12-02 |
| 1001 | pc | dell | 1005 | 1 | 500 | 2015-12-02 |
+-------+-----------+--------+-------+------+-------+------------+
EQUIJOIN:
mysql> select*from item join inventory on [Link]=[Link];
+-------+-----------+--------+-------+------+-------+------------+
| ICODE | INAME | IMAN | ICODE | NOI | PRICE | DOM |
+-------+-----------+--------+-------+------+-------+------------+
| 1001 | pc | dell | 1001 | 5 | 50000 | 2007-06-10 |
| 1002 | mouse | razor | 1002 | 2 | 1000 | 2007-10-11 |
| 1003 | keyboard | acer | 1003 | 2 | 1500 | 2013-11-11 |
| 1004 | processor | razen7 | 1004 | 7 | 2000 | 2009-10-03 |
| 1005 | pendrive | hp | 1005 | 1 | 500 | 2015-12-02 |
+-------+-----------+--------+-------+------+-------+------------+
NATURAL JOIN:
mysql> select*from item natural join inventory;
+-------+-----------+--------+------+-------+------------+
| ICODE | INAME | IMAN | NOI | PRICE | DOM |
+-------+-----------+--------+------+-------+------------+
| 1001 | pc | dell | 5 | 50000 | 2007-06-10 |
| 1002 | mouse | razor | 2 | 1000 | 2007-10-11 |
| 1003 | keyboard | acer | 2 | 1500 | 2013-11-11 |
| 1004 | processor | razen7 | 7 | 2000 | 2009-10-03 |
| 1005 | pendrive | hp | 1 | 500 | 2015-12-02 |
+-------+-----------+--------+------+-------+------------+
53
PRACTICAL 26:Prepare a table BOOK under the database LIBRARY the
table should have following structures:
[Link]-integer(primary key)
[Link]-floating point=non null
[Link]=variable characters (size=50),non null
Enter records into tablerand display its content using a python program
import [Link] as mc
if c1.is_connected():
info=c1.get_server_info()
print(info)
workarea=[Link]()
[Link]('SELECT DATABASE();')
result=[Link]()
print('You are connected to the database :',result)
else:
print('Connection error')
8.0.28
You are connected to the database : ('library',)
54
55
Display of records from table using python
import [Link] as mc
import sys
from tabulate import tabulate
head=['ACCNO','TITLE','PRICE']
try:
c1=[Link](host='localhost', user='user4', database='library',
password='KesRKP#2022', auth_plugin='mysql_native_password')
if c1.is_connected():
cursor=[Link]()
[Link]('SELECT DATABASE();')
data=[Link]()
print('You are connected to the database :',data)
[Link]('USE library;')
[Link]('SELECT*FROM book;')
d=[Link]()
print(d)
print(tabulate(d,headers=head,tablefmt='grid'))
except:
print('connection error')
exit()
56
OUTPUT
You are connected to the database : ('library',)
[(58964, 'BERSERK', 966.32), (58965, 'TIGERKING', 526.33), (58966,
'INDIGO', 800.25), (58967, 'ANSWERS TO BIG QUESTIONS', 800.25),
(58968, 'ITS ALL IN YOUR HEAD', 987.25)]
+---------+--------------------------+---------+
| ACCNO | TITLE | PRICE |
+=========+==========================+=========+
| 58964 | BERSERK | 966.32 |
+---------+--------------------------+---------+
| 58965 | TIGERKING | 526.33 |
+---------+--------------------------+---------+
| 58966 | INDIGO | 800.25 |
+---------+--------------------------+---------+
| 58967 | ANSWERS TO BIG QUESTIONS | 800.25 |
+---------+--------------------------+---------+
| 58968 | ITS ALL IN YOUR HEAD | 987.25 |
+---------+--------------------------+---------+
57
Addition of records from table using python
import [Link] as mc
import sys
from tabulate import tabulate
try:
c1=[Link](host='localhost', user='user4', database='library',
password='KesRKP#2022', auth_plugin='mysql_native_password')
if c1.is_connected():
cursor=[Link]()
[Link]('SELECT DATABASE();')
data=[Link]()
print('You are connected to the database :',data)
[Link]('USE library;')
[Link]('SELECT*FROM book;')
d=[Link]()
print('existing records are:')
head=['ACCNO','TITLE','PRICE']
print(tabulate(d,headers=head,tablefmt='grid'))
except:
print('connection error')
exit()
else:
print('addition of new records')
print('=====================')
58
print('1. Add a new record')
print('[Link]()')
59
| 58967 | ANSWERS TO BIG QUESTIONS | 800.25 |
+---------+--------------------------+---------+
| 58968 | ITS ALL IN YOUR HEAD | 987.25 |
+---------+--------------------------+---------+
addition of new records
=====================
1. Add a new record
[Link]()
Enter your choice:1
enter the accession number :89568
add records
enter the title of the book:computer science
enter the price of the book:450.36
OUTPUT
mysql> use library;
Database changed
mysql> desc book;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ACCNO | int | NO | PRI | NULL | |
| TITLE | varchar(50) | NO | | NULL | |
| PRICE | float | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
60
| 58968 | ITS ALL IN YOUR HEAD | 987.25 |
| 89568 | computer science | 450.36 |
+-------+--------------------------+--------+
import [Link] as mc
import sys
from tabulate import tabulate
try:
c1=[Link](host='localhost', user='user4', database='library',
password='KesRKP#2022', auth_plugin='mysql_native_password')
if c1.is_connected():
cursor=[Link]()
[Link]('SELECT DATABASE();')
data=[Link]()
print('You are connected to the database :',data)
[Link]('USE library;')
[Link]('SELECT*FROM book;')
d=[Link]()
print('existing records are:')
head=['ACCNO','TITLE','PRICE']
print(tabulate(d,headers=head,tablefmt='grid'))
except:
print('connection error')
exit()
else:
print('addition of new records')
print('=====================')
61
option=int(input('Enter your choice:'))
if option==1:
accno=int(input('enter the accession number of the book for which the
price is changed:'))
s='select*from book where ACCNO=%s'
v=(accno,)
[Link](s,v)
result=[Link]()
if result is not None:
price=float(input('enter the price of the book:'))
record1=[price,accno]
sql='UPDATE book SET price=%s WHERE accno=%s'
[Link](sql,record1)
[Link]()
else:
print('Duplicate accession number')
62
63
64