• 【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)
  • 相关阅读:
    OC-为何用copy修饰block
    OC-RunLoop运行循环
    苹果审核之遇到IPV6问题被拒的解决方法
    LeetCode学习_day1:原地算法
    OC-加载h5富文本的代码,并计算高度
    OC-bug: Undefined symbols for architecture i386: "_OBJC_CLASS_$_JPUSHRegisterEntity", referenced from:
    了解iOS各个版本新特性总结
    iOS 快速打包方法
    iOS tableView侧滑删除的第三方控件
    Object_C 集成环信时,中文环境下不显示中文
  • 原文地址:https://www.cnblogs.com/jakeyChen/p/4863843.html
Copyright © 2020-2023  润新知