这是一个糟糕的设计,而且很可能是不可能的。
为什么不使用VIEW
?
CREATE VIEW table1_ownerfield AS SELECT ownerfield_1 FROM table1;
CREATE VIEW table2_ownerfield AS SELECT ownerfield_2 FROM table2;
然后将它们用作表格:
SELECT * FROM table1_ownerfield;
SELECT * FROM table2_ownerfield;
对于您问题的第二部分,请查看 using triggers。
编辑:
听起来您想要的视图类似于:
CREATE VIEW my_posts AS
SELECT * FROM posts WHERE post_owner = CURRENT_USER()
WITH CHECK OPTION;
CREATE VIEW my_categories AS
SELECT * FROM categories WHERE category_owner = CURRENT_USER()
WITH CHECK OPTION;
你可能想做这样的事情:
创建表:
CREATE TABLE user_connections (
user_connection_id INT AUTO_INCREMENT PRIMARY KEY,
mysql_connection_id INT,
user_id INT
);
然后当用户登录时,使用函数 mysql 连接 ID 填充此表CONNECTION_ID()
。
然后您可以创建如下视图:
CREATE VIEW my_posts AS
SELECT * FROM posts p JOIN user_connections uc
WHERE uc.mysql_connection_id = CONNECTION_ID()
AND p.post_user_id = uc.user_id
WITH CHECK OPTION;
CREATE VIEW my_categories AS
SELECT * FROM categories c JOIN user_connections uc
WHERE uc.mysql_connection_id = CONNECTION_ID()
AND c.category_user_id = uc.user_id
WITH CHECK OPTION;