English 中文(简体)
我将如何创造我的SQL Schema?
原标题:How would I create this MySQL Schema?

附录一有一个博客员额实体。

  • It has many attributes
  • It has comments attached to it.
  • It has many states (deleted/locked/invisible, etc).
  • It has many "tags". (keywords, school_id, user_id)

显然,评论应该成为自己的表,与博客表有着多对一的关系。

But what about "states" or "tags"? Would you put that in another table? Or would you stick that in many columns?

www.un.org/Depts/DGACM/index_spanish.htm 特性是什么? 由于我的网站不断发展,博客员额的属性将增加(标题、作者、blah、blah.......)。 如果属性清单高达100,会发生什么情况?

最佳回答

首先,国家应当是一个结构严密的工作,因此,应该为它们设立单独的一栏。 想一开始你需要什么,但你可以轻易地在稍后再增加一栏或两栏。

Tags like keywords shouldn t be stored in columns, because the amount is growing rapidly over time. That wouldn t make any sense. So for that, build a table with id and keyword in it and a link table with post_id and keyword_id. You could also omit the keyword_id and directly link post_id and keyword. Make sure that both columns combined define the primary key, so you can not end up with a keyword stored several time to one particular post.

For attributes it can be the same. It is not a bad practice to create an attribute table with attribute_id, attribute_name and maybe more information and a link table attribute_id and post_id and content. You can also easily enhance it to be multilingual by using attribute_ids.

意见相同,在单独的表格中储存,与用户和员额链接:评论_id、用户_id、海报_id、内容和可能父母_id,如果你希望再次发表评论的话,这可以成为评论。

That s it for a brief overview.

问题回答

样本:

同样。 它只是一个样本。 你可以采用其他办法。

Here we go:

-- basic-basic blog
CREATE TABLE blog_entry (
    blog_entry_id INT NOT NULL AUTO_INCREMENT,
    blog_entry_title VARCHAR(255) NOT NULL,
    blog_entry_text VARCHAR(4000) NOT NULL,
    create_date DATETIME,
    state_id INT
);

-- create a look-up table for your blog entry s state
CREATE TABLE be_state (
     state_id INT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (state_id)
);

-- create a look-up table for your blog entry s tag/s
CREATE TABLE be_tag (
     tag_id INT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (tag_id)
);

-- a table to store multiple tags to one entry
CREATE TABLE blog_entry_tags (
    blog_entry_id INT NOT NULL,
    tag_id INT NOT NULL,
    PRIMARY KEY (blog_entry_id, tag_id)
);

-- a table to store definitions of attributes
CREATE TABLE be_attribute (
    attribute_id INT NOT NULL AUTO_INCREMENT,
    name CHAR(30)
);

-- now have a table to which you can assign multiple attributes to one blog
-- of course, this is if I understand you correctly
-- where you want to have additional attributes
-- aside from the basic properties of a blog entry 
-- and will allow you, if you choose to do it
-- to not necessarily have all attributes for each entry
CREATE TABLE blog_entry_attributes (
    blog_entry_id INT NOT NULL,
    attribute_id INT NOT NULL,
    PRIMARY KEY (blog_entry_id, attribute_id) 
    -- PK enforces one blog entry may have only one attribute of its type
    -- meaning, no multiple attributes of  location  attribute,
    -- for example, for one blog. Unless of course you wrote half the entry
    -- in one location and finished it in the next.. then you should
    -- NOT enforce this primary key
);
  1. www.un.org/chinese/sc/presidency.asp

  2. <代码>be_state - definition them here and内插入state_id Value in blog_enter.state_id

  3. <代码>be_tag/code> - 具有像我们在这里做的那样的多个标的

  4. <条码>blog_enter_tags-因为您可能拥有多个标子,用于一个博客条目,在此储存,并插入<条码>blog_enter_id和相应的<条码>be_tag_id。 一起。 每一博客条目中的一端。 意思是,你可以两次或两次以上对标的编号为php

  5. <代码>be_attribute - 仓储属地定义,如地点、作者等

  6. blog_enter_attributes - 类似于<代码>blog_enter_tags 您可向博客条目分配一个或多个<代码>be_attribute。

Again, this is just one approach.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签