[英]Microblog table relationship with foreign key, trouble with followers?
I'm having a small problem thinking a small microblog system i'm doing for exercise. 考虑到我正在做的小型微博客系统,我遇到了一个小问题。 I have three tables: 我有三个表:
users
id
username
tweets
id
tweet
followers
id_user
id_following
How do i make the relationship for followers? 我如何与追随者建立关系? id_user and id_following are both PKs that relate to the same table? id_user和id_following都是与同一张表相关的PK吗?
Here's my query: 这是我的查询:
CREATE TABLE tweets (
tweet_id INT NOT NULL AUTO_INCREMENT,
tweet VARCHAR(140) NOT NULL,
PRIMARY KEY (tweet_id)
) ENGINE=INNODB;
CREATE TABLE users (
user_id INT NOT NULL AUTO_INCREMENT,
user VARCHAR(255) NOT NULL,
password VARCHAR(40) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (user_id)
) ENGINE=INNODB;
CREATE TABLE user_tweets (
id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
id_tweet INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY (id_tweet)
REFERENCES tweets(tweeth_id)
ON UPDATE NO ACTION ON DELETE NO ACTION,
FOREIGN KEY (id_user)
REFERENCES users(user_id)) ENGINE=INNODB;
CREATE TABLE followers (
id_user INT NOT NULL REFERENCES users (user_id),
id_following INT NOT NULL REFERENCES users (user_id),
PRIMARY KEY (id_user, id_following)
) ENGINE=INNODB;
Something along these lines? 遵循这些原则?
create table followers (
id_user integer not null references users (id),
id_following integer not null references users (id),
primary key (id_user, id_following)
);
If MySQL supported CHECK constraints, you'd also CHECK (id_user <> id_following)
. 如果MySQL支持CHECK约束,则还应CHECK (id_user <> id_following)
。 But it doesn't. 但事实并非如此。 You'd have to use a trigger instead, but it's simpler (and probably not a problem for a Twitter clone) to just let people follow themselves if they want to. 您必须使用触发器来代替,但是如果人们愿意的话,让人们跟随自己会更简单(Twitter克隆可能不是问题)。 I think most of them will quickly tire of following themselves. 我认为大多数人会很快厌倦自己。
The CHECK clause is parsed but ignored by all storage engines. CHECK子句已解析,但被所有存储引擎忽略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.