Sql 显示使用外键创建表时出错
请尝试此查询Sql 显示使用外键创建表时出错,sql,foreign-keys,Sql,Foreign 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, CONSTRA
确保父表存在您的版本几乎正常。问题在于外键引用中的
位置
您对级联内容或列的顺序没有问题。因此,这是可行的:
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)
);
这是一个SQL小提琴
请注意,通常在列定义之后放置显式的外键
(和其他约束),这是传统的做法,对此没有规则或标准。事实上,大多数数据库都支持内联外键定义。但是,MySQL没有。获取重复的键名位置时出现了什么错误使用此约束描述外键(nameofcolumn)引用RefferanceTable(primaryKey name)k我将在我的数据库中尝试
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)
);
CREATE TABLE location (
uid int not null auto_increment primary key,
name varchar(255) NOT NULL,
`state_uid` int not null,
FOREIGN KEY (state_uid) REFERENCES state(uid) ON UPDATE CASCADE ON DELETE RESTRICT,
`city_uid` int not null,
FOREIGN KEY (city_uid) REFERENCES city(uid) ON UPDATE CASCADE ON DELETE RESTRICT,
`area_uid` int not null,
FOREIGN KEY (area_uid) REFERENCES area(uid) ON UPDATE CASCADE ON DELETE RESTRICT
);