MySQL - auto-increment for guidance

I have a table with an Auto-increment ID field as shown below.

  + ------------ + ------------------------------------ - +
 |  company_id |  name |
 + ------------ + ------------------------------------ - +
 |  1 |  International Client |
 |  2 |  Oracle |
 |  3 |  test |
 |  4 |  testabc |
 |  5 |  testdef |
 |  6 |  abcd |
 + ------------ + ------------------------------------ - +

I want to update a GUID column using a function

  uuid () 
.

Also, how do I update foreign key references to the correct GUID?

+4
source share
2 answers

Use triggers.

CREATE TABLE `tbl_test` ( `GUID` char(40) NOT NULL, `Name` varchar(50) NOT NULL, PRIMARY KEY (`GUID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

table and pk, now a trigger ..

 DELIMITER // CREATE TRIGGER `t_GUID` BEFORE INSERT ON `tbl_test` FOR EACH ROW begin SET new.GUID = uuid(); end// DELIMITER ; 

Now try

 insert into tbl_test(Name) value('trigger happy...'); 

Regards, / T

+7
source

you cannot use it with auto increments

guid char not intger

you need to insert it yourself

also you will need to change the id to char (40)

 insert into table_name (id,name) values (uuid(),'jon'); 
+3
source

Source: https://habr.com/ru/post/1342780/


All Articles