1

「学生がコースに登録する」という関係でstudents、、、coursesの3つのテーブルがあります。enroll

主キーとしてstudentsテーブルを作成しましたsid

主キーとしてcoursesテーブルを作成しましたcid

次のようにテーブルを作成する必要がenrollありますが、キーワードreferencesにエラーが表示されます。間違いは何ですか?

create table enroll(
    grade char(2),
    sid int not null,
    cid int not null,
    primary key(sid,cid),
    foreign key cid references courses on delete cascade,
    foreign key sid references students on delete cascade
);
4

3 に答える 3

3

You have to specify which fields you're referencing in the foreign table, and both sets of key field(s) must be bracketed () as well.

foreign key (cid) references courses (name_of_foreign_field_here) on delete cascade,
            ^   ^                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
于 2012-04-13T18:01:05.577 に答える
0

simple piece of advice

for many to many relationship tables you have to make another relation table, and take PK ids of both the tables and save in this relationship table

while for a one to many relationship table put an attribute for the PK of the one table in the attributes of the many table and always link them like that

于 2012-04-13T18:00:24.377 に答える
0

I think you need to put the cid and sid in braces. This is a sample code try it out and let know your result. Please make sure that the size and data type of the references i.e. the primary keys of course and student and that of enroll are the same.

CREATE TABLE `enroll` (
    `sid` INT(10) NOT NULL,
    `cid` INT(10) NOT NULL,
    PRIMARY KEY (`sid`, `cid`),
    INDEX `FK_enroll_course` (`cid`),
    CONSTRAINT `FK_enroll_course` FOREIGN KEY (`cid`) REFERENCES `course` (`id`) ON DELETE CASCADE,
    CONSTRAINT `FK_enroll_student` FOREIGN KEY (`sid`) REFERENCES `student` (`id`) ON DELETE CASCADE
)
于 2012-04-13T18:04:44.420 に答える