I try add foreign key:
CREATE TABLE `invoices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
` name` varchar(255) NOT NULL,
`code_text` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `leads` (
`lead_id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`lead_id`)
);
CREATE TABLE `leads_invoices` (
leads_invoice_id int(11) AUTO_INCREMENT NOT NULL,
invoice_id int(11) NOT NULL,
lead_id_i int(11) NOT NULL,
PRIMARY KEY(leads_invoice_id),
FOREIGN KEY (invoice_id) REFERENCES invoices(id),
FOREIGN KEY (lead_id_i) REFERENCES leads(lead_id)
ON UPDATE CASCADE
ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8
But I got error with my lead_id_i key:
Can't create table crm_test.leads_invoices (errno: 150 "Foreign
key constraint is incorrectly formed")
I checked all types and tables, seems they are correct..
How can I solve this problem? Hope you will help me. Thanks!
You have to add ENGINE=InnoDB DEFAULT CHARSET=utf8 to leads table
CREATE TABLE `leads` (
`lead_id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`lead_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Because if you want to add a foreign key, both tables must have the same engine
Related
So I have two tables, customers and appointments.
Im trying to create a very simple FK relation between appointments.customer_id and customers.id, however when I do my ALTER TABLE trying to add the FK im getting this error:
MySQL said: Cannot add or update a child row: a foreign key constraint fails (wax.#sql-2c5_100, CONSTRAINT customer_fk FOREIGN KEY (id) REFERENCES customers (id) ON DELETE CASCADE ON UPDATE CASCADE)
This constraint "#sql-2c5_100" seems to be some randomly generated constraint, that I can not find ANYWHERE. I've looked on every table on the database, ive looked on all the tables on the information schema and it simply does not exist.
Thanks!
Edit:
Here's the create table outputs
CREATE TABLE `appointments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`customer_id` int(11) unsigned NOT NULL,
`name` varchar(255) DEFAULT NULL
PRIMARY KEY (`id`),
KEY `customer_id` (`customer_id`)
) ENGINE=InnoDB AUTO_INCREMENT=290958 DEFAULT CHARSET=utf8;
CREATE TABLE `customers` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) NOT NULL DEFAULT '',
`last_name` varchar(255) NOT NULL DEFAULT '',
`phone` varchar(20) DEFAULT NULL
PRIMARY KEY (`id`),
KEY `sf_id` (`sf_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Hi i make a table from this tutorial MySQL Foreign Key On Delete and this work good for my project
user table :
CREATE TABLE `User` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
UserStaff Table :
CREATE TABLE `UserStaff` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`UserId` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`),
KEY `UserId` (`UserId`),
CONSTRAINT `UserStaff_ibfk_1`
FOREIGN KEY (`UserId`)
REFERENCES `User` (`Id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
my question is : whate is UserStaff_ibfk_1 ?
can i change it to example_name or not
thanks
Trying to create a simple relation between two fields in two tables - 'Task' table with the field 'USER_TOKEN' and the 'USER' table with the field 'TOKEN'. The two fields are the same structure. As you can see the error and other things that may assist you to help me understand the problem and fix it.
System: MacOS 10.12.3 | DB : MySQL 5.7.17 | DBM : Sequel Pro
Error : MySQL said: Cannot add foreign key constraint
CREATE TABLE `TASK` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`USER_TOKEN` int(11) unsigned NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `USER` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`TOKEN` int(11) unsigned NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE USER
ADD CONSTRAINT TOKENS
FOREIGN KEY (`TOKEN`) REFERENCES `category`(`USER_TOKEN`)
Thanks.
If you really want to create a foreign key to a non-primary key, it MUST be a column that has a unique constraint on it.
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.
So your USER_TOKEN column of table TASK and TOKEN column of USER table must be UNIQUE. So run the following query:
CREATE TABLE `TASK` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`USER_TOKEN` int(11) unsigned NOT NULL UNIQUE,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `USER` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`TOKEN` int(11) unsigned NOT NULL UNIQUE,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `USER`
ADD CONSTRAINT `TOKENS`
FOREIGN KEY (`TOKEN`) REFERENCES `TASK`(`USER_TOKEN`);
Check Demo here
whether it is possible to use a foreign key to the table enum?
CREATE TABLE wiele (
id_wiele INT(11) NOT NULL AUTO_INCREMENT,
nazwa_wiele CHAR(50) NOT NULL DEFAULT '0',
PRIMARY KEY (id_wiele),
UNIQUE INDEX id (id_wiele)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=4;
CREATE TABLE `gotowe` (
`id` INT(11) NOT NULL,
`parametr` ENUM('Y','N','D') NOT NULL,
`parametry` SET('A','B','C') NOT NULL,`enter code here`
PRIMARY KEY (`id`),
UNIQUE INDEX `id` (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
Foreign Key Mismatch Error :] .
I want to create a database and connect it with two database. But I don't know when i create the Foreign Key to the second database (time_shift table) it always result error 150;
Here is the structure of table outlet:
And this is structure of table time_shift:
And this the query to create new table tide_cart:
create table `tide_chart` (
`id` int(10) not null auto_increment primary key,
`date` date null,
`outletId` int(11) not null,
`timeShiftId` int(11) not null,
`value` varchar(255) not null,
unique (`date`, `outletId`, `timeShiftId`),
foreign key (`outletId`) references `outlet`(`id`)
ON update cascade ON delete cascade,
foreign key (`timeShiftId`) references `time_shift`(`id`)
ON update cascade ON delete cascade
) engine=innoDB;
Please explain to me, why i get error when try to make foreign key connect to table time_shift?
Update: add dump export structure table for outlet and time_shift:
CREATE TABLE `outlet` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
CREATE TABLE `time_shift` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`time_start` time NOT NULL,
`time_end` time NOT NULL,
`is_active` tinyint(4) NOT NULL DEFAULT '1',
`ref_area` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `ref_area` (`ref_area`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
You need to use the InnoDB engine for your tables. time_shift is using MyISAM.
You must define indexes for outletId and timeShiftId (either UNIQUE or KEY as needed) in order to be able to create a foreign key using that field.