数据库
Database,简称DB。按照一定数量的数据结构来组织、存储和管理数据的仓库。
SQL:
(Structured Query Language),结构化查询语言
关系型数据库
由多张相互连接的二维表组成的数据库,就像Excel一样,有行和列组成的二维表
特点:
- 使用表存储数据,格式统一,便于维护
- 使用SQl语句操作,标准统一,使用方便
- 数据存储在磁盘中,安全
代表:
- Mysql
- Oracle
MySQL
概述
MySQL是一种广泛使用的开源关系型数据库管理系统(RDBMS),它是最流行的开源数据库之一。MySQL由瑞典的MySQL AB公司开发,后来被sun收购,最终被Oracle收购。MySQL以其开源免费、简单易用、高性能和可靠性而闻名。
数据库操作表
-
添加数据(insert into)
-- 添加多条数据
insert into user values (0,'张三','zs123',13335388346,0),
(0,'李四','ls123',14445468965,1),
(0,'王五','ww123',15552533689,2),
(0,'柳六','ll123',16663645689,1),
(0,'宋七','sq123',17745767978,3),
(0,'黎八','lb123',18884658374,4),
(0,'高九','gj123',19935265925,2)
-- 根据字段名添加数据
insert into account(id,name,money) values (0,'小小橘',520)
-- 添加数据
insert into account values (0,'小粉',999)
2.修改数据(update)
-- 修改数据
update account set money = 200 where name = '小绿'
3.删除数据(delete)
-- 删除数据
delete from account where id = 5
4.查询(select)
-- 查询
select * from account
select * from account where id between 1 and 3
select * from account where id in (1,3)
select * from account where id = 1 and name = '小白'
-- 子查询 (将查询到的结果当作另一个查询语句的条件)
SELECT name FROM user where id in (select id from dept where deptname = '销售部')
-- 条件查询 where
select u.id,u.name,password,p.deptname from user u ,dept p where u.deptid = p.id
-- having
select u.id,u.name,password,age,p.deptname from user u ,dept p where u.deptid = p.id having u.age >14
-- 模糊查询 like % _
% 可以匹配0或多个字符 _匹配一个字符
select id,name from user where name like '高%'
-- 分页查询 limit m,n m 为起始索引位置, n为显示数据条数
select id,name,password from user limit 1,3
-- 分组查询 按性别分组 group by
select sex,count(0),avg(age) from user group by sex
-- 内连接查询 inner join inner可以不写
select u.id,u.name,password,d.deptname,p.postname from user u inner join dept d ,post p where u.deptid = d.id or u.postid = p.id
-- 左外连接 left join on 主表在左,从表在右
select u.id,u.name,password,age,d.deptname,p.postname from user u left join dept d on u.deptid = d.id left join post p on u.postid = p.id having u.age > 15
-- 右外连接 right join on 主表在右,从表在左
select u.id,u.name,password,age,d.deptname,p.postname from dept d right join user u on d.id =u.deptid left join post p on u.postid = p.id
5.聚合函数
-- 平均值
select avg(age) from user
-- 求和
select sum(age) from user where sex = '男'
-- 最大值
select max(age) from user where sex = '男'
-- 最小值
select min(age) from user where sex = '男'
-- 数据条数
select count(age) from user where sex = '男'
条件
- bewteen and 在社么是社么之间
- in() 查询指定位置的数据
- and 与
- or 或
- not 非