【3】数据库的表设计和初始化

对于数据库的表设计,直接使用廖雪峰的教程当中的数据表

廖雪峰博客地址

我稍作更改摘抄过来:

主要更改:

id我个人比较喜欢用自增的int

创建时间数据类型改为datetime

用户表增加字段status,表示此账号是否可用

 1 -- schema.sql
 2 
 3 drop database if exists blog;
 4 
 5 create database blog;
 6 
 7 use blog;
 8 
 9 grant all on blog.* to 'jakey'@'localhost' identified by '123';
10 
11 create table users (
12     `id` mediumint not null auto_increment,
13     `email` varchar(50) not null,
14     `password` varchar(50) not null,
15     `admin` bool not null,
16     `status` bool not null default 1,
17     `name` varchar(50) not null,
18     `image` varchar(500) not null,
19     `created_at` datetime not null,
20     unique key `idx_email` (`email`),
21     key `idx_created_at` (`created_at`),
22     primary key (`id`)
23 ) engine=innodb default charset=utf8;
24 
25 create table blogs (
26     `id` mediumint not null auto_increment,
27     `user_id` int not null,
28     `user_name` varchar(50) not null,
29     `user_image` varchar(500) not null,
30     `name` varchar(50) not null,
31     `summary` varchar(200) not null,
32     `content` mediumtext not null,
33     `created_at` datetime not null,
34     key `idx_created_at` (`created_at`),
35     primary key (`id`)
36 ) engine=innodb default charset=utf8;
37 
38 create table comments (
39     `id` mediumint not null auto_increment,
40     `blog_id` int not null,
41     `user_id` int not null,
42     `user_name` varchar(50) not null,
43     `user_image` varchar(500) not null,
44     `content` mediumtext not null,
45     `created_at` datetime not null,
46     key `idx_created_at` (`created_at`),
47     primary key (`id`)
48 ) engine=innodb default charset=utf8;
49 
50 insert into users 
51     (
52         `email`,
53         `password`,
54         `admin`,
55         `status`,
56         `name`,
57         `image`,
58         `created_at`
59     ) 
60     values 
61     (
62         'Jakey.Chen@example.com',
63         'MTIz',
64         '1',
65         '1',
66         'Jakey.Chen',
67         'None',
68         now()
69     );

 

主要有三个表:用户表,博文表,和评论表

在终端可进行初始化:

$ mysql -u root -p < schema.sql

确认是否初始化完成:

jakeychen@JakeyPC:~/Desktop$ mysql -u jakey -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.6.19-0ubuntu0.14.04.4 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use blog;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_blog |
+----------------+
| blogs          |
| comments       |
| users          |
+----------------+
3 rows in set (0.00 sec)

 

posted @ 2015-10-09 14:20  jakey.chen  阅读(534)  评论(0编辑  收藏  举报