CS Practical File (XII)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

Practical-1

Q.

1) create database LOANS;


2) use LOANS;
3) create table Loan_Accounts(AccNo int primary key,Cust_Name
varchar(20) not null,Loan_Amount int,Instalments int,Int_Rate
float(5,2),Start_Date date,Interest int);
insert into Loan_Accounts values(1,’R. K.
Gupta’,300000,36,12,’2009/7/19’,null);
insert into Loan_Accounts values(2,’S. P.
Sharma’,500000,48,10,’2008/3/22’,null);
insert into Loan_Accounts values(3,’K. P.
Jain’,300000,36,null,’2007/3/8’,null);
insert into Loan_Accounts values(4,’M. P.
Yadav’,800000,60,10,’2008/12/6’,null);
insert into Loan_Accounts values(5,’S. P.
Sinha’,200000,36,12.5,’2010/1/3’,null);
insert into Loan_Accounts values(6,’P.
Sharma’,700000,60,12.5,’2008/6/5’,null);
insert into Loan_Accounts values(7,’K. S.
Dhall’,500000,48,null,’2008/3/5’,null);
4) select * from Loan_Accounts;

5) select AccNo,Cust_Name,Loan_Amount from Loan_Accounts;

6) select * from Loan_Accounts where Instalments<40;

7) select AccNo,Loan_Amount from Loan_Accounts where


Start_Date<’2009/4/1’;
8) select Int_Rate from Loan_Accounts where Start_Date>’2009/4/1’;

9) select * from Loan_Accounts where Int_Rate is null;

10) select * from Loan_Accounts where Int_Rate is not null;

11) select distinct Loan_Amount from Loan_Accounts;


12) select distinct Instalments from Loan_Accounts;

13) select * from Loan_Accounts where Start_Date>’2008/12/31’ and


Instalments>36;

14) select Cust_Name,Loan_Amount from Loan_Accounts where


Instalments!=36;

15) select Cust_Name,Loan_Amount from Loan_Accounts where


Loan_Amount<500000 or Int_Rate>12;
16) select * from Loan_Accounts where Start_Date>=’2009/1/1’ and
Start_Date<=‘2009/12/31’;

17) select * from Loan_Accounts where Loan_Amount>=400000 and


Loan_Amount<=500000;

18) select * from Loan_Accounts where Int_Rate>=11 and


Int_Rate<=12;

19) select Cust_Name,Loan_Amount from Loan_Accounts where


Instalments in (24,36,48);
20) select Cust_Name,Loan_Amount from Loan_Accounts where
Instalments=24 or Instalments=36 or Instalments=48;

21) select * from Loan_Accounts where Loan_Amount between 400000


and 500000;

22) select * from Loan_Accounts where Int_Rate between 11 and 12;

23) select * from Loan_Accounts where Int_Rate>=11 and


Int_Rate<=12;
24) select AccNo,Cust_Name,Loan_Amount from Loan_Accounts where
Cust_Name like "%Sharma";

25) select AccNo,Cust_Name,Loan_Amount from Loan_Accounts where


Cust_Name like "%a";

26) select AccNo,Cust_Name,Loan_Amount from Loan_Accounts where


Cust_Name like "%a%";

27) select AccNo,Cust_Name,Loan_Amount from Loan_Accounts where


Cust_Name not like "%P%";
28) select AccNo,Cust_Name,Loan_Amount from Loan_Accounts where
Cust_Name like "%a_";

29) select * from Loan_Accounts where Cust_Name like "_____";

30) select * from Loan_Accounts order by Loan_Amount;

31) select * from Loan_Accounts order by Start_Date desc;


32) select * from Loan_Accounts order by Loan_Amount asc,Start_Date
desc;

33) update Loan_Accounts set Int_Rate=11.5 where Int_Rate is null;


34) update Loan_Accounts set Int_Rate=Int_Rate+0.5 where
Loan_Amount>400000;
35) update Loan_Accounts set
Interest=(Loan_Amount*Int_Rate*Instalments)/(12*100);
Practical-2

1) create table Hospital(No int Primary Key,Name varchar(30) not


null,Age int check(Age>10),Dateofadm date default(curdate()),Charges
int check(Charges>=250),Sex char check(Sex in ('M','F','O')),Department
varchar(30) not null);

2) insert into Hospital values(1,'Arpit',62,'1998/1/21',300,'M','Cardiology');


insert into Hospital values(2,'Zarina',22,'1997/12/12',250,'F','ENT');
insert into Hospital values(3,'Kareem',32,'1998/2/19',200,'M','ENT');
insert into Hospital values(4,'Arun',12,'1998/1/11',300,'M','Eyes');
insert into Hospital values(5,'Zubin',30,'1998/1/12',250,'M','Cardiology');
insert into Hospital values(6,'Ketika',16,'1998/2/24',250,'F','ENT');
insert into Hospital values(7,'Ankita',29,'1998/2/20',800,'F','ENT');
insert into Hospital values(8,'Zareen',45,'1998/2/22',300,'F','Cardiology');
insert into Hospital values(9,'Kush',19,'1998/1/13',800,'M','ENT');
insert into Hospital values(10,'Shilpa',23,'1998/2/21',400,'F','ENT');

3) select * from Hospital where Department='Cardiology';

4) select Name from Hospital where Sex='F' and Department in


('ENT','Surgery');

5) select Name from Hospital order by Dateofadm desc;


6) select Name,Charges,Age from Hospital where Name like 'K%';

7) select count(Age) from Hospital where Age>30;

8) select distinct Department from Hospital;

9) select Sex,min(Age),max(Age) from Hospital group by Sex;


10) select Department,sum(Charges),avg(Charges) from Hospital group
by Department;

11) select Department,count(*) from Hospital group by Department


having count(*)>1;

12) update Hospital set Age=22 where Name='Kush';


13) update Hospital set Charges=Charges+(Charges * (10/100)) where
Sex='M' and Department='Cardiology';
14) delete from Hospital where Sex=’F’ and Charges between 300 and
400;
15) Alter table Hospital add DocName varchar(20);
16) Alter table Hospital rename column Sex to Gender;
17) desc Hospital;
18) select Left(Name,4) from Hospital;
19) select * from Hospital where Dateofadm between '1997/1/1' and
'1997/12/31';

20) select Name,Length(Name) from Hospital where age<25;


Practical-3
Q. Write SQL commands on the basis of relations given below:
Table: BOOKS
Book_ID Book_name Author_name Publishers Price Type qty
K0001 Let us C Y. Kanetkar EPB 450 Prog 15
P0001 Computer Networks B. Agarwal FIRST PUBL 755 Comp 24
M0001 Mastering C++ K.R. Venugopal EPB 165 Prog 60
N0002 VC++ advance P. Purohit TDH 250 Prog 45
K0002 Programming with Python Sanjeev FIRST PUBL 350 Prog 30

Table: ISSUED
Book_I Qty_Issued
D
L02 13
L04 5
L05 21

i) select * from Books where Author_name='P. Purohit' and


Publishers='FIRST PUBL';

ii) select Price from Books where Publishers='FIRST PUBL';

iii) update Books set Price=Price-(Price*(5/100)) where


Publishers='EPB';
iv) select Book_name,Price from Books B,Issued I where
B.Book_ID=I.Book_ID and Qty_Issued>3;

v) select Type,sum(Price) Total_Price from Books group by Type;

vi) select * from Books where Price=(select max(Price) from Books);


Practical-4

i) select * from Store order by LastBuy;

ii) select ItemNo,Item from Store where Rate>15;


iii) select * from Store where Scode=22 or Qty>110;

iv) select Scode,min(Rate) Min_Rate from Store group by


Scode;
Practical-5
Q. To create a new database from python & show all databases
from python

Source Code:
import mysql.connector as SQL
db=SQL.connect(host='localhost',user='root',password='')
cursor=db.cursor()
try:
n=input('Enter Database name: ')
query='create database {}'.format(n)
cursor.execute(query)
query='show databases'
cursor.execute(query)
r=cursor.fetchall()
print('\nDatabase')
for i in r:
print(i[0])
except:
print("Error")
finally:
db.close()
Output:
Practical-6
Q. To create a table in MySQL from Python

Source Code:
import mysql.connector as SQL
try:
database_name=input("Enter name of database: ")

db=SQL.connect(host='localhost',user='root',password='',database=data
base_name)
cursor=db.cursor()

Table_name=input("Enter table name: ")


n_col=int(input("Enter number of columns: "))
L=[]
for i in range(n_col):
Lt=[]
name_col=input("\nEnter column name: ")
Datatype=input("Enter datatype: ")
Constraint=input("Enter Constraint (Leave Blank is None): ")
Lt.append(name_col)
Lt.append(Datatype)
Lt.append(Constraint)
L.append(Lt)
query="create table {} ({} {} {})".format(Table_name,L[0][0],L[0][1],L[0]
[2])
cursor.execute(query)
if n_col>1:
for i in range(1,n_col):
query="alter table {} add column {} {} {}".format(Table_name,L[i]
[0],L[i][1],L[i][2])
cursor.execute(query)
except:
print("Error")
finally:
db.close()

Output:
Practical-7
Q. To add new col in MySQL Table from Python

Source Code:
import mysql.connector as SQL
try:
database_name=input("Enter name of database: ")

db=SQL.connect(host='localhost',user='root',password='',database=data
base_name)
cursor=db.cursor()

Table_name=input("Enter table name: ")


while True:
print("\nPress 1 to add new column")
print("Press 2 to close program")
ch=int(input("Input: "))
if ch==1:
L=[]
name_col=input("\nEnter column name: ")
Datatype=input("Enter datatype: ")
Constraint=input("Enter Constraint (Leave Blank is None): ")
L.append(name_col)
L.append(Datatype)
L.append(Constraint)
query="alter table {} add column {} {}
{}".format(Table_name,L[0],L[1],L[2])
cursor.execute(query)
if ch==2:
break
except:
print("Error")
finally:
db.close()

Output:
Practical-8
Q. To insert records in MySQL from Python

Source Code:
import mysql.connector as SQL
try:
database_name=input("Enter name of database: ")

db=SQL.connect(host='localhost',user='root',password='',database=data
base_name)
cursor=db.cursor()

Table_name=input("Enter table name: ")


n_col=int(input("Enter number of columns: "))
while True:
print("\nPress 1 to input data")
print("Press 2 to close program")
ch=int(input("Input: "))
if ch==1:
L=[]
print("\n")
for i in range(n_col):
val=input("Enter Value: ")
L.append(val)
query = "insert into {} values ({})".format(Table_name,",
".join(["%s"]*n_col))
cursor.execute(query,L)
db.commit()
if ch==2:
break
except:
print("Error")
db.rollback()
finally:
db.close()

Output:
Practical-9
Q. To update a record in MySQL from Python

Source Code:
import mysql.connector as SQL
try:
database_name=input("Enter name of database: ")

db=SQL.connect(host='localhost',user='root',password='',database=data
base_name)
cursor=db.cursor()

Table_name=input("Enter table name: ")


n_col=input("Enter name of column: ")
value=input("Enter new value: ")
query="update {} set {}= %s".format(Table_name,n_col)
cursor.execute(query,(value,))
db.commit()
except:
print("Error")
db.rollback()
finally:
db.close()
Output:
Practical-10
Q. To delete records in MySQL from Python

Source Code:
import mysql.connector as SQL
try:
database_name=input("Enter name of database: ")

db=SQL.connect(host='localhost',user='root',password='',database=data
base_name)
cursor=db.cursor()

Table_name=input("Enter table name: ")


print("Press 1 to CONFIRM DELETION")
ch=int(input("Input: "))
if ch==1:
query="delete from {}".format(Table_name)
cursor.execute(query)
db.commit()
print("Data Deleted")
except:
print("Error")
db.rollback()
finally:
db.close()
Output:

You might also like