Time for action - creating database tables for a blog

  1. Create the database by executing the following query:
    CREATE DATABASE myblog
    
  2. Create the posts table:
    CREATE TABLE `myblog`.`posts` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `title` VARCHAR( 255 ) NOT NULL ,
    `content` TEXT NOT NULL ,
    `author_id` INT UNSIGNED NOT NULL ,
    `publish_date` DATETIME NOT NULL
    ) ENGINE = MYISAM;
    
  3. Create the authors table:
    CREATE TABLE `myblog`.`authors` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `name` VARCHAR( 50 ) NOT NULL
    ) ENGINE = MYISAM;
    
  4. Create the categories table:
    CREATE TABLE `myblog`.`categories` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `name` VARCHAR( 50 ) NOT NULL
    ) ENGINE = MYISAM;
    
  5. Create the posts_categories table:
    CREATE TABLE `myblog`.`posts_categories` (
    `post_id` INT UNSIGNED NOT NULL ,
    `category_id` INT UNSIGNED NOT NULL ,
    PRIMARY KEY ( `post_id` , `category_id` )
    ) ENGINE = MYISAM;
    

What just happened?

We created a database to hold the tables needed for our blog application. We then created the following tables:

  • posts: Table to store the actual post's content.
  • authors: Table to store the author's names. Each post belongs to one author and an author can have many posts.
  • categories: Table to store the category names. Each post can belong to multiple categories and each category can have multiple posts.
  • posts_categories: Table to store the relationship between posts and categories.
What just happened?
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset