简体   繁体   English

SQL-是否可以替换uuid主键中的值?

[英]SQL - is it possible to replace value in primary key for uuid?

I have a test table having say ID field (primary key) with values like (abcd1234, mnop5678). 我有一个测试表,该表具有ID字段(主键),其值类似于(abcd1234,mnop5678)。 Now I want to replace all values of ID to introduce hyphens so that the value becomes (abcd-1234, mnop-5678). 现在,我想替换ID的所有值以引入连字符,以便该值变为(abcd-1234,mnop-5678)。 How do I do it using SQL)? 如何使用SQL进行操作?

PS: The real use case is that the ID field used to be an alphanumeric value and is now changed to uuid format (having multiple hyphens in 8-4-4-4-12 format) Ask is to convert the existing data so as to avoid doing a truncate and reload of data as it contains millions of rows. PS:真正的用例是ID字段曾经是一个字母数字值,现在已更改为uuid格式 (具有8-4-4-4-12格式的多个连字符)Ask是将现有数据转换为避免截断和重载数据,因为它包含数百万行。 Sorry for any typos, typing using mobile. 很抱歉输入错误,请使用手机输入。

MYSQL Solution: MYSQL解决方案:
Suppose we have a table test1 where primary key is of varchar type, refer below table structure. 假设我们有一个表test1,其中主键是varchar类型,请参见下面的表结构。

CREATE TABLE `test1` (
 `id` varchar(20) NOT NULL COMMENT 'Primary Key',
 `name` varchar(255) DEFAULT NULL COMMENT 'Name',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1  

Inserting sample records: 插入样本记录:

INSERT INTO `test1` (`id`, `name`) VALUES ('abcd1234', 'abcd');
INSERT INTO `test1` (`id`, `name`) VALUES ('mnop5678', 'mnop');

Now performing update query, where we are considering ID field format should be first 4 characters are letter, 5th character is -(hyphen) & then after numbers. 现在执行更新查询,我们正在考虑ID字段格式应该是前4个字符是字母,第5个字符是-(连字符),然后是数字。

UPDATE `test1` SET `id` = CONCAT(SUBSTRING(`id`, 1, 4), '-', SUBSTRING(`id`, 5)) WHERE 1;

Have excluded where clause here, because need to do update operation on complete table. 这里排除了where子句,因为需要对完整表进行更新操作。

A sketch using some pseudo SQL dialect, as you didn't tag your DBMS: 使用一些伪SQL方言的草图,因为您没有标记DBMS:

  1. You should first create a new column in the table with an UUID type. 您应该首先在表中使用UUID类型创建一个新列。

     ALTER TABLE t ADD (new_id uuid); 
  2. Then fill it with GUID. 然后用GUID填充它。

     UPDATE t SET new_id = new_uuid(); 
  3. Then alter all tables that reference the table as well. 然后更改所有引用该表的表。

     ALTER u ADD (new_t uuid); ... 
  4. Update them as well. 也更新它们。

     UPDATE u SET new_t = (SELECT t.new_id FROM t WHERE t.id = ut); 
  5. Drop the old column in the referencing tables. 将旧列放在引用表中。

     ALTER TABLE u DROP t; ... 
  6. Drop the column in the table. 将表中的列删除。

     ALTER TABLE t DROP id; 
  7. Rename the column in the table. 重命名表中的列。

     ALTER TABLE t RENAME new_id TO id; 
  8. Rename the column in the referencing tables. 重命名引用表中的列。

     ALTER TABLE u RENAME new_t TO t; ... 
  9. Reintroduce the constraints on the table. 重新引入表上的约束。

     ALTER TABLE t ADD (PRIMARY KEY (id)); 
  10. Reintroduce the constraints on the referencing tables. 重新引入引用表上的约束。

     ALTER TABLE u ADD (FOREIGN KEY (t) REFERENCES t (id)); 

You might also need to reindex the columns depending on the indexes you had on the old columns. 您可能还需要根据旧列上的索引来重新索引列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM