I run the following query:
SELECT name FROM shops ORDER BY name;
The current result:
ABC shop
ÁDE shop
ALT shop
Expected result should be:
ABC shop
ALT shop
ÁDE shop
Looks like A and Á are equal. (Correct order: a < á < e < é < o < ó < ö...)
I tried to use hungarian collation (with mysql 8.0.17):
ALTER DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_hu_0900_ai_ci;
I found that the mysql team have fixed this issue in mysql 8.0.1: https://bugs.mysql.com/bug.php?id=12519
SHOW CREATE TABLE shops;
| shops | CREATE TABLE `shops` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`address_id` bigint(20) unsigned DEFAULT NULL,
`shop_category_id` bigint(20) unsigned DEFAULT NULL,
`shop_chain_id` bigint(20) unsigned DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`partner_id` bigint(20) unsigned DEFAULT NULL,
`location_id` bigint(20) unsigned DEFAULT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`user_id` bigint(20) unsigned DEFAULT NULL,
`deputy_user_id` bigint(20) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `shops_address_id_foreign` (`address_id`),
KEY `shops_shop_category_id_foreign` (`shop_category_id`),
KEY `shops_shop_chain_id_foreign` (`shop_chain_id`),
KEY `shops_partner_id_foreign` (`partner_id`),
KEY `shops_location_id_foreign` (`location_id`),
KEY `shops_user_id_foreign` (`user_id`),
KEY `shops_deputy_user_id_foreign` (`deputy_user_id`),
CONSTRAINT `shops_address_id_foreign` FOREIGN KEY (`address_id`) REFERENCES `addresses` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_deputy_user_id_foreign` FOREIGN KEY (`deputy_user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_location_id_foreign` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_partner_id_foreign` FOREIGN KEY (`partner_id`) REFERENCES `partners` (`id`) ON DELETE CASCADE,
CONSTRAINT `shops_shop_category_id_foreign` FOREIGN KEY (`shop_category_id`) REFERENCES `shop_categories` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_shop_chain_id_foreign` FOREIGN KEY (`shop_chain_id`) REFERENCES `shop_chains` (`id`) ON DELETE SET NULL,
CONSTRAINT `shops_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=564 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hu_0900_ai_ci
No simple solution:
Plan A: Write your own collation.
Plan B: Write a bug at bugs.mysql.com complaining that even in 8.0.17, utf8mb4_hu_0900_ai_ci is "incorrect".
(Meanwhile, I suspect utf8mb4_hungarian_ci was left with the 5.7 collation for backward compatibility.)
The question itself is wrong. The expected result is:
ABC shop
ÁDE shop
ALT shop
and it's right.
https://helyesiras.mta.hu/helyesiras/default/akh12#F2_4
Related
I'm very new in MySQL and im making a school project. I have this table:
CREATE TABLE IF NOT EXISTS `Reserva` (
`idReservacion` int(10) NOT NULL AUTO_INCREMENT,
`importe` int(10) DEFAULT NULL,
`fecha` date DEFAULT NULL,
`idCliente` int(11) DEFAULT NULL,
PRIMARY KEY (`idReservacion`),
KEY `idCliente` (`idCliente`),
CONSTRAINT `Reserva_ibfk_1` FOREIGN KEY (`idCliente`) REFERENCES `Cliente` (`idcliente`)
) ENGINE=InnoDB AUTO_INCREMENT=60001 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
And im triying to make Importe a generated field by an update using the next tables
CREATE TABLE IF NOT EXISTS `PaseDeAbordar` (
`idPaseDeAbordar` int(11) NOT NULL AUTO_INCREMENT,
`hora` time DEFAULT NULL,
`idReservacion` int(5) NOT NULL,
`idAsiento` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
`idVuelo` int(11) NOT NULL,
`precioc` int(10) DEFAULT NULL,
PRIMARY KEY (`idPaseDeAbordar`),
KEY `idReservacion` (`idReservacion`),
KEY `idAsiento` (`idAsiento`),
KEY `idVuelo` (`idVuelo`),
CONSTRAINT `PaseDeAbordar_ibfk_1` FOREIGN KEY (`idVuelo`) REFERENCES `Vuelo` (`idvuelo`),
CONSTRAINT `PaseDeAbordar_ibfk_2` FOREIGN KEY (`idReservacion`) REFERENCES `Reservacion` (`idreservacion`),
CONSTRAINT `PaseDeAbordar_ibfk_3` FOREIGN KEY (`idAsiento`) REFERENCES `Asiento` (`idasiento`)
) ENGINE=InnoDB AUTO_INCREMENT=225001 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Pasedeabordar has the foreign keys from Vuelo and Asiento, this is the structure of those:
CREATE TABLE IF NOT EXISTS `Asiento` (
`idAsiento` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`idClase` int(11) NOT NULL,
`precio` int(11) NOT NULL,
PRIMARY KEY (`idAsiento`),
KEY `idClase` (`idClase`),
CONSTRAINT `Asiento_ibfk_1` FOREIGN KEY (`idClase`) REFERENCES `Clase` (`idclase`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Vuelo:
CREATE TABLE IF NOT EXISTS `Vuelo` (
`idVuelo` int(8) NOT NULL AUTO_INCREMENT,
`matriculaAvion` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`origen` varchar(4) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`destino` varchar(4) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`fechaVuelo` date NOT NULL,
`horaSalida` time NOT NULL,
`horaLlegada` time NOT NULL,
`duracion` time NOT NULL,
`tieneEscala` varchar(2) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT 'NO',
`precio` int(10) NOT NULL,
PRIMARY KEY (`idVuelo`),
KEY `origen` (`origen`),
KEY `destino` (`destino`),
KEY `matriculaAvion` (`matriculaAvion`),
CONSTRAINT `Vuelo_ibfk_1` FOREIGN KEY (`origen`) REFERENCES `Terminal` (`idterminal`),
CONSTRAINT `Vuelo_ibfk_2` FOREIGN KEY (`destino`) REFERENCES `Terminal` (`idterminal`),
CONSTRAINT `Vuelo_ibfk_3` FOREIGN KEY (`matriculaAvion`) REFERENCES `Avion` (`matricula`)
) ENGINE=InnoDB AUTO_INCREMENT=5501 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Importe is suposed to be generated on this way:
reservacion.importe=(precio from asiento+precio from vuelo) where paseabordar.reservacionid=importe.reservacionid
UPDATE reservacion r, pasedeabordar pa, vuelo v, asiento a
SET r.importe=SUM(v.precio+a.precio)
WHERE pa.idreservacion=r.idreservacion
AND pa.idAsiento=a.idAsiento
AND pa.idVuelo=v.idVuelo;
But this generates me an error
Error(1111): Invalid use of group function
Any ideas on what im doing wrong?
SUM() is a group function, which is used to add up cells from the same column. You are simply trying to add up two values from the same row.
r.importe=v.precio+a.precio should work.
By the way, it's considered good practice to join your tables explicitly, like this:
UPDATE reservacion r
JOIN pasedeabordar pa ON pa.idreservacion = r.idreservacion
JOIN vuelo v ON pa.idVuelo = v.idVuelo
JOIN asiento a ON pa.idAsiento = a.idAsiento
SET r.importe = v.precio + a.precio;
CREATE TABLE `call_session` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`contact_id` bigint(20) unsigned NOT NULL,
`campaign_id` bigint(20) unsigned DEFAULT NULL,
`user_id` bigint(20) unsigned DEFAULT NULL,
`start_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`type` varchar(16) DEFAULT NULL,
`status` varchar(16) DEFAULT NULL,
`continue_reason_id` bigint(20) unsigned DEFAULT NULL,
`continue_by` bigint(20) unsigned DEFAULT NULL,
`end_reason_id` bigint(20) unsigned DEFAULT NULL,
`comment` varchar(255) DEFAULT '',
`call_count` int(11) NOT NULL DEFAULT '0',
`answered_call_count` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ix_call_session_contact_id` (`contact_id`),
KEY `ix_call_session_user_id` (`user_id`),
KEY `ix_call_session_campaign_id` (`campaign_id`),
KEY `call_session_continue_by_foreign` (`continue_by`),
KEY `call_session_end_reason_id_foreign` (`end_reason_id`),
KEY `ix_call_session_end_time` (`end_time`),
KEY `ix_call_session_start_time` (`start_time`),
KEY `ix_call_session_continue_reason_id` (`continue_reason_id`),
CONSTRAINT `call_session_campaign_id_foreign` FOREIGN KEY (`campaign_id`) REFERENCES `campaign` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_continue_by_foreign` FOREIGN KEY (`continue_by`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_continue_reason_id_foreign` FOREIGN KEY (`continue_reason_id`) REFERENCES `continue_reason` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `call_session_end_reason_id_foreign` FOREIGN KEY (`end_reason_id`) REFERENCES `end_reason` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `call_session_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
)
CREATE TABLE `continue_reason` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`project_id` bigint(20) unsigned DEFAULT NULL,
`sort` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `ix_continue_reason_project_id` (`project_id`),
CONSTRAINT `continue_reason_project_id_foreign` FOREIGN KEY (`project_id`) REFERENCES `project` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
The call_session table contain more than 500,000 rows.
The continue_reason table contain only one row.
When I join the continue_reason table for the name column, mysql ignore the start_date index so the query takes between 1.8 to 3 seconds.
I can't figure out why.
The problematic query is:
SELECT `call_session`.`id`,
`continue_reason`.`name`
FROM `call_session`
LEFT JOIN `continue_reason` ON `continue_reason`.`id` = `call_session`.`continue_reason_id`
ORDER BY `call_session`.`start_time` DESC LIMIT 1;
Explain result:
Same query without join works great:
SELECT `call_session`.`id`,
(SELECT `name` FROM `continue_reason` WHERE id = `call_session`.`continue_reason_id`) AS `continue_reason.name`
FROM `call_session`
ORDER BY `call_session`.`start_time` DESC LIMIT 1;
Explain results:
I found many similar questions but I don't have any DISTINCT, no GROUP BY, and there is a very clear benefit for using the index.
Any thoughts?
Thanks in advance
P.S. Force index didn't help.
Here is the Optimizer report
I'm struggling around to create a foreign key with the following query:
alter table `users` add constraint `users_sales_partner_id_foreign` foreign key (`sales_partner_id`) references `structures` (`sales_partner_id`) on update cascade
InnoDB log says, it can't match this index:
2017-02-27 10:25:47 Error in foreign key constraint of
table website_backend/users: foreign key
(sales_partner_id) references structures (sales_partner_id) on update cascade:
Cannot find an index in the referenced table where the referenced
columns appear as the first columns, or column types in the table and
the referenced table do not match for constraint.
I have already checked typos and data type incompatibility, but everything seems to be alright:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`profile_id` int(10) unsigned NOT NULL,
`sales_partner_id` int(11) NOT NULL,
`email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`state` enum('pending','confirmed','active','deactivated') COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_profile_id_unique` (`profile_id`),
UNIQUE KEY `users_sales_partner_id_unique` (`sales_partner_id`),
UNIQUE KEY `users_email_unique` (`email`),
CONSTRAINT `users_profile_id_foreign` FOREIGN KEY (`profile_id`) REFERENCES `profiles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `structures` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sales_partner_id` int(11) NOT NULL,
`sales_partner_structure` int(11) DEFAULT NULL,
`active` tinyint(1) NOT NULL,
`blocked` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
I can't get my problem, does somebody have a clue? Thanks in advance!
Thanks to Paul Spiegel for his reminding that I can only set FKs to the indexed fields. After another review I noticed that my referenced field was not indexed, that solved my problem.
I can run the below query and there are no nulls in the user_id column in the result -
select c.*,r.user_id from chat_group_users c left join roleuser r
on c.user_id = r.user_id;
But I cannot apply the foreign key constraint and mysql just says
Error Code: 1215. Cannot add foreign key constraint
The command i wrote -
ALTER TABLE chat_group_users
ADD CONSTRAINT fk_roleuser FOREIGN KEY (user_id) REFERENCES roleuser (user_id)
ON DELETE cascade ON UPDATE cascade;
The table structures -
CREATE TABLE `roleuser` (
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`UserNM` varchar(50) NOT NULL,
`UserPS` varchar(150) NOT NULL,
`r_username` varchar(50) DEFAULT NULL,
`UGroup` varchar(5) NOT NULL DEFAULT 'Usar',
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
PRIMARY KEY (`User_ID`),
UNIQUE KEY `Index_2` (`UserNM`),
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=latin1;
CREATE TABLE `chat_group_users` (
`auto_id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` int(11) DEFAULT NULL,
`user_id` int(10) DEFAULT NULL,
`is_admin` varchar(5) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`auto_id`),
UNIQUE KEY `unique_user_group` (`user_id`,`group_id`),
KEY `fc_group_id` (`group_id`),
CONSTRAINT `fc_group_id` FOREIGN KEY (`group_id`)
REFERENCES `chat_group`(`auto_id`)
ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
missed the unsigned on the chat_group_users -
`User_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, ...
i have problem in mysql field is " the class name " I'm Arabic man so our class name
like that :11D2 assume that D is Arabic letter , but I'm mysql field it appears like that "112D" cuz my Arabic language start from Right to left .
so i want to ask how to fix it and store it like 11D2
CREATE TABLE `class` (
`CLASS_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CLASS_NAME` varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`Grade_id` int(10) unsigned NOT NULL,
`Stage_id` int(10) unsigned NOT NULL,
`school_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`CLASS_ID`),
KEY `school_ids3` (`school_id`),
KEY `grade_ids` (`Grade_id`) USING BTREE,
KEY `stage_ids2` (`Stage_id`) USING BTREE,
CONSTRAINT `grade_id4` FOREIGN KEY (`Grade_id`) REFERENCES `grade` (`Grade_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `school_id8` FOREIGN KEY (`school_id`) REFERENCES `schools` (`school_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `stage_id3` FOREIGN KEY (`Stage_id`) REFERENCES `stage` (`STAGE_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8