一、基本增删改查语法
1. 创建数据
1.1 创建数据库
create database 数据库名称 charset = 编码格式;
create database student_test charset = utf8 ;
1.2 创建表结构的语法格式
约束字段
- 主键(primary key):值不能重复,auto_increment代表值自动增长;
- 非空(not null):此字段不允许填写空值;
- 唯一(unique):此字段的值不允许重复;
- 默认值(default):当不填写此值时会使用默认值,如果填写时以填写为准
- 简单创建 : create table 表名 (列表字段(类型));
create table b(
name VARCHAR(10),
height DECIMAL(5,2)
);
- 创建带约束字段的语法格式: create table 表名 ( 字段名 数据类型 约束 ) ;
create table gg(
id int unsigned primary key auto_increment,
name varchar(10) unique,
age tinyint unsigned
);
1.3 数据类型
字符类型:
- int:数字类型,固定长度,最大存储10位以内的整数
- varchar:可根据实际数据调整存储空间,主要存储文本(大量文本数据可能会影响性能)
- mediumtext:存储较长的字符串
- date:存储日期
- time:存储时间
- datetime:存储日期时间,精确至时分秒
- timeMap:存储日期时间,可精确至微秒
- tinyint:可以存储 256 个不同的整数值(有符号时范围为包括 -128 和 127,无符号时范围为0-256)
1.4 插入数据
insert into 表名 value (数据,数据) //按照表结构顺序插入,根据字段类型增加符号
insert into c value (001,'大郎',21)
- 根据指定字段插入相应的值
insert into 表名 (字段1,....) values (值1,....)
- 一次性插入多条语句,中间逗号隔开
insert into 表名 (字段1,....) values (值1,....),(值1,....),(值1,....),(值1,....),......
2. 查询数据
2.1 简单查询
- 查询表的全部数据
select * from 表名
- 查询表的指定字段
select 字段1,字段2 from 表名
- 查询表的指定数据的指定字段
select 字段1,字段2 from 表名 where 条件
- 查询表中的数据并作去重处理
select distinct 字段1 from 表名
2.2 where条件查询
select中的where子句决定了返回什么样子的记录
- eg.查询stu表中id等于001的记录
select * from stu where id = 001;
where后面支持多种运算符,进行条件的处理
- 比较运算
- 逻辑运算
- 模糊查询
- 范围查询
- 空判断
2.3 比较运算符
- 等于 : =
- 大于 : >
- 小于 : <
- 小于等于 : <=
- 大于等于 : >=
- 不等于 : != 或 <>
//查询所有年龄不等于三十岁的学生
select * from student where age != 30;
2.4 逻辑运算符
- and且:条件1 and 条件2 ,两个条件必须同时满足
//查询所有年龄小于25岁且性别为女的同学
select * from students where age < 25 and sex = '女';
- or或: 条件1 or 条件2 ,两个条件有一个满足就行
//查询性别为女或者年龄为15的美女子
select * from students where sex = '女' or age = 15;
- not 非:not 条件,not只有一个条件,如果条件为满足,not后变为不满足,如果条件为不满足,not后变为满足
//查询地址非八嘎丫路的同学们
select * from students where not idress = '八嘎丫路'
2.5 模糊查询
- like
- %表示任意多个字符
- _表示任意一个字符
- 查找名字中以 '孙' 开头的学生记录
select * from students where name like '孙%'
- 查找名字中以 '孙' 开头且名只有一个字的学生记录
select * from students where name like '孙_'
- 查询任意姓氏,但名字包含 ‘ 乔’ 的学生记录
select * from students where name like '%乔'
2.6 范围查询
- in 表示在一个非连续的范围内
//查询地址是北京/上海/广东/深圳的学生记录
select * from students where idress in ('北京','上海','深圳','广东');
- between...and..表示在一个连续的范围内
//查询年龄在15-18的美女子们
select * from students where age between 15 and 18 ;
2.7 空判断
- null与 " " 是不同的
null表示什么都没有
" " 表示长度为0 的字符串
- 判断空: is null
- 判断非空: is not null
//查询身份证号非null的美女子
select * from students where cards is not null ;
2.8 排序
将行数按照字段1 进行排序,如果某些字段1的值相同,则按照字段2的值进行排序,依次类推
select * from 表名 order by 字段1 asc|desc,字段2 asc|desc,......
- 默认按照字段值从小到大排序
- asc(默认值)从小到大排序,即升序
- desc,从大到小排序,即降序
//查询按照年龄从大到小排序的同学数据
select * from gg order by age desc;
- limit 值1,值2 :分页展示,展示页数和每页数据量
/*查询XX表,按XX升序分页,展示第X页,一页展示XX数据*/
select * from 表名 order by 排序字段 asc limit 每页记录数*(第几页 - 1),每页展示行数
2.9 统计
select count(*) | count(列名) from 表名 where 条件、
- count(*) 统计
//统计成绩大于90分的学生总数量
seletct count(*) from student where math > 90;
//统计总分大于250的总人数有多少?
select count(*) from student where (分数+分数+分数) > 250;
- sum(列名) :求和
//求全部同学的年龄总和
select sum(age) from gg
- avg(列名) :求平均值
//求一个班级数学平均分
select avg(math) from student;
- max 和 min 的使用
//求班级的最高得分和最低得分 select max(math + english + chinese),min(math + english + chinese) from student;
3. update修改数据
3.1 update简单修改
- 修改表中指定字段的所有数据
update 表名 set 字段1 = '值1';
- 修改表中指定数据的指定字段的数据
update 表名 set 字段1 = '值1' where 条件;
4. 删除数据
4.1 delete简单修改
- 删除表中全部数据
delete form 表名; //truncate同理
- 删除表中指定数据
delete form 表名 where 条件;
delete 和 truncate 的区别
- 在速度上,truncate > delete
- 如果想删除部分数据用delete,注意带上 where 子句 ;
- 如果想保留表而将所有数据删除,自增长字段恢复从1开始,用truncate
二、基本函数
1. 1 日期函数
- current_time : 查询当前时间
- current_date : 查询当前日期
- current_timemap : 查询当前时间戳
- date(datetime) : 返回datetime的日期部分
- date_add(date2,interval d_value a_type) : 在date2中加上日期或时间
- date_sub(date2,interval d_value a_type) : 在date2上减去一个时间
- datediff(date1,date2) : 两个日期差(单位是天)
- timediff(time1,time2) : 两个时间差(时分秒)
- now() : 现在时间,此时此刻
//插入数据中,时间为当前
insert into 表名 values (字段名,....,now()); //now()为时间字段所在位置
//查询某个表的全部数据,但是时间只返回日期 (日期date,时间time)(前提是数据库表字段创建时包含函数查询范围)
select 字段名,....,date(时间字段)
from 表名;
/* 查询10分钟内的数据*/
select * from 表名 where DATE_ADD(时间字段,interval 10 minute) >= now()
/*查询两个日期之间相隔多少天*/
select datediff('2023-02-14','2024-01-06') from dual
1.2 加密函数和系统函数
1.2.1 加密函数
- MD5:数据加密(md5加密后的字符长度为32,注意加密的字段类型)
- password : 加密密码
/*将插入的数据转为加密格式*/
insert into 表名 values (值1,...,MD5('值2'),...)
1.2.2 流程函数
- if(ex1,ex2,ex3) : 如果ex1为true,则返回ex2,否则返回ex3
- ifnull(ex1,ex2) :如果ex1不为null,则返回ex1,否则返回ex2
/*如果ex1为true,则返回ex2,如果ex2为true,则返回ex4,否则返回ex5*/
select case when ex1 then ex2 when ex3 then ex4 else ex5 end;
/* 如果sex返回0,则展示男,如果返回1,则展示女 */
select staff_name,age,(select case
when sex = '1' then '女'
when sex = '0' then '男'
else sex end) as 'sex'
from staff_info;