#1005 - 无法创建表“classorganizer.turma” (错误号:150)
我不断收到以下错误:*#1005 - Can't create table 'classorganizer.turma' (errno: 150) (Detalhes...) *
试图创建表 Turma,尽管我'我们已经仔细检查了该类中的所有外键案例。有谁知道出了什么问题吗?
谢谢你!
CREATE TABLE Usuario(
email VARCHAR(50) NOT NULL,
nome VARCHAR(30),
senha INTEGER NOT NULL,
dataCadastro DATE NOT NULL,
CONSTRAINT pkUsu PRIMARY KEY(email),
CONSTRAINT formatoEmail CHECK(email LIKE '%@%.%')
)ENGINE=InnoDB;
CREATE TABLE Professor(
id INTEGER NOT NULL AUTO_INCREMENT,
nome VARCHAR(30) NOT NULL UNIQUE,
ranking INTEGER DEFAULT 3,
usuario VARCHAR(50) NOT NULL,
CONSTRAINT pk_prof PRIMARY KEY (id),
CONSTRAINT fk_usu FOREIGN KEY (usuario) REFERENCES Usuario(email) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT limites_rank CHECK(ranking >0 AND ranking<6)
)ENGINE=InnoDB;
CREATE TABLE Materia(
codigo VARCHAR(8) NOT NULL,
nro_turmas INTEGER DEFAULT 0,
nome VARCHAR(20) NOT NULL UNIQUE,
nro_cred_aula INTEGER DEFAULT 0,
nro_cred_trab INTEGER DEFAULT 0,
prioridade INTEGER DEFAULT 3,
usuario VARCHAR(50) NOT NULL,
CONSTRAINT pk_prof PRIMARY KEY (id),
CONSTRAINT fk_usu FOREIGN KEY (usuario) REFERENCES Usuario(email) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT limites_prio CHECK(prioridade >0 AND prioridade<6)
)ENGINE=InnoDB;
CREATE TABLE Turma(
nro INTEGER NOT NULL AUTO_INCREMENT,
prioridade INTEGER DEFAULT 3,
materia VARCHAR(8) NOT NULL,
professor INTEGER NOT NULL,
usuario VARCHAR(50) NOT NULL,
CONSTRAINT pk_turma PRIMARY KEY (nro),
CONSTRAINT fk_mat FOREIGN KEY (materia) REFERENCES Materia(codigo) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_prof FOREIGN KEY (professor) REFERENCES Professor(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_usu FOREIGN KEY (usuario) REFERENCES Usuario(email) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT limites_prio CHECK(prioridade >0 AND prioridade<6)
)ENGINE=InnoDB;
I keep getting the following error: *#1005 - Can't create table 'classorganizer.turma' (errno: 150) (Detalhes...) *
from trying to create the table Turma although I've double checked all the foreign keys cases in that class. Does anyone knows whats wrong?
Thank you!
CREATE TABLE Usuario(
email VARCHAR(50) NOT NULL,
nome VARCHAR(30),
senha INTEGER NOT NULL,
dataCadastro DATE NOT NULL,
CONSTRAINT pkUsu PRIMARY KEY(email),
CONSTRAINT formatoEmail CHECK(email LIKE '%@%.%')
)ENGINE=InnoDB;
CREATE TABLE Professor(
id INTEGER NOT NULL AUTO_INCREMENT,
nome VARCHAR(30) NOT NULL UNIQUE,
ranking INTEGER DEFAULT 3,
usuario VARCHAR(50) NOT NULL,
CONSTRAINT pk_prof PRIMARY KEY (id),
CONSTRAINT fk_usu FOREIGN KEY (usuario) REFERENCES Usuario(email) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT limites_rank CHECK(ranking >0 AND ranking<6)
)ENGINE=InnoDB;
CREATE TABLE Materia(
codigo VARCHAR(8) NOT NULL,
nro_turmas INTEGER DEFAULT 0,
nome VARCHAR(20) NOT NULL UNIQUE,
nro_cred_aula INTEGER DEFAULT 0,
nro_cred_trab INTEGER DEFAULT 0,
prioridade INTEGER DEFAULT 3,
usuario VARCHAR(50) NOT NULL,
CONSTRAINT pk_prof PRIMARY KEY (id),
CONSTRAINT fk_usu FOREIGN KEY (usuario) REFERENCES Usuario(email) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT limites_prio CHECK(prioridade >0 AND prioridade<6)
)ENGINE=InnoDB;
CREATE TABLE Turma(
nro INTEGER NOT NULL AUTO_INCREMENT,
prioridade INTEGER DEFAULT 3,
materia VARCHAR(8) NOT NULL,
professor INTEGER NOT NULL,
usuario VARCHAR(50) NOT NULL,
CONSTRAINT pk_turma PRIMARY KEY (nro),
CONSTRAINT fk_mat FOREIGN KEY (materia) REFERENCES Materia(codigo) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_prof FOREIGN KEY (professor) REFERENCES Professor(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_usu FOREIGN KEY (usuario) REFERENCES Usuario(email) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT limites_prio CHECK(prioridade >0 AND prioridade<6)
)ENGINE=InnoDB;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多错误。
约束 pk_prof 主键(id)
。id
列在表Materia
中不存在。Professor
表中使用。您已在Materia
和Turma
表中使用了它。通过选择唯一的名称来纠正它。最简单的方法是省略名称。 MySQL 处理它。
Materia
的codigo
列,但它不是 *key.*教授
Turma
列定义为NOT NULL
。但您使用了ON DELETE SET NULL
。There are lots of errors.
CONSTRAINT pk_prof PRIMARY KEY (id)
.id
column doesn't exist in tableMateria
.Professor
table. You have used it in bothMateria
andTurma
table.Correct it by choosing unique names. Easiest way is to omit the name. MySQL handle it.
codigo
column ofMateria
is referenced inTurma
table, But its not a *key.*professor
column ofTurma
is defined to beNOT NULL
. But you usedON DELETE SET NULL
.