MySQL填充链接表

时间:2018-11-28 15:00:13

标签: mysql

我是MySQL的新手,作为练习的一部分,我必须创建一个用于航班预订系统的数据库,我试图创建一个客户,这意味着将一行插入3个链接表中。我正在尝试作为事务来执行此操作,但是我的语法似乎很笨拙。有一个更好的方法吗? 这是我的交易(确实有效),但我认为必须有一种FAR更优雅的方式来做到这一点:

START TRANSACTION;
SET @account_id = NULL;
INSERT INTO ezy_account (account_id, username, password)
VALUES (@account_id = @account_id, 'customer@mysite.com',AES_ENCRYPT('password1', 'encryptionkey'));

SET @payment_id = NULL;
INSERT INTO ezy_payment_details (payment_details_id, payment_type, account_id, card_number, name, valid_from, expiry)
VALUES (@payment_id = @payment_id, 1, (SELECT account_id FROM ezy_account WHERE username ='ngray@mysite.com'), '1234567890123456', 'chris cust', '2018-03-00', '2021-10-00');


INSERT INTO ezy_customer (customer_id, payment_details, title, first_name, last_name, email_address, address, address_continued, city, postcode, country, dialling_code, phone_number, account_id, easyjet_plus_number, preferred_airport1, preferred_airport2, preferred_airport3)
VALUES (NULL, (SELECT payment_details_id FROM ezy_payment_details WHERE card_number ='1234567890123456'), 1, 'chris', 'customer', 'customer@nmysite.com', '123 Road Street', NULL, 'london', 'SE1 4HG', 7, 7, '1898765432', (SELECT account_id FROM ezy_account WHERE username ='customer@mysite.com'), NULL, NULL, NULL, NULL);
COMMIT;

0 个答案:

没有答案