设计表

通过设计经典的学生管理系统,熟悉数据库的使用。以下是根据课程,成绩,老师这几者的关系设计表结构。

学生表:

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操作据二相关推荐

  1. C语言文件操作解析(二)【转载】

    http://www.cnblogs.com/dolphin0520/archive/2011/10/05/2199598.html C语言文件操作解析(二) C语言中对文件进行操作必须首先打开文件, ...

  2. C语言文件操作解析(二)

    C语言文件操作解析(二) C语言中对文件进行操作必须首先打开文件,打开文件主要涉及到fopen函数.fopen函数的原型为 FILE* fopen(const char *path,const cha ...

  3. PyTorch框架学习六——图像预处理transforms(二)

    PyTorch框架学习六--图像预处理transforms(二) (续)二.transforms的具体方法 4.图像变换 (1)尺寸变换:transforms.Resize() (2)标准化:tran ...

  4. java监听数据库操作_第十六篇——JDBC操作数据库之监听器

    JavaWeb应用中,很多的地方都和session有关.因此session相关的事件监听器,在日常工作中非常有用. 有时候我们需要统计当前在线的人数和访问人数总数,此时就可以使用监听器技术来很简单的实 ...

  5. saiku+kettle整合(六)olap操作

    title: saiku+kettle整合(六)olap操作 tags: categories: saiku date: 2016-08-25 18:18:54 使用saiku可以对应使用相关olap ...

  6. 【STM32】标准库与HAL库对照学习教程六--位带操作

    [STM32]标准库与HAL库对照学习教程六--位带操作 一.前言 二.准备工作 三.位带介绍 1.位带操作 2.STM32位带及位带别名区域 四.位带区与位带别名区地址转换 五.GPIO的位带操作 ...

  7. 【Visual C++】游戏开发五十六 浅墨DirectX教程二十三 打造游戏GUI界面(一)

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接: http://blog.csdn.net/poem_qianmo/article/details/16384009 作者:毛星 ...

  8. 数据结构实验六 图的操作实现

    数据结构实验六 图的操作实现 一.实验目的 1. 理解图的存储结构与基本操作: 2. 掌握图的创建过程 二.实验内容 1.根据下图,采用邻接矩阵的存储结构保存此图,并打印出邻接矩阵. 图的创建代码参考 ...

  9. 鸟哥的Linux私房菜(服务器)- 第十六章、文件服务器之二: SAMBA 服务器

    第十六章.文件服务器之二: SAMBA 服务器 最近更新日期:2011/07/29 如果想要共享档案,在 Linux 对 Linux 的环境下,最简单的方法就是透过 NIS 这玩意儿了!至于 Wind ...

  10. matlab 实验6 高层绘图操作,实验六 高层绘图操作答案

    实验六 高层绘图操作答案 实验六 高层绘图操作 实验目的: 1. 掌握绘制二维图形的常用函数 2. 掌握绘制三维图形的常用函数 3. 掌握绘制图形的辅助操作 实验内容: 1. 1. 设,在区间取101 ...

最新文章

  1. .NET中小数,浮点数和双精度之间的区别?
  2. 用AfxExtractSubString()解析复合串
  3. redis06----消息订阅
  4. html注释绕过,关于javascript:提交时绕过HTML的“ required”属性
  5. Banana PI 香蕉派项目
  6. boost::mp11::mp_eval_if_q相关用法的测试程序
  7. 【知识小课堂】4 之 索引
  8. camel 调用soap_使用Apache Camel通过soap添加WS-Security
  9. CSS3 动画 思维导图
  10. rabbitmq+topic+java_译:5.RabbitMQ Java Client 之 Topics (主题)
  11. html怎么设计自己的网页,求一份自己设计的简单网页 HTML格式
  12. Bootstrap3 源码版本的文件结构
  13. log4j输出多个自定义日志文件
  14. POJ 3233 Matrix Power Series (矩阵快速幂和)
  15. 码神之路博客部署总结补充
  16. MySQL中的B+树索引结构
  17. android存储视频文件夹在哪,Android 中 视频存储路径的一个方案
  18. Android获得IMEI和IMSI号
  19. 嵌入式C语言编程规范
  20. Java项目:JSP在线学生选课管理系统

热门文章

  1. deepin关闭ACPI
  2. 【软考】2017年11月软件设计师上午真题9-12题答案解析
  3. Spring在web开发中的应用
  4. matlabpython建模_一直在用Matlab建模,现在Python很火,用学么?
  5. Mac下PyCharm CE 配置PyQt5环境
  6. mysql全局唯一id_Mysql实现全局唯一ID
  7. 键盘按下某键 停止运行java_实现按下一个键执行操作/松开一个键停止操作
  8. aspx mysql类_aspx中的mysql操作类sqldatasource使用示例分享
  9. esp32外部中断_玩转 ESP32 + Arduino (四) 电容按键 霍尔传感器 外部中断 延时 脉冲检测...
  10. python布尔系列_python数据分析类库系列-Numpy之布尔型索引