mysql 数据仓库主键_数据仓库_MySQL(2)

整理 建表规范

整理 DDL语句的

整理 三句话

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

create table rzdata(

idint(11) not nullauto_increment,

name varchar(200),

ageint(3),

createuser varchar(200) ,

createtime timestamp notnull defaultcurrent_timestamp,

updateuser varchar(200) ,

updatetime timestamp notnull defaultcurrent_timestamp on update current_timestamp,

primary key (id)

);

a. 表名

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

b. 风格统一

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

c. 第一个字段

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

d. 主键

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

e. 后四个字段

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

createuser varchar(200) ,

createtime timestamp notnull defaultcurrent_timestamp,

updateuser varchar(200) ,

updatetime timestamp notnull default current_timestamp on update current_timestamp,

f. 业务字段

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

业务字段都必须加上注释

COMMENT '用户名称'

g. 字符集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 * fromtb_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 fromtb_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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值