数据库应用程序使用时需要在语句结束的时候加上分号
数据库的表类似于java类,表中的字段类似表中的成员变量
每一行数据类似java对象,所有数据可以理解为list包裹对象
1.显示所有数据库
show databases;
2.创建数据库
create database if not exists 数据库名 character set utf8mb4;
–表示做注释,上面语句的意思是没有的话就创建数据库,有就不做任何操作
3.使用数据库(操作表,数据,要先进入数据库,再操作)
use 数据库名;
4.删除数据库
drop database if exists 数据库名;
一般先把删除写在前面,再创建
drop database if exists 数据库名;
create database if not exists 数据库名 character set utf8mb4;
use 数据库名;
常用数据类型
1.数值类型
整型:
bit(m) m指定位数,默认为1
对应java类型是boolean,默认1位,只能存0和1
int 对应java类型是integer
浮点型:
decimal(m,d)
m是总长度,包含整数位和小数位
d是小数位,不会发生精度丢失
对应java里面bigdecimal
比如101.4
Decimal(4,1)
2.字符串类型
varchar(size) 可变长度字符串,不怎么长的 对应java中的String
text 存放长文本数据,0-65535字节 对应java中的String
blob 二进制形式的长文本数据 对应java中的byte[ ]
3.日期类型
java.util.date d=new java.util.date() ;//1.无参2.long类型参数
java.sql.timestamp t=new java.sql.timestamp(99);//long类型参数
表的操作
查看表结构
先进入数据库,再
desc 表名
新增create
创建表
create table 表名(
id int,
name varchar(20) comment ‘姓名’ ,
sex varchar(1)
);
可以使用comment增加字段说明
-- 创建一张学生表
DROP TABLE IF EXISTS student;
CREATE TABLE student (
id INT,
sn INT comment '学号',
name VARCHAR(20) comment '姓名',
qq_mail VARCHAR(20) comment 'QQ邮箱'
);
插入单行数据+全列插入
数量必须和定义表的列的数量及顺序一致
insert into 表名 values ( 所有的列 );
例如:
--插入两条记录,value_list 数量必须和定义表的列的数量及顺序一致
INSERT INTO student VALUES (100, 10000, '唐三藏', NULL);
INSERT INTO student VALUES (101, 10001, '孙悟空', '11111');
多行数据+指定列
insert into 表名 ( 指定的内容 ) values
( 指定列的内容 ),
( 指定列的内容 );
-- 插入两条记录,value_list 数量必须和指定列数量及顺序一致
INSERT INTO student (id, sn, name) VALUES
(102, 20001, '曹孟德'),
(103, 20002, '孙仲谋');
查询retrive
全列查询
通常情况下不建议使用进行全列查询
– 1. 查询的列越多,意味着需要传输的数据量越大;
– 2. 可能会影响到索引的使用
select * from 表名;
指定列查询
select 指定内容 from 表名;
查询字段为表达式,支持计算,修改等
– 表达式不包含字段
select id, name, 10 from exam_result;
– 表达式包含一个字段
select id, name, english + 10 from exam_result;
– 表达式包含多个字段
select id, name, chinese + math + english from exam_result;
别名
为查询结果中的列指定别名,表示返回的结果集中,以别名作为该列的名称
select id, name, chinese + math + english 总分 from exam_result;
去重
使用distinct关键字对某列数据进行去重
如
select distinct math from exam_result;
排序
select * from 表名 order by 需要排序的内容;
此为升序
select * from 表名 order by 需要排序的内容 desc;
此为降序
select * from 表名 order by 内容1 desc, 内容2;
内容1降序,内容1 相同的话内容2升序
条件查询
- where条件可以使用表达式,但不能使用别名。
- and的优先级高于or,在同时使用时,需要使用小括号()包裹优先执行的部分
– 查询英语不及格的同学及英语成绩 ( < 60 )
select name, english from exam_result where english < 60;
– 查询语文成绩好于英语成绩的同学
select name, chinese, english from exam_result where chinese > english;
– 查询总分在 200 分以下的同学
select name, chinese + math + english 总分 from exam_result
where chinese + math + english < 200;
–查询语文成绩在 [80, 90] 分的同学及语文成绩
select name, chinese from exam_result where chinese between 80 and 90;
– 查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
select name, math from exam_result where math in (58, 59, 98, 99);
– 使用 or 也可以实现
select name, math from exam_result where math = 58 or math = 59 or math= 98 or math = 99;
is null 进行判断内容就是null
=进行判断是否为空
like模糊匹配
% 匹配任意多个(包括 0 个)字符
like ‘孙%’-- 匹配到孙悟空、孙权
_ 匹配严格的一个任意字符
like ‘孙_’-- 匹配到孙权
日期类型
年月日,时分秒
MySQL默认时间格式
YYYY- MM–DD HH(24小时制):mm:ss
2020-08-04 12:45:09
分页查询
– 从 0 开始,筛选 n 条结果
select … from table_name[where …… ][order by……] limit n;
– 从 s 开始,筛选 n 条结果
select … from table_name [where …… ][order by……] limit s,n;
select id, name, math, english, chinese from exam_result order by id limit 3;
修改 update
update exam_result set math = 80 where name = ‘孙悟空’;
– 将孙悟空同学的数学成绩变更为 80 分
update exam_result set math = math + 30 order by chinese + math + english limit 3;
– 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
修改语文成绩倒数第2-5名,并且名称包含“悟”或者“孟”的同学,语文成绩修改为+30
先进行查找
select * from exam_result where name like’%悟%’ or name like ‘%孟%’ order by chinese limit 1,4;
update 不支持从第几行开始,后面补充
删除 delete
delete from exam_result where name = ‘孙悟空’;