建表示例:
图片来源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;
- 左外连接
返回左边所有的行,包括右边没有匹配的行
表 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; - 右外连接
返回右边所有的行,包括左边没有匹配的行
表 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;