此结构中的表
usersfollowspostslikescomments
表结构(SQL)
MySQL 方言的 DDL。在编辑器中打开可获得可编辑的可视化 ER 图,或直接将 SQL 复制到你的项目中。
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(254) NOT NULL UNIQUE,
bio VARCHAR(280),
created_at DATETIME NOT NULL
);
CREATE TABLE follows (
follower_id INT NOT NULL,
followee_id INT NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (follower_id, followee_id),
FOREIGN KEY (follower_id) REFERENCES users(id),
FOREIGN KEY (followee_id) REFERENCES users(id)
);
CREATE TABLE posts (
id INT PRIMARY KEY AUTO_INCREMENT,
author_id INT NOT NULL,
body TEXT NOT NULL,
created_at DATETIME NOT NULL,
FOREIGN KEY (author_id) REFERENCES users(id)
);
CREATE TABLE likes (
user_id INT NOT NULL,
post_id INT NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (user_id, post_id),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (post_id) REFERENCES posts(id)
);
CREATE TABLE comments (
id INT PRIMARY KEY AUTO_INCREMENT,
post_id INT NOT NULL,
author_id INT NOT NULL,
body VARCHAR(1000) NOT NULL,
created_at DATETIME NOT NULL,
FOREIGN KEY (post_id) REFERENCES posts(id),
FOREIGN KEY (author_id) REFERENCES users(id)
);