日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

(六)6-3Mysql操作据二

發布時間:2024/10/12 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (六)6-3Mysql操作据二 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設計表

通過設計經典的學生管理系統,熟悉數據庫的使用。以下是根據課程,成績,老師這幾者的關系設計表結構。

學生表:

Student

?

?

?

?

字段名

類型

是否為空

主鍵

描述

stdid

int

學生ID

stdname

varchar(100)

 

學生姓名

gender

enum('M', 'F')

 

性別

age

int

 

年齡

課程表:

Course

?

?

?

?

字段名

類型

是否為空

主鍵

描述

couid

int

課程ID

cname

varchar(50)

 

課程名字

tid

int

 

老師ID

成績表

字段名

類型

是否為空

主鍵

描述

sid

int

分數ID

stdid

int

 

學生id

couid

int

 

課程id

grade

int

 

分數

教師表:

Teacher

?

?

?

?

字段名

類型

是否為空

主鍵

描述

tid

int

老師ID

tname

varcher(100)

 

老師名字

?


?有了表結構,創建表

#!/usr/bin/env python #coding:utf8import MySQLdb def connect_mysql():db_config = {"host":"172.16.61.158","port":3306,"user":"root","passwd":"123456","db":"stu","charset":"utf8",}try:cnx = MySQLdb.connect(**db_config)except Exception as e :raise ereturn cnxstudent = '''create table student(stdid int not NULL ,stdname varchar(100) not null,gender enum('M','F'),age int);''' course = '''create table course(couid int not null,cname varchar(50) not null ,tid int not null );'''score = '''create table score(sid int not null,stdid int not null,couid int not null,grade int not null);'''teacher = '''create table teacher(tid int not null,tname varchar(100) not null );'''if __name__ == "__main__":cnx = connect_mysql()print(cnx)cus = cnx.cursor()try :print studentcus.execute(student)print coursecus.execute(course)print scorecus.execute(score)print teachercus.execute(teacher)cus.close()cnx.commit()except Exception as e :cnx.rollback()print("error,{0}".format(e))raise efinally:cnx.close()

執行結果:

<_mysql.connection open to '172.16.61.158' at 27b6e48> create table student(stdid int not NULL ,stdname varchar(100) not null,gender enum('M','F'),age int); create table course(couid int not null,cname varchar(50) not null ,tid int not null ); create table score(sid int not null,stdid int not null,couid int not null,grade int not null); create table teacher(tid int not null,tname varchar(100) not null );

?有了表,下步添加數據:

?

#!/usr/bin/env python #coding:utf8import MySQLdb def connect_mysql():db_config = {"host":"172.16.61.158","port":3306,"user":"root","passwd":"123456","db":"stu","charset":"utf8",}try:cnx = MySQLdb.connect(**db_config)except Exception as e :raise ereturn cnxstudent = '''set @i:= 10000; insert into student select @i:=@i +1,substr(concat(sha1(rand()),sha1(rand())),1,5+floor(rand() +100)),case floor(rand()*10)mod 2 when 1 then 'M' else 'F' end ,25-floor(rand()*5) from tmp a,tmp b,tmp c,tmp d; ''' course = '''set @i := 10; insert into course select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 40)), 1 + floor(rand() * 100) from tmp a;''' score = '''set @i := 10000; insert into score select @i := @i +1, floor(10001 + rand()*10000), floor(11 + rand()*10), floor(1+rand()*100) from tmp a, tmp b, tmp c, tmp d;''' teacher = '''set @i := 100; insert into teacher select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 80)) from tmp a, tmp b;''' if __name__ == "__main__" :cnx = connect_mysql()try :print studentcus_students = cnx.cursor()cus_students.execute(student)cus_students.close()print coursecus_course = cnx.cursor()cus_course.execute(course)cus_course.close()print scorecus_score = cnx.cursor()cus_score.execute(score)cus_score.close()print teachercus_theacher = cnx.cursor()cus_theacher.execute(teacher)cus_theacher.close()print("OK")cnx.commit()except Exception as e :cnx.rollback()print('error')raise efinally:cnx.close()

運行結果:

set @i:= 10000; insert into student select @i:=@i +1,substr(concat(sha1(rand()),sha1(rand())),1,5+floor(rand() +100)),case floor(rand()*10)mod 2 when 1 then 'M' else 'F' end ,25-floor(rand()*5) from tmp a,tmp b,tmp c,tmp d;set @i := 10; insert into course select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 40)), 1 + floor(rand() * 100) from tmp a;set @i := 10000; insert into score select @i := @i +1, floor(10001 + rand()*10000), floor(11 + rand()*10), floor(1+rand()*100) from tmp a, tmp b, tmp c, tmp d;set @i := 100; insert into teacher select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 80)) from tmp a, tmp b;OK

?驗證:

查詢數據

?

#!/usr/bin/env python #coding:utf8import MySQLdb import codecs def connect_mysql():db_config = {"host":"172.16.61.158","port":3306,"user":"root","passwd":"123456","db":"stu","charset":"utf8",}try:cnx = MySQLdb.connect(**db_config)except Exception as e :raise ereturn cnxif __name__ == "__main__":cnx = connect_mysql()sql = '''select * from student where stdname in (select stdname from student group by stdname having count(1)>1 ) order by stdname;'''print sqltry:cus = cnx.cursor()cus.execute(sql)result = cus.fetchall()for res in result:print rescus.close()cnx.commit()except Exception as e:cnx.rollback()print('error')raise efinally:cnx.close()

運行結果:

select * from student where stdname in (select stdname from student group by stdname having count(1)>1 ) order by stdname; (19001L, u'e03fd17e980627e67c2e1b583f611e4a0855e46e176237ea5e6ba7c2c6a992787447ad57d0a4597d', u'F', 21L) (19021L, u'e03fd17e980627e67c2e1b583f611e4a0855e46e176237ea5e6ba7c2c6a992787447ad57d0a4597d', u'M', 25L) (19028L, u'e03fd17e980627e67c2e1b583f611e4a0855e46e176237ea5e6ba7c2c6a992787447ad57d0a4597d', u'F', 23L)

?

轉載于:https://www.cnblogs.com/pythonlx/p/7881152.html

總結

以上是生活随笔為你收集整理的(六)6-3Mysql操作据二的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。