Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret mysqldump output?

Tags:

mysql

triggers

My intent is to extract the triggers, functions, and stored procedures from a database, edit them, and add them to another database.

Below is a partial output from mysqldump. I understand how the database is updated with the DROP, CREATE, andINSERT INTO statements, but don't understand the triggers. I expected the following:

CREATE TRIGGER users_BINS BEFORE INSERT ON users
FOR EACH ROW
if(IFNULL(NEW.idPublic, 0) = 0) THEN
   INSERT INTO _inc_accounts (type, accountsId, idPublic) values ("users",NEW.accountsId,1)
   ON DUPLICATE KEY UPDATE idPublic = idPublic + 1;
   SET NEW.idPublic=(SELECT idPublic FROM _inc_accounts WHERE accountsId=NEW.accountsId AND type="users");
END IF;

What does /*!50003 mean? I thought it was some comment which would mean the CREATE for the trigger isn't present, but I must be misinterpreting the output. How should one interpret a mysqldump output?

mysqldump -u username-ppassword --routines mydb

--
-- Table structure for table `users`
--

DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `idPublic` int(11) NOT NULL,
  `accountsId` int(11) NOT NULL,
  `firstname` varchar(45) NOT NULL,
  `lastname` varchar(45) NOT NULL,
  `email` varchar(45) NOT NULL,
  `username` varchar(45) NOT NULL,
  `password` char(255) NOT NULL COMMENT 'Password currently uses bcrypt and only requires 60 characters, but may change over time.',
  `tsCreated` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `osTicketId` int(11) NOT NULL,
  `phone` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniqueEmail` (`accountsId`,`email`),
  UNIQUE KEY `uniqueUsername` (`accountsId`,`username`),
  KEY `fk_users_accounts1_idx` (`accountsId`),
  CONSTRAINT `fk_users_accounts1` FOREIGN KEY (`accountsId`) REFERENCES `accounts` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `users`
--

LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` VALUES (xxx
/*!40000 ALTER TABLE `users` 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  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`michael`@`12.34.56.78`*/ /*!50003 TRIGGER `users_BINS` BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
if(IFNULL(NEW.idPublic, 0) = 0) THEN
   INSERT INTO _inc_accounts (type, accountsId, idPublic) values ("users",NEW.accountsId,1)
   ON DUPLICATE KEY UPDATE idPublic = idPublic + 1;
   SET NEW.idPublic=(SELECT idPublic FROM _inc_accounts WHERE accountsId=NEW.accountsId AND type="users");
END IF;
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 */ ;
like image 530
user1032531 Avatar asked Feb 23 '19 19:02

user1032531


Video Answer


1 Answers

/* ... */ 

is a comment that can be part of a line or span many lines.

-- ...

is a comment that stops at the end of the line.

/*!40000 ALTER TABLE `users` DISABLE KEYS */;

is a special comment that is a comment if run by a server with a version lower than 4.00.00. For 4.0.0 or later, it is an ALTER. (It should not matter whether the ; is inside the special comment.)

DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`michael`@`12.34.56.78`*/ /*!50003 TRIGGER `users_BINS` BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
    ...
END */;;
DELIMITER ;

Peeling that apart...

DELIMITER ;;   -- Change the statement terminator
;;             -- finally terminate the "one" statement, namely the entire trigger
DELIMITER ;    -- change there terminator back

/*!50003 CREATE*/   -- same as  CREATE  for >= 5.0.3

/*!50017 DEFINER=`...`*/  -- same as  DEFINER=`...`  for >= 5.0.17

/*!50003 TRIGGER `users_BINS` BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
    ...
END */   -- again, >= 5.0.3

Triggers came in in 5.0.3; the DEFINER clause was added in 5.0.17.

If you are going to edit the text, you can either remove /*!50017 and */ and assume your version is new enough, or you could leave them in place and work around them. Do whatever is convenient for your code.

like image 169
Rick James Avatar answered Nov 08 '22 11:11

Rick James