mariadb中的外键约束格式不正确

cgyqldqp  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(365)

这是我的sql代码,每次我按命令执行它时,它都会给我一个错误,有人能告诉我这里缺少了什么吗?这是给我“外键约束格式不正确”的错误。任何帮助都会很棒。

Create table course (course_id varchar(8), title varchar(50), dept_name varchar(20), credits numeric(2,0) check (credits > 0), primary key (course_id), foreign key (dept_name) references department on delete set null );

这是参考表:

create table department
    (dept_name      varchar(20), 
     building       varchar(15), 
     budget             numeric(12,2) check (budget > 0),
     primary key (dept_name)
    );
72qzrwbm

72qzrwbm1#

见文件:
外键
...

[CONSTRAINT [symbol]] FOREIGN KEY
    [index_name] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name,...)
    [ON DELETE reference_option]
    [ON UPDATE reference_option]

reference_option:
    RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT

...
例子:

create table if not exists course (
  course_id varchar(8),
  title varchar(50),
  dept_name varchar(20),
  credits numeric(2,0) check (credits > 0),
  primary key (course_id),
  foreign key (dept_name)
  -- references department on delete set null
  references department (dept_name) on delete set null
);

见小提琴。

相关问题