#1005 - Can't create table 'classorganizer.turma' (errno: 150) - mysql

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;

There are lots of errors.
CONSTRAINT pk_prof PRIMARY KEY (id). id column doesn't exist in table Materia.
Constraint names (pk_prof, fk_usu) are already used in Professor table. You have used it in both Materia and Turma table.
Correct it by choosing unique names. Easiest way is to omit the name. MySQL handle it.
codigo column of Materia is referenced in Turma table, But its not a *key.*
professor column of Turma is defined to be NOT NULL. But you used ON DELETE SET NULL.

Related

what is the problem with my tables in mysql?

I'm going to create 3 table. table "post" and "taxonomy" are connected to "taxonomy_relationship" table with primary key and foreign key but i don't know why do i get this error:
1005 - Can't create table taxonomy_relationship (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE `post`(
id int not null AUTO_INCREMENT,
title varchar(255) not null,
content TEXT not null,
PRIMARY KEY(id)
);
CREATE TABLE `taxonomy`(
id int not null AUTO_INCREMENT,
tax_title varchar(255) not null,
type varchar(255) not null,
PRIMARY KEY(id, tax_title)
);
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
title varchar(255) not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
);
when i use "SHOW ENGINE INNODB STATUS" to show error details it returns:
>
2020-01-19 16:08:31 0x2fb8 Error in foreign key constraint of table blog.taxonomy_relationship:
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Please refer to https://mariadb.com/kb/en/library/foreign-keys/ for correct foreign key definition.
Create table blog.taxonomy_relationship with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
)'.
You should to add index KEY title_idx (tax_title) on taxonomy table :
CREATE TABLE `taxonomy` (
`id` int NOT NULL AUTO_INCREMENT,
`tax_title` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `title_idx` (`tax_title`)
);
After you can create foreign key:
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
title varchar(255) not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (title)
REFERENCES taxonomy(tax_title)
ON DELETE CASCADE
ON UPDATE CASCADE
);
Another way you can duplicate title in taxonomy_relationship but link it by id field:
CREATE TABLE taxonomy_relationship(
id INT AUTO_INCREMENT,
post_id int not null,
taxonomy_id int not null,
PRIMARY KEY(id),
FOREIGN KEY (post_id)
REFERENCES post(id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (taxonomy_id)
REFERENCES taxonomy(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);

Error on add foreign key constraint

Every time I'm trying to insert a foreign key to the table I got that message:
Error Code: 1215. Cannot add foreign key constraint 0.281 sec
My create table code:
CREATE TABLE `test`.`buy`(
`id` INT NOT NULL AUTO_INCREMENT,
`id_customer` INT UNSIGNED NOT NULL,
`code` VARCHAR(45) NOT NULL,
PRIMARY KEY(`id`),
UNIQUE INDEX `code_UNIQUE`(`code`),
CONSTRAINT `id_customer` FOREIGN KEY(`id_customer`) REFERENCES `test`.`customer`(`id_customer`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `code` FOREIGN KEY(`code`) REFERENCES `test`.`product`(`code`) ON DELETE RESTRICT ON UPDATE CASCADE
)
What should I do ?
Please format your code next time so it's easier to read.
You don't need the CONSTRAINT xxx bit, try this instead:
CREATE TABLE test.buy (
id INT NOT NULL AUTO_INCREMENT,
id_customer INT UNSIGNED NOT NULL,
code VARCHAR(45) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX code_UNIQUE (code),
FOREIGN KEY (id_customer)
REFERENCES test.customer (id_customer)
ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (code)
REFERENCES test.product (code)
ON DELETE RESTRICT ON UPDATE CASCADE
);
If that still doesn't work then make sure the other tables you reference (test.customer and test.product) already exist and that they have matching fields of the same data type.

MySql Code did not working, I want insert into table treats , same disease's

I am facing problems when querying my SQL code.
If i create the table "treats" i got an Error,
and simular by creating table "operated".
I know that I can write, that my Disease in the table "patients" is unique, this will fix my problem but it is not correct. Every patient can have the same disease. Person 1 can get a headache and person 2 too, but if I say that disease is unique, I can't add the same values on disease, so I can't say that person 1 had a headache and person 2 too.
How can i fix this code, so that i can insert into table treats , same disease's ?
For example:
insert into treats ( persNr, patNr, disease) values (123, 12, "cough");
insert into treats (persnr, patnr, disease) values(234, 21, "cough");
My code:
CREATE TABLE Doctors
(PersNr INTEGER PRIMARY KEY UNIQUE,
Name VARCHAR(30) NOT NULL,
Rang CHAR(2) CHECK (Rang in ('C2', 'C3', 'C4')));
CREATE TABLE Insurance
(InsuranceNr INTEGER PRIMARY KEY UNIQUE,
Name VARCHAR(30) NOT NULL,
TelefonNr VARCHAR(30) NOT NULL,
Headquarter VARCHAR(30) NOT NULL);
CREATE TABLE Patients
(PatNr INTEGER PRIMARY KEY UNIQUE,
Name VARCHAR(30) NOT NULL,
InsuranceNr INTEGER REFERENCES Insurance(InsuranceNr) on DELETE CASCADE,
Disease VARCHAR(30) NOT NULL,
Damage VARCHAR(2) CHECK (Insurance in('J','j','N','n')),
DescriptionDamage VARCHAR(30)Not Null);
CREATE TABLE assistant
(PerlNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Boss INTEGER,
FOREIGN KEY (Boss) REFERENCES doctors(PersNr));
CREATE TABLE hospital
(hospitalNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL UNIQUE,
Street VARCHAR(30) NOT NULL);
CREATE TABLE operations
(OperationId INTEGER PRIMARY KEY,
DoctorNr INTEGER REFERENCES doctors(PersNr) ON DELETE CASCADE,
PatientNr INTEGER REFERENCES patients(PatNr) ON DELETE CASCADE,
room INTEGER UINQUE,
hospitalnr INTEGER REFERENCES hospital(hospitalnr) ON DELETE CASCADE);
CREATE TABLE treats
(PersNr INTEGER REFERENCES doctors(PersNr) ON DELETE CASCADE,
PatNr INTEGER REFERENCES patients(PatNr) ON DELETE CASCADE,
Disease VARCHAR(30) NOT NULL References patients(Disease ) ON DELETE CASCADE,
PRIMARY KEY (PersNr, PatNr, Disease ));
CREATE TABLE operated
(PersNr INTEGER REFERENCES doctors(PersNr) ON DELETE CASCADE,
PatNr INTEGER REFERENCES patients(PatNr) ON DELETE CASCADE,
OperationID INTEGER REFERENCES operations(OperationID) ON DELETE CASCADE,
damage VARCHAR(30) NOT NULL REFERENCES patients(descriptiondamage) ON DELETE CASCADE,
PRIMARY KEY (PersNr, PatNr, OperationID, damage));
My error:
ERROR: there is no unique constraint matching given keys for referenced table "patienten"
Your table creation script is utterly messed up and full of errors. It should be like below rather. Here is a demo fiddle proving that it works: http://sqlfiddle.com/#!9/08e1e0. Comapre it with your posted script and the mistakes or errors would become obvious. Many a places you have tried referencing a column which is not a primary key and that's the biggest mistake since you can't refer a non-key column per normalization rules. I have removed all those FOREIGN KEY construction. Along with that there were many spelling mistake like UINQUE instead of UNIQUE. Also I see you are defining CHECK constraint. I have removed them as well since MySQL doesn't support it and this even if you have it; MySQL engine simply ignores it and doesn't enforce it at all.
CREATE TABLE Doctors
(PersNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Rang CHAR(2));
CREATE TABLE Insurance
(InsuranceNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
TelefonNr VARCHAR(30) NOT NULL,
Headquarter VARCHAR(30) NOT NULL);
CREATE TABLE Patients
(PatNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
InsuranceNr INTEGER ,
Disease VARCHAR(30) NOT NULL,
Damage VARCHAR(2),
DescriptionDamage VARCHAR(30) Not Null,
FOREIGN KEY (InsuranceNr) REFERENCES Insurance(InsuranceNr) on DELETE CASCADE);
CREATE TABLE assistant
(PerlNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Boss INTEGER,
FOREIGN KEY (Boss) REFERENCES doctors(PersNr));
CREATE TABLE hospital
(hospitalNr INTEGER PRIMARY KEY,
Name VARCHAR(30) NOT NULL UNIQUE,
Street VARCHAR(30) NOT NULL);
CREATE TABLE operations
(OperationId INTEGER PRIMARY KEY,
DoctorNr INTEGER ,
PatientNr INTEGER ,
room INTEGER UNIQUE,
hospitalnr INTEGER ,
FOREIGN KEY (hospitalnr) REFERENCES hospital(hospitalnr) ON DELETE CASCADE,
FOREIGN KEY (DoctorNr) REFERENCES doctors(PersNr) ON DELETE CASCADE,
FOREIGN KEY (PatientNr) REFERENCES patients(PatNr) ON DELETE CASCADE
);
CREATE TABLE treats
(PersNr INTEGER ,
PatNr INTEGER ,
Disease VARCHAR(30) NOT NULL ,
PRIMARY KEY (PersNr, PatNr, Disease ),
FOREIGN KEY (PersNr) REFERENCES doctors(PersNr) ON DELETE CASCADE,
FOREIGN KEY (PatNr) REFERENCES patients(PatNr) ON DELETE CASCADE
);
CREATE TABLE operated
(PersNr INTEGER ,
PatNr INTEGER ,
OperationID INTEGER ,
damage VARCHAR(30) NOT NULL ,
PRIMARY KEY (PersNr, PatNr, OperationID, damage),
FOREIGN KEY (PersNr) REFERENCES doctors(PersNr) ON DELETE CASCADE,
FOREIGN KEY (PatNr) REFERENCES patients(PatNr) ON DELETE CASCADE,
FOREIGN KEY (OperationID) REFERENCES operations(OperationID) ON DELETE CASCADE
);

Missing index for constraint

Getting this error - "Failed to add the foreign key constraint. Missing index for constraint 'FK_transcriptionServiceName' in the referenced table 'transcriptionservices' "
I have searched google to no avail. Any ideas?
Here's the SQL:
CREATE TABLE transcriptionConfig (
id BIGINT,
transcriptionEnabled BOOLEAN,
PRIMARY KEY(id),
CONSTRAINT `FKUSER` FOREIGN KEY (id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=ndb;
CREATE TABLE transcriptionServices (
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(80) NOT NULL,
URL VARCHAR(80) NOT NULL
) ENGINE=ndb AUTO_INCREMENT=0;
ALTER TABLE transcriptionConfig
ADD `serviceName` VARCHAR(80) NOT NULL;
ALTER TABLE transcriptionConfig
ADD CONSTRAINT FK_transcriptionServiceName
FOREIGN KEY (serviceName) REFERENCES transcriptionServices(name)
ON DELETE CASCADE;

multiple foreign keys cannot be added

I am wondering why i cannot be able to add this foreign keys.This is my schema
CREATE TABLE members(
member_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
num_1 int,
num_2 int,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (member_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE contacts(
contact_id INT NOT NULL AUTO_INCREMENT,
s1 int,
phone_number VARCHAR(10) NOT NULL,
s2 int,
s3 int,
PRIMARY KEY (contact_id),
FOREIGN KEY (s1) REFERENCES members(num_1) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s2) REFERENCES members(num_2) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s3) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8
I get this error on the mysql terminal
ERROR 1215 (HY000): Cannot add foreign key constraint
Is there a problem with my schema?.
Works for me this way:
CREATE TABLE members(
member_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
num_1 int,
num_2 int,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (member_id),
key idx_num1 (num_1),
key idx_num2 (num_2)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE contacts(
contact_id INT NOT NULL AUTO_INCREMENT,
s1 int,
phone_number VARCHAR(10) NOT NULL,
s2 int,
s3 int,
PRIMARY KEY (contact_id),
FOREIGN KEY (s1) REFERENCES members(num_1) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s2) REFERENCES members(num_2) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (s3) REFERENCES members(member_id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Just added
key idx_num1 (num_1),
key idx_num2 (num_2)
in table members. Foreign keys need to reference an indexed column (not necessarily unique and not necessarily NOT NULLable).
From the manual:
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.