1,数据库:存储数据的仓库
2,数据库管理系统:操纵和管理数据库的大型软件
3,SQL:操作关系型数据库的编程语言,是一套标准
SQL分类:
①DDL(Data definition Language) 数据定义语言,用来定义数据库对象(数据库,表,字段)
②DML(Data Manipulation Langyage) 数据操作语言,用来怼数据库表中的数据进行增删改
③DQL(Data Query Language) 数据查询语言,用来查询数据库中表的记录
④DCL(Data control Language) 数据控制语言,用来创建数据库用户,控制数据库的访问权限
①DDL-数据库的库操作
查询所有数据库: show databases;
查询当前数据库: select database()
使用某个数据库 use 数据库名字;
删除数据库 drop database 数据库名字;
创建数据库 create database (if not exits) 数据库名字 ;
①DDL-数据库的表操作
查询当前数据库中的所有表 show tables;
查询表结构 desc 表名;
查询指定表的建表语句 show create table 表名;
创建表: create table 表名字 (
字段1 字段1类型 comment '字段1注释'
字段2 字段2类型 comment '字段2注释',
字段3 字段3类型 comment '字段3注释'
)comment '表注释';
创建一个两人的账户表
create table account ( id int primary key auto_increment comment '主键ID', name varchar(10) comment '姓名', money int comment '余额' ) comment '账户表';
表中插入内容的两种方式:
第一种方式:
insert into account(id, name, money) values
(null,'张三',2000),
(null,'李四',2000);
第二种方式:
insert into account(id, name, money) values
(null,'张三',2000),
(null,'李四',2000);
①DDL表操作-修改
修改数据类型: alter table 表名 modify 字段名 新数据类型();
修改字段名和字段类型:alter table 表名 change 旧字段名 新字段名 新数据类型 comment '注释';
删除字段 : alter table 表名 drop 字段名;
修改表名 : alter table 表名 rename to 新表名;
①DDL表操作-删除
删除表: drop table (if exits) 表名;
删除指定表,并重新创建新表(只不过把表中全部内容删除了 只剩下一个空表)
truncate table 表名;
DML介绍-数据操作语言,用来怼数据库表中的数据进行增删改
添加数据:insert
1,给指定字段添加数据:insert into 表名 (字段名1,字段名2,字段名3)values(值1,值2,值3);
2,给全部字段添加数据insert into 表名 values(值1,值2,值3,值4,值5);
3,批量添加数据 两种方法 (见上述 账户表的插入)
修改数据:update
update 表名 set 修改的内容 where 条件;
1,修改id为1的数据,将name修改为ithema
update employee set name = 'ithema where id = 1;
2,修改id为1的数据,将name修改为小昭,gender修改为女
update employee set name = '小昭',gender = '女' where id = 1;
3,将所有员工的入职日期修改为2008-09-07
update employee set entrydate='2008-09-07';
删除数据:dalete
delete from 表名 [where 条件]
注:delete语句的条件可以有也可以没有 如果没有条件 则会删除整个表中的数据
delete语句不能删除某一个字段的值(可以使用 update来执行)
DQL语法(Data Query Language) 数据查询语言,用来查询数据库中表的记录
1,基本查询
①查询多个字段: select 字段1,字段2,字段3 from 表名;
②设置别名 select 字段1 [as 别名1],字段2 [别名2] ,......from 表名
③去除重复记录 select distinct 字段列表 from 表名;
2,条件查询(where 条件列表)
1,语法 select 字段列表 from 表名 where 条件列表;
2,条件
比较运算符 | 功能 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
= | 等于 |
<>或者!= | 不等于 |
between....and... | 在某个范围之内(含最小,最大值) |
in | 在in之后的列表中的值,多选一 |
like 占位符 | 模糊匹配(_匹配单个字符,%匹配任意个字符) |
is null | 是null |
逻辑运算符 | 功能 |
and或者&& | 并且(多个条件同时成立) |
or或者|| | 或者(多个条件任意一个成立) |
not或者! | 非,不是 |
条件查询案例:
1,查询年龄等于29岁的所有员工
select * from employee where age='29';
2,查询年龄小于等于33岁的所有员工
select * from employee where age<=33;
3,查询没有身份证号码的员工信息
select * from employee where id is null;
4,查询有身份证号码的员工信息
select * from employee where id is not null;
5,查询年龄不等于34的员工,以下两种均可以
select * from employee where age !='34';
select * from employee where age <> '34';
6,查询年龄从20到30岁之间的员工 以下三种均可以
select * from employee where age>='20' and age<'30';
select * from employee where age>='20' && age<='30';
select * from employee where age between 20 and 30;
7,查询性别为女并且年龄小于24岁的员工
select * from employee where gender='女' and age<='24';
8,查询年龄等于34或者35或者39岁的员工
select * from employee where age='34' or age = '35' or age ='39';
select * from employee where age in(34,35,39);
9,查询查询身份证尾号是6的员工信息 该处用百分号
select *from employee where idcard like '%6';
10,查询姓名有两个字的员工信息 该处用下划线 不能用百分号;
select * fron employee where name like '_ _ ';
3,DQL-聚合函数(count,max,min,avg,sum )
介绍:将一列数据作为一个整体,进行纵向计算.
表中为常见集合函数
函数 | 功能 |
count | 统计数量 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
sum | 求和 |
语法: select 聚合函数(字段列表) from 表名;
注意:null值不参与所有聚合函数运算.
聚合函数案例
1,统计该企业员工的数量 以下均可以
select count(*) from employee;
select count(id) from employee;
2,统计该企业员工的平均年龄
select avg(age) from employee;
3,统计该企业员工的最大年龄
select max(age) from employee;
4,统计该企业员工的最小年龄
select min(age) from employee;
5,统计该企业员工在郑州工作的员工年龄之和
select sum(age) from employee where workaddress='郑州';
4,DQL-分组查询(group by 分组字段列表)
1,语法: select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];
2,where和having的区别
①执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后进行过滤.
②判断条件不同:where不能对聚合函数进行判断,而having可以
分组查询-案例
1,根据性别分组,统计男性员工和女性员工的数量
select gender,count(*) from employee group by gender;
2,根据性别分组,统计男性员工和女性员工的平均年龄
select gender,avg(age) from employee group by gender;
3,查询年龄小于35的员工,并根据工作地址进行分组,获取员工数量大于等于1的工作地址
select workaddress,count(*) from employee where age<35 group by workaddress having count(*) >=3;
select workaddress,count(*) address_count from employee where age<35 group by workaddress having address_count >=3;
注意:address_count是为count(*)起的别名
注意:执行顺序: where>聚合函数>having;
分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义;
5,DQL-排序查询(order by 排序字段列表)
①语法: select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2;
②排序方式: asc为升序(默认值) desc为降序
注意:如果是多字段排序,当第一个字段值相同时,才会有第二个字段进行排序
排序查询案例:
1,根据对公司员工的年龄 进行升序/降序排列 注意 此时asc可以省略
select * from employee order by age asc;
select * from employee order by age desc;
2,根据入职时间进行降序排序
select * from employee order by age desc;
3,根据公司员工的年龄进行升序排列 年龄相同 在进行入职时间降序排列
select * from employee order by age asc,endrydate desc;
6,DQL-分页查询(limit 分页参数)
语法:select 字段列表 from 表名 limit 起始索引,查询记录数;
注意:1,起始索引从0开始,起始索引=(查询页码-1)*每页显示记录数;
2,分页查询时数据库的方言,不同的数据库有不同的表现,mysql中是limit;
3,如果查询的是第一页数据,起始索引可以省略,直接简写为limit 0;
分页查询案例 语法:select 字段列表 from 表名 limit 起始索引,查询记录数;
1,查询第一页员工的数量,每页展示8条记录;
select * from employee limit 0,8;
2,查询第二页员工数量,每页展示8条记录, 起始索引为:(页码-1)*页展示记录数
select * from employee limit 8,8;