以基本的学生选课为例,建立选课数据库,学生、班级、选课信息三张表,并分别对表进行插删改操作:import MySQLdb
try:
conn = MySQLdb.connect(host = ‘localhost‘, user = ‘root‘, passwd = ‘root‘, db = ‘xuanke‘, port = 3306)
cur = conn.cursor()
cur.execute("CREATE DATABASE xuanke")
cur.execute("CREATE TABLE Student (Sno VARCHAR(9) PRIMARY KEY, Sname VARCHAR(20) UNIQUE, Ssex VARCHAR(2), Sage SMALLINT, Sdept VARCHAR(20))")
cur.execute("CREATE TABLE Course (Cno VARCHAR(4) PRIMARY KEY, Cname VARCHAR(40), Cpno VARCHAR(4), Ccredit SMALLINT, FOREIGN KEY (Cpno) REFERENCES Course(Cno))")
cur.execute("CREATE TABLE SC (Sno VARCHAR(9), Cno VARCHAR(4), Grade SMALLINT, PRIMARY KEY (Sno, Cno), FOREIGN KEY (Sno) REFERENCES Student(Sno), FOREIGN KEY (Cno) REFERENCES Course(Cno))")
cur.execute("INSERT INTO Student VALUES (‘200215121‘, ‘Stephen Zhou‘, ‘M‘, ‘20‘, ‘