掌握MySQL的建表规范、DDL语句和权限操作

目标

  1. 整理 建表规范
  2. 整理 DDL语句的
  3. 整理 三句话

首先看一个建表例子,再去研究应该遵循哪些规范

create table rzdata(
id int(11) not null auto_increment,

name varchar(200),
age int(3),

createuser varchar(200) ,
createtime timestamp not null default current_timestamp,
updateuser varchar(200) ,
updatetime timestamp not null default current_timestamp on update current_timestamp,

primary key (id)
);
  1. 表名

    不能是中文,不能是汉语拼音 ,不然很low

  2. 风格统一

    统一所有表的风格,可以从已有的表中查看,或者找leader检查,方便后期维护

  3. 第一个字段

    第一个字段必须是id,并且自增长,是主键,没有意义 –>拓展: 为什么?

  4. 主键

    一张表只有一个主键,primary key == unique+not null

  5. 后四个字段

    后四个字段包括:用户、创建时间、修改用户、修改时间

    createuser varchar(200) ,
    createtime timestamp not null default current_timestamp,
    updateuser varchar(200) ,
    updatetime timestamp not null default current_timestamp on update current_timestamp,
  6. 业务字段

    业务字段需要唯一存在,使用unique约束,如订单号

    业务字段都必须加上注释

    COMMENT '用户名称'
  7. 字符集CHARSET

    查看字符集

    >> show variables like '%char%';
    +--------------------------+---------------------------------------------------------------+
    | Variable_name | Value |
    +--------------------------+---------------------------------------------------------------+
    | character_set_client | utf8 |
    | character_set_connection | utf8 |
    | character_set_database | latin1 |
    | character_set_filesystem | binary |
    | character_set_results | utf8 |
    | character_set_server | latin1 |
    | character_set_system | utf8 |
    | character_sets_dir | /usr/local/mysql-5.6.23-linux-glibc2.5-x86_64/share/charsets/ |
    +--------------------------+---------------------------------------------------------------+
    8 rows in set (0.00 sec)

DDL语句以及需要注意的点

查询语句:select 查询字段 from 表 ;

注意:

  1. 生产环境下不要用 * 代替所有字段

    错误示范:

    mysql> select * from tb_user;
    +----+----------+----------------------------------+-------------+---------------------+----------------------------------+
    | id | username | password | phone | created | salt |
    +----+----------+----------------------------------+-------------+---------------------+----------------------------------+
    | 28 | zhangsan | e21d44f200365b57fab2641cd31226d4 | 13600527634 | 2018-05-25 17:52:03 | 05b0f203987e49d2b72b20b95e0e57d9 |
    | 30 | leyou | 4de9a93b3f95d468874a3c1bf3b25a48 | 15855410440 | 2018-09-30 11:37:30 | 4565613d4b0e434cb496d4eb87feb45f |
    +----+----------+----------------------------------+-------------+---------------------+----------------------------------+
    2 rows in set (0.01 sec)

    正确示范:

    mysql> select username,password from tb_user;
    +----------+----------------------------------+
    | username | password |
    +----------+----------------------------------+
    | zhangsan | e21d44f200365b57fab2641cd31226d4 |
    | leyou | 4de9a93b3f95d468874a3c1bf3b25a48 |
    +----------+----------------------------------+
    2 rows in set (0.00 sec)
  2. 查询语句如果数据量特别大必须使用where 或者 limit,否则需要使用大量的资源

    错误示范:

    select name,image,letter from tb_brand;

    正确示范:

    select name,image,letter from tb_brand limit 100;
    select name,image,letter from tb_brand where id=100;

新增语句:insert into 表名(字段1,字段2…) values(数据1,数据2…);

注意:在表名后加上对应要添加的字段名

insert into tb_brand (name,letter) values(tunan,T);

修改语句:update 表名 set 修改后的字段 where 条件;

注意:一定要加上条件,否则是全局修改

update tb_brand set name="xiaoqi" where id = 1;

删除语句:delete from 表名 where 条件;

注意:一定要加上条件,否则是全局删除;

delete from tb_brand tb where id = 1;

当某条SQL验证拖累进程时怎么办?

使用 show processlist;查看mysql中的 sql 进程

mysql> show processlist;
+-----+------+---------------------+--------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+------+---------------------+--------+---------+------+----------+------------------+
| 272 | root | localhost | leyou1 | Query | 0 | starting | show processlist |
| 273 | root | 121.62.184.34:56629 | leyou1 | Sleep | 448 | | NULL |
| 274 | root | 121.62.184.34:56631 | leyou1 | Sleep | 592 | | NULL |
+-----+------+---------------------+--------+---------+------+----------+------------------+
3 rows in set (0.00 sec)

然后根据 id 删除即可

kill id;

必须要记住的三条命令

修改密码:

update user set password=password('密码') where user='用户名';

修改权限:

grant all privileges on *.* to 用户名@'%' identified by '密码';

刷新权限:

flush privileges;
Author: Tunan
Link: http://yerias.github.io/2018/10/02/mysql/2/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.