#1005-无法创建表“agencasurance”“operation”(errno:150“外键约束格式不正确”)

wko9yo5t  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(328)

errno:150“外键约束格式不正确”)

DROP DATABASE IF EXISTS AgenceAssurance;
CREATE DATABASE IF NOT EXISTS AgenceAssurance;
USE AgenceAssurance;

CREATE TABLE Logininfo (
  idLogin    INT(3)  PRIMARY KEY  NOT NULL AUTO_INCREMENT,
  userName       VARCHAR(50) NOT NULL,
  pass       VARCHAR(50),
  usertype   VARCHAR(50)          DEFAULT NULL,
  GrantedKey VARCHAR(8)           DEFAULT NULL
)ENGINE = InnoDB;

CREATE TABLE Operation (
  idOpt    INT PRIMARY KEY  NOT NULL AUTO_INCREMENT,
  libelOpt TEXT,
  dateOpt  DATETIME DEFAULT now(),
  userName VARCHAR(50),
   CONSTRAINT fk_login FOREIGN KEY (userName) REFERENCES Logininfo (userName)
)ENGINE = InnoDB;

错误SQL查询:

CREATE TABLE Operation (
  idOpt    INT PRIMARY KEY  NOT NULL AUTO_INCREMENT,
  libelOpt TEXT,
  dateOpt  DATETIME DEFAULT now(),
  userName VARCHAR(50),
  CONSTRAINT fk_login FOREIGN KEY (userName) REFERENCES Logininfo (userName)
)ENGINE = InnoDB

mysql说:

1005-无法创建表agencasurance.operation(errno:150“外键约束格式不正确”)(详细信息…)

acruukt9

acruukt91#

logininfo表中的字段用户名必须是唯一的。只能有引用唯一字段的外键。
就像这样:

userName VARCHAR(50) NOT NULL UNIQUE
nxagd54h

nxagd54h2#

constraint pk\u log primary key(idlogin,username):尝试如下:constraint pk\u log primary key(username)因为:一次两个字段有pk,不能用作fk。

相关问题