Coding Cheatsheets - Learn web development code and tutorials for Software developers which will helps you in project. Get help on JavaScript, PHP, XML, and more.

Post Page Advertisement [Top]

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');

No comments:

Post a Comment

Bottom Ad [Post Page]