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

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;

Related

Does MySQL different engine types (InnoDB and Aria) affect on query with large amount of LEFT JOIN?

I have one heavy MySQL query with 12 LEFT JOIN and noticed that some of tables have InnoDB engine and some Aria.
I've attached EXPLAIN of this query and pointed engine types at image with corresponding letters I and A.
Does fact of different engines affects on query speed and how seriously? Does changing of engines types helps to optimize query execution?
What else I could do to optimize this query?
Query (if needed)
SELECT user.id, user.first_name, user.last_name, user.birthday, user.email, user.phone, user.address_id,
user.alt_address_id, user.type, user.level_id, user_level.level, user.consecutive_orders, user.orders_count,
user.code_id, user.period, user.preferences, user.is_pick_up, user.pickup_address_id, user.is_active,
user.first_delivery_date, user.change_delivery_date, user.eway_id, user.date_created,
user.rest_referral_discount as `rest_referral_discount`, user.rest_code_discount as `rest_code_discount`,
user.rest_code_box_discount as `rest_code_box_discount`, user.rest_code_mp_discount as `rest_code_mp_discount`,
user.rest_code_tup_discount as `rest_code_tup_discount`, user.nf_pantry_list, user.nf_next_weeks_menu,
user.nf_paused_reminder, user.nf_welcome, user.nf_expected_delivery_time,
user.nf_delivery, address_pickup.address as `pickup_address`, address.address, address.unit,
address.instructions, location.id location_id, location.postcode, location.suburb,
location_state.code state_code, delivery_area.delivery_area_window_id delivery_area_window_id,
delivery_area_by_location.fee delivery_fee_value, delivery_area_window.day delivery_day,
delivery_area_window.day_name_full delivery_day_name_full, delivery_area_window.window delivery_window,
user.delivery_area_id, user.alt_delivery_area_id, if (user.is_pick_up, 0, delivery_area.fee) delivery_fee,
if (user.type = 1 || user.type = 2, 5, 0) ondemand_fee, code.code code_name, code.amount code_amount,
code.value code_value, code.is_permanent is_code_permanent, code.repeat code_repeat, code.times code_times,
code.apply_to code_apply_to, code.valid_from code_valid_from, code.valid_until code_valid_until,
if (code_used.count > 0, 1, 0) is_code_used, if (code_used.count > 0, code_used.count, 0) code_used_count,
if (user.is_pick_up, IFNULL(delivery_area_by_location.topup_available, 0), IFNULL(delivery_area.topup_available, 0)) topup_available,
(SELECT `order`.`id` FROM `order` LEFT JOIN `gift` ON gift.order_id = order.id
WHERE (`order`.`delivery_date` BETWEEN '2021-02-07' and '2021-02-13') and (user.id = order.user_id) and (`gift`.`id` IS NULL)
and (`order`.`status` = 10) ORDER BY `order`.`date_created` DESC LIMIT 1) as `order_id`,
(SELECT `order`.`delivery_date` FROM `order`
LEFT JOIN `gift`
ON gift.order_id = order.id
WHERE (user.id = order.user_id) and (`gift`.`id` IS NULL) and (`order`.`status` = 10) ORDER BY `order`.`date_created` DESC LIMIT 1) as `last_order_delivery_date`
FROM `user`
LEFT JOIN `address`
ON user.address_id = address.id
LEFT JOIN `address_pickup`
ON user.pickup_address_id = address_pickup.id
LEFT JOIN `delivery_area`
ON user.delivery_area_id = delivery_area.id
LEFT JOIN `delivery_area_window`
ON delivery_area_window.id = delivery_area.delivery_area_window_id
LEFT JOIN `user_level`
ON user.level_id = user_level.id
LEFT JOIN `location`
ON address.location_id = location.id
LEFT JOIN `location_state`
ON location.state_id = location_state.id
LEFT JOIN `delivery_area` `delivery_area_by_location` ON delivery_area_by_location.location_id = location.id
LEFT JOIN (SELECT count(*) count, `user_id`, `code_id` FROM `order` WHERE (code_id > 0) and (`order`.`status` = 10) GROUP BY `user_id`, `code_id`) `code_used`
ON ((user.id = code_used.user_id) and (code_used.code_id = user.code_id))
LEFT JOIN `code`
ON user.code_id = code.id WHERE (`user`.`type` = 0) and (`user`.`status` = 10)
GROUP BY `user`.`id` ORDER BY `last_name` LIMIT 15
UPD: CREATE TABLE
CREATE TABLE `address` (
`id` int(11) UNSIGNED NOT NULL,
`location_id` int(11) UNSIGNED NOT NULL,
`address` text NOT NULL,
`unit` char(4) NOT NULL,
`instructions` text NOT NULL,
`regular` tinyint(1) NOT NULL DEFAULT '1',
`topup` tinyint(1) NOT NULL DEFAULT '1',
`date_from` date DEFAULT NULL,
`date_to` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `address_pickup` (
`id` tinyint(3) UNSIGNED NOT NULL,
`address` text NOT NULL
) ENGINE=Aria DEFAULT CHARSET=utf8;
CREATE TABLE `code` (
`id` smallint(6) UNSIGNED NOT NULL,
`code` varchar(255) NOT NULL,
`amount` decimal(7,2) NOT NULL,
`value` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0 - number; 1 - percent',
`is_mswa` tinyint(4) DEFAULT '0',
`is_permanent` tinyint(4) DEFAULT '0',
`repeat` tinyint(4) DEFAULT '0',
`apply_to` tinyint(4) DEFAULT '0' COMMENT '0 - both; 1 - box; 2 - mp;',
`times` int(11) NOT NULL DEFAULT '0',
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
`valid_from` date NOT NULL,
`valid_until` date DEFAULT NULL,
`author` varchar(255) NOT NULL,
`checkout_text` text NOT NULL,
`status` smallint(6) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `delivery_area` (
`id` int(10) UNSIGNED NOT NULL,
`location_id` int(11) NOT NULL,
`fee` decimal(7,2) NOT NULL DEFAULT '0.00',
`delivery_area_window_id` tinyint(4) UNSIGNED NOT NULL DEFAULT '1',
`topup_available` tinyint(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `delivery_area_window` (
`id` tinyint(4) UNSIGNED NOT NULL,
`day` tinyint(3) UNSIGNED NOT NULL,
`day_name` varchar(255) NOT NULL,
`day_name_full` varchar(255) NOT NULL,
`window` varchar(255) NOT NULL DEFAULT '',
`pickup` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=Aria DEFAULT CHARSET=utf8;
CREATE TABLE `gift` (
`id` int(11) UNSIGNED NOT NULL,
`user_id` int(11) UNSIGNED NOT NULL,
`recipient_id` int(11) UNSIGNED NOT NULL,
`type` tinyint(1) NOT NULL COMMENT '0 - card; 1 - box;',
`message` text NOT NULL,
`token` char(24) DEFAULT NULL,
`order_id` int(11) UNSIGNED NOT NULL,
`is_redeemed` tinyint(1) NOT NULL,
`date_exp` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `location` (
`id` int(10) UNSIGNED NOT NULL,
`postcode` int(4) UNSIGNED NOT NULL,
`suburb` varchar(45) NOT NULL,
`dc` varchar(45) NOT NULL,
`state_id` tinyint(3) UNSIGNED NOT NULL,
`type_id` tinyint(4) UNSIGNED NOT NULL,
`lat` double DEFAULT NULL,
`lon` double DEFAULT NULL
) ENGINE=Aria DEFAULT CHARSET=utf8;
CREATE TABLE `location_state` (
`id` tinyint(4) UNSIGNED NOT NULL,
`code` char(3) NOT NULL,
`state` varchar(255) NOT NULL
) ENGINE=Aria DEFAULT CHARSET=utf8;
CREATE TABLE `order` (
`id` int(11) UNSIGNED NOT NULL,
`user_id` int(11) UNSIGNED NOT NULL,
`shipping_address_id` int(11) UNSIGNED NOT NULL,
`topup_address_id` int(10) UNSIGNED NOT NULL,
`pickup_address_id` tinyint(3) UNSIGNED DEFAULT NULL,
`preferences` int(11) UNSIGNED NOT NULL,
`referral_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`code_id` int(11) UNSIGNED NOT NULL,
`code_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`pickup_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`admin_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`level_discount` decimal(7,2) NOT NULL DEFAULT '0.00',
`delivery_fee` decimal(7,2) NOT NULL DEFAULT '0.00',
`topup_delivery_fee` decimal(7,2) NOT NULL,
`ondemand_fee` decimal(7,2) NOT NULL DEFAULT '0.00',
`amount` decimal(7,2) NOT NULL DEFAULT '0.00',
`retry_payment` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' COMMENT '1-wait answer; 2-recharge with sms; 3-recharge without sms',
`retry_payment_sms` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '0 - processed; >0 - sms id',
`retry_payment_email` int(10) UNSIGNED NOT NULL DEFAULT '0',
`eway_errors` text NOT NULL,
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
`delivery_date` date DEFAULT NULL,
`status` smallint(6) UNSIGNED NOT NULL DEFAULT '10',
`processing_status` smallint(6) UNSIGNED NOT NULL,
`processing_status_edited` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`has_topup` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`is_topup_only` tinyint(1) UNSIGNED NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
CREATE TABLE `user` (
`id` int(11) UNSIGNED NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`birthday` date DEFAULT NULL,
`auth_key` varchar(32) NOT NULL,
`password_hash` varchar(255) NOT NULL,
`password_reset_token` varchar(255) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`phone` varchar(255) DEFAULT NULL,
`custom_ref_code` varchar(255) DEFAULT NULL,
`eway_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'eWAY Customer Token ID',
`active_campaign_id` int(11) UNSIGNED DEFAULT NULL COMMENT 'ActiveCampaign subscriber_id',
`level_id` tinyint(4) UNSIGNED NOT NULL DEFAULT '1',
`address_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
`delivery_area_id` int(10) UNSIGNED DEFAULT NULL,
`alt_address_id` int(10) UNSIGNED DEFAULT NULL,
`alt_delivery_area_id` int(10) UNSIGNED DEFAULT NULL,
`code_id` int(11) UNSIGNED NOT NULL DEFAULT '0',
`period` tinyint(4) UNSIGNED NOT NULL DEFAULT '1' COMMENT '1 - Once a Week; 2 - Fortnightly',
`preferences` int(11) UNSIGNED NOT NULL DEFAULT '0',
`is_pick_up` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`pickup_address_id` tinyint(3) UNSIGNED DEFAULT NULL,
`is_active` tinyint(4) NOT NULL DEFAULT '1' COMMENT '0 - Inactive; 1 - Active; -1 - Blocked',
`status` smallint(6) NOT NULL DEFAULT '10',
`type` tinyint(4) UNSIGNED NOT NULL COMMENT '0 - Set And Forget; 1 - On Demand; 2 - Trial; 3 - Potential;',
`first_delivery_date` date DEFAULT NULL,
`last_delivery_date` date DEFAULT NULL,
`change_delivery_date` date DEFAULT NULL,
`nf_pantry_list` tinyint(1) NOT NULL DEFAULT '1',
`nf_next_weeks_menu` tinyint(1) NOT NULL DEFAULT '1',
`nf_paused_reminder` tinyint(1) NOT NULL DEFAULT '1',
`nf_expected_delivery_time` tinyint(1) NOT NULL DEFAULT '1',
`nf_delivery` tinyint(1) NOT NULL DEFAULT '1',
`nf_welcome` tinyint(1) NOT NULL DEFAULT '1',
`date_created` datetime NOT NULL,
`date_updated` datetime NOT NULL,
`consecutive_orders` smallint(5) UNSIGNED DEFAULT NULL,
`orders_count` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
`rest_referral_discount` decimal(7,2) UNSIGNED NOT NULL,
`rest_code_discount` decimal(7,2) UNSIGNED NOT NULL,
`rest_code_box_discount` decimal(7,2) UNSIGNED NOT NULL,
`rest_code_mp_discount` decimal(7,2) UNSIGNED NOT NULL,
`rest_code_tup_discount` decimal(7,2) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user_level` (
`id` int(11) NOT NULL,
`level` varchar(255) NOT NULL,
`icon` varchar(255) NOT NULL,
`description` text NOT NULL,
`from` int(11) NOT NULL DEFAULT '0',
`to` int(11) NOT NULL DEFAULT '0',
`discount` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And indexes
--
-- Indexes table `address`
--
ALTER TABLE `address`
ADD PRIMARY KEY (`id`),
ADD KEY `location_id` (`location_id`),
ADD KEY `regular` (`regular`),
ADD KEY `topup` (`topup`),
ADD KEY `date_to` (`date_to`),
ADD KEY `date_from` (`date_from`);
--
-- Indexes table `address_pickup`
--
ALTER TABLE `address_pickup`
ADD PRIMARY KEY (`id`);
--
-- Indexes table `code`
--
ALTER TABLE `code`
ADD PRIMARY KEY (`id`),
ADD KEY `status` (`status`),
ADD KEY `valid_from` (`valid_from`),
ADD KEY `valid_until` (`valid_until`),
ADD KEY `date_created` (`date_created`),
ADD KEY `code` (`code`) USING BTREE,
ADD KEY `is_mswa` (`is_mswa`);
--
-- Indexes table `delivery_area`
--
ALTER TABLE `delivery_area`
ADD PRIMARY KEY (`id`),
ADD KEY `day` (`delivery_area_window_id`),
ADD KEY `location_id` (`location_id`) USING BTREE,
ADD KEY `friday_topup_available` (`topup_available`);
--
-- Indexes table `delivery_area_window`
--
ALTER TABLE `delivery_area_window`
ADD PRIMARY KEY (`id`),
ADD KEY `day` (`day`),
ADD KEY `pickup_only` (`pickup`);
--
-- Indexes table `gift`
--
ALTER TABLE `gift`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `token` (`token`),
ADD KEY `user_id` (`user_id`),
ADD KEY `recipient_id` (`recipient_id`),
ADD KEY `order_id` (`order_id`),
ADD KEY `is_redeemed` (`is_redeemed`);
--
-- Indexes table `location`
--
ALTER TABLE `location`
ADD PRIMARY KEY (`id`),
ADD KEY `idx_lon` (`lon`),
ADD KEY `idx_lat` (`lat`),
ADD KEY `type_id` (`type_id`),
ADD KEY `state_id` (`state_id`),
ADD KEY `postcode` (`postcode`),
ADD KEY `suburb_2` (`suburb`),
ADD KEY `postcode_suburb` (`postcode`,`suburb`) USING BTREE;
ALTER TABLE `location` ADD FULLTEXT KEY `suburb` (`suburb`);
--
-- Indexes table `location_state`
--
ALTER TABLE `location_state`
ADD PRIMARY KEY (`id`);
--
-- Indexes table `order`
--
ALTER TABLE `order`
ADD PRIMARY KEY (`id`),
ADD KEY `client_id` (`user_id`),
ADD KEY `status` (`status`),
ADD KEY `shipping_address_id` (`shipping_address_id`),
ADD KEY `preferences` (`preferences`),
ADD KEY `processing_status` (`processing_status`),
ADD KEY `date_created` (`date_created`),
ADD KEY `delivery_date` (`delivery_date`),
ADD KEY `pickup_address_id` (`pickup_address_id`),
ADD KEY `processing_status_edited` (`processing_status_edited`),
ADD KEY `retry_payment_sms` (`retry_payment_sms`),
ADD KEY `retry_payment` (`retry_payment`),
ADD KEY `retry_payment_email` (`retry_payment_email`),
ADD KEY `has_topup` (`has_topup`),
ADD KEY `is_topup_only` (`is_topup_only`),
ADD KEY `friday_address_id` (`topup_address_id`),
ADD KEY `code_id` (`code_id`),
ADD KEY `user_id` (`user_id`);
--
-- Indexes table `user`
--
ALTER TABLE `user`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `password_reset_token` (`password_reset_token`),
ADD KEY `level_id` (`level_id`),
ADD KEY `eway_id` (`eway_id`),
ADD KEY `password_hash` (`password_hash`),
ADD KEY `auth_key` (`auth_key`),
ADD KEY `email` (`email`),
ADD KEY `period` (`period`),
ADD KEY `preferences` (`preferences`),
ADD KEY `is_pick_up` (`is_pick_up`),
ADD KEY `is_active` (`is_active`),
ADD KEY `date_created` (`date_created`),
ADD KEY `phone` (`phone`) USING BTREE,
ADD KEY `active_campaign_id` (`active_campaign_id`),
ADD KEY `custom_ref_code` (`custom_ref_code`),
ADD KEY `consecutive_orders` (`consecutive_orders`),
ADD KEY `pickup_address_id` (`pickup_address_id`),
ADD KEY `rest_referral_discount` (`rest_referral_discount`),
ADD KEY `rest_code_discount` (`rest_code_discount`),
ADD KEY `rest_code_box_discount` (`rest_code_box_discount`),
ADD KEY `rest_code_mp_discount` (`rest_code_mp_discount`),
ADD KEY `delivery_area_id` (`delivery_area_id`),
ADD KEY `rest_code_tup_discount` (`rest_code_tup_discount`),
ADD KEY `orders_count` (`orders_count`),
ADD KEY `alt_delivery_address_id` (`alt_address_id`),
ADD KEY `alt_delivery_area_id` (`alt_delivery_area_id`),
ADD KEY `address_id` (`address_id`),
ADD KEY `code_id` (`code_id`),
ADD KEY `status` (`status`),
ADD KEY `type` (`type`),
ADD KEY `last_name` (`last_name`);
--
-- Indexes table `user_level`
--
ALTER TABLE `user_level`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT
--
ALTER TABLE `address`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `address_pickup`
MODIFY `id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `code`
MODIFY `id` smallint(6) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `delivery_area`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `delivery_area_window`
MODIFY `id` tinyint(4) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `gift`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `location`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `location_state`
MODIFY `id` tinyint(4) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `order`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `user`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE `user_level`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
For table order:
INDEX(status, code_id, order_id)
INDEX(status, order_id, code_id)
(I can't tell which would be better; the Optimizer can decide.)
Don't use LEFT JOIN when the 'right' table is not optional. It makes it tedious for a human (and the Optimizer) to figure the intent.
User needs
INDEX(type, status, code_id, code, id, last_name)
It is almost always beneficial to DROP INDEX(a) when you ADD INDEX(a,b). I bring that up because you probably have some one-column indexes.
If you need further help, please provide SHOW CREATE TABLE. I suspect that the query could be turned inside-out to great benefit. This involves finding the 15 ids first; then after that do all the JOINs.
That might have this as the first "derived" table since it generates only 15 rows, not the 9976 estimated in the Explain:
FROM ( SELECT id, `code`, code_id, last_name
FROM user
WHERE `type` = 0
AND `status` = 10
GROUP BY `id`
ORDER BY `last_name`
LIMIT 15
) AS u
JOIN ...

osticket how to get ticket id by the form entry value?

In osticket we have a number of custom form fields setup and I need help querying them. The data that I want to query is stored in form_entry.value.
SELECT * FROM `form_entry_values` fev, `form_entry` fe WHERE fev.value = '{$ibn}' AND fe.id = fev.entry_id
This is returning the correct data but how can I find the ticket ID if i know the form_id? I can't seem to find any joining tables.
This is the full db: https://github.com/osTicket/osTicket-1.8/blob/b1c845bf0591b1f5da593a55e462b07e5a4ee5de/setup/inc/streams/core/install-mysql.sql
DROP TABLE IF EXISTS `form`;
CREATE TABLE `form` (
`id` int(11) unsigned NOT NULL auto_increment,
`type` varchar(8) NOT NULL DEFAULT 'G',
`deletable` tinyint(1) NOT NULL DEFAULT 1,
`title` varchar(255) NOT NULL,
`instructions` varchar(512),
`notes` text,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_field`;
CREATE TABLE `form_field` (
`id` int(11) unsigned NOT NULL auto_increment,
`form_id` int(11) unsigned NOT NULL,
`type` varchar(255) NOT NULL DEFAULT 'text',
`label` varchar(255) NOT NULL,
`required` tinyint(1) NOT NULL DEFAULT 0,
`private` tinyint(1) NOT NULL DEFAULT 0,
`edit_mask` tinyint(1) NOT NULL DEFAULT 0,
`name` varchar(64) NOT NULL,
`configuration` text,
`sort` int(11) unsigned NOT NULL,
`hint` varchar(512),
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_entry`;
CREATE TABLE `form_entry` (
`id` int(11) unsigned NOT NULL auto_increment,
`form_id` int(11) unsigned NOT NULL,
`object_id` int(11) unsigned,
`object_type` char(1) NOT NULL DEFAULT 'T',
`sort` int(11) unsigned NOT NULL DEFAULT 1,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `entry_lookup` (`object_type`, `object_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_entry_values`;
CREATE TABLE `form_entry_values` (
-- references form_entry.id
`entry_id` int(11) unsigned NOT NULL,
`field_id` int(11) unsigned NOT NULL,
`value` text,
`value_id` int(11),
PRIMARY KEY (`entry_id`, `field_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `list`;
CREATE TABLE `list` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`name_plural` varchar(255),
`sort_mode` enum('Alpha', '-Alpha', 'SortCol') NOT NULL DEFAULT 'Alpha',
`masks` int(11) unsigned NOT NULL DEFAULT 0,
`type` VARCHAR( 16 ) NULL DEFAULT NULL,
`notes` text,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `type` (`type`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `list_items`;
CREATE TABLE `list_items` (
`id` int(11) unsigned NOT NULL auto_increment,
`list_id` int(11),
`status` int(11) unsigned NOT NULL DEFAULT 1,
`value` varchar(255) NOT NULL,
-- extra value such as abbreviation
`extra` varchar(255),
`sort` int(11) NOT NULL DEFAULT 1,
`properties` text,
PRIMARY KEY (`id`),
KEY `list_item_lookup` (`list_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket`;
CREATE TABLE `ticket` (
`ticket_id` int(11) unsigned NOT NULL auto_increment,
`number` varchar(20),
`user_id` int(11) unsigned NOT NULL default '0',
`user_email_id` int(11) unsigned NOT NULL default '0',
`status_id` int(10) unsigned NOT NULL default '0',
`dept_id` int(10) unsigned NOT NULL default '0',
`sla_id` int(10) unsigned NOT NULL default '0',
`topic_id` int(10) unsigned NOT NULL default '0',
`staff_id` int(10) unsigned NOT NULL default '0',
`team_id` int(10) unsigned NOT NULL default '0',
`email_id` int(11) unsigned NOT NULL default '0',
`flags` int(10) unsigned NOT NULL default '0',
`ip_address` varchar(64) NOT NULL default '',
`source` enum('Web','Email','Phone','API','Other') NOT NULL default 'Other',
`isoverdue` tinyint(1) unsigned NOT NULL default '0',
`isanswered` tinyint(1) unsigned NOT NULL default '0',
`duedate` datetime default NULL,
`reopened` datetime default NULL,
`closed` datetime default NULL,
`lastmessage` datetime default NULL,
`lastresponse` datetime default NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`ticket_id`),
KEY `user_id` (`user_id`),
KEY `dept_id` (`dept_id`),
KEY `staff_id` (`staff_id`),
KEY `team_id` (`team_id`),
KEY `status_id` (`status_id`),
KEY `created` (`created`),
KEY `closed` (`closed`),
KEY `duedate` (`duedate`),
KEY `topic_id` (`topic_id`),
KEY `sla_id` (`sla_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_attachment`;
CREATE TABLE `ticket_attachment` (
`attach_id` int(11) unsigned NOT NULL auto_increment,
`ticket_id` int(11) unsigned NOT NULL default '0',
`file_id` int(10) unsigned NOT NULL default '0',
`ref_id` int(11) unsigned NOT NULL default '0',
`inline` tinyint(1) NOT NULL default '0',
`created` datetime NOT NULL,
PRIMARY KEY (`attach_id`),
KEY `ticket_id` (`ticket_id`),
KEY `ref_id` (`ref_id`),
KEY `file_id` (`file_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_lock`;
CREATE TABLE `ticket_lock` (
`lock_id` int(11) unsigned NOT NULL auto_increment,
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(10) unsigned NOT NULL default '0',
`expire` datetime default NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`lock_id`),
UNIQUE KEY `ticket_id` (`ticket_id`),
KEY `staff_id` (`staff_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_email_info`;
CREATE TABLE `ticket_email_info` (
`id` int(11) unsigned NOT NULL auto_increment,
`thread_id` int(11) unsigned NOT NULL,
`email_mid` varchar(255) NOT NULL,
`headers` text,
PRIMARY KEY (`id`),
KEY `email_mid` (`email_mid`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_event`;
CREATE TABLE `ticket_event` (
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(11) unsigned NOT NULL,
`team_id` int(11) unsigned NOT NULL,
`dept_id` int(11) unsigned NOT NULL,
`topic_id` int(11) unsigned NOT NULL,
`state` enum('created','closed','reopened','assigned','transferred','overdue') NOT NULL,
`staff` varchar(255) NOT NULL default 'SYSTEM',
`annulled` tinyint(1) unsigned NOT NULL default '0',
`timestamp` datetime NOT NULL,
KEY `ticket_state` (`ticket_id`, `state`, `timestamp`),
KEY `ticket_stats` (`timestamp`, `state`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_status`;
CREATE TABLE IF NOT EXISTS `ticket_status` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL DEFAULT '',
`state` varchar(16) DEFAULT NULL,
`mode` int(11) unsigned NOT NULL DEFAULT '0',
`flags` int(11) unsigned NOT NULL DEFAULT '0',
`sort` int(11) unsigned NOT NULL DEFAULT '0',
`properties` text NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `state` (`state`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_priority`;
CREATE TABLE `ticket_priority` (
`priority_id` tinyint(4) NOT NULL auto_increment,
`priority` varchar(60) NOT NULL default '',
`priority_desc` varchar(30) NOT NULL default '',
`priority_color` varchar(7) NOT NULL default '',
`priority_urgency` tinyint(1) unsigned NOT NULL default '0',
`ispublic` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`priority_id`),
UNIQUE KEY `priority` (`priority`),
KEY `priority_urgency` (`priority_urgency`),
KEY `ispublic` (`ispublic`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_thread`;
CREATE TABLE `ticket_thread` (
`id` int(11) unsigned NOT NULL auto_increment,
`pid` int(11) unsigned NOT NULL default '0',
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(11) unsigned NOT NULL default '0',
`user_id` int(11) unsigned not null default 0,
`thread_type` enum('M','R','N') NOT NULL,
`poster` varchar(128) NOT NULL default '',
`source` varchar(32) NOT NULL default '',
`title` varchar(255),
`body` mediumtext NOT NULL,
`format` varchar(16) NOT NULL default 'html',
`ip_address` varchar(64) NOT NULL default '',
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `ticket_id` (`ticket_id`),
KEY `staff_id` (`staff_id`),
KEY `pid` (`pid`)
) DEFAULT CHARSET=utf8;
CREATE TABLE `ticket_collaborator` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`isactive` tinyint(1) NOT NULL DEFAULT '1',
`ticket_id` int(11) unsigned NOT NULL DEFAULT '0',
`user_id` int(11) unsigned NOT NULL DEFAULT '0',
-- M => (message) clients, N => (note) 3rd-Party, R => (reply) external authority
`role` char(1) NOT NULL DEFAULT 'M',
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `collab` (`ticket_id`,`user_id`)
) DEFAULT CHARSET=utf8;
In the values table, there is a field object_id this is the ticket ID.
SELECT t.* FROM form_entry_values fev
LEFT JOIN form_entry fe ON(fev.entry_id = fe.id)
LEFT JOIN ticket t ON(fe.object_id = t.ticket_id)
WHERE fev.value = '123'
in the Osticket 1.15 version, the object_id in the form_entry has a relation to the user_id.
SELECT t.* FROM form_entry_values fev
LEFT JOIN form_entry fe ON fev.entry_id = fe.id
LEFT JOIN ticket t ON fe.object_id = t.user_id

SQL {setCharset} AUTO_INCREMENT=1' at line 1

DROP TABLE IF EXISTS `{DB_TABLE_PREFIX}broadcast`;
#[QUERY]
CREATE TABLE `{DB_TABLE_PREFIX}broadcast` (
`bid` mediumint(8) NOT NULL auto_increment,
`sortid` mediumint(8) NOT NULL default '0',
`content` varchar(255) NOT NULL default '',
`gourl` varchar(255) NOT NULL default '#',
PRIMARY KEY (`bid`)
) TYPE=MyISAM {setCharset} AUTO_INCREMENT=1 ;
#[QUERY]
DROP TABLE IF EXISTS `{DB_TABLE_PREFIX}items`;
#[QUERY]
CREATE TABLE `{DB_TABLE_PREFIX}items` (
`itemid` mediumint(8) unsigned NOT NULL auto_increment,
`uid` mediumint(8) unsigned NOT NULL default '0',
`account` varchar(15) NOT NULL default '',
`content` varchar(255) NOT NULL default '',
`dateline` int(10) unsigned NOT NULL default '0',
`digg` mediumint(8) unsigned NOT NULL default '0',
`reply` mediumint(5) unsigned NOT NULL default '0',
PRIMARY KEY (`itemid`),
KEY `uid` (`uid`)
) TYPE=MyISAM {setCharset} AUTO_INCREMENT=1 ;
#[QUERY]
DROP TABLE IF EXISTS `{DB_TABLE_PREFIX}reply`;
#[QUERY]
CREATE TABLE `{DB_TABLE_PREFIX}reply` (
`rid` mediumint(8) NOT NULL auto_increment,
`replyto` mediumint(8) unsigned NOT NULL default '0',
`uid` mediumint(8) NOT NULL default '0',
`account` varchar(15) NOT NULL default '',
`content` varchar(255) NOT NULL default '',
`dateline` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`rid`),
KEY `uid` (`uid`),
KEY `replyto` (`replyto`)
) TYPE=MyISAM {setCharset} AUTO_INCREMENT=1 ;
#[QUERY]
DROP TABLE IF EXISTS `{DB_TABLE_PREFIX}setting`;
#[QUERY]
CREATE TABLE `{DB_TABLE_PREFIX}setting` (
`setname` varchar(255) NOT NULL default '',
`data` text NOT NULL,
PRIMARY KEY (`setname`)
) TYPE=MyISAM {setCharset} ;
I try to import this to mysql database and receive this error
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '{setCharset} AUTO_INCREMENT=1' at line 1
Did I did miss something? Please help me out!
Try this:
CREATE TABLE `broadcast` (
`bid` mediumint(8) NOT NULL auto_increment,
`sortid` mediumint(8) NOT NULL default '0',
`content` varchar(255) NOT NULL default '',
`gourl` varchar(255) NOT NULL default '#',
PRIMARY KEY (`bid`)
) TYPE=MyISAM AUTO_INCREMENT=50 ;
The curly brackets were just for explanation as you refereed the syntax from some other source.
Replace
TYPE=MyISAM
by
ENGINE=MYISAM
Try this, I have used table prefix as "t_"
DROP TABLE IF EXISTS `t_broadcast`;
CREATE TABLE `t_broadcast` (
`bid` MEDIUMINT(8) NOT NULL AUTO_INCREMENT,
`sortid` MEDIUMINT(8) NOT NULL DEFAULT '0',
`content` VARCHAR(255) NOT NULL DEFAULT '',
`gourl` VARCHAR(255) NOT NULL DEFAULT '#',
PRIMARY KEY (`bid`)
) ENGINE=MYISAM;
DROP TABLE IF EXISTS `t_items`;
CREATE TABLE `t_items` (
`itemid` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`uid` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`account` VARCHAR(15) NOT NULL DEFAULT '',
`content` VARCHAR(255) NOT NULL DEFAULT '',
`dateline` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`digg` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`reply` MEDIUMINT(5) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`itemid`),
KEY `uid` (`uid`)
) ENGINE=MYISAM;
DROP TABLE IF EXISTS `t_reply`;
CREATE TABLE `t_reply` (
`rid` MEDIUMINT(8) NOT NULL AUTO_INCREMENT,
`replyto` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`uid` MEDIUMINT(8) NOT NULL DEFAULT '0',
`account` VARCHAR(15) NOT NULL DEFAULT '',
`content` VARCHAR(255) NOT NULL DEFAULT '',
`dateline` INT(10) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`rid`),
KEY `uid` (`uid`),
KEY `replyto` (`replyto`)
) ENGINE=MYISAM;
DROP TABLE IF EXISTS `t_setting`;
CREATE TABLE `t_setting` (
`setname` VARCHAR(255) NOT NULL DEFAULT '',
`data` TEXT NOT NULL,
PRIMARY KEY (`setname`)
) ENGINE=MYISAM ;

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.

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.