sql - 显示使用外键创建表的错误
问题描述
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
);
解决方案
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)
);
试试这个查询
确保父表存在
推荐阅读
- javascript - 如何在 IONIC 3 的 ion-tab 中停止传播
- javascript - 为什么我必须像“state.topicsReducer.topics”一样访问我的 Redux 状态?
- web-scraping - 通过设置多个过滤器来抓取画面仪表板
- react-native - 如何从 Promise 中获取号码
- python-3.x - 将字符串列表转换为 x,y 元组列表
- python - 用普通括号括起来的方括号有什么作用?
- javascript - 如何在引导下拉功能中正确显示所选值?
- flutter - ListView.build 过滤后渲染错误的元素
- ruby-on-rails - Rails ActiveStorage 为 PNG 上传添加黑色背景颜色
- c - time_base 适用于视频但对音频有不良影响