Hi everyone, I am pleased to be a member of this wonderful forum~
I have a question about using the “INSERT” query to insert multiple values into a table, consider the following sql:
INSERT INTO tablename(fielda,fieldb)
VALUES
(‘value1a’, ‘value1b’),
(‘value2a’, ‘value2b’),
(‘value3a’, ‘value3b’)
Suppose that the “fielda” field is the primary key of the table, and there exists a row in the table that takes the value “value3a” in the “fielda” field.
I want to execute the given SQL in a PHP script, clearly this will give me an SQL error since (‘value3a’, ‘value3b’) violates the uniqueness constraint in the table. Now I have the following questions:
Will (‘value1a’, ‘value1b’) and (‘value2a’, ‘value2b’) be inserted into the table?
If these values are inserted into the table, what shall I do in order to undo the partial effect of this SQL? (ie. to delete (‘value1a’, ‘value1b’) and (‘value2a’, ‘value2b’))
What the OP means is, that in his test the first two lines ARE inserted in the table, so after the query ended with a ‘duplicate key’ error, he still has two new lines in his table. As you can see in the vid he published (nice vid by the way ).
While Rudy claims that in his test this doesn’t happen.
When you inserted data to the table, did you miss the yellow line across the top of your page saying duplicate key value3a for Primary key?
however, it seems that MySQL doesn’t support this property.
not sure what you mean by that. what property?
don’t know what you mean by partial effect either. But this is how to delete records from your table. (Too tired to recall if the brackets are necessary in the delete statement).
delete from table
where ( fielda = ‘value1a’
or
fielda= ‘value2a’
)
i’ve checked the manual and it says that unless you use IGNORE, any duplicates cause the statement to be aborted
it sure looks like this is different from what i thought it was supposed to do, which is to roll back to the beginning of the statement
so, to get back to your original question, i guess to “undo the partial effect” you will have to delete the rows which got inserted up to the point of the duplicate
how you are supposed to isolate which key values those are, i have no idea…
i don’t believe there is a partial effect – the query works or it doesn’t
here, i’ll test it for ya
first, let’s set it up: [indent]Suppose that the “fielda” field is the primary key of the table, and there exists a row in the table that takes the value “value3a” in the “fielda” field.[/indent]