0
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Chạy MySQL 5.5.8 Tôi muốn chèn các bản ghi vào nhiều bảng thông qua Trigger. Đây là kích hoạt của tôi cho đến nay:
Tạo Trình kích hoạt userlevel_insert sau khi chèn vào db.tablename cho mỗi hàng chèn bỏ qua các giá trị db.differenttable [col1, col2] [new.val1,2];
Những công việc này. Không có vấn đề. Tuy nhiên tôi muốn chèn để nhấn nhiều bảng. Bảng thực sự là cùng một cấu trúc, nhưng trong các cơ sở dữ liệu khác nhau. Sau đây ném lỗi:
Tạo Trình kích hoạt userlevel_insert sau khi chèn vào db.tablename cho mỗi hàng chèn bỏ qua các giá trị db.differenttable [col1, col2] [new.val1,2]; Chèn bỏ qua các giá trị db.secondtable [col1, col2] [new.val1,2];
Lỗi là: Mã lỗi: 1054 Cột không xác định 'new.val1' trong 'Danh sách trường'
Vậy làm thế nào tôi có thể sử dụng trình kích hoạt để chèn cùng một dữ liệu vào nhiều bảng?
Đã hỏi ngày 4 tháng 3 năm 2015 lúc 17:31Mar 4, 2015 at 17:31
1
Tôi tìm thấy giải pháp này ở nơi khác: Đặt các giá trị mới là một biến, sau đó chèn vào bảng của bạn. Có thể có vấn đề về hiệu suất, nhưng chèn của tôi sẽ chỉ chạy một lần mỗi ngày và tổng số hồ sơ được thêm vào sẽ là tối thiểu. Tôi có thể thấy điều này hiệu quả hơn như một thủ tục được lưu trữ, nhưng điều này hoạt động ngay bây giờ.
CREATE TRIGGER doku_users_ins AFTER INSERT ON users_web.users
FOR EACH ROW
BEGIN
DECLARE new_pass VARCHAR[255]; -- choose the datatypes
DECLARE new_email VARCHAR[255]; --
DECLARE new_user INT; --
SET new_pass = NEW.pass ;
SET new_email = NEW.email ;
SET new_user = NEW.user ;
INSERT INTO users_web.doku_users [login, pass, email]
VALUES [new_user, md5[new_pass], new_email];
INSERT INTO users_web.doku_usergroup [uid, gid]
SELECT du.uid, '2' FROM doku_users du
WHERE new_user = du.login;
END |
Tín dụng cho YperCube trên DBA.Stackexchange cho giải pháp.
Đã trả lời ngày 5 tháng 3 năm 2015 lúc 14:40Mar 5, 2015 at 14:40
trftrftrf
1432 Huy hiệu bạc14 Huy hiệu Đồng2 silver badges14 bronze badges
Tôi có một bảng
id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
7 chứa các trường sau:id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
Tôi cần điền vào bảng
id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
7 bằng cách sử dụng trình kích hoạt nàyCREATE TRIGGER some_name AFTER INSERT ON `x` FOR EACH ROW INSERT INTO m [name] VALUES [NEW.name];
mà tôi nhận được từ câu hỏi DBA này
Nhưng vấn đề là mỗi hàng trên
id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
7 nên được điền bằng CREATE TRIGGER some_name AFTER INSERT ON `x` FOR EACH ROW INSERT INTO m [name] VALUES [NEW.name];
0 trên nhiều bảng khác nhau như CREATE TRIGGER some_name AFTER INSERT ON `x` FOR EACH ROW INSERT INTO m [name] VALUES [NEW.name];
1. Vì vậy, làm thế nào điều này có thể được thực hiện.Tôi hy vọng rằng câu hỏi của tôi đủ rõ ràng vì nó là trong tâm trí tôi.
CẬP NHẬT 1: Bảng
7 có hàng sau: table id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
7 has the following row:id - name - something_else
1 - null - not important
Trên
CREATE TRIGGER some_name AFTER INSERT ON `x` FOR EACH ROW INSERT INTO m [name] VALUES [NEW.name];
0 cho Bảng CREATE TRIGGER some_name AFTER INSERT ON `x` FOR EACH ROW INSERT INTO m [name] VALUES [NEW.name];
4, Bảng id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
7 Hàng sẽ trở nên giống như:id - name - something_else
1 - newthing - not important
Sau đó, vào
CREATE TRIGGER some_name AFTER INSERT ON `x` FOR EACH ROW INSERT INTO m [name] VALUES [NEW.name];
0 cho Bảng CREATE TRIGGER some_name AFTER INSERT ON `x` FOR EACH ROW INSERT INTO m [name] VALUES [NEW.name];
7, Bảng id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
7 Hàng sẽ trở nên giống như:id - name - something_else
1 - newthing - new value
Do đó, hàng trên bảng
id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
7 đã được điền bằng nhiều chèn trên các bảng khác nhau và được cập nhật tương ứng. Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu cách tạo trình kích hoạt MySQL
0 để chèn dữ liệu vào bảng sau khi chèn dữ liệu vào một bảng khác.: in this tutorial, you will learn how to create a MySQL id - name - something_else
1 - null - not important
id - name - something_else
1 - null - not important
0 trigger to insert data into a table after inserting data into another table.Giới thiệu về Trình kích hoạt MySQL id - name - something_else
1 - null - not important
0
id - name - something_else
1 - null - not important
Các kích hoạt MySQL
id - name - something_else
1 - null - not important
0 được tự động được gọi sau khi một sự kiện chèn xảy ra trên bảng.Sau đây cho thấy cú pháp cơ bản của việc tạo trình kích hoạt MySQL
id - name - something_else
1 - null - not important
0:Code language: SQL [Structured Query Language] [sql]
CREATE TRIGGER trigger_name AFTER INSERT ON table_name FOR EACH ROW trigger_body
Trong cú pháp này:
Đầu tiên, chỉ định tên của bộ kích hoạt mà bạn muốn tạo sau từ khóa
id - name - something_else
1 - null - not important
4.Thứ hai, sử dụng mệnh đề
id - name - something_else
1 - null - not important
0 để chỉ định thời gian gọi kích hoạt.Thứ ba, chỉ định tên của bảng mà bạn muốn tạo trình kích hoạt sau từ khóa
id - name - something_else
1 - null - not important
6.Cuối cùng, chỉ định phần thân kích hoạt bao gồm một hoặc nhiều câu lệnh thực thi khi kích hoạt được gọi.
Trong trường hợp cơ thể kích hoạt có nhiều câu lệnh, bạn cần sử dụng khối
id - name - something_else
1 - null - not important
7 và thay đổi dấu phân cách mặc định:Code language: SQL [Structured Query Language] [sql]
DELIMITER $$ CREATE TRIGGER trigger_name AFTER INSERT ON table_name FOR EACH ROW BEGIN -- statements END$$ DELIMITER ;
Trong trình kích hoạt
id - name - something_else
1 - null - not important
0, bạn có thể truy cập các giá trị id - name - something_else
1 - null - not important
9 nhưng bạn không thể thay đổi chúng. Ngoài ra, bạn không thể truy cập các giá trị id - name - something_else
1 - newthing - not important
0 vì không có id - name - something_else
1 - newthing - not important
0 trên id - name - something_else
1 - newthing - not important
2 kích hoạt.Xem xét ví dụ kích hoạt
id - name - something_else
1 - null - not important
0 sau đây.Thiết lập bảng mẫu
Đầu tiên, hãy tạo một bảng mới có tên
id - name - something_else
1 - newthing - not important
4:Code language: SQL [Structured Query Language] [sql]
DROP TABLE IF EXISTS members; CREATE TABLE members [ id INT AUTO_INCREMENT, name VARCHAR[100] NOT NULL, email VARCHAR[255], birthDate DATE, PRIMARY KEY [id] ];
Thứ hai, tạo một bảng khác được gọi là
id - name - something_else
1 - newthing - not important
5 lưu trữ các tin nhắn nhắc nhở cho các thành viên.Code language: SQL [Structured Query Language] [sql]
DROP TABLE IF EXISTS reminders; CREATE TABLE reminders [ id INT AUTO_INCREMENT, memberId INT, message VARCHAR[255] NOT NULL, PRIMARY KEY [id , memberId] ];
Tạo ví dụ kích hoạt id - name - something_else
1 - null - not important
0
id - name - something_else
1 - null - not important
Câu lệnh sau đây tạo ra một kích hoạt
id - name - something_else
1 - null - not important
0 chèn một lời nhắc vào bảng id - name - something_else
1 - newthing - not important
5 nếu ngày sinh của thành viên là id - name - something_else
1 - newthing - not important
9.id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
0Trong kích hoạt này:
Đầu tiên, tên của kích hoạt là
id - name - something_else
1 - newthing - new value
0 được chỉ định trong mệnh đề id - name - something_else
1 - null - not important
4:id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
1Thứ hai, sự kiện kích hoạt là:
id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
2Thứ ba, bảng mà bộ kích hoạt được liên kết là bảng
id - name - something_else
1 - newthing - not important
4:id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
3Cuối cùng, bên trong thân kích hoạt, chèn một hàng mới vào bảng nhắc nhở nếu ngày sinh của thành viên là
id - name - something_else
1 - newthing - not important
9.Kiểm tra trình kích hoạt MySQL id - name - something_else
1 - null - not important
0
id - name - something_else
1 - null - not important
Đầu tiên, chèn hai hàng vào bảng
id - name - something_else
1 - newthing - not important
4:id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
4Thứ hai, dữ liệu truy vấn từ bảng
id - name - something_else
1 - newthing - not important
4:id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
5third, dữ liệu truy vấn từ bảng id - name - something_else
1 - newthing - not important
5:Third, query data from
id - name - something_else
1 - newthing - not important
5 table:id int[11] not null auto_increment, //PK
name varchar[100] null,
something_else varchar[100] default 'not important'
6we đã chèn hai hàng vào bảng id - name - something_else
1 - newthing - not important
4. Tuy nhiên, chỉ có hàng đầu tiên có giá trị ngày sinh id - name - something_else
1 - newthing - not important
9, do đó, bộ kích hoạt chỉ chèn một hàng vào bảng id - name - something_else
1 - newthing - not important
5.We inserted two rows into the
id - name - something_else
1 - newthing - not important
4 table.
However, only the first row that has a birth date value id - name - something_else
1 - newthing - not important
9, therefore, the trigger inserted only one row into the id - name - something_else
1 - newthing - not important
5 table.Trong hướng dẫn này, bạn đã học được cách tạo trình kích hoạt MySQL
id - name - something_else
1 - null - not important
0 cho & nbsp; chèn dữ liệu vào bảng sau khi chèn dữ liệu vào một bảng khác.
Hướng dẫn này có hữu ích không?