数据库
概念:是一款有特殊文件结构的软件
作用: 管理数据
特点:
1、通过sql语句可以方便管理和获取数据
2、安全,完善的账号和权限管理
3、方便的将数据进行组合生成新的数据
4、可存储大量数据(普通文本,二进制文件)
5、保持数据的一致性、完整性
数据库的结构
1.png
2.png
数据库的安装与使用
sql
概念:结构化查询语句,操作数据的一门语句,可以对数据库进行增删改查。 语句的结构或者是格式固定的,每个语句的作用是固定的,但是内容不固定,查询出来的结果也是不固定的。
特点: - SQL语句可以单行或多行书写,以分号结尾; - 可以用空格和缩进来来增强语句的可读性; - 关键字不区别大小写,建议使用大写; - sql语句之间可以相互嵌套
sql分类
- DDL(Data Definition Language):数据定义语言,用来定义数据库对象:库、表、列等;
- DCL(Data Control Language):数据控制语言,用来定义访问权限和安全级别;
- DML(Data Manipulation Language):数据操作语言,用来定义数据库记录(数据);
- DQL(Data Query Language):数据查询语言,用来查询记录(数据)。
DDL
#创建数据库
create database 数据库名称 charset utf8 collate utf8_general_ci;
#删除数据库
drop database 数据库名称;
create table 表名 (字段名1 数据类型(位数),字段名2 数据类型(位数));
drop table 表名;
alter table 表名 modify 字段名 类型;
DML
insert [into] 表名 (字段名1,字段名2,...) values(值1,值2,...);
update 表名 set 字段名1=值1,字段名2=值2 where 条件;
delete from 表名 where 条件;
DQL
#基本查询
select 列名1,列名2... from 表名 where 条件;
#and
where 条件1 and 条件2;
#or
where 条件1 or 条件2;
#<> !=
where age<>23 and age!=24;
#in
where 字段名 in(23,24,25);
#not in
where id not in(1,2,3);#选出id不是1,2,3的记录
#between and
where age between 22 and 25;#选出age在22到25之间的记录(包括22和25)
#is null
where age is null;#选出age是空的记录
#模糊查询
where name like '%小%';#选出名字带有小的记录
where name like '___';#选出名字是3个字的记录
#order by 排序
order by age desc;#安装age降序
order by age asc,salary desc;#先按照age升序,在按照salary降序
#限制
limit 0,3;#从0开始选出3条记录
#分组
select * from 表名 group by sex;#按照sex进行分组,例如男的一组,女的一组
#聚合函数 一般都和group by 配合使用
select count(*) from 表名 group by sex;#先分组,在统计,统计出男女的个数
select sum(age) from 表名 group by sex;#统计出男女的年龄总和
select avg(age) from 表名 group by sex;#统计出男女的年龄平均数
#having 对分组之后的数据进行条件查询
select count(*) from 表名 group by sex having 条件;
高级查询
子查询
一个查询语句可以嵌套在另一个查询语句中
#选出价格最高的商品信息
select * from good where price=(select max(price) from good);
#选出价格高于3000
select * from good where price in(select price from good where price>3000);
#从一张临时表中筛选出库存大于700的商品信息
select * from (select * from good where price>3000) aa where num>700;
#给每个商品添加一列数据
select gname,(select max(price) from good) num from good;
#下面两条语句要求字段名对应
#将选出来的数据插入到表中
insert into type(typename) select gtype from good group by gtype;
#创建表,并将选出来的数据插入到表中
create table type(typeid int auto_increment primary key,typename varchar(20) unique)
select gtype as typename from good group by gtype;
连接
连接:通过两张表共同字段,实现两张板数据的连接
sql连接的通用写法
内连接:与连接的概念一致
语法:select * from 表名1 inner join 表名2 on 条件;
select * from stu inner join class on stu.classid=class.classid;
左外连接
概念:以作左表为主,无论左表是否为空,都要查询出来,如果为空,那么用null代替
语法:select 字段名 from 表名1 left outer join 表名2 on 条件
例子:select * from stu left outer join class on stu.classid=class.classid;
右外连接
概念:以作右表为主,无论右表是否为空,都要查询出来,如果为空,那么用null代替
语法:select 字段名 from 表名1 right outer join 表名2 on 条件
例子:select * from stu right outer join class on stu.classid=class.classid;
概念:全外(可以通过表连接实现)两张表的数据结构要一样
例子:
select * from stu left outer join class on stu.classid=class.classid
union
select * from stu right outer join class on stu.classid=class.classid;
mysql特有的连接方式
select * from 表名1,表名2 where 条件;
例子:select * from stu,class where stu.classid=class.classid;