如何在MySQL中创建关系表? [英] How to create a relation table in MySQL?

查看:119
本文介绍了如何在MySQL中创建关系表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张表,学生课程注册与<学生注册课程的关系



我已创建学生 > sid 为主键

我创建了课程 c> cid 作为主键



现在我必须创建 enroll 如下所示,但它在关键字引用上显示错误,错误是什么?

  create table enroll(
grade char(2),
sid int not null,
cid int not null,
主键(sid,cid)
外键cid引用关于删除级联的课程,
外键sid引用学生删除级联
);


解决方案

您必须指定要引用的字段外部表和两个关键字段集必须包括()

 外键(cid)引用删除级联上的课程(name_of_foreign_field_here),
^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^


I have 3 tables, students, courses, enroll with relation as "students enroll in courses"

I have created students table with sid as primary key

I have created courses table with cid as primary key

Now I have to create enroll table which I did as follows, but it shows an error on the keyword references, what is the mistake ?

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
);

解决方案

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,
            ^   ^                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

这篇关于如何在MySQL中创建关系表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆