mysql create table with foreign keys failing - mysql

I am trying to create a tables in a db that have foreign keys. what am i missing?
CREATE TABLE `users` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`userName` varchar(24) NOT NULL DEFAULT '',
`password` varchar(32) NOT NULL DEFAULT '',
`firstName` varchar(32) NOT NULL DEFAULT '',
`lastName` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
CREATE TABLE `topLevelPermissions` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`userId` bigint(10) NOT NULL DEFAULT '0',
`user` char(1) NOT NULL DEFAULT '0',
`group` char(1) NOT NULL DEFAULT '0',
`someField` char(1) NOT NULL DEFAULT '0',
`someOtherField` char(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
FOREIGN KEY (`userId`) REFERENCES users (`id`) ON DELETE CASCADE
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
and i see nothing related to foreign keys. in fact when i
show create table topLevelPermissions;
i see
CREATE TABLE `topLevelPermissions` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`userId` bigint(10) NOT NULL DEFAULT '0',
`user` char(1) NOT NULL DEFAULT '0',
`group` char(1) NOT NULL DEFAULT '0',
`someField` char(1) NOT NULL DEFAULT '0',
`someOtherField` char(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `userId` (`userId`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1

Innodb only supports foreign key constraints. So switch to Innodb if you want to utilize foreign key constraints.

Related

mysql #1005 - Can't create table 'xxx.house_auctions' (errno: 150) (Details…)

I have no idea why this error is happening. Below is the chain of tables that I use. I don't think this should be anything too difficult. Would appreciate all the help I can get with this.
mysql #1005 - Can't create table 'xxx.house_auctions' (errno: 150) (Details…)
CREATE TABLE IF NOT EXISTS `players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`group_id` int(11) NOT NULL DEFAULT '1',
`account_id` int(11) NOT NULL DEFAULT '0',
`level` int(11) NOT NULL DEFAULT '1',
`vocation` int(11) NOT NULL DEFAULT '0',
`health` int(11) NOT NULL DEFAULT '150',
`healthmax` int(11) NOT NULL DEFAULT '150',
`experience` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `vocation` (`vocation`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `houses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner` int(11) NOT NULL,
`paid` int(10) unsigned NOT NULL DEFAULT '0',
`warnings` int(11) NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`rent` int(11) NOT NULL DEFAULT '0',
`town_id` int(11) NOT NULL DEFAULT '0',
`bid` int(11) NOT NULL DEFAULT '0',
`bid_end` int(11) NOT NULL DEFAULT '0',
`last_bid` int(11) NOT NULL DEFAULT '0',
`highest_bidder` int(11) NOT NULL DEFAULT '0',
`size` int(11) NOT NULL DEFAULT '0',
`beds` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `owner` (`owner`),
KEY `town_id` (`town_id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `house_auctions` (
`house_id` INT UNSIGNED NOT NULL,
`player_id` INT NOT NULL,
`bid` INT UNSIGNED NOT NULL DEFAULT 0,
`limit` INT UNSIGNED NOT NULL DEFAULT 0,
`endtime` BIGINT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`house_id`,`player_id`),
FOREIGN KEY (`house_id`) REFERENCES `houses` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB;
I think the house_auctions columns have different types than the ones you want to reference. Try this (note INT(11) instead of INT UNSIGNED):
CREATE TABLE IF NOT EXISTS `house_auctions` (
`house_id` INT(11) NOT NULL,
`player_id` INT(11) NOT NULL,
`bid` INT UNSIGNED NOT NULL DEFAULT 0,
`limit` INT UNSIGNED NOT NULL DEFAULT 0,
`endtime` BIGINT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`house_id`,`player_id`),
FOREIGN KEY (`house_id`) REFERENCES `houses` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE
) ENGINE=INNODB;

I can't create a foreign key in my table - #1005

#1005 - Can't create table 'forum.#sql-da8_f' (errno: 150)
I keep getting this error when i want to apply a foreign key constraint to my table. I don't know what could be the problem. I learned that one will need to use an InnoDB to be able to use a FOREIGN KEY on Mysql. Doesn't that come with Mysql by default?
Btw, here
ALTER TABLE boards
ADD FOREIGN KEY (CategoryId)
REFERENCES categories(CategoryId)
EDIT:
Here's how i created my table
CREATE TABLE IF NOT EXISTS `boards` (
`BoardId` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`CategoryId` int(11) DEFAULT '0',
`ChildLevel` int(11) DEFAULT '0',
`ParentId` int(11) DEFAULT '0',
`BoardOrder` int(11) DEFAULT '0',
`LastMessageId` int(11) DEFAULT '0',
`MessageUpdatedId` int(11) DEFAULT '0',
`Groups` varchar(255) DEFAULT '',
`ProfileId` int(11) DEFAULT '0',
`BoardName` varchar(255) DEFAULT '',
`BoardDescription` text,
`NumberOfTopics` int(11) DEFAULT '0',
`NumberOfPosts` int(11) DEFAULT '0',
`CountPosts` int(11) DEFAULT '0',
`HiddenPosts` int(11) DEFAULT '0',
`HiddenTopics` int(11) DEFAULT '0',
PRIMARY KEY (`BoardId`),
UNIQUE KEY `BoardId` (`BoardId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And here's my "categories" table:
CREATE TABLE IF NOT EXISTS `categories` (
`CategoryId` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`CategoryOrder` int(11) DEFAULT '0',
`CategoryName` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`CategoryId`),
UNIQUE KEY `CategoryId` (`CategoryId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=56 ;
The problem is, that the types don't match: your primary key on categories is a bigint unsigned while your foreign key in boards is of type int. E.g. change the boards table:
CREATE TABLE IF NOT EXISTS `boards` (
`BoardId` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`CategoryId` bigint(20) unsigned DEFAULT '0',
-- ...
)
See this demo.

#1005 - Can't create table errno: 150 Magento

I am trying to create a new table for magento and I am trying to reference existing magento tables. From what I googled, the problem that I am getting can be 1 of the 2 issues.
The FK must have a index
The PK must exist before the FK can reference
In both cases, I believe I did both of these correctly. Below is existing table schemas
ALREADY EXISTING TABLES BEING REFERENCED
CREATE TABLE IF NOT EXISTS `core_store` (
`store_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(32) NOT NULL DEFAULT '',
`website_id` smallint(5) unsigned DEFAULT '0',
`group_id` smallint(5) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`sort_order` smallint(5) unsigned NOT NULL DEFAULT '0',
`is_active` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`store_id`),
UNIQUE KEY `code` (`code`),
KEY `FK_STORE_WEBSITE` (`website_id`),
KEY `is_active` (`is_active`,`sort_order`),
KEY `FK_STORE_GROUP` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Stores' AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(32) NOT NULL DEFAULT '',
`lastname` varchar(32) NOT NULL DEFAULT '',
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(40) NOT NULL DEFAULT '',
`password` varchar(100) NOT NULL DEFAULT '',
`created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified` datetime DEFAULT NULL,
`logdate` datetime DEFAULT NULL,
`lognum` smallint(5) unsigned NOT NULL DEFAULT '0',
`reload_acl_flag` tinyint(1) NOT NULL DEFAULT '0',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`extra` text,
`failures_num` smallint(6) NOT NULL DEFAULT '0',
`first_failure` datetime DEFAULT NULL,
`lock_expires` datetime DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `UNQ_ADMIN_USER_USERNAME` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Users' AUTO_INCREMENT=25 ;
Table I am trying to create
CREATE TABLE `oro_dashboard`
( `id` int unsigned NOT NULL,
`name` varchar(255) NOT NULL default '',
`description` varchar(64) NOT NULL default '',
`created_by` int unsigned NOT NULL default '0',
`created_at` date,
`layout` varchar(255) NOT NULL default '',
`default_store_id` int,
PRIMARY KEY (`id`),
KEY `IDX_ORO_DASHBOARD_CREATED_BY` (`created_by`),
CONSTRAINT `FK_ORO_DASHBOARD_CREATED_BY_ADMIN_USER_USER_ID` FOREIGN KEY (`created_by`) REFERENCES `admin_user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ORO_DASHBOARD_DEFAULT_STORE_ID_CORE_STORE_STORE_ID` FOREIGN KEY (`default_store_id`) REFERENCES `core_store` (`store_id`) ON DELETE SET NULL ON UPDATE CASCADE )
ENGINE=INNODB charset=utf8 COLLATE=utf8_unicode_ci
In magento, I found there is in deed a table called admin_user that does have a column called user_id. There is a core_store table that does have a column called store_id. and both my columns have INDEXES made.
Does anyone have any clue what the issue maybe ? Below is my error message
#1005 - Can't create table 'db.oro_dashboard' (errno: 150)
It is necessary to have data type to be same for the foreign key and its corresponding parent key. The data type is different of the foreign key and its referencing parent key in your table that is why it is giving an error.
created by is int unsigned while userid is medium int in parent table
same is problem in the second foreign key
reference http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
When defining foreign keys, the data type must be the same.
You are defining core_store.store_id as smallint(5) unsigned and so the referencing column must be the same: oro_dashboard.default_store_id.
Do also with oro_dashboard.created_by
Final oro_dashboard CREATE TABLE query,
CREATE TABLE `oro_dashboard`
( `id` int unsigned NOT NULL,
`name` varchar(255) NOT NULL default '',
`description` varchar(64) NOT NULL default '',
`created_by` mediumint(9) unsigned NOT NULL default '0',
`created_at` date,
`layout` varchar(255) NOT NULL default '',
`default_store_id` smallint(5) unsigned,
PRIMARY KEY (`id`),
KEY `IDX_ORO_DASHBOARD_CREATED_BY` (`created_by`) ,
CONSTRAINT `FK_ORO_DASHBOARD_CREATED_BY_ADMIN_USER_USER_ID`
FOREIGN KEY (`created_by`)
REFERENCES `admin_user` (`user_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ORO_DASHBOARD_DEFAULT_STORE_ID_CORE_STORE_STORE_ID`
FOREIGN KEY (`default_store_id`)
REFERENCES `core_store` (`store_id`)
ON DELETE SET NULL ON UPDATE CASCADE
)
SQLFIddle Demo
It appears that the column definition for default_store_id does not match core_store.store_id. it should be smallint(5) unsigned in your table. created_by has the same problem, although it did not prevent the table from being created

MySQL Foreign keys - Error 150 while creating tables

I have written some SQL code to create five tables with several relations(foreign keys).
The first foreign key relation works fine, the tables are created without any errors, but when I try to create gasten_url_toegangscodes I get the following error:
#1005 - Can't create table 'dbname.gasten_url_toegangscodes' (errno: 150).
I've read the docs about foreign keys and I still can't find the problem.. The most strange thing about this is that it works in the first three tables..
Can anyone help me please?
My full SQL code is:
CREATE TABLE IF NOT EXISTS `groepen` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`naam` varchar(50) NOT NULL DEFAULT '',
`status` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `gasten` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`bedrijf` varchar(100) NOT NULL DEFAULT '',
`uniek` varchar(64) NOT NULL,
`groepid` tinyint(3) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `passen` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`naam` varchar(30) NOT NULL DEFAULT '',
`color` varchar(7) NOT NULL DEFAULT '#FFFFFF',
`bekend` tinyint(1) NOT NULL DEFAULT '0',
`welkom` tinyint(1) NOT NULL DEFAULT '0',
`aantal` tinyint(1) NOT NULL DEFAULT '0',
`nummer` int(11) NOT NULL DEFAULT '0',
`begrens` tinyint(1) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL DEFAULT '1',
`priority` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `naam` (`naam`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
`uniek` varchar(64) NOT NULL,
`unieke_url_code` char(64) NOT NULL,
`salt` char(16) NOT NULL,
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `gasten_tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pas_id` tinyint(3) NOT NULL,
`uniek` varchar(64) NOT NULL,
`barcode` char(16) NOT NULL,
`secret_key` char(32) NOT NULL,
`download_count` int(11) NOT NULL DEFAULT '0',
`last_download_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`scanned` enum('N','Y') NOT NULL,
`scanned_datetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (pas_id) REFERENCES `passen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
Thanks in advance!
Create an index on gasten.uniek
CREATE TABLE IF NOT EXISTS `gasten` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`bedrijf` varchar(100) NOT NULL DEFAULT '',
`uniek` varchar(64) NOT NULL,
`groepid` tinyint(3) NOT NULL,
PRIMARY KEY (`id`),
INDEX `idx_uniek` (`uniek`),
FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
And set the charset of the FK in gasten_url_toegangscodes the same as that of the related column in gasten.
Assuming gasten is UTF-8:
CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
/* use same charset for this column */
`uniek` varchar(64) NOT NULL CHARSET UTF-8,
`unieke_url_code` char(64) NOT NULL,
`salt` char(16) NOT NULL,
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

What is the key attribute in the table definition

What are these key attributes, I'm not able to search in the Mysql manual:
KEY `node_changed` (`changed`),
KEY `node_created` (`created`),
KEY `node_moderate` (`moderate`),
KEY `node_promote_status` (`promote`,`status`),
KEY `node_status_type` (`status`,`type`,`nid`),
KEY `node_title_type` (`title`,`type`(4)),
KEY `node_type` (`type`(4)),
KEY `uid` (`uid`),
KEY `tnid` (`tnid`),
KEY `translate` (`translate`)
full table structure is here:
CREATE TABLE IF NOT EXISTS `node` (
`nid` int(10) unsigned NOT NULL auto_increment,
`vid` int(10) unsigned NOT NULL default '0',
`type` varchar(32) NOT NULL default '',
`language` varchar(12) NOT NULL default '',
`title` varchar(255) NOT NULL default '',
`uid` int(11) NOT NULL default '0',
`status` int(11) NOT NULL default '1',
`created` int(11) NOT NULL default '0',
`changed` int(11) NOT NULL default '0',
`comment` int(11) NOT NULL default '0',
`promote` int(11) NOT NULL default '0',
`moderate` int(11) NOT NULL default '0',
`sticky` int(11) NOT NULL default '0',
`tnid` int(10) unsigned NOT NULL default '0',
`translate` int(11) NOT NULL default '0',
PRIMARY KEY (`nid`),
UNIQUE KEY `vid` (`vid`),
KEY `node_changed` (`changed`),
KEY `node_created` (`created`),
KEY `node_moderate` (`moderate`),
KEY `node_promote_status` (`promote`,`status`),
KEY `node_status_type` (`status`,`type`,`nid`),
KEY `node_title_type` (`title`,`type`(4)),
KEY `node_type` (`type`(4)),
KEY `uid` (`uid`),
KEY `tnid` (`tnid`),
KEY `translate` (`translate`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1041 ;
From the manual:
KEY is normally a synonym for INDEX.