Description:
My understanding:
Backticks can be used on column and table names.
Backticks can not be used on variables.
Explanation:
Within a trigger, when using a variable with the same name as a column, the backticks should force it to be a column. Only when prepending a table name, NEW or OLD, it is treated as a column.
When running the test:
firsttest is "this_should_not_appear_anywhere" instead of "value_of_testcolumn" in the sample code.
How to repeat:
-- MySQL dump 10.13 Distrib 8.0.31, for Linux (x86_64)
--
-- ------------------------------------------------------
-- Server version 8.0.31
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `mysqltest`
--
DROP TABLE IF EXISTS `mysqltest`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `mysqltest` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`firsttest` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '',
`secondtest` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '',
`thirdtest` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '',
`fourthtest` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `mysqltest`
--
LOCK TABLES `mysqltest` WRITE;
/*!40000 ALTER TABLE `mysqltest` DISABLE KEYS */;
INSERT INTO `mysqltest` VALUES (1,'this_should_not_appear_anywhere','value_of_testcolumn','value_of_notconflictingcolumn','normal_behaviour');
/*!40000 ALTER TABLE `mysqltest` ENABLE KEYS */;
UNLOCK TABLES;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8mb4 */ ;
/*!50003 SET character_set_results = utf8mb4 */ ;
/*!50003 SET collation_connection = utf8mb4_unicode_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = '' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`%`*/ /*!50003 TRIGGER `insert_mysqltest_before` BEFORE INSERT ON `mysqltest` FOR EACH ROW BEGIN
DECLARE testcolumn CHAR(255);
DECLARE firstvar CHAR(255);
DECLARE secondvar CHAR(255);
DECLARE thirdvar CHAR(255);
DECLARE fourthvar CHAR(255);
SET testcolumn='this_should_not_appear_anywhere';
SELECT `testcolumn` INTO firstvar FROM mysqltest2 WHERE id=1 LIMIT 1;
SELECT `mysqltest2`.`testcolumn` INTO secondvar FROM mysqltest2 WHERE id=1 LIMIT 1;
SELECT `mysqltest2`.`notconflicting` INTO thirdvar FROM mysqltest2 WHERE id=1 LIMIT 1;
SET fourthvar='before';
SET testcolumn='normal_behaviour';
SELECT testcolumn INTO fourthvar FROM mysqltest2 WHERE id=2 LIMIT 1;
SET NEW.`firsttest`=firstvar;
SET NEW.`secondtest`=secondvar;
SET NEW.`thirdtest`=thirdvar;
SET NEW.`fourthtest`=fourthvar;
END */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
--
-- Table structure for table `mysqltest2`
--
DROP TABLE IF EXISTS `mysqltest2`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `mysqltest2` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`testcolumn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`notconflicting` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `mysqltest2`
--
LOCK TABLES `mysqltest2` WRITE;
/*!40000 ALTER TABLE `mysqltest2` DISABLE KEYS */;
INSERT INTO `mysqltest2` VALUES (1,'value_of_testcolumn','value_of_notconflictingcolumn'),(2,'this_is_expected','this_is_expected');
/*!40000 ALTER TABLE `mysqltest2` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2023-01-10 0:11:20
truncate table mysqltest; insert into mysqltest () values (); select * from mysqltest;
Suggested fix:
Either specify in the docs that backticks can enclose variables, columns and tables instead of just columns and tables or fix the code.