Foreign key constraint is incorrectly formed (MySQL) - mysql

I am trying to create a database for this project: https://sourceforge.net/projects/ess/
I've created a database called ess via PHPmyadmin and when I run the SQL query to create the tables I get an error on the second table areasuper.
The error is: "#1005 - Can't create table `ess.areasuper` (errno: 150 "Foreign key constraint is incorrectly formed")"
I don't really understand what I have been able to find online regarding this type of error so I'm hoping someone here can spell it out for me.
Also, I'm not sure how much info anyone will need to understand and replicate this so please let me know what to include if I have missed anything.
Thank you.
CREATE TABLE areas (
area_id int(11) unsigned NOT NULL auto_increment,
area_name varchar(20) default NULL,
area_desc varchar(100) NOT NULL default '',
area_templ text,
PRIMARY KEY (area_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `areasuper`
--
CREATE TABLE areasuper (
as_id int(11) unsigned NOT NULL auto_increment,
as_area_id int(11) unsigned NOT NULL default '0',
as_uid int(11) unsigned NOT NULL default '0',
PRIMARY KEY (as_id),
KEY as_area_id (as_area_id),
KEY as_uid (as_uid),
CONSTRAINT areasuper_ibfk_1 FOREIGN KEY (as_area_id) REFERENCES areas (area_id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT areasuper_ibfk_2 FOREIGN KEY (as_uid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `assignments`
--
CREATE TABLE assignments (
assign_id int(10) unsigned NOT NULL auto_increment,
assign_uid int(10) unsigned NOT NULL default '0',
assign_pid int(10) unsigned NOT NULL default '0',
assign_eid int(10) unsigned NOT NULL default '0',
PRIMARY KEY (assign_id),
KEY assign_uid (assign_uid),
KEY assign_pid (assign_pid),
KEY assign_eid (assign_eid),
CONSTRAINT assignments_ibfk_1 FOREIGN KEY (assign_uid) REFERENCES users (user_id) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT assignments_ibfk_2 FOREIGN KEY (assign_pid) REFERENCES positions (pos_id) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT assign_assign_eid_fk FOREIGN KEY (assign_eid) REFERENCES `events` (event_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `availabletimes`
--
CREATE TABLE availabletimes (
avail_id int(10) unsigned NOT NULL auto_increment,
avail_uid int(11) unsigned NOT NULL default '0',
avail_day char(1) NOT NULL default '',
avail_start time NOT NULL default '00:00:00',
avail_end time NOT NULL default '00:00:00',
PRIMARY KEY (avail_id),
KEY avail_uid (avail_uid),
CONSTRAINT availabletimes_ibfk_1 FOREIGN KEY (avail_uid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `dayoff`
--
CREATE TABLE dayoff (
day_id int(11) unsigned NOT NULL auto_increment,
day_uid int(11) unsigned NOT NULL default '0',
day_start date NOT NULL default '0000-00-00',
day_end date NOT NULL default '0000-00-00',
day_desc varchar(50) NOT NULL default '',
PRIMARY KEY (day_id),
KEY day_uid (day_uid),
CONSTRAINT dayoff_ibfk_1 FOREIGN KEY (day_uid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `events`
--
CREATE TABLE `events` (
event_id int(11) unsigned NOT NULL auto_increment,
event_start time NOT NULL default '00:00:00',
event_end time NOT NULL default '00:00:00',
event_area_id int(11) unsigned NOT NULL default '0',
event_name varchar(50) NOT NULL default '',
event_comments text,
event_date date NOT NULL default '0000-00-00',
PRIMARY KEY (event_id),
KEY event_area_id (event_area_id),
CONSTRAINT events_ibfk_1 FOREIGN KEY (event_area_id) REFERENCES areas (area_id) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `positions`
--
CREATE TABLE positions (
pos_id int(10) unsigned NOT NULL auto_increment,
pos_area_id int(10) unsigned NOT NULL default '0',
pos_name varchar(20) NOT NULL default '',
pos_desc varchar(100) NOT NULL default '',
PRIMARY KEY (pos_id),
KEY pos_area_id (pos_area_id),
CONSTRAINT positions_ibfk_1 FOREIGN KEY (pos_area_id) REFERENCES areas (area_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `supervisors`
--
CREATE TABLE supervisors (
super_emp int(11) unsigned NOT NULL default '0',
super_super int(11) unsigned NOT NULL default '0',
KEY super_emp (super_emp),
CONSTRAINT supervisors_ibfk_1 FOREIGN KEY (super_emp) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `types`
--
CREATE TABLE `types` (
type_id int(10) unsigned NOT NULL auto_increment,
type_name varchar(10) NOT NULL default '',
PRIMARY KEY (type_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `users`
--
CREATE TABLE users (
user_id int(10) unsigned NOT NULL auto_increment,
user_name varchar(10) NOT NULL default '',
user_pass varchar(40) NOT NULL default '',
user_first varchar(10) NOT NULL default '',
user_last varchar(20) NOT NULL default '',
user_email varchar(50) NOT NULL default '',
user_phone1 bigint(20) NOT NULL default '0',
user_phone2 bigint(20) default NULL,
user_type int(11) unsigned NOT NULL default '0',
user_pay_rate float(5,2) default '0.00',
PRIMARY KEY (user_id),
KEY user_type (user_type),
CONSTRAINT users_ibfk_1 FOREIGN KEY (user_type) REFERENCES `types` (type_id) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Related

mySQL: Cannot add foreign key constraint when creation a table

I want to create this table
CREATE TABLE `t_plan` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `t_plan_x_currency` (
`plan_id` bigint(20) NOT NULL ,
`currency_symbol` varchar(20) NOT NULL DEFAULT 'EUR',
`price` decimal(12,6) NOT NULL DEFAULT '0',
PRIMARY KEY (`plan_id`,`currency_symbol`),
CONSTRAINT `t_plan_x_currency_ibfk_1` FOREIGN KEY (`plan_id`) REFERENCES `t_plan` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
but I got this error:
Cannot add foreign key constraint
The columns involved in a foreign key relationship have to be of the same type. You have different types, int(11) for t_plan.id and bigint(20) for t_plan_x_currency.plan_id.
Make t_plan.id also a bigint(20).
CREATE TABLE `t_plan` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `t_plan_x_currency` (
`plan_id` bigint(20) NOT NULL ,
`currency_symbol` varchar(20) NOT NULL DEFAULT 'EUR',
`price` decimal(12,6) NOT NULL DEFAULT '0',
PRIMARY KEY (`plan_id`,`currency_symbol`),
CONSTRAINT `t_plan_x_currency_ibfk_1` FOREIGN KEY (`plan_id`) REFERENCES `t_plan` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Or make t_plan_x_currency.plan_id an int(11), what ever is more desirable for you.

mysql cannot add foreign key for unknown reason

I cannot a add a foreign key constraint. the sql i m running is -
ALTER TABLE image_shout ADD CONSTRAINT `fk_image` FOREIGN KEY (image_id)
REFERENCES images(image_id);
the collation and the data types( int(10) ) are same in the two tables.
mysql says -
Error Code: 1215. Cannot add foreign key constraint
The images table structure -
CREATE TABLE `images` (
`image_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`image_name` varchar(100) CHARACTER SET latin1 NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`caption` varchar(450) CHARACTER SET latin1 DEFAULT NULL,
`image_visibility` int(10) unsigned NOT NULL,
`album_id` int(10) unsigned NOT NULL,
`album_view` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`album_thumb_view` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`image_id`),
KEY `Index_2` (`album_id`),
CONSTRAINT `FK_images_1` FOREIGN KEY (`album_id`) REFERENCES `photo_album` (`Album_ID`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4314 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
The image_shout table -
CREATE TABLE `image_shout` (
`auto_id` int(11) NOT NULL AUTO_INCREMENT,
`shout_id` int(11) DEFAULT NULL,
`image_id` int(10) DEFAULT NULL,
PRIMARY KEY (`auto_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1132 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
UPDATE -
The new error after changing the image_id column to unsigned is -
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails
(`void`.`#sql-36b_7285`, CONSTRAINT `fk_image` FOREIGN KEY (`image_id`)
REFERENCES `images` (`image_id`) ON DELETE SET NULL ON UPDATE CASCADE)
Regards
Is because the image_id in table images is defined as unsigned
`image_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
and in image_shout not
`image_id` int(10) DEFAULT NULL,
Change both columns to the same data type and it should work.
The problem is below. According to the documentation, a foreign key must be indexed. Further, a foreign key should reference the KEY of another table. Not just a column. Try using auto_id as the constraint if you don't wish to change your table structure.
Please see MySQL Documentation for the list of requirements of a Foreign Key constraint.
CREATE TABLE `image_shout` (
`auto_id` int(11) NOT NULL AUTO_INCREMENT,
`shout_id` int(11) DEFAULT NULL,
`image_id` int(10) DEFAULT NULL,
PRIMARY KEY (`auto_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1132 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Cannot add a foreign key in mysql where a field is a unique index

I'm having problem adding a foreign key based on AUTH_ISLE_CODE:
Create Table: CREATE TABLE `AUTH_ACCOUNT` (
`ACCOUNT_ID` int(11) NOT NULL AUTO_INCREMENT,
`PASSWORD` varchar(20) DEFAULT NULL,
...
`AUTH_ISLE_ID` int(11) NOT NULL DEFAULT '1',
`AUTH_ISLE_CODE` varchar(5) NOT NULL DEFAULT 'AAAAA',
PRIMARY KEY (`ACCOUNT_ID`),
KEY `fk_ACCOUNT_ISLE` (`AUTH_ISLE_ID`),
CONSTRAINT `fk_ACCOUNT_ISLE` FOREIGN KEY (`AUTH_ISLE_ID`) REFERENCES `AUTH_ISLE` (`ISLE_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=166 DEFAULT CHARSET=utf8
Create Table: CREATE TABLE `AUTH_ISLE` (
`ISLE_ID` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) DEFAULT NULL,
`CODE` varchar(5) NOT NULL DEFAULT 'AAAAA',
PRIMARY KEY (`ISLE_ID`),
UNIQUE KEY `CODE_UNIQUE` (`CODE`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
the error I'm having is:
ERROR 1005 (HY000): Can't create table 'masterdata.#sql-73c1_57910' (errno: 150)
Is it something about the fact that the two tables have already a relationship with AUTH_ISLE_ID
Edit 1: the SHOW CREATE TABLE you see is the current situation. I'm trying to add just the constraint to AUTH_ISLE_CODE -> CODE from AUTH_ISLE
If this is a sequence of queries you are trying to create tables then it will always fail because you are referencing a table REFERENCES AUTH_ISLE (ISLE_ID) which is not created yet ,try creating first AUTH_ISLE table then create your second table AUTH_ACCOUNT,and when AUTH_ISLE is present you can simply reference its columns
CREATE TABLE `AUTH_ISLE` (
`ISLE_ID` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) DEFAULT NULL,
`CODE` varchar(5) NOT NULL DEFAULT 'AAAAA',
PRIMARY KEY (`ISLE_ID`),
UNIQUE KEY `CODE_UNIQUE` (`CODE`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
CREATE TABLE `AUTH_ACCOUNT` (
`ACCOUNT_ID` int(11) NOT NULL AUTO_INCREMENT,
`PASSWORD` varchar(20) DEFAULT NULL,
`AUTH_ISLE_ID` int(11) NOT NULL DEFAULT '1',
`AUTH_ISLE_CODE` varchar(5) NOT NULL DEFAULT 'AAAAA',
PRIMARY KEY (`ACCOUNT_ID`),
KEY `fk_ACCOUNT_ISLE` (`AUTH_ISLE_ID`),
CONSTRAINT `fk_ACCOUNT_ISLE` FOREIGN KEY (`AUTH_ISLE_ID`)
REFERENCES `AUTH_ISLE` (`ISLE_ID`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=166 DEFAULT CHARSET=utf8;
Demo
insert into AUTH_ISLE (NAME,CODE) values('Test','Test');
insert into AUTH_ACCOUNT (PASSWORD,AUTH_ISLE_ID,AUTH_ISLE_CODE)
values('Test',last_insert_id(),'Test');
Insert Demo

MySQL foreign key contraint allow nulls not working

I can't seem to figure out why my foreign key constraints aren't working. Here's my table schema:
CREATE TABLE `templates_textboxes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`template_id` int(10) unsigned DEFAULT NULL,
`textbox_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `templates_textboxes_to_templates_idx` (`template_id`),
KEY `templates_textboxes_to_textboxes_idx` (`textbox_id`),
CONSTRAINT `templates_textboxes_to_templates` FOREIGN KEY (`template_id`) REFERENCES `templates` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1$$
CREATE TABLE `textboxes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
`columns` tinyint(1) unsigned NOT NULL DEFAULT '1',
`width` int(5) unsigned NOT NULL DEFAULT '0',
`height` int(5) unsigned NOT NULL DEFAULT '0',
`x` int(5) unsigned NOT NULL DEFAULT '0',
`y` int(5) unsigned NOT NULL DEFAULT '0',
`z` int(5) unsigned NOT NULL DEFAULT '500',
PRIMARY KEY (`id`),
CONSTRAINT `textboxes_to_templates_textboxes` FOREIGN KEY (`id`) REFERENCES `templates_textboxes` (`textbox_id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8$$
When I create a textbox in PHP, first I insert into textboxes to get the ID then insert a row in the match table templates_textboxes with the template id and new textbox id. I've read every post I can find on here about foreign keys allowing nulls and all it says to do is to set the foreign key column (templates_textboxes.textbox_id) to allow nulls. I tried that and when I try to insert into textboxes I get the following error:
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`pitchperfect`.`textboxes`, CONSTRAINT `textboxes_to_templates_textboxes` FOREIGN KEY (`id`) REFERENCES `templates_textboxes` (`textbox_id`) ON DELETE CASCADE ON UPDATE NO ACTION)INSERT INTO `textboxes` (`content`, `columns`, `width`, `height`, `x`, `y`, `z`) VALUES ('', '1', '400', '300', '133', '93', '500')
What I'm trying to accomplish is have cascade deletes chain from themes, to templates and their assets (templates_textboxes -> textboxes).
Thanks!
Since textboxes.id is not yet defined and is AUTO_INCREMENT, that value must first exist in templates_textboxes.textbox_id. This is why the constraint is failing.

#1005 - Can't create table 'table_name' (errno: 150)

here is my tables:
DROP TABLE IF EXISTS `tbl_comments`;
CREATE TABLE IF NOT EXISTS `tbl_comments` (
`id` int(11) NOT NULL auto_increment,
`topic_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`text` text NOT NULL,
`create_dt` datetime NOT NULL,
`update_dt` timestamp NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `topic_id_2` (`topic_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `tbl_comments_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`),
CONSTRAINT `tbl_comments_ibfk_1` FOREIGN KEY (`topic_id`) REFERENCES `tbl_topics` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `tbl_users`;
CREATE TABLE IF NOT EXISTS `tbl_users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL default '',
`password` varchar(128) NOT NULL default '',
`email` varchar(128) NOT NULL default '',
`activkey` varchar(128) NOT NULL default '',
`superuser` int(1) NOT NULL default '0',
`status` int(1) NOT NULL default '0',
`create_at` timestamp NOT NULL default CURRENT_TIMESTAMP,
`lastvisit_at` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `user_username` (`username`),
UNIQUE KEY `user_email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `tbl_topics`;
CREATE TABLE IF NOT EXISTS `tbl_topics` (
`id` int(11) NOT NULL auto_increment,
`group_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`header` varchar(255) NOT NULL,
`text` text NOT NULL,
`create_dt` datetime NOT NULL,
`update_dt` timestamp NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `group_id` (`group_id`),
CONSTRAINT `tbl_topics_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `tbl_groups` (`id`),
CONSTRAINT `tbl_topics_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
now i have the error when i try to import:
#1005 - Can't create table 'kobeco_yii.tbl_comments' (errno: 150) (Details: Percona-XtraDB, Supports transactions, row-level locking, and foreign keys )
You're trying to create tbl_comments before your other tables. tbl_comments requires tables tbl_users and tbl_topics for foreign key constraints.
Try moving the CREATE TABLE for tbl_comments to below the others.
Your first table (table_commets) has constraints to tables that doesn't exist yet. This may be an issue of you are creating them in that order.
Create table_usr and table_topics first.