MySQL - autoincrement to guid - mysql

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 the ID column to be a GUID using the uuid() function.
Additionally, how do I update the foreign key references to the correct GUID?

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 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

you can't use it with autoincrement
guid is char not intger
you need to insert it your self
also you will need to change the id to char(40)
insert into table_name (id,name) values (uuid(),'jon');

In mysql 8.0 you can use something like below:
For UUID without dashes:
create table temp (`company_id` VARCHAR(64) NOT NULL DEFAULT (REPLACE(UUID(),'-','')), 'name' varchar(100) NOT NULL);
For UUID with dashes:
create table temp (`company_id` VARCHAR(64) NOT NULL DEFAULT (UUID()), 'name' varchar(100) NOT NULL);
I hope this answers your question, Let me know if it is not or you need more.

Related

Get id of records updated with ON DUPLICATE KEY UPDATE

I want to know if there is a way to get the ID of records updated with ON DUPLICATE KEY UDATE.
For example, I have the users table with the following schema:
CREATE TABLE `users` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx-users-email` (`email`)
);
and insert some users:
INSERT INTO users (email, username) VALUES ("pioz#example.org", "pioz"),("luke#example.org", "luke"),("mike#example.org", "mike");
the result is:
+----+------------------+----------+
| id | email | username |
+----+------------------+----------+
| 1 | pioz#example.org | pioz |
| 2 | luke#example.org | luke |
| 3 | mike#example.org | mike |
+----+------------------+----------+
Now I want to know if, with a query like the following one, is possible to get the ID of the updated records:
INSERT INTO users (email, username) VALUES ("luke#example.org", "luke2"),("mike#example.org", "mike2") ON DUPLICATE KEY UPDATE username=VALUES(username);
In this example ID 2 and 3.
It seems that the only solution is to used a stored procedure. Here is an example for one row, which could be expanded.
See dbFiddle link below for schema and testing.
CREATE PROCEDURE add_update_user(IN e_mail VARCHAR(25), IN user_name VARCHAR(25) )
BEGIN
DECLARE maxB4 INT DEFAULT 0;
DECLARE current INT DEFAULT 0;
SELECT MAX(ID) INTO maxB4 FROM users;
INSERT INTO users (email, username) VALUES
(e_mail, user_name)
ON DUPLICATE KEY UPDATE username=VALUES(username);
SELECT ID INTO current FROM users WHERE email =e_mail;
SELECT CASE WHEN maxB4 < current THEN CONCAT('New user with ID ', current, ' created')
ELSE CONCAT('User with ID ', current, ' updated') END Report;
/*SELECT CASE WHEn maxB4 < current THEN 1 ELSE 0 END;*/
END
call add_update_user('jake#example.com','Jake');
| Report |
| :------------------------- |
| New user with ID 6 created |
call add_update_user('jake#example.com','Jason');
| Report |
| :--------------------- |
| User with ID 6 updated |
db<>fiddle here
Plan A: Use the technique in the ref manual -- see LAST_INSERT_ID()
Plan B: Get rid of id and make email the PRIMARY KEY

What is the best way to generate primary key automatically?

I want to generate my primary key automatically in MySQL data table. But I want it to be a character string though in sequence.
Example:
user_id (Primary Key)
USER000001
USER000002
USER000003
USER000004 ....and so on..
By separate table for sequencing and a trigger, you can generate PK automatically with your format.
Tables
CREATE TABLE tableName_seq
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
CREATE TABLE tableName
(
id VARCHAR(10) NOT NULL PRIMARY KEY DEFAULT '0', name VARCHAR(30)
);
Now the trigger
DELIMITER $$
CREATE TRIGGER tg_table_insert
BEFORE INSERT ON tableName
FOR EACH ROW
BEGIN
INSERT INTO tableName_seq VALUES (NULL);
SET NEW.id = CONCAT('USER', LPAD(LAST_INSERT_ID(), 6, '0'));
END$$
DELIMITER ;
Then you just insert rows to table
INSERT INTO tableName (name)
VALUES ('Jhon'), ('Mark');
And you'll have
| ID | NAME |
------------------
| USER000001 | Jhon |
| USER000002 | Mark |

How to make MySQL table primary key auto increment with some prefix

I have table like this
table
id Varchar(45) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name CHAR(30) NOT NULL,
I want to increment my id field like 'LHPL001','LHPL002','LHPL003'... etc.
What should I have to do for that? Please let me know any possible way.
If you really need this you can achieve your goal with help of separate table for sequencing (if you don't mind) and a trigger.
Tables
CREATE TABLE table1_seq
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
CREATE TABLE table1
(
id VARCHAR(7) NOT NULL PRIMARY KEY DEFAULT '0', name VARCHAR(30)
);
Now the trigger
DELIMITER $$
CREATE TRIGGER tg_table1_insert
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table1_seq VALUES (NULL);
SET NEW.id = CONCAT('LHPL', LPAD(LAST_INSERT_ID(), 3, '0'));
END$$
DELIMITER ;
Then you just insert rows to table1
INSERT INTO Table1 (name)
VALUES ('Jhon'), ('Mark');
And you'll have
| ID | NAME |
------------------
| LHPL001 | Jhon |
| LHPL002 | Mark |
Here is SQLFiddle demo
Create a table with a normal numeric auto_increment ID, but either define it with ZEROFILL, or use LPAD to add zeroes when selecting. Then CONCAT the values to get your intended behavior. Example #1:
create table so (
id int(3) unsigned zerofill not null auto_increment primary key,
name varchar(30) not null
);
insert into so set name = 'John';
insert into so set name = 'Mark';
select concat('LHPL', id) as id, name from so;
+---------+------+
| id | name |
+---------+------+
| LHPL001 | John |
| LHPL002 | Mark |
+---------+------+
Example #2:
create table so (
id int unsigned not null auto_increment primary key,
name varchar(30) not null
);
insert into so set name = 'John';
insert into so set name = 'Mark';
select concat('LHPL', LPAD(id, 3, 0)) as id, name from so;
+---------+------+
| id | name |
+---------+------+
| LHPL001 | John |
| LHPL002 | Mark |
+---------+------+
I know it is late but I just want to share on what I have done for this. I'm not allowed to add another table or trigger so I need to generate it in a single query upon insert. For your case, can you try this query.
CREATE TABLE YOURTABLE(
IDNUMBER VARCHAR(7) NOT NULL PRIMARY KEY,
ENAME VARCHAR(30) not null
);
Perform a select and use this select query and save to the parameter #IDNUMBER
(SELECT IFNULL
(CONCAT('LHPL',LPAD(
(SUBSTRING_INDEX
(MAX(`IDNUMBER`), 'LHPL',-1) + 1), 5, '0')), 'LHPL001')
AS 'IDNUMBER' FROM YOURTABLE ORDER BY `IDNUMBER` ASC)
And then Insert query will be :
INSERT INTO YOURTABLE(IDNUMBER, ENAME) VALUES
(#IDNUMBER, 'EMPLOYEE NAME');
The result will be the same as the other answer but the difference is, you will not need to create another table or trigger. I hope that I can help someone that have a same case as mine.
Here is PostgreSQL example without trigger if someone need it on PostgreSQL:
CREATE SEQUENCE messages_seq;
CREATE TABLE IF NOT EXISTS messages (
id CHAR(20) NOT NULL DEFAULT ('message_' || nextval('messages_seq')),
name CHAR(30) NOT NULL,
);
ALTER SEQUENCE messages_seq OWNED BY messages.id;

How to generate a dynamic sequence table in MySQL?

I'm trying to generate a sequence table in MySQL, so that I can get unique ids from last_insert_id.
The problem is that I need multiple sequences dynamically.
At the first, I created a table:
CREATE TABLE `sequence` (
`label` char(30) CHARACTER SET latin1 NOT NULL,
`id` mediumint(9) NOT NULL DEFAULT '0',
PRIMARY KEY (`label`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
And then tried to get the number, using example from http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
UPDATE sequence SET id = LAST_INSERT_ID(id + 1) WHERE label = 'test';
SELECT LAST_INSERT_ID();
After a while I realized that I also need to generate rows for new labels safely.
So I changed this schema into:
CREATE TABLE `sequence` (
`label` char(30) CHARACTER SET latin1 NOT NULL,
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
And I simply gave up using WHERE clause to update its id.
INSERT INTO sequence (label) values ( ? )
SELECT LAST_INSERT_ID()
Is this a proper way? I want to know if there is a better solution.
The MyISAM engine will do it for you -
Table definition:
CREATE TABLE `sequence` (
`label` char(30) CHARACTER SET latin1 NOT NULL,
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Populate table:
INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);
INSERT INTO sequence VALUES ('a', NULL);
INSERT INTO sequence VALUES ('b', NULL); -- add another labels 'b'
INSERT INTO sequence VALUES ('b', NULL);
INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);
Show result:
SELECT * FROM sequence;
+-------+----+
| label | id |
+-------+----+
| a | 1 |
| a | 2 |
| a | 3 |
| a | 4 |
| a | 5 |
| a | 6 |
| b | 1 |
| b | 2 |
+-------+----+

Insert data from a table into another table when a trigger fires

I have 4 tables :
pacienti
cabinete
tmp
vizite.
DROP SCHEMA IF EXISTS tr;
CREATE SCHEMA IF NOT EXISTS tr;
USE tr;
CREATE TABLE IF NOT EXISTS pacienti
(
id_pac INT NOT NULL AUTO_INCREMENT PRIMARY KEY
Nume VARCHAR(40) DEFAULT 'anonim',
Prenume VARCHAR(40) DEFAULT 'anonim',
UNIQUE (Nume,Prenume)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS cabinete
(
id_cab INT NOT NULL AUTO_INCREMENT PRIMARY KEY
Denumire VARCHAR(40) DEFAULT 'nespecificat',
UNIQUE (Denumire)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS tmp
(
id_tmp INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
DataVizita VARCHAR(10),
OraIntrare TIME,
ComplDate DATETIME,
NumePacient VARCHAR(40) DEFAULT 'anonim',
PrenumePacient VARCHAR(40) DEFAULT 'anonim',
NumeMedic VARCHAR(40) DEFAULT 'nespecificat',
PrenumeMedic VARCHAR(40) DEFAULT 'nespecificat',
Cabinet VARCHAR(30) DEFAULT 'nespecificat'
);
CREATE TABLE IF NOT EXISTS vizite
(
id_viz INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
Data_viz DATETIME,
Medic_id INT(5) NOT NULL,
Pacient_id INT(5) NOT NULL,
Cabinet_id INT(5) NOT NULL,
FOREIGN KEY (`Medic_id`) REFERENCES `tr`.`medici`(`id_med`)
ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`Pacient_id`) REFERENCES `tr`.`pacienti`(`id_pac`)
ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`Cabinet_id`) REFERENCES `tr`.`cabinete`(`id_cab`)
ON UPDATE RESTRICT ON DELETE CASCADE
) ENGINE=INNODB;
I have a file from which I have to import the data into the tmp table.
The file (*.txt) have the next fields:
DataVizita | OraIntrare | NumePacient | PrenumePacient | NumeMedic | PrenumeMedic | Cabinet
--------------------------------------------------------------------------------------------
22/04/2005 | 01:52:34 | Huruiala |Carmen Alexandra| Garbeata | Panait | 1
05/12/2005 | 04:16:02 | Schornig | Petre | Rusu | Dragos Mihai | 2
07/11/2004 | 11:24:27 | Graur |Alexandra-Cris. | Soarece | Dan-Cristian | 4
07/05/2008 | 06:35:44 | Rudnitchi | Marian | Galescu |George Valentin| 5
.
.
.
etc…
AFTER / BEFORE I made the import in the tmp table, I have to do some operations :
Create a trigger which must concat the DataVizita and OraIntrare into a DATETIME field (I succed by creating a function which convert DataVizita to the right format and concatenate it with OraIntrare and place the result in ComplDate from tmp table.
Here is what I already did and it works:
CREATE FUNCTION formdate(ziua VARCHAR(10), timpul TIME)RETURNS DATETIME
DERMINISTIC
RETURN
CONCAT (DATE_FORMAT(STR_TO_DATE(ziua, '%d/%m/%Y' ) , '%Y-%m-%d' ),' ',timpul);
DELIMITER $$
CREATE TRIGGER upd_date BEFORE INSERT ON tmp
FOR EACH ROW
BEGIN
SET new.ComplDate = formdate(new.DataVizita,new.OraIntrare);
END$$
DELIMITER ;
The second thing I have to do is to fill the pacienti and cabinete tables with data from this file.
And this one I need it because I can’t figure it out how to do it :(
Sorry for this long presentation, but I was thinking that someone will need all the details in order to understand my question.
I have realized that actually is very simple :). The final trigger should look like this :
DROP TRIGGER IF EXISTS upd_date;
DELIMITER $$
CREATE TRIGGER upd_date BEFORE INSERT ON tmp
FOR EACH ROW
BEGIN
SET new.ComplDate=formdate(new.DataVizita,new.OraIntrare);
INSERT INTO pacienti(nume,prenume) VALUES(new.NumePacient,new.PrenumePacient)
ON DUPLICATE KEY UPDATE nume=new.NumePacient, prenume=new.PrenumePacient;
INSERT INTO cabinete(Denumire) VALUES(new.Cabinet)
ON DUPLICATE KEY UPDATE Denumire=new.Cabinet;
END$$
DELIMITER ;
Hope that helped somebody !