Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 显示使用外键创建表时出错_Sql_Foreign Keys - Fatal编程技术网

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
);