我正在思考一个小小的微博系统,我正在做运动。我有三张桌子:
users
id
username
tweets
id
tweet
followers
id_user
id_following
如何为粉丝建立关系? id_user和id_following都是与同一个表相关的PK吗?
这是我的疑问:
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;
答案 0 :(得分:0)
这些方面的东西?
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)
);
如果MySQL支持CHECK约束,您还需要CHECK (id_user <> id_following)
。但事实并非如此。你必须使用一个触发器,但它更简单(并且可能不是一个Twitter克隆的问题)让人们如果他们想要跟随自己。我想他们中的大多数人很快就会厌倦自己。
The CHECK clause is parsed but ignored by all storage engines.