zoukankan      html  css  js  c++  java
  • 【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)
  • 相关阅读:
    HDU 2041--超级楼梯(递推求解)
    HDOJ2037 今年暑假不AC (经典的贪心问题)
    JavaWeb---总结(十)HttpServletRequest对象(一)
    JavaWeb---总结(十一)使用Cookie进行会话管理
    JavaWeb---总结(十二)Session
    JavaWeb---总结(十三)使用Session防止表单重复提交
    JavaWeb---总结(十四)JSP原理
    JavaWeb---总结(十五)JSP基础语法
    JavaWeb---总结(十六)JSP指令
    JavaWeb---总结(十七)JSP中的九个内置对象
  • 原文地址:https://www.cnblogs.com/jakeyChen/p/4863843.html
Copyright © 2011-2022 走看看