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说:
2条答案
按热度按时间acruukt91#
logininfo表中的字段用户名必须是唯一的。只能有引用唯一字段的外键。
就像这样:
nxagd54h2#
constraint pk\u log primary key(idlogin,username):尝试如下:constraint pk\u log primary key(username)因为:一次两个字段有pk,不能用作fk。