如何使用发布/草稿表设计sql数据库?

时间:2016-11-15 22:48:59

标签: sql database postgresql database-schema publish

我想知道如何制作博客数据库方案。作者撰写文章并将其发布在博客上。使用像

这样的表格非常简单
  

作者,文章,博客

但文章也可以有草稿。读者无法看到草稿,文章发表时,博客的读者可以看到。已发布的文章可以取消发布,也可以成为草稿。

如何连接

  

草稿

  

公布

带有文章和博客表的表格?有必要吗?或者只是在Article表中添加一些列?喜欢IsPublished还是其他什么?

2 个答案:

答案 0 :(得分:4)

有几种方法可以解决这个问题。一种是在您的内容上标记status,这对简单网站很有用。另一种方法是使用连接表将内容连接到应该显示的位置,方式和时间。

对于简单网站,您只需在内容表中添加status标记。

create type statuses as enum ('published', 'draft');

create table posts (
    id serial,
    author integer references people(id),
    content text not null,
    ...whatever other data...
    status statuses default 'draft'
);

我使用PostgreSQL enumerated type来减少存储空间(不是那么重要),因此错别字会被捕获(重要),所以有一个地方可以看到所有可能的状态而不是它们被添加willy nilly(也很重要)。

然后您可以选择已发布的所有帖子。

select *
from posts
where author = ? and
      status = 'published'

这很简单,但显示和内容一起使用。如果您忘记查看status标记,则会显示草稿帖。

status标志的变体是“发布日期”。在此之前,它将不会显示。在这之后它会。

create table posts (
    id serial,
    author integer references people(id),
    content text not null,
    ...whatever other data...
    publish_at datetime default '9999-12-31'
);

然后,您可以通过查看publish_at是否小于当前日期时间来检查是否应该显示它。

select *
from posts
where author = ? and
      publish_at < current_timestamp

默认情况下,默认情况下,默认情况下,所有帖子都默认为“9999-12-31”。这将已发布/草稿与自动发布帖子的功能相结合,而无需运行任何额外的代码。

更强大的解决方案是为要发布的内容和位置提供连接表。从相同的posts表开始,但没有status列。

create table posts (
    id serial,
    author integer references people(id),
    content text not null,
    ...whatever other data...
);

比一个人的博客还要一个。

create table blogs (
    id serial,
    curator integer references people(id)
);

然后创建一个连接帖子,将帖子与博客帖子连接起来。

create table blog_posts (
    blog integer references blogs(id),
    post integer references posts(id),
    posted datetime not null default current_timestamp
);

现在当某些内容被“发布”时,它会被插入到blog_posts中。没有状态标志。如果你想看到用户的博客帖子......

select *
from blog_posts
join blogs on blogs.id = blog_posts.blog
where blogs.curator = ?
order by posted desc;

此处的优点是,通过向blog_posts表添加更多连接表或更多字段,可以在多个位置显示一个帖子。并且没有status字段要记住包含在每个语句中。它可以在连接表中,也可以不是。

blog_posts还可以包含publish_at字段。

答案 1 :(得分:0)

使用它可视化:

enter image description here

在“作者”表上:

  

基本上所有关于作者的信息

在“文章”表格中:

  

每篇文章都会有一位作者(通常......嗯,但它可能有多个,但这样做)。所以外键authors_id。

对于已发布和草稿:

  

“已发布”和“草稿”位于状态字段中。在这种情况下是枚举('已发布','草稿')。你的isPublished?我没有任何针对具有布尔值的数据库设计(即是/否,真/假),但是如果你将它作为枚举则会更清楚。可能在以后您将添加另一个状态,并且您的isPublished不再相关。一个案例可能是:您稍后需要一个状态:“预定发布”或说“预定取消发布”或任何其他状态。

关于“博客”表:

  

如果您只有一个博客,则不一定需要。但如果你有几个博客,那么你需要那个表。

上面的sql:

CREATE TABLE IF NOT EXISTS `authors` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(200) NULL,
  `status` ENUM('active', 'inactive') NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB

CREATE TABLE IF NOT EXISTS `articles` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(60) NULL,
  `content` TEXT NULL,
  `status` ENUM('published', 'draft') NULL,
  `datetime` INT NULL,
  `authors_id` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_articles_authors_idx` (`authors_id` ASC),
  CONSTRAINT `fk_articles_authors`
    FOREIGN KEY (`authors_id`)
    REFERENCES `authors` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
 ENGINE = InnoDB