mysql 如果表已经存在,则不覆盖数据;如果不存在,则使用数据创建表

o7jaxewo  于 2023-03-17  发布在  Mysql
关注(0)|答案(2)|浏览(211)

我正在尝试修改MySQL(InnoDB引擎)生成的转储文件,我想

  • 检查是否存在表
  • 如果已经存在,则不要覆盖数据
  • 如果未找到,则创建一个新的,并填写默认数据

我可以让上述两个条件分别工作,但不能使用IF语句。
这是一个示例脚本:
planned_objective的表结构

/*DROP TABLE IF EXISTS `planned_objective`*/;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `planned_objective` (
  `objective_id` int(11) NOT NULL AUTO_INCREMENT,
  `objective` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `start_depth` float NOT NULL,
  `end_depth` float NOT NULL,
  `depth_uom` varchar(45) CHARACTER SET latin1 NOT NULL,
  `wellID` int(12) NOT NULL,
  PRIMARY KEY (`objective_id`,`start_depth`,`wellID`)
) ENGINE=InnoDB AUTO_INCREMENT=1195 DEFAULT CHARSET=utf8 COMMENT='details about planned objectives like casing, cementing, hole conditioning, tripping for BHA (agitator or change in steering unit) ';
/*!40101 SET character_set_client = @saved_cs_client */;

正在转储表planned_objective的数据

LOCK TABLES `planned_objective` WRITE;
/*!40000 ALTER TABLE `planned_objective` DISABLE KEYS */;
INSERT INTO `planned_objective` VALUES (*VALUES TO BE WRITTEN*);
/*!40000 ALTER TABLE `planned_objective` ENABLE KEYS */;
UNLOCK TABLES;

创建部分是好的,但转储数据部分是我可能需要有一个IF语句,纠正我,如果我错了。感谢提前帮助!!

whlutmcx

whlutmcx1#

如果还插入了具有主键的字段或具有唯一键的字段,则可以使用INSERT IGNORE ....。这将仅插入新行

INSERT IGNORE INTO `planned_objective` VALUES (*VALUES TO BE WRITTEN*);
zazmityj

zazmityj2#

下面是另一个技巧,您可以这样做,或者必须使用存储过程
1.创建你的表如果不存在
1.用相同的结构创建临时表
1.将init数据导入临时表
1.检查主表是否有行并将结果存储在变量中
1.仅当为空时插入int主表6清除临时表

CREATE TABLE IF NOT EXISTS `planned_objective` (
 `objective_id` int(11) NOT NULL AUTO_INCREMENT,
 `objective` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
 `start_depth` float NOT NULL,
 `end_depth` float NOT NULL,
 `depth_uom` varchar(45) CHARACTER SET latin1 NOT NULL,
 `wellID` int(12) NOT NULL, PRIMARY KEY(`objective_id`,`start_depth`,`wellID`)) ENGINE=InnoDB AUTO_INCREMENT=1195 DEFAULT CHARSET=utf8 COMMENT='details about planned objectives like casing, cementing, hole conditioning, tripping for BHA (agitator or change in steering unit) ';

 DROP TABLE IF EXISTS `tmp_planned_objective`;

 CREATE TABLE IF NOT EXISTS `tmp_planned_objective` (
   `objective_id` int(11) NOT NULL AUTO_INCREMENT,
   `objective` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
   `start_depth` float NOT NULL,
   `end_depth` float NOT NULL,
   `depth_uom` varchar(45) CHARACTER SET latin1 NOT NULL,
   `wellID` int(12) NOT NULL,
   PRIMARY KEY (`objective_id`,`start_depth`,`wellID`)
 ) ENGINE=InnoDB AUTO_INCREMENT=1195 DEFAULT CHARSET=utf8 COMMENT='details about planned objectives like casing, cementing, hole conditioning, tripping for BHA (agitator or change in steering unit) ';

 /* insert init data into tmp table */
 INSERT INTO `tmp_planned_objective` (`objective_id`, `objective`, `start_depth`, `end_depth`, `depth_uom`, `wellID`)
 VALUES
     (1, '2', 3, 4, '5', 6),
     (2, '2', 3, 4, '5', 6),
     (3, '2', 3, 4, '5', 6),
     (4, '2', 3, 4, '5', 6),
     (5, '2', 3, 4, '5', 6),
     (6, '2', 3, 4, '5', 6),
     (7, '2', 3, 4, '5', 6);

/* 检查是否为空 /
选择计数(
)进入@空标志从planned_objective;
SELECT @空标志;/* 仅用于调试 /
/
仅在表为空时复制 */
插入planned_objectivetmp_planned_objective中选择 ,其中@空标志= 0;
/
保存临时表 */
如果存在tmp_planned_objective,则丢弃表格;

相关问题