Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a query with multiple insert values fails, what happens to prior values

Tags:

postgresql

I am using postgresql, and suppose I have the following query.

insert into table_name values (1, 1), (1, 3);

And lets say inserting values (1,3) fail. Do the values (1,1) get inserted?

From the tests I have done, the first values that do not cause any errors do not get inserted, but I haven't been able to find any sources to back that up. What I have tried to google has come up with different subjects.

In general when inserting multiple values, is it an all or nothing type of thing? Or is it possible for only some of the values to get inserted?

Thanks

like image 774
jlars62 Avatar asked Mar 22 '23 09:03

jlars62


1 Answers

Do the values (1,1) get inserted?

No. Each statement in Postgresql is a transaction in itself

From the manual:

PostgreSQL actually treats every SQL statement as being executed within a transaction. If you do not issue a BEGIN command, then each individual statement has an implicit BEGIN and (if successful) COMMIT wrapped around it.

like image 73
Clodoaldo Neto Avatar answered Apr 25 '23 07:04

Clodoaldo Neto