(MySql) Insert or Update in the database

0

I need to perform a Insert in the database if there is no person's cpf. if it exists it has to do a Update . But all this through a single script in MySql . I have illustrated the example below as I am doing today:

1 If the cpf of the entity (not the primary code) does not exist, then I need to execute according to the SQL below:

insert into entidade (cpf, nome, email) values (11111111111, 'robson', 'email')

2 If the CPF exists, then I will just update the data, according to the SQL below:

update entidade set nome = 'robson', email = 'email' where cpf = 11111111111

Is it possible that this is a single SQL?

    
asked by anonymous 16.05.2018 / 03:39

1 answer

2
INSERT INTO table (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE    
name="A", age=19

Reference: link

    
16.05.2018 / 05:53