Monday, 3 September 2018

How to set varchar primary key field in Mysql

How to set varchar primary key field in Mysql

Table structure of Users table

CREATE TABLE `users` (
   `id` varchar(36) NOT NULL DEFAULT 'InitiallyEmpty',
   `first_name` varchar(100) NOT NULL,
   `last_name` varchar(100) NULL,
   `email` varchar(100) NOT NULL,
   `password` varchar(100) NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Want to fill id an automatically filled. You need to create trigger


Trigger structure for automatically update

DROP trigger if exists before_insert_users;
delimiter $$
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
    SET new.id = uuid();
END $$

delimiter ;

Now create insert query

insert into users (first_name,last_name,email,password) values ('Sudhir','Pandey','psudhir20@gmail.com','123465');

0 comments:

Post a Comment