By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE TableA(
id int auto_increment unique,
user_id INTEGER,
content_id VARCHAR(10),
points INTEGER,
content_type INTEGER,
description VARCHAR(100),
created_date DATETIME,
PRIMARY KEY(user_id, content_id, content_type)
);
ALTER TABLE TableA
ADD COLUMN enumeration INT UNSIGNED UNIQUE;
CREATE TRIGGER tr_bi_TableA
BEFORE INSERT
ON TableA
FOR EACH ROW
SET NEW.enumeration = COALESCE(1 + (SELECT MAX(enumeration) FROM TableA), 1);
INSERT INTO TableA (id, user_id, content_id, points, content_type, description, created_date)
VALUES
(1 , 1 , '1a1' , 5 , 1 , null , '2021-02-26 08:26:54'),
(3 , 1 , '1a1' , 5 , 2 , null , '2021-02-26 08:26:55'),
(5 , 1 , '1a2' , 5 , 1 , null , '2021-02-26 08:26:56'),
(6 , 1 , '1a3' , 5 , 2 , null , '2021-02-26 08:26:57');
SELECT * FROM TableA;
id | user_id | content_id | points | content_type | description | created_date | enumeration |
---|---|---|---|---|---|---|---|
1 | 1 | 1a1 | 5 | 1 | null | 2021-02-26 08:26:54 | 1 |
3 | 1 | 1a1 | 5 | 2 | null | 2021-02-26 08:26:55 | 2 |
5 | 1 | 1a2 | 5 | 1 | null | 2021-02-26 08:26:56 | 3 |
6 | 1 | 1a3 | 5 | 2 | null | 2021-02-26 08:26:57 | 4 |