下载地址mysql
常用数据类型
- double(5,2):表示最多5位,其中必须有2位小数
 - char(10):固定长度的字符串类型;char(10) ‘abc ‘
 - varchar(10):可变长度字符串类型;varchar(10) ‘abc’
 - text:字符串类型
 - blob:二进制类型
 - date:日期类型,格式位yyyy-MM-dd
 - time:时间类型,格式位hh:mm:ss
 - datetime:日期时间类型,yyyy-MM-dd hh:mm:ss
 
在mysql中,字符串类型和日期类型都要用单引号括起来。’MXADAQ’,’2020-1-11 ‘
数据表常用操作(DDL)
创建数据库:
create database db_users character set utf8 collate utf8_general_ci;
collate utf8_general_ci表示数据库字符校对规则
修改默认字符集 ,这样数据库建的表和字段都会使用设置的字符集’utf8’,collate utf8_general_ci为设置校对规则,可以省略不写-- 查看数据库编码show create database db_name;-- 查看数据库表编码show create table tb_name;-- 查看字段的编码show full columns from tb_name;-- 修改数据库字符集alter database db_users default character set utf8 collate utf8_general_ci;-- 修改表的字符集alter table tb_students character set gbk;-- 让字段可以填入中文,修改字段的字符集alter table tb_students change name name varchar(20) character set utf8 collate utf8_general_ci;
2.删除数据库:(谨慎使用)
drop database db_users;
使用数据库:
use db_users;
显示全部数据库
show databases;
查看数据库表
use db_users;show tables;
创建数据库表
use db_users;create table tb_users(id bigint auto_increment not null primary key,name varchar(30) not null,age int(16) not null,address varchar(30) not null,createtime datetime);
查看数据库的属性
describe tb_users;-- 简写为desc employees;

修改数据库表结构
#新增一列 alter table tb_users add scores int not null;#添加主键 alter table tb_users add primary key(id);#添加唯一索引 alter table tb_users add unique(name);#删除主键 alter table tb_users drop primary key cascade;#删除not null约束 alter table tb_users modify name null;#修改字段类型 alter table tb_users modify name varchar(20);#修改列名 alter table tb_users change name address varchar(30) not null;#删除一列 alter table tb_users drop address;#更改表名字 rename table tb_users to tb_students;#查看创建表的过程 show create table tb_users;>>>>| Table | Create Table |+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| tb_users | CREATE TABLE `tb_users` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`username` varchar(30) CHARACTER SET latin1 NOT NULL,`password` varchar(30) CHARACTER SET latin1 NOT NULL,`createtime` datetime NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 |+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
删除数据库表
drop table if exists teachers; #'if exists'避免删除不存在的表
数据常用操作(DML)
- 插入数据
 
- 列名与列值类型,个数,顺序要一一对应
 - 值不要超过定义的长度
 插入日期和字符要用引号括起来
#添加一条insert into teachers (id,address,name,score) values (3,'上海','张三',70);#添加多条insert into teachers (id,address,name,score) values (4,'广州','王五',79),(5,'广州','洪五',19);>>Query OK, 2 rows affected (0.01 sec)>>Records: 2 Duplicates: 0 Warnings: 0
如果再插入前数据库中就已经有这个记录,那就需要先删除原纪录,再插入新记录
情景示例:这张表存的每个客户最近一次交易订单信息,要求保证单个用户数据不重复录入,且执行效率最高,与数据库交互最少,支撑数据库的高可用
此时可以使用replace into 这样就不用先查询再决定是否先删除再插入,“replace into”语句是基于唯一索引或主键来判断唯一(是否存在)的。
replace into last_transaction (transId,username,amount,trans_time,remark)values (null, 'chenhaha', 30, '2020-06-11 20:00:20', '会员充值');replace into last_transaction (transId,username,amount,trans_time,remark)values (null, 'chenhaha', 100, '2020-06-11 21:00:00', '购买盲僧至高之拳皮肤');
如果希望插入一条新记录,但是记录已经存在就更新改记录,可以使用”insert into…on duplicate key update…”
- 情景示例:这张表存了用户历史充值金额,如果第一次充值就新增一条数据,如果该用户充值过就累加历史充值金额,需要保证单个用户数据不重复录入。
如果我们希望插入一条新记录(INSERT),但如果记录已经存在,就啥事也不干直接忽略,此时,可以使用insert ignore into…insert into total_transction(t_tansId,username,total_amount,last_transTime,last_remark)values (null,'chenhaha',30,'2020-06-11 20:00:01','充值会员') on duplicate key update total_amount=total_amount+30,last_transTime='2020-06-11 20:00:20',last_remark='充会员';
insert ignore into users_info (id, username, sex, age ,balance, create_time)values (null, 'chenhaha', '男', 12, 0, '2020-06-11 20:00:20');
 
更改数据
select * from teachers\G;
* 1. row * id: 1 address: 安徽省合肥市 name: 王五 score: 70 * 2. row * id: 2 address: 北京 name: 李四 score: 90 * 3. row * id: 3 address: 上海 name: 张三 score: 70 * 4. row * id: 4 address: 广州 name: 王五 score: 79 * 5. row * id: 5 address: 广州 name: 洪五 score: 19 5 rows in set (0.00 sec)
#未加条件,所以的分数变为90update teachers set score=90;>>>>+----+--------------+------+-------+| id | address | name | score |+----+--------------+------+-------+| 1 | 安徽省合肥市 | 王五 | 90 || 2 | 北京 | 李四 | 90 || 3 | 上海 | 张三 | 90 || 4 | 广州 | 王五 | 90 || 5 | 广州 | 洪五 | 90 |+----+--------------+------+-------+5 rows in set (0.00 sec)#改张三的为60update teachers set score=60 where name='张三';>>>>+----+--------------+------+-------+| id | address | name | score |+----+--------------+------+-------+| 1 | 安徽省合肥市 | 王五 | 90 || 2 | 北京 | 李四 | 90 || 3 | 上海 | 张三 | 60 || 4 | 广州 | 王五 | 90 || 5 | 广州 | 洪五 | 90 |+----+--------------+------+-------+5 rows in set (0.00 sec)#改变名字是张三且id为3的人的分数为70(多个条件用‘and’连接)update teachers set score=70 where id=3 and name='张三';>>>>+----+--------------+------+-------+| id | address | name | score |+----+--------------+------+-------+| 1 | 安徽省合肥市 | 王五 | 90 || 2 | 北京 | 李四 | 90 || 3 | 上海 | 张三 | 70 || 4 | 广州 | 张三 | 90 || 5 | 广州 | 张三 | 90 |+----+--------------+------+-------+5 rows in set (0.00 sec)#名字为李四的年龄加1alter table teachers add age int not null;update teachers set age=20;update teachers set age=age+1,score=90 where name='李四';>>>>+----+--------------+------+-------+-----+| id | address | name | score | age |+----+--------------+------+-------+-----+| 1 | 安徽省合肥市 | 王五 | 90 | 20 || 2 | 北京 | 李四 | 90 | 22 || 3 | 上海 | 张三 | 70 | 21 || 4 | 广州 | 张三 | 90 | 21 || 5 | 广州 | 张三 | 90 | 21 |+----+--------------+------+-------+-----+
删除数据库表记录
delete from teachers where name='王五';select * from teachers;+----+---------+------+-------+-----+| id | address | name | score | age |+----+---------+------+-------+-----+| 2 | 北京 | 李四 | 90 | 22 || 3 | 上海 | 张三 | 70 | 21 || 4 | 广州 | 李四 | 80 | 31 || 5 | 广州 | 张三 | 90 | 21 |+----+---------+------+-------+-----+delect from teachers;#直接删除表中数据,但是表结构还在,数据可以找回truncate table teachers;#等同于先drop table teachers;然后create table if not exists teachers();表的结构还在,但是数据无法找回了
修改数据库密码
选择到mysql数据库里面的user表,用户相关信息都在这里面
update mysql.user set authentication_string=password('12345678') where user='root' and Host='localhost';flush privileges;#刷新注册表exit;#退出重新登录mysql -uroot -q12345678
mysqladmin -u root -p password 123456Enter password: **********mysqladmin: [Warning] Using a password on the command line interface can be insecure.Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
数据库查询操作(DQL)
查询所有的列
select * from teachers;
查询指定的列
select address,name,age from teachers;
按条件查询
where语句常用的运算符
| 字段名 | 默认值 | 描述 | 
|---|---|---|
| = | 等于 | id=1 | 
| > | id>5 | |
| < | id<3 | |
| >= | id>=4 | |
| <= | id<=9 | |
| != | id!=10 | |
| is null | id is null | |
| is not null | id is not null | |
| between … and … | id between 1 and 10 | |
| in | id in (4,5,6) | |
| not in | name not in (a,b) | |
| like | %匹配一个或者多个,_至匹配一个字符 | name like (‘abc_’) | 
| not like | %匹配一个或者多个,_至匹配一个字符 | name not like (‘abc%’) | 
| regexp | 常规表达式 | name正则表达式 | 
select * from teachers where id=1 or name='李四';select * from teachers where id in (1,4,5);select * from teachers where name is not null;select * from teachers where gender != '男';select * from teachers where age between 18 and 20;select * from teachers where name like '__'; #name是2个字符的全部数据select * from teachers where name like '王%'; #name以王开头的数据select * from teachers where name like '%三%';#查出全部name包含这个‘三’的数据
- 在结果中去除重复行
 
distinct可以在结构中去除掉重复行
select distinct name from teachers;

- 查询结果进行运算,必须都是数据型
 
ifnull(score,0) 表示:如果score为null则设置为0
select *, age+ifnull(score,0) from teachers;

列名很难看,使用as修改:
select *, age+ifnull(score,0) as total from teachers;

as可以为任何字段取别名:
select name as tername from teachers;

- 字段排序操作
 
asc表示升序排序
select * from teachers order by age; #默认是升序排序

desc表示降序排序
select * from teachers order by age desc,id desc;

聚合函数
count 统计不为null的数据
select count(*) from teachers;select count(*) as total from teachers where ifnull(score,0)+age>100;

统计多个select count(*) as total,count(age) from teachers where ifnull(score,0)+age>100;

max
select max(age) from teachers;
min
select min(age) from teachers;
sum
select sum(age),sum(score) from teachers;

avg
select avg(age) from teachers;

分组查询
select * from teachers group by gender;

只会显示每组中的第一个数据,要都显示通常需要把需要显示的字段加到group by里面,例如:select name,gender from teachers group by gender,name;

select gender,group_concat(name),group_concat(age),group_concat(score) from teachers group by gender;
按照男女分组,显示包含的名字

分组最主要的作用是分组进行聚合计算select gender,group_concat(age),sum(age) from teachers group by gender;

查找男、女年龄大于20的人数:select gender,group_concat(age),count(age) from teachers where age>20 group by gender;

HAVING,只显示满足条件的结果
- 分组后进行筛选
 - having后面可以使用聚合函数
 
只显示成绩大于100的城市老师信息:
select address,group_concat(age),group_concat(score),sum(score) from teachersgroup by address having sum(score)>100;

- 限制查询显示的列数(分页操作)
 
角标从0开始
select * from teachers limit 0,3;

