首页 > 解决方案 > 显示使用外键创建表的错误

问题描述

CREATE TABLE location (
  uid int not null auto_increment primary key,
  name varchar(255) NOT NULL,
  `state_uid` int not null,
  FOREIGN KEY location(state_uid)
  REFERENCES state(uid)
  ON UPDATE CASCADE
  ON DELETE RESTRICT,
  `city_uid` int not null,
  FOREIGN KEY location(city_uid)
  REFERENCES city(uid)
  ON UPDATE CASCADE
  ON DELETE RESTRICT,
  `area_uid` int not null,
  FOREIGN KEY location(area_uid)
  REFERENCES area(uid)
  ON UPDATE CASCADE
  ON DELETE RESTRICT
);

标签: sqlforeign-keys

解决方案


CREATE TABLE location  (
  uid int not null auto_increment primary key,
  name varchar(255) NOT NULL,
  state_uid int not null, 
  city_uid int not null,
  area_uid int not null,

  CONSTRAINT fk_state FOREIGN KEY (state_uid)  REFERENCES state(uid)  ,
  CONSTRAINT fk_city  FOREIGN KEY (city_uid)  REFERENCES city(uid)  ,
  CONSTRAINT fk_area FOREIGN KEY (area_uid)  REFERENCES area(uid)   
);

试试这个查询

确保父表存在


推荐阅读