![](/img/trans.png)
[英]copy the foreign key constraints of one table to another in the same db in mysql
[英]MySQL foreign key constraints : on updating one table, fill another table with some data from the first table
我有兩個表,用戶表和登錄表。 說:
Create table users(
id int not null,
name varchar not null,
email varchar not null,
password varchar not null,
entries int default 0,
primary key(id));
Create table login(
id int not null,
email varchar not null,
password varchar not null,
user_id int not null,
Primary key(id)),
Foreign key (user_id) references users(id));
我可以添加哪些約束或如何解決問題,以便每當我在用戶表中添加用戶時, email和密碼列也會添加到登錄表中(就像說我創建了用戶的最小化版本僅包含用戶登錄憑據的表) 。 每當我從用戶表中刪除一個用戶時,他應該從登錄表中刪除。
我希望我已經足夠描述了
您可以使用 cascade delete 定義約束。 要實現依賴表的更新,您需要觸發器。 可以定義 AFTER INSERT 觸發器,您可以在其中更新任何依賴表。
同時,當您總是可以通過加入主表來獲得它時,我會重新考慮在依賴表中使用冗余字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.