MySQL error #1005 errno 150 - mysql

I can't figure out why I'm getting an error here.
CURRENT TABLES:
CREATE TABLE `Clients` (
`ClientID` varchar(8) NOT NULL,
`Age` varchar(20) NOT NULL,
`Postcode` varchar(6) NOT NULL,
PRIMARY KEY (`ClientID`,`Age`,`Postcode`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `Disclosure` (
`ClientID` varchar(8) NOT NULL,
`Fname` varchar(100) DEFAULT NULL,
`Lname` varchar(100) DEFAULT NULL,
`Email` varchar(100) DEFAULT NULL,
`Postcode` varchar(6) NOT NULL,
`Offender` varchar(45) DEFAULT NULL,
`Location` varchar(100) DEFAULT NULL,
`Age` varchar(20) NOT NULL,
`Support` varchar(45) DEFAULT NULL,
`Disclosure` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`ClientID`,`Postcode`,`Age`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
trying to add foreign key
ALTER TABLE `FYP`.`Disclosure`
ADD CONSTRAINT `client`
FOREIGN KEY (`ClientID` , `Postcode` , `Age`)
REFERENCES `FYP`.`Clients` (`ClientID` , `Postcode` , `Age`)
ON DELETE NO ACTION
ON UPDATE CASCADE;
I've tried using show status to see what the error is but I still cant figure it out.
SHOW ENGINE INNODB STATUS
returns:
------------------------ LATEST FOREIGN KEY ERROR
------------------------ 171102 11:35:10 Error in foreign key constraint of table FYP/#sql-266_33b:
FOREIGN KEY (ClientID , Postcode , Age) REFERENCES
FYP.Clients (ClientID , Postcode , Age) ON DELETE NO
ACTION 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. See
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

You need an index on the referenced columns. The order of the columns in an index matter. In the clients table change the primary key to
(`ClientID`,`Postcode`,`Age`)
This should do it
ALTER TABLE clients DROP PRIMARY KEY, ADD PRIMARY KEY (`ClientID`,`Postcode`,`Age`);

Related

Error creating foreign key on userid, followerid (check data types)

Hey I'm having issues setting foreign keys pointing from my followings table:
CREATE TABLE `followings` (
`id` int(26) NOT NULL,
`userid` varchar(256) NOT NULL,
`followerid` varchar(256) NOT NULL,
`time` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
to my users table:
CREATE TABLE `users` (
`id` int(26) NOT NULL,
`userid` varchar(256) NOT NULL,
`handle` varchar(256) NOT NULL,
`email` varchar(256) NOT NULL,
`password` varchar(256) NOT NULL,
`phone` varchar(256) NOT NULL,
`dateofbirth` varchar(99) NOT NULL,
`phoneverified` varchar(25) DEFAULT NULL,
`emailverified` varchar(25) DEFAULT NULL,
`authentic` tinyint(1) NOT NULL DEFAULT 0,
`datecreated` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `userid` (`userid`),
ADD UNIQUE KEY `handle` (`handle`);
I have checked the collation etc of the fields are the same yet i am still getting an error on phpmyadmin
Error creating foreign key on userid, followerid (check data types)
Any possible reasons for such error
This is the sql generated by phpmyadmin before the error
ALTER TABLE `followings` ADD FOREIGN KEY (`userid`, `followerid`) REFERENCES `users`(`userid`, `userid`) ON DELETE RESTRICT ON UPDATE RESTRICT;
You shouldn't be making a multi-key foreign key. You need separate foreign keys for userid and followerid, since they refer to different rows in the users table.
ALTER TABLE followings ADD FOREIGN KEY (userid) REFERENCES users (userid);
ALTER TABLE followings ADD FOREIGN KEY (followerid) REFERENCES users (userid);
Also, it's usually preferable to have the foreign key reference the primary key.

Why I am having a this error on phpmyadmin: Error creating foreign key on revision (check data types)?

I´m trying to create Foreign Keys in phpmyadmin, but I get this error:
Error creating foreign key on revision (check data types)
I don´t understand it because the data types are equal. So, what I want is to create a Foreign Key from 'acoustictreatment' to 'filterspecifications' which contains tag, offerid and revision. But I get the error that I mentioned.
This are my tables:
CREATE TABLE `offer` (
`projectid` varchar(20) NOT NULL,
`customer` varchar(255) NOT NULL,
`creator` varchar(255) NOT NULL,
`date` date NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `offer`
ADD PRIMARY KEY (`projectid`,`revision`);
CREATE TABLE `filterspecifications` (
`tag` varchar(100) NOT NULL,
`gasFlow` double NOT NULL,
`dustToHandle` double NOT NULL,
`offerid` varchar(20) NOT NULL,
`selectedFilter` varchar(20) NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `filterspecifications`
ADD PRIMARY KEY (`tag`,`offerid`,`revision`),
ADD KEY `offerid` (`offerid`,`revision`);
ALTER TABLE `filterspecifications`
ADD CONSTRAINT `filterspecifications_ibfk_1`
FOREIGN KEY (`offerid`,`revision`) REFERENCES `offer` (`projectid`, `revision`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE TABLE `acoustictreatment` (
`tag` varchar(100) NOT NULL,
`offerid` varchar(20) NOT NULL,
`outputFanSilencer` tinyint(1) NOT NULL,
`fanAcousticInsulation` tinyint(1) NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `acoustictreatment`
ADD PRIMARY KEY (`tag`,`offerid`,`revision`);
The solution that I found is to delete the 'acoustictreatment' table and create it again with the foreign key from the beginning. Because what I was trying to do was to create all the tables and then create the foreign keys, but that didn´t work for me

Mysql Connot add foreign key constraint. how can I resolve it?

I tried to excute the query. but a error was occurred. the error message is 'Cannot add foreign key constraint'.
I supposed to create this table. but it didn't work.
CREATE TABLE IF NOT EXISTS `mydb`.`member` (
`idseq` INT(1) NOT NULL AUTO_INCREMENT,
`id` VARCHAR(50) NOT NULL,
`pw` VARCHAR(20) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`email` VARCHAR(45) NOT NULL,
`mobile0` VARCHAR(3) NOT NULL,
`mobile1` VARCHAR(3) NOT NULL,
`mobile2` VARCHAR(4) NOT NULL,
`mobile3` VARCHAR(4) NOT NULL,
`birth` DATE NOT NULL,
`admin_YN` VARCHAR(45) NOT NULL DEFAULT 'N',
`reg_date` DATE NOT NULL,
`upd_date` DATE NOT NULL,
PRIMARY KEY (`idseq`, `id`))
ENGINE = InnoDB
AUTO_INCREMENT = 6
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `mydb`.`reservation` (
`reservation_seq` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR(50) NOT NULL,
`playMv_seq` INT(11) NOT NULL,
`reservaion_seat_code` VARCHAR(20) NOT NULL,
`reservaion_seat_num` INT(11) NOT NULL,
`reservation_charge` VARCHAR(20) NOT NULL,
`reservation_date` DATETIME NOT NULL,
PRIMARY KEY (`reservation_seq`, `user_id`, `playMv_seq`),
FOREIGN KEY (`user_id`)
REFERENCES `mydb`.`member` (`id`)
)
ENGINE = InnoDB
AUTO_INCREMENT = 8
DEFAULT CHARACTER SET = utf8;
So I checked it detail from a query'show engine innodb status. the detail error message is
Error in foreign key constraint of table mydb/reservation:
FOREIGN KEY (`user_id`)
REFERENCES `mydb`.`member` (`id`)
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.
how can I resolve the error
You're trying to create a foreign key referencing the id column of the target table:
FOREIGN KEY (`user_id`)
REFERENCES `mydb`.`member` (`id`)
But what is the actual key on that target table?:
PRIMARY KEY (`idseq`, `id`)
It's not id, if the composite of idseq and id. In order to reference that as a foreign key, you need local columns which match that:
`user_idseq` INT(1) NOT NULL,
`user_id` VARCHAR(50) NOT NULL,
And use both of them for the foreign key:
FOREIGN KEY (`user_idseq`, `user_id`)
REFERENCES `mydb`.`member` (`idseq`, `id`)
(Side Note: That primary key in member looks pretty strange to me in the first place. Why can't idseq by itself be the primary key? What is the purpose of id?)

ERROR 1215: Cannot add foreign key constraint (multiple column FK)

I'm going crazy trying to figure out the reason of the well-known ERROR 1215 (MySQL 6.3.5).
I have to declare a table with a four-column PK, in which 3 of the columns are FK referencing other table.
CREATE TABLE `zen`.`pago_abono` (
`fecha_pago` DATETIME NOT NULL,
`cod_abono` VARCHAR(4) NOT NULL,
`mes` INT(2) NOT NULL,
`anio` YEAR NOT NULL,
`monto_pago` DECIMAL(10,2) NULL,
PRIMARY KEY (`fecha_pago`, `cod_abono`, `mes`, `anio`),
INDEX `cuota_fk_idx` (`mes` ASC, `anio` ASC, `cod_abono` ASC),
CONSTRAINT `cuota_fk`
FOREIGN KEY (`mes` , `anio` , `cod_abono`)
REFERENCES `zen`.`cuota_abono` (`mes` , `anio` , `cod_abono`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
"cuota_fk" references the table "cuota_abono"
CREATE TABLE `cuota_abono` (
`cod_abono` varchar(4) NOT NULL,
`mes` int(2) NOT NULL,
`anio` year(4) NOT NULL,
`valor` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`cod_abono`,`anio`,`mes`),
CONSTRAINT `cod_abono_fk` FOREIGN KEY (`cod_abono`) REFERENCES `abonos`(`cod_abono`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Finally, "cuota_abono" has a FK, "cod_abono_fk", referencing "abonos"
CREATE TABLE `abonos` (
`cod_abono` varchar(4) NOT NULL,
`direccion` varchar(45) NOT NULL,
`tel_verificacion` varchar(45) NOT NULL,
`fecha_alta` date DEFAULT NULL,
`descripcion` varchar(45) DEFAULT NULL,
`cuenta_cte` decimal(10,2) DEFAULT '0.00',
`cod_localidad` varchar(3) DEFAULT NULL,
`cod_cobrador` varchar(3) DEFAULT NULL,
`nombre_servicio` varchar(45) DEFAULT NULL,
PRIMARY KEY (`cod_abono`),
KEY `zona_cobranza_fk_idx` (`cod_localidad`,`cod_cobrador`),
CONSTRAINT `zona_cobranza_fk` FOREIGN KEY (`cod_localidad`, `cod_cobrador`) REFERENCES `zonas_cobranza` (`cod_localidad`, `cod_cobrador`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Can you find the mistake?
Thanks!
P.S.: All tables have already been created, except from "pago_abono". I'm showing the CREATE statement of tables "abonos" and "cuota_abono" just to let you know their structure.
I even tried creating "pago_abono" just with the PK and then used ALTER to add the FKs, but it still shows the error.

MySQL: Error Message Can't create table (errno: 150)

I have two tables, 'po' and 'receive'
CREATE TABLE `po` (
`PO_ID` bigint(20) NOT NULL,
`SERVICE_TYPE` bit(1) DEFAULT NULL,
`ENTRY_DATE` date NOT NULL,
`RECEIPT_DATE` date DEFAULT NULL,
`TURNOVER` date DEFAULT NULL,
`MOBILIZATION` date DEFAULT NULL,
`SITE_NAME` varchar(255) NOT NULL,
`SITE_CODE` varchar(45) DEFAULT NULL,
`SITE_TIN` varchar(45) DEFAULT NULL,
`SITE_ADDRESS` varchar(255) NOT NULL,
`COST` decimal(11,2) NOT NULL,
`XML` text,
PRIMARY KEY (`PO_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `receive` (
`RECEIPT_ID` varchar(100) NOT NULL,
`RECEIPT_DATE` date NOT NULL,
`PO_NUMBER` bigint(20) NOT NULL,
`SUPPLIER_ID` int(11) NOT NULL,
PRIMARY KEY (`RECEIPT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
I'm trying to connect two tables by defining a foreign key 'fk_po' on the table 'receive'
ALTER TABLE `fourthwave`.`receive`
ADD CONSTRAINT `fk_po`
FOREIGN KEY (`PO_NUMBER` )
REFERENCES `fourthwave`.`po` (`PO_ID` )
ON DELETE SET NULL
ON UPDATE SET NULL
, ADD INDEX `fk_po` (`PO_NUMBER` ASC)
However, the alter query above throws an error :
Error Code: 1005. Can't create table 'fourthwave.#sql-aec_11' (errno:150)
Am i getting this error because the field's names, 'PO_ID' and 'PO_NUMBER' on both tables are different?
Execute SHOW ENGINE INNODB STATUS statement after ALTER TABLE, and you will see the error message - 'You have defined a SET NULL condition though some of the
columns are defined as NOT NULL'.
ALTER TABLE `receive`
ADD CONSTRAINT `fk_po`
FOREIGN KEY (`PO_NUMBER` )
REFERENCES `po` (`PO_ID` )
ON DELETE SET NULL
ON UPDATE SET NULL
, ADD INDEX `fk_po` (`PO_NUMBER` ASC);
SHOW ENGINE INNODB STATUS;
You need an index on PO_NUMBER in the receive table. The field you are referencing in a foreign key always should be indexed.