MariaDB,如何使用插入复制现有记录?

ercv8c1e  于 2022-11-08  发布在  其他
关注(0)|答案(2)|浏览(138)

我有很多现有记录,我想使用INSERT复制这些记录,例如,如果我有一个查询:

SELECT * FROM `threats` WHERE biDataset=9;

公共关键字是biDataset,此表中的主键是biPK,每次插入时都会自动递增。表结构如下:

CREATE TABLE `threats` (
      `biPK` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
      `biDataset` BIGINT(20) UNSIGNED NOT NULL COMMENT 'ID of dataset',
      `jsonParams` LONGTEXT NOT NULL COMMENT 'JSON object containing all parameters' COLLATE 'utf8mb4_bin',
      `txtTrainee` MEDIUMTEXT NULL DEFAULT NULL COMMENT 'Trainee host name (NULL if not applicable)' COLLATE 'utf8mb4_unicode_ci',
      PRIMARY KEY (`biPK`) USING BTREE,
      UNIQUE INDEX `dataset and json` (`biDataset`, `jsonParams`) USING HASH,
      INDEX `datasetIdx` (`biDataset`) USING BTREE,
      INDEX `jsonIdx` (`jsonParams`(768)) USING BTREE
)
COMMENT='Table of datasets'
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=174;

我想要做的是复制biDataset为9的所有记录,创建新记录,我还需要能够指定新的biDataset,15用于所有副本,而不是9。
我试过了:

INSERT INTO `threats` (biDataset, txtTrainee, jsonParams)
    SELECT 15, NULL, jsonParams FROM `threats` WHERE biDataset=9;

这将导致:

SQL Error (1364): Field 'DB_ROW_HASH_1' doesn't have a default value
k3bvogb1

k3bvogb11#

解决方案是修改表结构以:

CREATE TABLE `threats` (
      `biPK` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
      `biDataset` BIGINT(20) UNSIGNED NULL DEFAULT NULL COMMENT 'ID of dataset',
      `jsonParams` LONGTEXT NULL DEFAULT NULL COMMENT 'JSON object containing all parameters' COLLATE 'utf8mb4_bin',
      `txtTrainee` MEDIUMTEXT NULL DEFAULT NULL COMMENT 'Trainee host name (NULL if not applicable)' COLLATE 'utf8mb4_unicode_ci',
      PRIMARY KEY (`biPK`) USING BTREE,
      UNIQUE INDEX `dataset and json` (`biDataset`, `jsonParams`) USING HASH,
      INDEX `datasetIdx` (`biDataset`) USING BTREE,
      INDEX `jsonIdx` (`jsonParams`(768)) USING BTREE
)
COMMENT='Table of datasets'
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=174
;
jogvjijk

jogvjijk2#

这是mariadb的bug,它仍然没有解决...
checkout 日期:https://jira.mariadb.org/browse/MDEV-22756

相关问题