mysql个人使用学习记录

建表示例:
在这里插入图片描述

图片来源https://blog.csdn.net/weixin_37909391/article/details/81317723

1. NOT NULL DEFALUT 或者 DEFALUT
2. 类型括号内的长度是字符长度非字节长度
3. 自增:AUTO_INCREMENT
4. 注释
(1)字段注释:COMMENT ’ 注释’
(2)表名注释:COMMENT = '注释’

5. 数据库基本操作:
增:insert into 表名(字段1, 字段2,字段3,…) values(值1,值2,值3,…)
改:update 表名 set 字段1=值,字段2=值…
删:delete from 表名 [where] [order by] [limit]
查:select * 或者[字段名] from 表名
distinct去重:select distinct 字段名 from 表名
limit 指定结果行数: select 字段名 from 表名 limit [n]
order by 结果排序: select * from 表名 order by 字段名 desc [desc降序| asc升序]
ps: order by默认是升序

数据库索引不能太多:索引过多会增加开销,而插入,删除,更新也会增加开销,
所以,频繁进行数据库操作的表不要设置太多索引。

mysql连接操作

**1. 内连接
表 user_info, goods 通过user_Id分组
select user_info.,goods . from user_info inner join goods on user_info.id = goods.id group by user_info.id;
:
表 user_info, goods 通过having过滤条件
: select user_info.name,goods.name from user_info inner join goods on user_info.id=goods.id group by user_info.id having goods.price>60.0;

  1. 左外连接
    返回左边所有的行,包括右边没有匹配的行
    表 user_info, goods 通过user_Id分组
    select user_info.,goods . from user_info left join goods on user_info.id = goods.id group by user_info.id;
  2. 右外连接
    返回右边所有的行,包括左边没有匹配的行
    表 user_info, goods 通过user_Id分组
    select user_info.,goods . from user_info right join goods on user_info.id = goods.id group by user_info.id;

常用的聚合函数

count() 计数
sum() 求和
avg() 平均数
max() 最大值
min() 最小值

**distinct去重

select distinct expressiom…from table;
注意:distinct要在所有字段最前面

分库分表

mysql常用的数据架构:一主多从,读写分离
在这里插入图片描述
我个人的理解意思就是:一个主库有多个从库、主库负责写,从库负责读。这样就大大提高了读取速度。
[ps]:图片来源别处

分表命令

1.把一张50万数据的表拆成五个从表,每个从表10万数据。

例:表名:zhubiao 主键:zid

方法1:
create table congbiao1 select * from zhubiao order by zid limit 0,100000;
create table congbiao2 select * from zhubiao order by zid limit 100000,200000;
create table congbiao3 select * from zhubiao order by zid limit 200000,300000;
create table congbiao4 select * from zhubiao order by zid limit 300000,400000;
create table congbiao5 select * from zhubiao order by zid limit 400000,500000;

不足:primary key 属性丢失

方法2:
create table congbiao1 like zhubiao;
insert into congbiao1 select * from zhubiao order by limit 0,100000;
create table congbiao2 like zhubiao ;
insert into congbiao2 select * from zhubiao order by limit 100000,200000;
create table congbiao3 like zhubiao ;
insert into congbiao3 select * from zhubiao order by limit 200000,300000;
create table congbiao4 like zhubiao ;
insert into congbiao4 select * from zhubiao order by limit 300000,400000;
create table congbiao5 like zhubiao ;
insert into congbiao5 select * from zhubiao order by limit 400000,500000;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值