Adding a Foreign Key to MariaDB / MySQL table - mysql

I have 3 tables:
CREATE TABLE `channels` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`status` enum('active','inactive') NOT NULL DEFAULT 'active’,
`description` text DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=485 DEFAULT CHARSET=utf8;
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content` text DEFAULT NULL,
`description` text DEFAULT NULL,
`status` enum('active','inactive') NOT NULL DEFAULT 'active',
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=128 DEFAULT CHARSET=utf8;
CREATE TABLE `events` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content` text DEFAULT NULL,
`timezone` varchar(255) DEFAULT NULL,
`recurring` tinyint(1) NOT NULL DEFAULT 0,
`all_day` tinyint(1) NOT NULL DEFAULT 0,
`starts_at` timestamp NULL DEFAULT NULL,
`ends_at` timestamp NULL DEFAULT NULL,
`started_at` timestamp NULL DEFAULT NULL,
`completed_at` timestamp NULL DEFAULT NULL,
`status` enum('active','inactive','in_progress','complete') DEFAULT 'active',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=utf8;
I want to alter the events table to add more fields and add foreign keys to channel, event_type and client_event_type.
I've tried this:
alter table `events`
add `channel` int unsigned not null,
add `event_type` int unsigned not null,
add `client_event_type` int unsigned not null,
add constraint `events_channel_foreign` foreign key `channel` references `channels`(`id`),
add constraint `events_event_type_foreign` foreign key `event_type` references `categories`(`id`),
add constraint `events_clientEventType_foreign` foreign key `client_event_type` references `categories`(`id`),
It comes up with :
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'references channels(id), add constraint
events_event_type_foreign foreig' at line 5
What am I doing wrong?
Thanks

You're missing parantheses:
alter table `events`
add `channel` int unsigned not null,
add `event_type` int unsigned not null,
add `client_event_type` int unsigned not null,
add constraint `events_channel_foreign` foreign key (`channel`) references `channels`(`id`),
add constraint `events_event_type_foreign` foreign key (`event_type`) references `categories`(`id`),
add constraint `events_clientEventType_foreign` foreign key (`client_event_type`) references `categories`(`id`)

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 ...

Invalid Default value (Time Stamp) issue in Database(SQL)

I'm try to create table as below, but get error:
Invalid default value for 'updated_at'
I don't know what to do...what's wrong in query.
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` int(11) DEFAULT NULL,
`title` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL UNIQUE,
`views` int(11) NOT NULL DEFAULT '0',
`image` varchar(255) NOT NULL,
`body` text NOT NULL,
`published` tinyint(1) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
According SQL standard '0000-00-00 00:00:00' is not valid value for timestamp field. You should to use NULL as default value fro updated_at column.
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` int(11) DEFAULT NULL,
`title` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL UNIQUE,
`views` int(11) NOT NULL DEFAULT '0',
`image` varchar(255) NOT NULL,
`body` text NOT NULL,
`published` tinyint(1) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Also ON UPDATE CURRENT_TIMESTAMP part is not relevant for created_at field, but relevant for updated_at

"1062 Duplicate entry" on SELECT

I've got a SELECT query that's returning the following error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user_id-name email#address.com--2018-11-04 01:24:38' for key ''
(I replaced the user_id, name and email for privacy)
SELECT users.user_id, users.fullname, users.email, users.phone_formatted, users.date_created,
MAX(cards.card_id) AS latest_card_id, MAX(cards.date_created) AS latest_date_card, MAX(punches.date_created) AS date_lastvisited
FROM users
INNER JOIN cards ON cards.user_id = users.user_id
INNER JOIN punches ON punches.card_id = cards.card_id
WHERE punches.business_id=1626
GROUP BY users.user_id, users.fullname, users.email, users.phone_formatted, users.date_created
ORDER BY users.fullname;
Here are the table definitions for cards, punches and users:
CREATE TABLE `cards` (
`card_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`business_id` int(11) DEFAULT NULL,
`printjob_id` int(11) DEFAULT NULL,
`temp` varchar(256) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`whitelabel_id` int(11) NOT NULL DEFAULT '0',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`card_id`),
KEY `printjob_id` (`printjob_id`),
KEY `active` (`active`),
KEY `user_id` (`user_id`),
KEY `business_id` (`business_id`)
) ENGINE=InnoDB AUTO_INCREMENT=576475 DEFAULT CHARSET=latin1;
CREATE TABLE `punches` (
`punch_id` int(11) NOT NULL AUTO_INCREMENT,
`punch_count` int(11) unsigned NOT NULL DEFAULT '1',
`employeebonus` tinyint(1) NOT NULL DEFAULT '0',
`details` text,
`card_id` int(11) DEFAULT NULL,
`behaviour` set('basicpunch','employeebonus','visitsregularly','broughtfriend','mytreat','firstcustomer','dailyrepeater','visitslocations','facebookshare','mostfrequent','longabsence','registerer','employeepromoter','luckiest','enroll','opportunist') NOT NULL DEFAULT 'basicpunch',
`location_id` int(11) DEFAULT NULL,
`business_id` int(11) DEFAULT NULL,
`employee_id` int(11) DEFAULT NULL,
`v1` float DEFAULT NULL,
`eventcounter` smallint(5) unsigned NOT NULL DEFAULT '0',
`date_local` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`punch_id`),
KEY `business_id` (`business_id`),
KEY `employee_id` (`employee_id`),
KEY `card_id` (`card_id`),
KEY `date_created` (`date_created`),
KEY `behaviour_id` (`behaviour`),
KEY `location_id` (`location_id`),
KEY `employeebonus` (`employeebonus`),
KEY `punch_count` (`punch_count`)
) ENGINE=InnoDB AUTO_INCREMENT=807402 DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`fullname` varchar(60) DEFAULT NULL,
`email` varchar(50) NOT NULL,
`undeliverable` tinyint(4) DEFAULT '0',
`phone_formatted` varchar(25) DEFAULT NULL,
`phone_unformatted` varchar(15) DEFAULT NULL,
`password_hash` char(64) DEFAULT NULL,
`password_salt` char(6) DEFAULT NULL,
`reset_hash` binary(32) DEFAULT NULL,
`date_reset` datetime DEFAULT NULL,
`stripecustomer_id` int(11) DEFAULT NULL,
`stripe_customer_id` varchar(40) DEFAULT NULL,
`stripe_customer_access_id` varchar(40) DEFAULT NULL,
`problemwithpayment` tinyint(1) NOT NULL DEFAULT '0',
`enterprisemanager_whitelabel_id` int(11) NOT NULL DEFAULT '0',
`god` tinyint(1) NOT NULL DEFAULT '0',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
KEY `email` (`email`),
KEY `phone_unformatted` (`phone_unformatted`),
KEY `date_reset` (`date_reset`),
KEY `undeliverable` (`undeliverable`)
) ENGINE=InnoDB AUTO_INCREMENT=16809 DEFAULT CHARSET=latin1;
I don't understand how I can get this error with a SELECT query. When I remove the MAX() functions, I no longer get the error.

MySQL - Why can't I add a foreign key here?

I'm not sure why I'm not able to add a particular foreign key here. These tables were generated via MySQL Workbench, and based on the MySQL documentation and on searching for other similar problems / solutions, I think everything is in order...yet I can't add one foreign key in particular:
inspection_statuses.inspection_id needs to reference inspection_responses.tpa_result
What am I missing / overlooking?
This is the statement I'm trying to use to add the new foreign key:
ALTER TABLE `vipsouth_app`.`inspection_statuses`
ADD CONSTRAINT `inspection_statuses_ibfk_3`
FOREIGN KEY (`inspection_id`)
REFERENCES `vipsouth_app`.`inspection_responses` (`tpa_result`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
And that produces this error:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1005: Can't create table vipsouth_app.#sql-1f48_7 (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE `inspection_responses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`tpa_result` varchar(10) CHARACTER SET utf8 NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL DEFAULT '0',
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `tpa_result_UNIQUE` (`tpa_result`),
KEY `inspection_responses_ibfk_1_idx` (`inspection_request_id`),
CONSTRAINT `inspection_responses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `inspection_statuses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`inspection_response_id` int(10) unsigned NOT NULL,
`tpa_code` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`user_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`inspection_id` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`note` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`url` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `inspection_statuses_ibfk_1_idx` (`inspection_request_id`),
KEY `inspection_statuses_ibfk_2_idx` (`inspection_response_id`),
CONSTRAINT `inspection_statuses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `inspection_statuses_ibfk_2` FOREIGN KEY (`inspection_response_id`) REFERENCES `inspection_responses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
The data types and constraints defined on both the columns should be exactly the same. Except for a foreign key can be NULLable.
This should do it for you. I did not get any error.
SET foreign_key_checks = 0;
CREATE TABLE `inspection_responses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`tpa_result` varchar(10) CHARACTER SET utf8 NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL DEFAULT '0',
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `tpa_result_UNIQUE` (`tpa_result`),
KEY `inspection_responses_ibfk_1_idx` (`inspection_request_id`),
CONSTRAINT `inspection_responses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `inspection_statuses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`inspection_response_id` int(10) unsigned NOT NULL,
`tpa_code` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`user_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`inspection_id` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`note` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`url` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `inspection_statuses_ibfk_1_idx` (`inspection_request_id`),
KEY `inspection_statuses_ibfk_2_idx` (`inspection_response_id`),
CONSTRAINT `inspection_statuses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `inspection_statuses_ibfk_2` FOREIGN KEY (`inspection_response_id`) REFERENCES `inspection_responses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SET foreign_key_checks = 1;

FlashChat installation hiccup! SQL syntax error?

I've done away with the (14) and (11) but when trying to install tufat's Flashchat, I keep getting this error:
Could not create DB table 'smf_fc_bans'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 '(14) NOT NULL, userid int(11)
default NULL, banneduserid int(11) default NULL, r' at line 1
Granted I'm still such a novice. If someone could help me I would be so very very grateful.
Table structure for table bans:
CREATE TABLE `bans` (
`id` int NOT NULL auto_increment,
`created` timestamp NOT NULL,
`userid` int default NULL,
`banneduserid` int default NULL,
`roomid` int default NULL,
`ip` varchar default NULL,
KEY `id` (`id`),
KEY `userid` (`userid`),
KEY `created` (`created`)
) ENGINE=MyISAM;
Table structure for table connections:
CREATE TABLE `connections` (
`id` varchar(32) NOT NULL default '',
`updated` timestamp NOT NULL,
`created` timestamp NOT NULL,
`userid` int default NULL,
`roomid` int default NULL,
`state` tinyint(4) NOT NULL default '1',
`color` int default NULL,
`start` int default NULL,
`lang` char(2) default NULL,
`ip` varchar(16) default NULL,
`tzoffset` int default '0',
`chatid` int NOT NULL default '1',
PRIMARY KEY (`id`),
KEY `userid` (`userid`),
KEY `roomid` (`roomid`),
KEY `updated` (`updated`)
) ENGINE=MyISAM;
Table structure for table ignors:
CREATE TABLE `ignors` (
`created` timestamp NOT NULL,
`userid` int default NULL,
`ignoreduserid` int default NULL,
KEY `userid` (`userid`),
KEY `ignoreduserid` (`ignoreduserid`),
KEY `created` (`created`)
) ENGINE=MyISAM;
Table structure for table messages:
CREATE TABLE `messages` (
`id` int(11) NOT NULL auto_increment,
`created` timestamp NOT NULL,
`toconnid` varchar(32) default NULL,
`touserid` int(11) default NULL,
`toroomid` int(11) default NULL,
`command` varchar(255) NOT NULL default '',
`userid` int default NULL,
`roomid` int(11) default NULL,
`txt` text,
PRIMARY KEY (`id`),
KEY `touserid` (`touserid`),
KEY `toroomid` (`toroomid`),
KEY `toconnid` (`toconnid`),
KEY `created` (`created`)
) ENGINE=MyISAM AUTO_INCREMENT=14 ;
Table structure for table rooms:
CREATE TABLE `rooms` (
`id` int NOT NULL auto_increment,
`updated` timestamp NOT NULL,
`created` timestamp NOT NULL,
`name` varchar(64) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`ispublic` char(1) default NULL,
`ispermanent` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `ispublic` (`ispublic`),
KEY `ispermanent` (`ispermanent`),
KEY `updated` (`updated`)
) WNGINW=MyISAM AUTO_INCREMENT=5 ;
Table structure for table users:
CREATE TABLE `users` (
`id` int NOT NULL auto_increment,
`login` varchar(32) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`roles` int NOT NULL default '0',
`profile` text,
PRIMARY KEY (`id`),
KEY `login` (`login`)
) ENGINE=MyISAM AUTO_INCREMENT=2 ;`
The varchar datatype requires a parameter like varchar(50)
CREATE TABLE `bans` (
`id` int NOT NULL auto_increment,
`created` timestamp NOT NULL,
`userid` int default NULL,
`banneduserid` int default NULL,
`roomid` int default NULL,
`ip` varchar( requires a number ) default NULL, <-- HERE
KEY `id` (`id`),
KEY `userid` (`userid`),
KEY `created` (`created`)
) ENGINE=MyISAM;
You also have an error in
CREATE TABLE `rooms` (
`id` int NOT NULL auto_increment,
`updated` timestamp NOT NULL,
`created` timestamp NOT NULL,
`name` varchar(64) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`ispublic` char(1) default NULL,
`ispermanent` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `ispublic` (`ispublic`),
KEY `ispermanent` (`ispermanent`),
KEY `updated` (`updated`)
) WNGINW=MyISAM AUTO_INCREMENT=5 ;
WNGINW=MyISAM AUTO_INCREMENT=5 ;
Should be
ENGINE=MyISAM AUTO_INCREMENT=5 ;