0% found this document useful (0 votes)
26 views4 pages

Mysql HW 1

mysql query solved

Uploaded by

Harsheet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Mysql HW 1

mysql query solved

Uploaded by

Harsheet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Mysql HW-1

.1 Create table Student

import pymysql as pym

conn=pym.connect(host="localhost" , user="root" ,passwd="123456" , database ="h1")

cursorObject = conn.cursor()

sql='''CREATE TABLE STUDENT(SNO INT,


SNAME VARCHAR(30),
GENDER VARCHAR(30),
AGE INT ,
FEE INT
)'''

try:
cursorObject.execute(sql)

conn.commit()

except:

conn.rollback()

output in mysql :-

+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| SNO | int | NO | | NULL | |
| SNAME | varchar(35) | YES | | NULL | |
| GENDER | varchar(20) | YES | | NULL | |
| AGE | int | YES | | NULL | |
| FEE | int | YES | | NULL | |
| course | varchar(100) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+

.2 Insert values in student

import pymysql as pym

conn=pym.connect(host="localhost" , user="root" ,passwd="123456" , database ="h1")

cursorObject = conn.cursor()

sql='''INSERT INTO STUDENT VALUE(1,"Ashih", "M" , 17,6550)


'''

try:
cursorObject.execute(sql)

conn.commit()
except:

conn.rollback()

conn.close()

output in mysql -

+-----+------------+--------+------+-------+
| SNO | SNAME | GENDER | AGE | FEE |
+-----+------------+--------+------+-------+
| 1 | Ashih | M | 17 | 6550 |
| 2 | Madhulika | F | 20 | 7124 |
| 3 | Niti Sigh | F | 21 | 1800 |
| 4 | Pratyush | M | 18 | 1200 |
| 5 | Anand Seth | M | 16 | 19500 |
+-----+------------+--------+------+-------+

.3 Update age 20 for student number 4

import pymysql as pym

conn=pym.connect(host="localhost" , user="root" ,passwd="123456" , database ="h1")

cursorObject = conn.cursor()

sql='''UPDATE STUDENT SET AGE=20 WHERE SNO=4'''

try:
cursorObject.execute(sql)

conn.commit()

except:

conn.rollback()

conn.close()

- output in mysql -

+-----+------------+--------+------+-------+
| SNO | SNAME | GENDER | AGE | FEE |
+-----+------------+--------+------+-------+
| 1 | Ashih | M | 17 | 6550 |
| 2 | Madhulika | F | 20 | 7124 |
| 3 | Niti Sigh | F | 21 | 1800 |
| 4 | Pratyush | M | 20 | 1200 |
| 5 | Anand Seth | M | 16 | 19500 |
+-----+------------+--------+------+-------+

.4 arrange table on the base of Sname in descending order

import pymysql as pym


conn = pym.connect(host="localhost", user="root", passwd="123456", database="h1")

cursorObject = conn.cursor()

sql = '''SELECT *FROM STUDENT ORDER BY SNAME DESC'''

try:
cursorObject.execute(sql)
for i in cursorObject:
print(i)

conn.commit()

except:

conn.rollback()

conn.close()

-output in python:

(4, 'Pratyush', 'M', 20, 1200, 'Java')


(3, 'Niti Sigh', 'F', 21, 1800, 'C++')
(2, 'Madhulika', 'F', 20, 7124, 'C')
(1, 'Ashih', 'M', 17, 6550, 'PYTHON')
(5, 'Anand Seth', 'M', 16, 19500, 'Javascript')

.5 create a column course ,insert values like C , C++ and etc..

import pymysql as pym

conn=pym.connect(host="localhost" , user="root" ,passwd="123456" , database ="h1")

cursorObject = conn.cursor()

sql='''UPDATE STUDENT SET COURSE ="PYTHON" WHERE SNO=1


UPDATE STUDENT SET COURSE ="C" WHERE SNO=2
UPDATE STUDENT SET COURSE ="C++" WHERE SNO=3
UPDATE STUDENT SET COURSE ="Java" WHERE SNO=4
UPDATE STUDENT SET COURSE ="JavaScript" WHERE SNO=5'''

try:
cursorObject.execute(sql)

conn.commit()

except:

conn.rollback()

conn.close()
output :

+-----+------------+--------+------+-------+------------+
| SNO | SNAME | GENDER | AGE | FEE | course |
+-----+------------+--------+------+-------+------------+
| 1 | Ashih | M | 17 | 6550 | PYTHON |
| 2 | Madhulika | F | 20 | 7124 | C |
| 3 | Niti Sigh | F | 21 | 1800 | C++ |
| 4 | Pratyush | M | 20 | 1200 | Java |
| 5 | Anand Seth | M | 16 | 19500 | Javascript |
+-----+------------+--------+------+-------+------------+

- Display maximum and minimum FEE

import pymysql as pym

conn=pym.connect(host="localhost" , user="root" ,passwd="123456" , database ="h1")

cursorObject = conn.cursor()

sql='''SELECT MIN(FEE) FROM STUDENT'''


val ='''SELECT MAX(FEE)FROM STUDENT'''

try:
cursorObject.execute(val)
for i in cursorObject:
print(i)

cursorObject.execute(sql)

for i in cursorObject:
print(i)

conn.commit()

except:

conn.rollback()

conn.close()

-output
(19500,)
(1200,)

You might also like