SQL 基础

总结一波数据库相关

数据库有点忘了,总结一波,顺便学一学数据库高深方面的内容(譬如各种锁,索引,查询优化等等)
大概会分为三篇~

  1. SQL 基础(CURD)
  2. SQL 进阶(多表联查, 分页, 子查询, 事务, union, except, join, group by, etc…)
  3. SQL 高级(各种锁,索引,视图, 查询优化, 理论知识, etc…)

登录

1
2
mysql -u root -h localhost -P 3306 -p
Enter password: ******

其他常用参数:

  • -V 输出版本信息并且退出
  • –help 帮助
  • -D 打开指定的数据库
  • –prompt 设置提示符,默认为mysql>
    • \D 完整的日期
    • \d 当前数据库
    • \h 服务器名称
    • \u 当前用户

退出

1
2
3
quit;
exit;
\q;

数据库

1
2
3
4
5
6
7
8
9
-- 创建数据库
create database testblog;
-- 显示所有数据库
show databases;
-- 删除数据库
drop database abcde;
-- 切换数据库
use testblog;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- 创建新表
-- 存储引擎有 MEMORY, MyISAM, InnoDB, 默认新版本引擎. MEMORY数据存在内存中,处理速度最快,但是不安全。
create table if not exists `account`(
`id` int unsigned auto_increment,
`name` varchar(10) not null,
`age` varchar(2) not null,
`sex` varchar(1),
primary key (`id`)
)engine=InnoDB default charset=utf8;

-- 查看表结构
desc account;
-- 从已有的表创建新表(仅结构)
create table if not exists `user` like `account`;
-- 从已有的表创建新表(结构+记录)
create table if not exists `user` as select * from `account`;
-- 修改表名
alter table `users` rename to `user`;
-- 删除表
drop table `user`;
-- 表增加一个列(加在最后面)
alter table `user` add column `username` varchar(20) not null;
-- 表增加一个列(加在第一列)
alter table `user` add column `uid` varchar(20) not null first;
-- 表增加一个列(加在某一列后面)
alter table `user` add column `location` varchar(20) not null after `sex`;
-- 修改列名
alter table `user` change `sex` `sexual` char;
-- 删除列
alter table `user` drop column `sexual`;

记录

查询

1
2
3
4
-- 查询表所有列的记录
select * from `user`;
-- 查询特定列的记录
SELECT `name`, `title` FROM `user`;

查询条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- 精确条件 (不等于<>, !=  【>, >=, <, <=】)
select * from `user` where `name` = 'ada';
-- 条件连接 (and, or)
select * from `user` where `name` = 'ada' and `id` = 1;
-- 模糊查询记录
select * from `user` where `name` like '%John%';
-- 模糊排除查询记录
select * from `user` where `name` not like '%John%';
-- 加上 BINARY 操作符 来区分大小写
SELECT * FROM `user` WHERE LIKE BINARY '%azz%';
-- 多条件模糊查询,且有先后顺序(%匹配多个字符)
select * from `user` where `name` like '%Jo%n%';
-- 模糊匹配单个字符
select * from `user` where `name` like 'Jo_n';
-- 多选一字符 (匹配 a1, b1, c1)
select * from `user` where `name` like '[abc]1';
-- 多选一字符 (连续数字或字母)
select * from `user` where `name` like '[a-z][0-9]';
-- 多选一字符 (排除某字符 除了 1a, 2a, 3a)
select * from `user` where `name` like '[^123]a';
-- escape,转义字符后面的%或_,使其不作为通配符,而是普通字符匹配
select * from `user` where `name` like 'Lucy/_%' escape'/';

聚合 (group by)

1

排序

限制记录

插入

1
2
3
4
-- 插入一条记录
insert into `user` values (1, 2, 'amy', 13, 'china', 'Amy');
-- 插入一条记录(指定某列)
insert into `user` (`id`, `uid`, `age`, `name`) values (3, 'ES283268950', 18, '');

备份

  1. mysqldump
  2. 直接复制文件夹
  3. mysqlhotcopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 备份数据库 weekly
mysqldump -u root -h localhost -p weekly > D:/aa.sql
Enter password: ******
# 备份数据库 weekly 的product表
mysqldump -u root -h localhost -p weekly product> D:/aa.sql
Enter password: ******
# 备份数据库 weekly 的多个表
mysqldump -u root -h localhost -p weekly product, tag> D:/aa.sql
Enter password: ******
# 备份多个数据库
mysqldump -u root -h localhost -p --databases blog weekly> D:/aa.sql
Enter password: ******
# 备份所有数据库
mysqldump -u root -h localhost -p --all-databases > D:/aa.sql
Enter password: ******

常用函数

还原

1
2
mysql -u root -p weekly < D:/aa.sql
Enter password: ******
1
2
3
mysql> source D:/aa.sql;
Query OK, 0 rows affected (0.00 sec)
......
文章作者: Shengyaqingfeng
文章链接: https://creazyboyone.github.io/SQL_Base/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Shengyaqingfeng's Blog