# MySQL 多對多關係 中間表 (intermediary table) 設計 Example ```sql= CREATE TABLE follows ( follower_id INTEGER NOT NULL, -- 追隨者(粉絲) followee_id INTEGER NOT NULL, -- 被追隨者 created_at TIMESTAMP DEFAULT NOW(), FOREIGN KEY(follower_id) REFERENCES users(id), FOREIGN KEY(followee_id) REFERENCES users(id), PRIMARY KEY(follower_id, followee_id) -- 避免 table 中有兩筆像 (1,2) user1 追隨 user2 的資料 ); ``` 如果重複輸入兩次 `INSERT INTO follows(follower_id, followee_id) VALUES (1,2)` 就會出現 `ERROR 1062 (23000): Duplicate entry '1-2' for key 'PRIMARY'` 錯誤 ###### tags: `MySQL`
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.