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

MariaDB, How to copy existing records using insert?

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

SELECT * FROM `threats` WHERE biDataset=9;

公共键是biDataset,这个table中的主键是biPK,它在每次插入时自动递增.这是 table 结构:

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

解决方案是将 table 结构修改为:

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
;