表设计

表:student

字段名 类型 是否为空 主键 描述
StdID int 学生ID
StdName varchar(100) 学生姓名
Gender enum('M','F') 性别
Age int 年龄

表:Course

字段名 类型 是否为空 主键 描述
CouID int 课程ID
CName varchar(100) 课程名称
TID int 老师ID

表:Score

字段名 类型 是否为空 主键 描述
SID int 分数ID
StdDI int 学生ID
CouID int 课程ID
Grade int 分数

表:teacher

字段名 类型 是否为空 主键 描述
TID int 老师ID
Tname varchar(100) 老师姓名

创建表

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2017/11/22 23:04
# @Author : lijunjiang
# @File : Creater-tables.py
import MySQLdb # sql 语句 #创建student表
Student = '''
create table Student(
StdID int primary key not null,
StdName varchar(100) not null,
Gender enum('M','F') not null,
Age int
)
''' # 创建 Course 表
Course = '''
create table Course(
CouID int primary key not null,
CName varchar(100) not null,
TID int not null
)
''' # 创建 Score 表
Score= '''
create table Score(
SID int primary key not null,
StdID int not null,
CouID int not null,
Grade int not null
)
''' # 创建Teacher 表
Teacher='''
create table Teacher(
TID int primary key not null,
TName varchar(100) not null
)
'''
# 创建TMP 表 TMP='''
set @i :=0;
create table TMP as select (@i := @i + 1) as id from information_schema.tables limit 10;
''' def connect_mysql():
db_config = dict(host="11.11.11.11", port=3306, db="python", charset="utf8", user="python", passwd="python")
try:
cnx = MySQLdb.connect(**db_config)
except Exception as err:
raise err
return cnx if __name__ == "__main__":
sql = "create table test(id int not null);"
cnx = connect_mysql() # 连接mysql
cus = cnx.cursor() # 创建一个游标对象 try:
try:
cus.execute(Student)
cus.execute(Course)
cus.execute(Score)
cus.execute(Teacher)
cus.execute(TMP)
cus.close()
cnx.commit()
except Exception as err:
cnx.rollback()
raise err
finally:
cnx.close()

查看

mysql> use python;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show tables;
+------------------+
| Tables_in_python |
+------------------+
| Course |
| Score |
| Student |
| TMP |
| Teacher |
+------------------+
5 rows in set (0.00 sec) mysql> select * from Student;
Empty set (0.00 sec) mysql> show columns from Student;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| StdID | int(11) | NO | PRI | NULL | |
| StdName | varchar(100) | NO | | NULL | |
| Gender | enum('M','F') | NO | | NULL | |
| Age | int(11) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec) mysql> show columns from Course;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| CouID | int(11) | NO | PRI | NULL | |
| CName | varchar(100) | NO | | NULL | |
| TID | int(11) | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec) mysql> show columns from Score;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| SID | int(11) | NO | PRI | NULL | |
| StdID | int(11) | NO | | NULL | |
| CouID | int(11) | NO | | NULL | |
| Grade | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec) mysql> show columns from Teacher;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| TID | int(11) | NO | PRI | NULL | |
| TName | varchar(100) | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> show columns from TMP;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | bigint(21) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec) mysql> select * from TMP;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+------+
10 rows in set (0.00 sec) mysql>

python mysql创建表的更多相关文章

  1. Python MySQL 创建表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  2. Python MySQL 删除表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  3. Python MySQL 插入表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  4. Python MySQL 创建数据库

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  5. oracle与mysql创建表时的区别

    oracle创建表时,不支持在建表时同时增加字段注释.故采用以下方式: #创建表CREATE TABLE predict_data as ( id integer ), mid ), time dat ...

  6. 【转载】Mysql创建表时报错error150

    从mysql数据库中导出正常数据库的脚本语句,而后使用脚本语句创建数据库的过程中,执行语句提示Can't Create Table 'XXX' erro150的错误,语句执行中断,创建table失败, ...

  7. mysql 创建表时注意事项

    mysql  创建表时注意事项 mysql 想必大家都不会陌生吧  是我学习中第一个接触的的数据库 已学习就很快上手的   这是一个关系型数据库  不懂什么是关系型数据库 啊哈哈哈  现在知道啦  因 ...

  8. MySQL 创建表时,设置时间字段自己主动插入当前时间

    MySQL 创建表时,设置时间字段自己主动插入当前时间 DROP TABLE IF EXISTS `CONTENT`; CREATE TABLE `CONTENT` ( `ID` char(20) N ...

  9. mysql创建表分区

    MySQL创建表分区 create table erp_bill_index( id int primary key auto_increment, addtime datetime ); inser ...

随机推荐

  1. 15.5,centos下redis安全相关

      博文背景: 由于发现众多同学,在使用云服务器时,安装的redis3.0+版本都关闭了protected-mode,因而都遭遇了挖矿病毒的攻击,使得服务器99%的占用率!! 因此我们在使用redis ...

  2. 算法:枚举法---kotlin

    枚举法:效率低,循环所有的情况,找到正确答案 用于解决数学问题,还是很简单的. 比如,奥数里面: 算 法 描 述 题X题=题题题题题题 其中 算法描述题每一个为一个数字,请写出正确的数字. ok,我们 ...

  3. 自定义控件的getChildCount

    我真的是一步一步走过来,看过来的代码.不是能力问题,而是他们用的,我没用过,我用的他们不用.然后一句一句的问为什么,然后一句一句的去想为什么. 只有这样,才能慢慢的熟悉,东一榔头西一棒子,不是分模块再 ...

  4. jQuery的Ajax初识

    1. 什么是Ajax? Ajax是“Asynchronous Javascript And XML(异步Javascript和XML)”的缩写, 是指一种创建交互式网页应用的网站开发技术. Ajax不 ...

  5. Lazarus教程 中文版后续给出

    市面上有介绍Delphi的书籍(近来Delphi的书也是越来越少了),但没有一本系统的介绍Lazarus的书,这本书是网上的仅有的一本Lazarus教程,目前全部是英文,不过我已经着手开始翻译,争取尽 ...

  6. leetcode 211. 添加与搜索单词 - 数据结构设计 解题报告

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  7. linux sort的用法

    sort -n 表示按照数字 sort -k 表示第几列 sort -t : 表示按照:来分列 sort -r  表示从大到小排列

  8. table & colgroup

    table & colgroup // <caption>版本信息</caption> table = ` <table class="versions ...

  9. [UOJ#131][BZOJ4199][NOI2015]品酒大会

    [UOJ#131][BZOJ4199][NOI2015]品酒大会 试题描述 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个 ...

  10. 洛谷 P2597 [ZJOI2012]灾难 解题报告

    P2597 [ZJOI2012]灾难 题目描述 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发 ...