Taking the basic student elective course as an example, set up a database of elective courses, three tables for students, classes, and elective courses, and insert and revise the tables respectively:
Import mysqldbtry: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 () UNIQUE, Ssex VARCHAR (2), sage smallint, sdept varchar ()) Cur.execute ("Create table course (Cno varchar (4) primary key, cname varchar (+), 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 ', ', ' CS ') cur.execute ("insert into student values (' 200215122 ', ' Stan qian ', ' M ', ' ", ' CS ')") Cur.execute (" insert into student values (' 200215123 ', ' linda fan ', ' F ', ', ' ' MA ') ") Cur.execute (" insert into student values (' 200215125 ', ' zac gu ', ' M ', ', ' is ') Cur.execute ("insert into course values (' 2 ', ' Math ', null, ' 2 ') Cur.execute ("insert into course values (' 6 ', ' Data Processing '), Null, ' 2 ') "Cur.execute (" insert into course values (' 7 ', ' PASCAL ', ' 6 ', ' 4 ') Cur.execute ("insert into course values (' 5 ', ' data structure '), ' 7 ', ' 4 ') "Cur.execuTe ("insert into course values (' 1 ', ' Database ', ' 5 ', ' 4 ')") Cur.execute (" insert into course values (' 3 ', ' Information system ', ' 1 ', ' 4 ') ") Cur.execute ("insert into course values (' 4 ', ' Operating system ', ' 6 ', ' 3 ') Cur.execute ("Insert into sc values (' 200215121 ', ' 1 ', ')") Cur.execute ("INSERT into sc values (' 200215121 ', ' 2 ', ') ') Cur.execute ("insert into sc VALUES (' 200215121 ', ' 3 ', ') ") Cur.execute (" Insert into sc values (' 200215122 ', ' 2 ', ') Cur.execute ("Insert into sc values (' 200215122 ', ' 3 ', ' 80 ')") Cur.execute ("Insert into student values" (' 200215128 ', ' cuan qu ', ' M ', ' 22 ', ' is ') cur.execute ("update student set sage = ' where sno = ' 200215128 ') cur.execute("Update student set sage = sage + 7") Cur.execute ("DELETE FROM student where sno = ' 200215128 ') cur.close () Conn.commit () conn.close () except mysqldb.error, e:print ' mysql error msg: ', e
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/39/56/wKiom1O4-fajqnmcAACfaOj80aY929.jpg "title=" 2014-07-06-01-mysql-t-sql-01-show-databases.png "alt=" Wkiom1o4-fajqnmcaacfaoj80ay929.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/39/56/wKioL1O4-h_iD7TnAATwnDZ_XOo666.jpg "title=" 2014-07-06-01-mysql-t-sql-02-desc-tablename.png "alt=" Wkiol1o4-h_id7tnaatwndz_xoo666.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/39/56/wKioL1O4-fHCRBEZAAE8KCqxf-s462.jpg "title=" 2014-07-06-01-mysql-t-sql-03-select-student.png "alt=" Wkiol1o4-fhcrbezaae8kcqxf-s462.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/39/56/wKiom1O4-i_iY57mAAE_tM241o0447.jpg "title=" 2014-07-06-01-mysql-t-sql-03-select-course.png "alt=" Wkiom1o4-i_iy57maae_tm241o0447.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/39/56/wKiom1O4-kDi1qMuAADbtxl2BqE227.jpg "title=" 2014-07-06-01-mysql-t-sql-03-select-sc.png "alt=" Wkiom1o4-kdi1qmuaadbtxl2bqe227.jpg "/>
Note: The above Python program insert operation needs to run step by step, especially in the curriculum ' Cpno ' is the first course, which is the curriculum foreign key and with course. CNO references, must be inserted sequentially.
This article is from the "Kid's Turf" blog, make sure to keep this source http://zhoufwind.blog.51cto.com/1029821/1435026