How to store an article in MySQL?
I would like to store some articles (blog posts) in a mysql table, these posts will be made from more parts (e.g.: part1, part2 ... part x) I don`t know how to store them... Could I store each part in a t开发者_如何学运维ext file, or how could I store it in a mysql database ? What field can support data of this size ? And how should I design the table to store each part of the post ? It would be good to store each part in the same cell and just separate them with a word () and then cut it with php ?
Thanks!
A common design method is to create a "Parts" table, ex:
CREATE TABLE parts (page_id INTEGER, part_name VARCHAR(255), body TEXT);
which will work fine at lower traffic. (page_id in this case is the foreign key to the page which "owns" this part - you'd get all parts for a given page by saying, natch SELECT * FROM parts WHERE page_id = :some_page_id
)
As your traffic rises, the cost of pulling in and assembling the pages may become egregious, in which case the splitting apart the body contents from a larger text field (as you suggested) would not be a terrible idea. At this level, the speed gains from doing direct serialization of a hash into the database column and making the app server's CPU bear the brunt of the work (as opposed to the DB server) may be worth it.
The column types you'd be interested in are enumerated here under "Storage Requirements for String Types": http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
Summarized, TEXT (64KB) should be large enough to hold most basic data. MEDIUMTEXT (16 MB) or LONGTEXT (4096 MB) if your data is noticeably large or you foresee it growing. BLOB, MEDIUMBLOB or LONGBLOB (same sizes as the *TEXT types) if you intend to do any PHP variable deserialization from DB columns.
I'd suggest one table "Post" and second table "Post_part" with FK to "Post". In the "Post_part" table you could store the text in column of TEXT type.
精彩评论