1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| 设计一个简单的培训机构的管理系统,需求如下: 1. 学校有以下培训班,比如python go DBA linux运维 软件测试 2. 老师可以教不同班级 3. 学生只能在班级中的一个里边上课 根据上述的需求设计一个简单的数据库,以及表,关于表的字段可以随意发挥
分析: 班级和学生一对多,外键建在多的一方即学生 学生和学生信息一对一 班级和老师多对多
-- 创建库 create database manage_sys charset=utf8mb4; -- 创建表 -- 创建班级表 CREATE TABLE bj ( id INT PRIMARY KEY auto_increment, bj_name VARCHAR ( 10 ), bj_desc VARCHAR ( 36 ) ) ENGINE = INNODB charset = utf8mb4;
-- 创建学生表 CREATE TABLE xs ( id INT PRIMARY KEY auto_increment, xs_name VARCHAR ( 10 ), score FLOAT ( 5, 2 ) UNSIGNED, bj_id INT, info_id INT, FOREIGN KEY ( bj_id ) REFERENCES bj ( id ) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY ( info_id ) REFERENCES stu_info ( id ) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE = INNODB charset = utf8mb4;
-- 创建老师表 CREATE TABLE teacher ( id INT PRIMARY KEY auto_increment, teacher_name VARCHAR ( 10 ), age INT UNSIGNED, sex enum ( '男', '女' ) ) ENGINE = INNODB charset = utf8mb4;
-- 创建班级和老师的多对多关系表 CREATE TABLE bj2teacher ( id INT PRIMARY KEY auto_increment, bj_id INT, teacher_id INT, FOREIGN KEY ( bj_id ) REFERENCES bj ( id ) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY ( teacher_id ) REFERENCES teacher ( id ) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE = INNODB charset = utf8mb4;
-- 创建学生信息表 CREATE TABLE stu_info ( id INT PRIMARY KEY auto_increment, age INT, sex enum ( '男', '女' ), employment bool DEFAULT ( TRUE ) ) ENGINE = INNODB charset = utf8mb4;
|