在操作设计数据库之前,我们先要设计数据库表结构,我们就来分析分析经典的学生,课程,成绩,老师这几者他们之间的关系,我们先来分析各个主体他们直接有什么属性,并确定表结构,在实际开发过程中,根据自己的业务需要和属性,设计不同的表结构,以下是我设计的表结构:
Student
Course
Score
Teacher
有了表结构,下面我们就来创建表吧
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-11-22 14:37
-
# @file :maketable.py
-
-
import MySQLdb
-
-
def connect_mysql():
-
db_config = {
-
'host': '10.89.1.10',
-
'port': 3306,
-
'user': 'demo',
-
'passwd': '123qaz',
-
'db': 'python_test',
-
'charset': 'utf8'
-
}
-
cnx = MySQLdb.connect(**db_config)
-
return cnx
-
-
-
if __name__ == '__main__':
-
cnx = connect_mysql()
-
cus = cnx.cursor()
-
-
-
student = '''create table Student(
-
StdID int not null,
-
StdName varchar(100) not null,
-
Gender enum('M', 'F'),
-
Age tinyint
-
)'''
-
-
-
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,
-
CID int not null,
-
Grade int not null
-
)'''
-
-
-
teacher = '''create table Teacher(
-
TID int not null,
-
TName varchar(100) not null
-
)'''
-
-
-
tmp = '''set @i := 0;
-
create table tmp as select (@i := @i + 1) as id from information_schema.tables limit 10;
-
'''
-
try:
-
cus.execute(student)
-
cus.execute(course)
-
cus.execute(score)
-
cus.execute(teacher)
-
cus.execute(tmp)
-
cus.close()
-
cnx.commit()
-
except Exception as e:
-
cnx.rollback()
-
print('error')
-
raise e
-
finally:
-
cnx.close()
没有任何异常,在数据库中查看表,出现这五个表。说明这五个表已经创建成功。
执行结果:
阅读(32781) | 评论(0) | 转发(0) |