How to understand the trigger between two tables in java
This article mainly explains "how to understand the trigger between two tables in java". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the trigger between two tables in java".
Create a trigger to increase the number of books borrowed / reduce the number of books returned.
When the books is updated by the lending attribute islend, the borrowing quantity amount is updated in the lender table at the same time, in which the updated islend attribute saves that id is the primary key in the id,lender of the borrower lender, and islend is the foreign key in the books table, connecting the two tables.
The structure of the two database tables is given below, and the triggers written are as follows:
CREATE TRIGGER `lendBook`After UPDATE ON `books`for EACH ROW BEGINUPDATE lenderSET amount = ifnull (lender.amount, 0) + 1WHERE id = new.isLend; INSERT INTO test (v1) VALUES (new.isLend); END
Test statement
Try {ps = conn.prepareStatement (sql); ps.setString (1, "1"); ps.setString (2, String.valueOf (bookId)); ps.setString (3, String.valueOf (id)); row = ps.executeUpdate ();} catch (SQLException e) {e.printStackTrace ();}
Can normally realize the function of borrowing / returning books and the function of trigger.
Thank you for your reading, the above is the content of "how to understand how to create a trigger between two tables in java". After the study of this article, I believe you have a deeper understanding of how to understand how to create a trigger between two tables in java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!