辅助表(Auxiliary table)是指用于辅助其它数据表完成某些操作的数据表,比如常见的字典表、码表、映射表等。在数据库设计中,辅助表经常被用来存储静态数据,以提高查询效率。以下是一个使用辅助表查询学生名字对应的成绩的SQL代码示例:
CREATE TABLE student ( id int NOT NULL PRIMARY KEY, name varchar(50) NOT NULL );
CREATE TABLE grade ( id int NOT NULL PRIMARY KEY, grade varchar(10) NOT NULL );
CREATE TABLE student_grade ( student_id int NOT NULL, grade_id int NOT NULL, PRIMARY KEY (student_id, grade_id), FOREIGN KEY (student_id) REFERENCES student(id), FOREIGN KEY (grade_id) REFERENCES grade(id) );
INSERT INTO student VALUES (1, '张三'), (2, '李四'), (3, '王五'), (4, '赵六');
INSERT INTO grade VALUES (1, '优'), (2, '良'), (3, '中'), (4, '及格'), (5, '不及格');
INSERT INTO student_grade VALUES (1, 1), (1, 2), (2, 2), (2, 3), (3, 3), (3, 4), (4, 5);
SELECT student.name, grade.grade FROM student, grade, student_grade WHERE student.id = student_grade.student_id AND grade.id = student_grade.grade_id;