python mysql创建表
表设计
表: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创建表的更多相关文章
- Python MySQL 创建表
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- Python MySQL 删除表
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- Python MySQL 插入表
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- Python MySQL 创建数据库
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- oracle与mysql创建表时的区别
oracle创建表时,不支持在建表时同时增加字段注释.故采用以下方式: #创建表CREATE TABLE predict_data as ( id integer ), mid ), time dat ...
- 【转载】Mysql创建表时报错error150
从mysql数据库中导出正常数据库的脚本语句,而后使用脚本语句创建数据库的过程中,执行语句提示Can't Create Table 'XXX' erro150的错误,语句执行中断,创建table失败, ...
- mysql 创建表时注意事项
mysql 创建表时注意事项 mysql 想必大家都不会陌生吧 是我学习中第一个接触的的数据库 已学习就很快上手的 这是一个关系型数据库 不懂什么是关系型数据库 啊哈哈哈 现在知道啦 因 ...
- MySQL 创建表时,设置时间字段自己主动插入当前时间
MySQL 创建表时,设置时间字段自己主动插入当前时间 DROP TABLE IF EXISTS `CONTENT`; CREATE TABLE `CONTENT` ( `ID` char(20) N ...
- mysql创建表分区
MySQL创建表分区 create table erp_bill_index( id int primary key auto_increment, addtime datetime ); inser ...
随机推荐
- 17 rest-framework框架的基本组件
序列化 创建一个序列化类 简单使用 开发我们的Web API的第一件事是为我们的Web API提供一种将代码片段实例序列化和反序列化为诸如json之类的表示形式的方式.我们可以通过声明与Django ...
- Android 打印方法调用堆栈
RuntimeException here = new RuntimeException("here"); here.fillInStackTrace(); Log.w(" ...
- dealloc时取weakself引起崩溃
今天无意这中遇到一个奇怪的崩溃,先上引起崩溃的代码: - (void)dealloc { __weak __typeof(self)weak_self = self; NSLog(@"%@& ...
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目8
2014-03-18 02:12 题目:判断一个字符串是否由另一个字符串循环移位而成. 解法:首先长度必须相等.然后将第一个串连拼两次,判断第二个串是否在这个连接串中. 代码: // 1.8 Assu ...
- python利用PIL库使图片高斯模糊
一.安装PIL PIL是Python Imaging Library简称,用于处理图片.PIL中已经有图片高斯模糊处理类,但有个bug(目前最新的1.1.7bug还存在),就是模糊半径写死的是2,不能 ...
- 【Binary Search Tree Iterator 】cpp
题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...
- js valueOf和toString方法
JavaScript原生提供一个Object对象,所有其他对象都继承自这个对象,Object对象有valueOf和valueOf方法,所以所有JS数据类型都继承了这两种方法. valueOf:返回 ...
- 2013 ACM/ICPC Asia Regional Changsha Online – C题 Color Representation Conversion (坑爹模拟题)
题意:给你三种颜色表示模式,RGB,HSV和HSL,实现任意模式之间两两转化. 1.最好别看题目中给的转化公式描述,我觉得叙述的一点也不清楚,看维基百科,把维基百科上的公式一句一句翻译过来就好 2.在 ...
- Homework-09 二维数组动态显示
思路 主要是把计算的函数由一次跑完全部改成一次一步 主要是把一个3重for循环改为能一步一次的实现 public int nstep(int n){ while((n--)!=0){ //n步长 // ...
- Tomcat 顶层结构
Tomcat中最顶层的容器叫Server,代表整个服务器,Server中包含至少一个Service,用于具体提供服务. Service主要包含两部分:Connector 和 Container ...