0

我正在为 Yii Web 应用程序设计数据库,但我不确定我做的是否正确。

CREATE TABLE user
(
    id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(128) NOT NULL,
    password VARCHAR(128) NOT NULL,
    email VARCHAR(128) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE post
(
    id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(128) NOT NULL,
    description TEXT NOT NULL,
    media TEXT NOT NULL,
    tag_id INTEGER NOT NULL,
    status INTEGER NOT NULL,
    create_time INTEGER,
    update_time INTEGER,
    user_id INTEGER NOT NULL,
    CONSTRAINT FK_post_user FOREIGN KEY (user_id)
        REFERENCES user (id) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE comment
(
    id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    content TEXT NOT NULL,
    status INTEGER NOT NULL,
    create_time INTEGER,
    user_id INTEGER NOT NULL,
    post_id INTEGER NOT NULL,
    CONSTRAINT FK_comment_post FOREIGN KEY (post_id)
        REFERENCES post (id) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE tag
(
    id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(128) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

问题:

1.) 在 post 表中我有 tag_id。我正在考虑将所有现有和新添加的标签存储在标签表中,然后当有人使用已经存在的标签或制作新标签以添加为 tag_id 以使用“,”发布表时。这是最好的解决方案吗?

2.) 在评论表中我有 user_id 和 post_id。我认为外键如何工作应该在评论表中设置2个外键?我试过了,但出错了。

3.)在帖子表中有媒体行。我将存储在此行位置以上传图像或嵌入来自 youtube/或任何其他来源的代码。可以只使用同一行还是应该分开使用?1 用于图像,1 用于嵌入代码?

提前致谢

4

1 回答 1

0

1) In case I'd use column with name tags instead tag_id where i'd keep serialized array of tag_id's from table tags

2) The problem i think because you tried add foreign keys with same name. Try to use

CONSTRAINT FK_comment_post FOREIGN KEY
CONSTRAINT FK_comment_post_2 FOREIGN KEY

3) What will be if you'll decide to add another media info in post(pdf_file, or music). Add new column in table post it's not correct. i'd recommend you create additional table postmeta where you can create any type of content, and instead media use post_text. Like in wordpress.

于 2013-03-06T06:56:31.877 回答