문제

We all know that in programming languages the return values are a most basic thing. But when it comes to databases, we just don't expect any return value. This is a wrong concept, since it reduces our database to a black-box. It may be sometimes good to know e.g. which rows are affected by an update.

So how can we build databases that return a value after inserts or updates? (Not after selects, since this always returns a value per se)

And can you give me some sample code for e.g. MySQL, like a list with the primary keys of the updated rows?

도움이 되었습니까?

해결책

You might want to read this related SO topic (I've linked to the answer by @Erwin Brandstetter), quoted here:

In PostgreSQL you could use the RETURNING clause.
Oracle also has a RETURNING clause.
SQL-Server has an OUTPUT clause.
But MySQL doesn't have anything like that.

다른 팁

A good way would be to SELECT the values before the update inside a transaction: https://stackoverflow.com/a/10663439/2115135 Just remember that update still can fail so take it in consideration before using the returned values.

Depending on the method of execution used you usually can get the number of rows affected by any write operation performed on the my sql database. There are multiple ways, you can explicitly execute the statement as a batch with

SELECT ROW_COUNT()

at the end or you can use the features provided by the connection library to get the number of rows affected in the last set

For example : in PHP you can use

mysqli::$affected_rows

for getting the affected rows.

Edit : Is there any particular scenario, you would like more examples on?

Update : To answer the second part of the question.

Well, there is absolutely no way the you can get the primary keys of the affected rows in the same statement that I know. You will need to execute a statement batch as mentioned in the answer examples given by the other user.

START TRANSACTION; SELECT primary_key_columns FROM my_table WHERE status = 'O' FOR UPDATE; UPDATE my_table SET status = 'E' WHERE status = 'O'; COMMIT; More detail here : [https://stackoverflow.com/questions/10663338/i-need-primary-keys-of-the-affected-rows-to-be-returned-after-updating-a-table-i][1]


EDIT Modified the answer based on info given by Marcus in the comments below.

To retrieve the ID of every row affected by an update statement:

SET @uids := null;
UPDATE footable
   SET foo = 'bar'
 WHERE fooid > 5
   AND ( SELECT @uids := CONCAT_WS(',', fooid, @uids) );
SELECT @uids;

This will return a string with all the IDs concatenated by a colon. Source :How to get ID of the last updated row in MySQL?

The other option as mentioned by the answer given by @fenway is the output clauses for supported database.

Note that even in that case you essentially have to fill in the values before you send the data back following the same step mentioned above.

Now, I am sure, you are wondering why is it that the database do not return primary key values automatically. Well it has to do with the fact that

1) A DB does not insist on having a Primary key for each and every table. Its actually a UNIQUE constraint defined by us.

2) Not all scenarios do you need to have the affected primary keys returned . In most of the usual use-cases, after update you require at most the rows affected.

Remember, Relational DB programming requires a slightly different mindset from the usual problem set programming.

Atleast these are my two cents.

Regards

Shreyas N

One way to accomplish this is to use a select statement that has the same filter as the update statement. Make sure however, that both queries run inside the same transaction so that the data doesn't change between each call.

{

update Customers set CITY = 'CHICAGO'

where ContactFName = 'Dan'

//execute query

select CustomerID from Customers

where ContactName = 'Dan'

//execute reader
//while the reader returns a row, set that return value to an element in a list 

//commit transaction

}//Transaction
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top