是否可以使用外键将数据添加到2个表中?

时间:2016-11-03 15:05:16

标签: mysql

假设我有一张桌子

MEMBER member_id(使用序列), member_lastname, member_firstname,

图书 book_num(使用序列), 书名, BOOK_AUTHOR,

出租 rent_num(pk fk), member_id(FK), rent_date,

DETAILRENTAL rent_num(FK), detail_fee, detail_returndate, detail_duedate,

如何在租赁和租赁方面添加这些值?

member_lastname:Simon
member_id: (base on the last_name inserted)
detail_fee :40
detail_returndate :12-03-2016
detail_duedate:12-01-2016

1 个答案:

答案 0 :(得分:0)

试试这个,如果rent_num是自动增量:

BEGIN;
INSERT INTO RENTAL (member_id, rent_date)
SELECT member_id, NOW() FROM MEMBER WHERE member_lastname = 'Simon';
INSERT INTO DETAILRENTAL (rent_num, detail_fee, detail_returndate, detail_duedate) 
VALUES(LAST_INSERT_ID(), 40, '12-03-2016', '12-01-2016');
COMMIT;