MySQL 数据库 (目前使用最广泛、流行度最高的开源免费数据库;Community + Enterprise,关系型数据库)

安装

  • MySQL Server :专门用来提供数据存储和服务的软件
  • MySQL Workbench :可视化的 MySQL 管理工具,通过它,可以方便的操作存储在 MySQL Server 中的数据

DataType 常见的数据类型:

  • int 整数
  • varchar(len) 字符串
  • tinyint(1) 布尔值

字段的特殊标识

  • PKPrimary Key) — 主键、唯一标识
  • NNNot Null) — 值不允许为空
  • UQUnique) — 值唯一
  • AIAuto Increment) — 值自动增长

SQL 语句

注意: SQL 语句中的关键字对大小写不敏感。SELECT 等效于 select,FROM 等效于 from

注意:在执行语句的时候,需要选择默认的数据库,双击选择指定的数据库(字体加粗为选中),没选中会无法执行语句

查询语句

1
2
3
4
5
6
7
8
9
--- 从 from 指定的表中,查询出所有数据。* 表示所有列
select * from 名称
--- 查询 users 表中的所有的数据
select * from users

--- 从 from 指定的表中,查询出指定列名称(字段)的数据
select 列名称 from 表名称
-- 查询 users 表中列名称为username和password的数据(多列之间,使用英文逗号进行分隔)
select username,password from users

插入语句

1
2
3
4
5
--- 注意:列和值要--对应,多个列和多个值之间,使用英文的逗号分隔
insert into 表名 (列1,列2,...) values (值1,值2,...)

--- 在 users 表中插入一条username为mz,password为123456的数据,新的数据需要加上引号,否则会报错
insert into users (username,password) values ('mz','123456')

修改语句

1
2
3
4
5
6
7
-- 用 update 指定要更新哪个表中的数据
-- 用 set 指定要修改的列和对应的新值
-- where 指定要修改的条件(MySQL不写会报错,防止所有数据被修改)
update 表名称 set 列名称 = 新值 where 条件

-- 修改 users 表中id为4的行,password列的值为654321,status列的值为1
update users set password=654321,status=1 where id=4

删除语句

1
2
3
4
delete from 表名称 where 列名称 =

--- 删除 users 表中 id 为 4 的数据
delete from users where id=4

where 子句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
--- 查询语句中的where条件
select 列名称 from 表名称 where 列 运算符 值

--- 修改语句中的where条件
update 列名称 set=新值 where 列 运算符 值

--- 删除语句中的where条件
delete from 表名称 where 列 运算符 值

-----------------------------------------

-- 查询 id 为 1 的所有用户
select * from users where id=1

-- 查询 id 大于 2 的所有用户
select * from users where id>2

--查询 username 不等于 admin 的所有用户
select * from users where username<>'zs'

比较运算符

  • 注意:在某些版本的 SQL 中,操作符 <> 可以写为 !=
操作符 描述
= 等于
<> 不等于
> 大于
< 小于
>= 大于等于
<= 小于等于
between 在某个范围
like 搜索某种模式

and 和 or 逻辑运算符

  1. ANDOR 可在 WHERE 子语句中把两个或多个条件结合起来
  2. AND 表示必须同时满足多个条件,相当于 JavaScript 中的 && 运算符,例如 if (a !== 10 && a !== 20)
  3. OR 表示只要满足任意一个条件即可,相当于 JavaScript 中的 || 运算符,例如 if(a !== 10 || a !== 20)
1
2
3
4
-- and
-- 使用 AND 来显示所有 status 为 0,并且 id 小于 3 的用户

select * from users where status=0 and id<3
1
2
3
4
-- or
-- 使用 OR 来显示所有 status 为 1,或者 username 为 zs 的用户

select * from users where status=0 or username='zs'

order by 排序子句

  • ORDER BY 语句用于根据指定的列对结果集进行排序
  • ORDER BY 语句默认按照升序对记录进行排序,ASC 关键字代表升序排序
  • 如果您希望按照降序对记录进行排序,可以使用 DESC 关键字

升序

1
2
3
4
5
6
-- 下面这两条 SQL 语句是等价的,
-- 因为 order by 默认进行升序排序
-- 其中,ASC 关键字代表升序排序

-- select * from users order by status
select * from users order by status asc

降序

1
2
3
-- desc 代表降序排序

select * from users order by status desc

多重排序

1
2
--- 对 users 表中的数据,先按照 status 字段进行降序排序,再按照 username 的字母顺序,进行升序排序
select * from users order by status desc, username asc

COUNT(*) 统计函数

1
2
3
4
5
--- 用于返回查询结果的总数据条数
select count(*) from 表名称

-- 查询 users 表中 status 为 0 的总数据条数
select count(*) from users wheres status=0

AS 为列设置别名

1
2
3
4
5
6
7
-- 给查询出来的列名称设置别名

-- 将列名从 count(*) 修改为 total
select count(*) as total from users where status=0

-- 将列名 username 改为 uname, password 改为 upwd
select username as uname, password as upwd from users