mysql - creating table with foreign keys - error 150 - mysql

Actually I was used this sql script to create the table scheme on mysql 5.0.24, and it was just working fine, the problem is when i tried same script on mysql 5.5.16 i get below error message:
general error message from server: "can't create table 'amdb.am_wta_methodtree' (errno: 150)"
and here it is my sql:
create table AM_WTA_MethodInstance(
"ID" BIGINT NOT NULL,
"TRANSACTIONID" BIGINT NOT NULL,
"INVOCATIONTIME" BIGINT NOT NULL,
"METHODIDENTIFIERID" BIGINT NOT NULL,
"THREADID" VARCHAR(255) NOT NULL,
"INCLUSIVETIME" BIGINT NOT NULL DEFAULT 0,
"EXCLUSIVETIME" BIGINT NOT NULL DEFAULT 0,
"STATUS" INTEGER(1) NOT NULL DEFAULT 0,
"EXCEPTIONMESSAGE" TEXT,
PRIMARY KEY ("ID"),
FOREIGN KEY(METHODIDENTIFIERID) REFERENCES AM_WTA_MethodIdentifier(ID),
FOREIGN KEY(TRANSACTIONID) REFERENCES AM_WTA_Transaction(TRANSACTIONID) ON DELETE CASCADE)
create table AM_WTA_MethodTree(
"PARENTID" BIGINT NOT NULL,
"CHILDID" BIGINT NOT NULL,
"INVOCATIONTIME" BIGINT NOT NULL,
PRIMARY KEY ("PARENTID","CHILDID"),
FOREIGN KEY(PARENTID,CHILDID) REFERENCES AM_WTA_MethodInstance(ID,ID) ON DELETE CASCADE)

If you create the tables commit and after create the foreign key it should work.
Thanks

The method instance table should be created first before before it can be referenced. I suggest that you create the Tables first.

Related

how to fix Error in EER diagram to create tables?

when I wanted to generate tables, I got this error I don't know how to fix it. Is anyone face to this problem and how you solve it?
thanks
CREATE TABLE IF NOT EXISTS `tb_inv_detail` (
`inv_id_fk` INT UNSIGNED NOT NULL,
`part_id_fk` INT UNSIGNED NOT NULL,
`qty` DECIMAL(12,2) NOT NULL,
`tb_detailed` VARCHAR(50) NULL,
`tb_inv_detailcol` VARCHAR(45) NULL,
PRIMARY KEY (`inv_id_fk`),
INDEX `fk_tb_inv_detail_tb_parts1_idx` (`part_id_fk` ASC) VISIBLE,
CONSTRAINT `fk_tb_inv_detail_tb_parts1`
FOREIGN KEY (`part_id_fk`)
REFERENCES `tb_parts` (`part_id_pk`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_inv_detail_tb_invoice1`
FOREIGN KEY (`inv_id_fk`)
REFERENCES `tb_invoice` (`inv_id_pk`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 25 succeeded, 1 failed
Also this screenshot of my EER diagram:
Well the obvious code which is missing from what you posted above is the DDL for the two tables referred to by the foreign keys defined in tb_inv_detail. These tables are:
tb_parts
tb_invoice
These tables must be defined first, with correctly named primary key columns to match the foreign keys in your table above.
CREATE TABLE tb_parts (
part_id_pk INT UNSIGNED NOT NULL,
PRIMARY KEY (part_id_pk), -- referred to by part_id_fk
...
)
CREATE TABLE tb_invoice (
inv_id_pk INT UNSIGNED NOT NULL,
PRIMARY KEY (inv_id_pk), -- referred to by inv_id_fk
...
)

sql error that i cant solve (7)

can anyone help with this error , when i run my code it comes up with error 1005 can't create table my code looks like this can anyone point out the source of this error im using codio mysql if that helps
CREATE TABLE IF NOT EXISTS entries (
entries_id INT NOT NULL AUTO_INCREMENT,
students_id INT UNSIGNED NOT NULL,
Date_of_exam DATETIME NOT NULL,
subjects_id INT UNSIGNED NOT NULL,
PRIMARY KEY (entries_id),
FOREIGN KEY (students_id) REFERENCES students(students_id),
FOREIGN KEY (subjects_id) REFERENCES subjects(subjects_id));
this is the error
mysql> SOURCE task7
ERROR 1005 (HY000): Can't create table 'exams.entries' (errno: 150)
This error related to foreign keys. Check following items:
Name of tables and columns that exist on foreign keys are correct.
Type of foreign keys columns are same.
Data of foreign keys not conflict with them.
Note: You can disable check foreign key on query. Sure enable this option after query.
SET FOREIGN_KEY_CHECKS=0;
CREATE TABLE IF NOT EXISTS entries (
entries_id INT NOT NULL AUTO_INCREMENT,
students_id INT UNSIGNED NOT NULL,
Date_of_exam DATETIME NOT NULL,
subjects_id INT UNSIGNED NOT NULL,
PRIMARY KEY (entries_id),
FOREIGN KEY (students_id) REFERENCES students(students_id),
FOREIGN KEY (subjects_id) REFERENCES subjects(subjects_id)
);
SET FOREIGN_KEY_CHECKS=1;
The error itself is related with the foreign keys (as removing them creates the table with no errors).
Main possible causes:
One of the referenced tables doesn't exists
The field of the referenced tables doesn't exists
The data types of the referenced table doesn't match (I assume is this).
As i can see your primary key on your table is INT, assuming you use INT as your primary keys, students_id is INT UNSIGNED, possibly the cause of your error.

Cannot create foreign key to different database in mysql

I have two tables on different databases in mysql. I'm trying to create a composite foreign key from one table to the other and for some reason it doesn't work. It only works if I use a single primary key field instead of a composite key.
The problematic constraint in the example below is fk_dummy_table11. I get Error Code: 1215. Cannot add foreign key constraint when executing the statement.
Create table statement for "dummy" in DB NREAP:
CREATE TABLE IF NOT EXISTS `NREAP`.`dummy` (
`id` INT NOT NULL,
`table1_TDO_COD_TIP_DOC` VARCHAR(14) NOT NULL,
`table1_RFI_NUM_DOC` INT NOT NULL,
`table1_RFI_VER_DOC` INT NOT NULL,
`table1_RFI_NOM_FIC` VARCHAR(128) NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_dummy_table11_idx` (`table1_TDO_COD_TIP_DOC` ASC, `table1_RFI_NUM_DOC` ASC, `table1_RFI_VER_DOC` ASC, `table1_RFI_NOM_FIC` ASC),
CONSTRAINT `fk_dummy_table11`
FOREIGN KEY (`table1_TDO_COD_TIP_DOC` , `table1_RFI_NUM_DOC` , `table1_RFI_VER_DOC` , `table1_RFI_NOM_FIC`)
REFERENCES `TRANS`.`table1` (`TDO_COD_TIP_DOC` , `RFI_NUM_DOC` , `RFI_VER_DOC` , `RFI_NOM_FIC`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Create table statement for "table1" in DB TRANS:
CREATE TABLE IF NOT EXISTS `TRANS`.`table1` (
`TDO_COD_TIP_DOC` VARCHAR(14) NOT NULL,
`RFI_NUM_DOC` INT NOT NULL,
`RFI_VER_DOC` INT NOT NULL,
`RFI_NOM_FIC` VARCHAR(128) NOT NULL,
`RFI_LOC_FIC` VARCHAR(1000) NULL,
`RFI_DES_FIC` VARCHAR(255) NULL,
`RFI_TIPO` VARCHAR(100) NOT NULL,
`DAT_ALT` DATE NOT NULL,
`COD_UTI_ALT` VARCHAR(14) NOT NULL,
`DFI_VER_DOC` INT NULL,
`DFI_NUM_SEQ` INT NULL,
`CAM_ANO_INI_CAM` VARCHAR(4) NULL,
PRIMARY KEY (`TDO_COD_TIP_DOC`, `RFI_NUM_DOC`, `RFI_VER_DOC`, `RFI_NOM_FIC`))
ENGINE = InnoDB;
Please help, this is so frustrating...
Edit1 - I'm running mysql version 5.7.12
Edit2 - I've ran the SHOW ENGINE INNODB STATUS command, it gives me the following output:
I'm running mysql version 5.7.12, it's the latest one I think.
I've ran innodb status:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2016-07-13 10:28:34 0x124c Error in foreign key constraint of table nreap/dummy:
FOREIGN KEY (`table1_TDO_COD_TIP_DOC` , `table1_RFI_NUM_DOC` , `table1_RFI_VER_DOC` , `table1_RFI_NOM_FIC`)
REFERENCES `TRANS`.`table1` (`TDO_COD_TIP_DOC` , `RFI_NUM_DOC` , `RFI_VER_DOC` , `RFI_NOM_FIC`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB:
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 http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.
I've eventually figured out the problem.
I've narrowed it down to a problem in using VARCHAR keys, the restraint worked fine with other datatypes. After a few experiments I've added a specific charset for each schema and it worked. Adding the specific charset to each table also works.
Thanks for the help!

mysql table is not creating

I am getting error when I create table with foreign key
create table _users(_id int(20) unsigned NOT NULL AUTO_INCREMENT,
_user_fullname varchar(50)not null,
_user_username varchar(160) not null,
_user_password varchar(200) not null,_user_remember_me tinyint,
_user_email varchar(30),
_user_mobile varchar(15),
_user_age varchar(10)
,primary key(_id,_user_email,_user_mobile));
_users table created successfully..there were no error..
But When I want to create employee table :
CREATE TABLE employee ( _Id INT NOT NULL AUTO_INCREMENT,
_user_mobile VARCHAR(15) not null,
_name varchar(15),
_org varchar(10),
PRIMARY KEY (_Id),
foreign key (_user_mobile) references _users(_user_mobile));
Its showing error:
ERROR 1005 (HY000): Can't create table 'DB.employee' (errno: 150)
What am I doing wrong??
Hey In this case you just need to do one thing ,
you just need to add index to the reference column of the user table and then run the create table for employee
ALTER TABLE `_users` ADD INDEX (`_user_mobile`);
After running above query just run the below query :-
CREATE TABLE `employee`(
`_Id` INT(11) NOT NULL AUTO_INCREMENT,
`_user_mobile` VARCHAR(15) NOT NULL,
`_name` VARCHAR(15),
`_org` VARCHAR(10),
PRIMARY KEY (`_Id`),
FOREIGN KEY (`_user_mobile`) REFERENCES `_users`(`_user_mobile`) );
In this way you will get rid of the error 1005 of mysql which says that you need to have index on the reference column of parent table.
150 is a foreign key error:
C:\>perror 150
MySQL error code 150: Foreign key constraint is incorrectly formed
Getting the exact error message is very tricky. You need to run this query:
show engine innodb status
... and search in the output:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
160627 14:09:32 Error in foreign key constraint of table test/employee:
foreign key (_user_mobile) references _users(_user_mobile)):
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.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
Once you know that, it'd be easy to add the missing index:
ALTER TABLE `_users`
ADD UNIQUE INDEX `_user_email` (`_user_email`);
But I wouldn't if I were you. It's weird to use mobile phone number as key. Instead, just simplify the primary key:
create table _users(_id int(20) unsigned NOT NULL AUTO_INCREMENT,
_user_fullname varchar(50)not null,
_user_username varchar(160) not null,
_user_password varchar(200) not null,_user_remember_me tinyint,
_user_email varchar(30),
_user_mobile varchar(15),
_user_age varchar(10)
,primary key(_id));
... and use in the linked table:
CREATE TABLE employee ( _Id INT NOT NULL AUTO_INCREMENT,
_user_id int(20) unsigned not null,
_name varchar(15),
_org varchar(10),
PRIMARY KEY (_Id),
foreign key (_user_id) references _users(_id));
The problem is in the foreign key part. If you remove that, table will be created without a problem.
If you need to use that foreign key, you need to use InnoDB as the storage engine of MySQL. InnoDB allows a foreign key constraint to reference a non-unique key as can be seen in here.

Changing both PK and FK column type using MySQL Workbench

I need to change a bunch of PK column types, and use MySQL's Workbench to design my database.
Is it possible to change the PK's type in a given table, and have all the FK types automatically change?
I performed to check the workbench. I created the table employees and employees_hobbies. The PK of tableemployees is id_employees the type INT(11). The employees_hobbies table has a FK id_employees the type INT(11). I tried to change the type of the field in id_employees of table employees for CHAR(5) and the error is as follows:
ERROR 1025: Error on rename of './teste/#sql-4aa_10875' to
'./teste/employees' (errno: 150) SQL Statement: ALTER TABLE
teste.employees CHANGE COLUMN id_employees id_employees
CHAR(5) NOT NULL
ERROR: Error when running failback script.
The error means (errno: 150)
MySQL error code 150: Foreign key constraint is incorrectly formed
My tables:
CREATE TABLE `employees` (
`id_employees` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`url_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id_employees`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `employees_hobbies` (
`id_employees_hobbies` int(11) NOT NULL AUTO_INCREMENT,
`id_employees` int(11) NOT NULL,
`id_hobbies` int(11) NOT NULL,
PRIMARY KEY (`id_employees_hobbies`),
KEY `fk_employees_hobbies_1_idx` (`id_employees`),
KEY `fk_employees_hobbies_2_idx` (`id_hobbies`),
CONSTRAINT `fk_employees_hobbies_1` FOREIGN KEY (`id_employees`) REFERENCES `employees` (`id_employees`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_employees_hobbies_2` FOREIGN KEY (`id_hobbies`) REFERENCES `hobbies` (`id_hobbies`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You can not do this in Workbench.
The solution is to generate the script and parse all FK performing the exchange type.