MySQL是一個關系型數據庫管理系統,它使用SQL(結構化查詢語言)來管理數據。在MySQL中,創建表是存儲數據的重要組成部分之一。下面,介紹如何在MySQL中創建八個表。
1、創建students表
CREATE TABLE students ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, age INT(3) NOT NULL, gender VARCHAR(10) NOT NULL, email VARCHAR(50) NOT NULL );
2、創建teachers表
CREATE TABLE teachers ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, age INT(3) NOT NULL, gender VARCHAR(10) NOT NULL, email VARCHAR(50) NOT NULL );
3、創建courses表
CREATE TABLE courses ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL );
4、創建classes表
CREATE TABLE classes ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, teacher_id INT(6) UNSIGNED, FOREIGN KEY(teacher_id) REFERENCES teachers(id) );
5、創建students_courses表
CREATE TABLE students_courses ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, student_id INT(6) UNSIGNED, course_id INT(6) UNSIGNED, FOREIGN KEY(student_id) REFERENCES students(id), FOREIGN KEY(course_id) REFERENCES courses(id) );
6、創建teachers_courses表
CREATE TABLE teachers_courses ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, teacher_id INT(6) UNSIGNED, course_id INT(6) UNSIGNED, FOREIGN KEY(teacher_id) REFERENCES teachers(id), FOREIGN KEY(course_id) REFERENCES courses(id) );
7、創建class_courses表
CREATE TABLE class_courses ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, class_id INT(6) UNSIGNED, course_id INT(6) UNSIGNED, FOREIGN KEY(class_id) REFERENCES classes(id), FOREIGN KEY(course_id) REFERENCES courses(id) );
8、創建students_classes表
CREATE TABLE students_classes ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, student_id INT(6) UNSIGNED, class_id INT(6) UNSIGNED, FOREIGN KEY(student_id) REFERENCES students(id), FOREIGN KEY(class_id) REFERENCES classes(id) );