다음은 샘플입니다.여기
우리가 간다 :
-- 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
);
blog_entry
- 제품이
be_state
이동 메인 테이블을 - 여기를 정의하고, blog_entry.state_id
- 자신의
state_id
값을 삽입 be_tag
- 여기처럼 여러 태그가 있습니다
blog_entry_tags
- 하나의 블로그 항목에 여러 개의 태그가있을 수 있으므로 여기에 저장하고 blog_entry.blog_entry_id
및 해당하는 be_tag.tag_id
을 함께 삽입하십시오. 블로그 항목 당 하나의 태그가 있습니다. 태그 # 1 (예 :)에 php
태그를 두 번 이상 태그 할 수 없음을 의미합니다.
be_attribute
- 유사한 blog_entry_tags
에 하나 또는 블로그 항목에 하나 이상의 be_attribute
을 할당 할 수 있습니다 - 여기 등의 위치, 저자,
blog_entry_attributes
같은 매장 속성 정의.
이 방법은 다시 한 가지 방법 일뿐입니다.
항상 다른 테이블에 있습니다. 단일 필드에 이들을 포함 시키면 관계형 데이터베이스를 갖는 목적이 무효화됩니다. 즉, 관련성을 잃을 수 있습니다. –
"states"는 자체 테이블일까요? – TIMEX
상태는 게시물 테이블의 열이어야합니다. 키워드는 확실히 별도의 테이블에 있어야하며 게시물 ID와 관련이 있으며 키워드 및 게시물 ID 열 모두에 기본 키가 있어야합니다. 속성 목록이 늘어나는 이유를 모르겠다. 어떤 종류의 구조를 유지하려고한다면, 속성 목록은 같아야합니다. 따라서 속성을 포스트 테이블의 컬럼으로 사용하는 것이 좋습니다. –