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

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.

Related

Foreign key constraint is incorrectly formed (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;

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.

Export localhost Sql and import it on Server gives me errno 150

I suggest something is wrong with the foreign keys, but i can't find a hint.
error while importing the sql:
ALTER TABLE `claim`
ADD CONSTRAINT `FK_66A8F1231B7B246A` FOREIGN KEY (`purchaseOrder_id`) REFERENCES `PurchaseOrder` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `FK_66A8F1237294869C` FOREIGN KEY (`article_id`) REFERENCES `Article` (`id`);
#1005 - Can't create table 'databasename.#sql-5c7_568c0' (errno: 150)
I also checked the conditions of this answer MySQL Creating tables with Foreign Keys giving errno: 150
But i couldn't find the mistake.
Any guesses?
Tables:
CREATE TABLE `Article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`number` int(11) NOT NULL,
`currency` tinyint(1) NOT NULL,
`price` double NOT NULL,
`tolerance` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
CREATE TABLE `claim` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`comment` longtext COLLATE utf8_unicode_ci NOT NULL,
`purchaseOrder_id` int(11) DEFAULT NULL,
`visible` tinyint(1) NOT NULL,
`article_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_66A8F1231B7B246A` (`purchaseOrder_id`),
KEY `IDX_66A8F1237294869C` (`article_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `purchaseorder` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`state` int(11) NOT NULL,
`supplier_id` int(11) DEFAULT NULL,
`delivery_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_AA004F112ADD6D8C` (`supplier_id`),
KEY `IDX_AA004F1112136921` (`delivery_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Your table name is purchaseorder
--------------------------^-----------^
ALTER TABLE `claim`
ADD CONSTRAINT `FK_66A8F1231B7B246A` FOREIGN KEY (`purchaseOrder_id`) REFERENCES `purchaseorder` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `FK_66A8F1237294869C` FOREIGN KEY (`article_id`) REFERENCES `Article` (`id`);

Put unique index across more than one table

I have run into a problem where the easiest solution would be to add a unique index that goes across multiple tables in mysql. Is this possible?
For the purpose of this question I will have three tables; status, tracks and events. The status and tracks tables both have an auto incrementing ID (T_ID or S_ID) in. Information from them is added to the events table with a trigger. The problem is that there could be the same auto incrementing ID in tracks and status, this means that their could be the same ID more than once in events.
tracks;
CREATE TABLE `tracks` (
`ID` int(11) NOT NULL,
`url` varchar(200) COLLATE latin1_general_ci NOT NULL,
`name` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`T_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`T_ID`),
UNIQUE KEY `url` (`url`),
UNIQUE KEY `T_ID` (`T_ID`),
KEY `ID` (`ID`),
CONSTRAINT `tracks_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
status
CREATE TABLE `status` (
`ID` int(11) NOT NULL,
`status` varchar(300) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`S_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`S_ID`),
UNIQUE KEY `S_ID` (`S_ID`),
KEY `ID` (`ID`),
CONSTRAINT `status_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `artists` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
events
CREATE TABLE `events` (
`ID` int(11) NOT NULL,
`action` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`E_ID` int(11) NOT NULL,
KEY `ID` (`ID`),
CONSTRAINT `events_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='This table shows what the artist has done and is used feed'
Agreed, you cannot have an index across multiple tables.
However, consider changing your schema by adding a "source" column to your event table and making a unique constraint on the combination of the source and the foreign key. This would prevent duplicate keys being inserted from the same source.

Foreign Key Constraint Fails - Probably misunderstanding something about my relationships

I'm having a little trouble with some MySQL relationships. I think I'm missing something obvious in my structure. Here's my SQL:
DROP TABLE IF EXISTS `parentlist_comments`;
CREATE TABLE `parentlist_comments` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`user_id` char(36) NOT NULL,
`comment` char(50) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_parentlist_comments_parentlist` (`parentlist_id`),
KEY `fk_parentlist_comment_user` (`user_id`),
CONSTRAINT `fk_parentlist_comments_parentlist` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_comment_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlist_submissions`;
CREATE TABLE `parentlist_submissions` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(25) NOT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_parentlist_submissions_user` (`user_id`),
KEY `fk_parentlist_submissions_list` (`parentlist_id`),
KEY `fk_parentlist_submissions_type` (`type_id`),
CONSTRAINT `fk_parentlist_submissions_list` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlists`;
CREATE TABLE `parentlists` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(50) NOT NULL,
`user_id` char(36) NOT NULL,
`max_comments` int(3) NOT NULL DEFAULT '0',
`max_submissions` int(3) NOT NULL DEFAULT '10',
`max_votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_list_user` (`user_id`),
CONSTRAINT `fk_list_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;
DROP TABLE IF EXISTS `submissions`;
CREATE TABLE `submissions` (
`id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(30) NOT NULL,
`description` char(50) NOT NULL,
`embed` char(200) DEFAULT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_submission_user` (`user_id`),
KEY `fk_submission_type` (`type_id`),
CONSTRAINT `fk_submission_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_submission_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `types`;
CREATE TABLE `types` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`password` char(20) NOT NULL,
`email` char(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I created an column called submission_id in parentlist_submissions. I'm trying to create a foreign key relationship between parentlist_submissions.submission_id and submissions.id, when I attempt to do this I get the error: Foriegn key constraint fails. For whatever reason my query browser won't let me copy this.
Any help here is greatly appreciated!
That error is usually caused by the tables already being populated with data that violate the constraint. (Note that nulls may be a problem if you've just added the column.)
I'm guessing, because I don't see that you've posted the statement where you create the submission_index column or where you create the foreign key constraint.
You seem to be missing the "parentlist_submissions.submission_id" column.