(一)需求的分析和整理 1.用户可以发表文章,约束用户是登录状态 2.用户注册 3.用户登录 4.用户可以对任意一篇文章进行评论,约束用户是登录状态 5.用户可以点赞任意一篇文章,约束用户是登录状态 6.可以有条件的查看文章列表
(二)通过简单的E—R图,梳理实体和实体之间的关系,并找出复杂关系 (三)根据需求细化字段(约束和类型) (四)设计建表语句以及关于动作的sql语句
-- 用户名 create table users ( id int primary key auto_increment comment"选择自增的数列作为主键", username varchar (200) not null unique comment"用户名", nickname varchar (200) not null comment"昵称", password varchar (200) not null comment "密码" ); --文章 create table articals ( id int primary key auto_increment comment"选择自增的数列作为主键", auothor_id int not null comment"作者id", title varchar(200) not null comment "文章标题", content text not null comment "正文", publish_time datatime not null comment "发表时间" ); --评论 create table comments ( id int primary key auto_increment comment"选择自增的数列作为主键", user_id int not null comment "评论者id", artical_id int not null comment "评论文章的id", content varchar(200) not null comment "评论内容", publish_time datatime not null comment "发表时间" ) --点赞关系 create table user_like_article_relation ( id int primary key auto_increment comment"选择自增的数列作为主键", user_id int not null comment "点赞者id", artical_id int not null comment "点赞文章的id", ) -- 动作-某个用户发表文章 insert into articals (auothor_id,title,content,publish_time)values(?,?,?,?); -- 动作-用户注册 insert into users (username,nickname,password)values(?,?,?); -- 动作-用户登录 select username,password from users where username = ? and password = ?; -- 动作-用户点赞 insert into user_like_article_relation(user_id,artical_id) values (?,?); -- 动作-查看所有文章的标题列表,最新发表的文章在最前面 select title from articals order by publish_time desc;注:建表的三大范式 1.表中的字段,必须是不能再切分的。 2.表中的字段必须和主键是全部关联的,不能是部分关联的。 3.表中的字段必须和主键是直接关联而非间接。