如何在MySQL中创建关系表?

编程入门 行业动态 更新时间:2024-10-27 03:29:35
本文介绍了如何在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中创建关系表?

本文发布于:2023-10-19 15:05:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   如何在   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!