在mysql中生成数据库和数据表

 

#创建数据库
CREATE DATABASE s4day65 DEFAULT CHARSET utf8;
#班级表 CREATE TABLE class (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
            title CHAR(20)
            )ENGINE
=INNODB DEFAULT CHARSET=utf8; #学生表 有一个指向班级表的外键 CREATE TABLE student (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,              NAME CHAR(20),              class_id INT,              CONSTRAINT fk_s_c FOREIGN KEY (class_id) REFERENCES class(id)              )ENGINE=INNODB DEFAULT CHARSET=utf8; #教师表 CREATE TABLE teacher (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,              NAME CHAR(20)
             )ENGINE=INNODB DEFAULT CHARSET=utf8; #教师和班级关系表
CREATE TABLE teacher2class(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                class_id INT,
                teacher_id INT,
CONSTRAINT fk_tc_c FOREIGN KEY (class_id) REFERENCES class(id),
                CONSTRAINT fk_tc_t FOREIGN KEY (teacher_id) REFERENCES teacher(id)
                )ENGINE=INNODB DEFAULT CHARSET=utf8;
posted on 2019-10-09 20:37  闪亮的金子  阅读(184)  评论(0编辑  收藏  举报