总结一波数据库相关
数据库有点忘了,总结一波,顺便学一学数据库高深方面的内容(譬如各种锁,索引,查询优化等等)
大概会分为三篇~
- SQL 基础(CURD)
- SQL 进阶(多表联查, 分页, 子查询, 事务, union, except, join, group by, etc…)
- 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 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
|
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';
select * from `user` where `name` = 'ada' and `id` = 1;
select * from `user` where `name` like '%John%';
select * from `user` where `name` not like '%John%';
SELECT * FROM `user` WHERE LIKE BINARY '%azz%';
select * from `user` where `name` like '%Jo%n%';
select * from `user` where `name` like 'Jo_n';
select * from `user` where `name` like '[abc]1';
select * from `user` where `name` like '[a-z][0-9]';
select * from `user` where `name` like '[^123]a';
select * from `user` where `name` like 'Lucy/_%' escape'/';
|
聚合 (group by)
排序
限制记录
插入
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, '');
|
备份
- mysqldump
- 直接复制文件夹
- mysqlhotcopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysqldump -u root -h localhost -p weekly > D:/aa.sql Enter password: ******
mysqldump -u root -h localhost -p weekly product> D:/aa.sql Enter password: ******
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) ......
|