外键多场帮助!

时间:2011-07-21 11:15:56

标签: foreign-keys

我有2张桌子

TABLE CLASS
(
    school varchar(50),
    year   varchar(50),
    grade  varchar(50),
    className varchar(50),
    PRIMARY KEY (school,year,grade,className)
)
TABLE STUDENT
(
    student_id varchar(50) PRIMARY KEY,
    detail varchar(50)
)

现在,我想为学生创建子类。如何创建参考?

1 个答案:

答案 0 :(得分:1)

做其他人做的事情......

CREATE TABLE CLASS (
    id int not null auto_increment PRIMARY KEY, -- CREATE A KEY COLUMN
    school varchar(50),
    year   varchar(50),
    grade  varchar(50),
    className varchar(50),
    unique (school,year,grade,className)
);

CREATE TABLE STUDENT (
    student_id varchar(50) PRIMARY KEY,
    class_id int references class,  -- REFERENCE THE KEY
    detail varchar(50)
);