我的目的是从数据库中提取触发器,函数和存储过程,对其进行编辑,然后将其添加到另一个数据库中。
以下是mysqldump
的部分输出。我了解如何使用DROP
,CREATE
和INSERT INTO
语句更新数据库,但是不了解触发器。我期望以下几点:
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;
/*!50003
是什么意思?我认为这是一些注释,这表示触发器的CREATE
不存在,但是我一定对输出有误解。
应该如何解释mysqldump的输出?
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 */ ;
答案 0 :(得分:8)
/* ... */
是注释,可以是一行的一部分,也可以跨很多行。
-- ...
是在行尾停止的注释。
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
是特殊注释,如果由版本低于4.00.00的服务器运行,则为特殊注释。对于4.0.0或更高版本,它是ALTER
。 (;
是否在特殊注释中并不重要。)
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`michael`@`12.34.56.78`*/ /*!50003 TRIGGER `users_BINS` BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
...
END */;;
DELIMITER ;
将其剥离...
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
5.0.3中引入了触发器; DEFINER
子句是在5.0.17中添加的。
如果您要编辑文本,则可以删除/*!50017
和*/
并假定您的版本足够新,或者可以将其保留在原处并解决它们。做一些方便的代码。