本文不是技术性文章,也不是什么教程,仅仅记录了一些SQL语句、数据类型和完整性约束条件,忘记SQL语句怎么写的时候可以当做字典。
- 数据库的相关操作
1. 创建数据库
2. 删除数据库
3. 修改数据库
4. 查看数据库
5. 选择数据库
- 表的相关操作
1. 表的创建
2. 表的删除
3. 表的修改
- 修改表名
- 增加字段
- 修改字段
- 删除字段
4. 表的查询
- 对表中数据的操作
1. 增加纪录
2. 删除纪录
3. 更改纪录
4. 查询纪录
- 进一步select
1. select语句关键字执行顺序
2. 正则查询
3. 连表操作
4. 子查询
- 整形
- 浮点型
- 字符串
- 时间
- 枚举
- 集合
- 主键
- 默认值
- 唯一
- 不为空
- 填充0
- 外键
- 自增加
常见SQL语句
- [] 中的内容是可选的
- { a | b } 或,a和b选择其一
- … 省略,表示可能有多个
- <> 表示是名称
数据库的相关操作
# 创建数据库
create database [if not exists] <数据库名>
[[default] character set <字符集名>][[default] collate <校对规则名>];
# if not exists 在创建数据库前检查
# 只有数据库中不存在该想要创建的数据库
# 的同名数据库时创建。
# character set 指定字符集。
# character set 可简写为 charset
# collate 指定校对规则名。
# 例如:
create database if not exists db1;
create database db2 charset utf8; # 创建数据库并指定字符集
# 删除数据库
drop database [if exists] <数据库名>;
# 例如:
drop database db1;
# 修改数据库
alter database [数据库名] {[default] charset <字符集名> |
[default] collate <校对规则名>};
# 例如:
alter database db1 charset utf8;
# 查看数据库
show databases;
# 查看指定数据库信息
show create database <数据库名>;
# 选择数据库
use <数据库名>;
# 如:
use db1;
表的操作
# 表的创建
create table [if not exists]
[<数据库名>.]<表名>(<字段名1> 类型[(宽度)] [约束条件...]
,...
<字段名2> 类型[(宽度)] [约束条件...]);
# 例如:
create table t1(
id int primary key auto_increment,
name char(50) not null,
sex enum('male','female') not null,
age int not null unsigned default 20
);
# 常见的数据类型和约束条件文章后面有
# 表的删除
drop table [if exists] [<数据库名>.]<表名>;
# 例如:
drop table t1;drop table if exists db2.t1;
# 表的修改
# 1.修改表名
alter table [<数据库名>.]<表名> rename [<数据库名>.]<新表名>;
# 2.增加字段
alter table [<数据库名>.]<表名>
add [<字段名> 类型] [约束条件] [first|after <原表中的某个字段名>];
# 可添加多个字段,用“,”隔开
# first 表示将该字段加到第一列
# after 表示将该字段插入表中的指定字段之后
# 例如:
alter table db1.t1 add age int,add sex enum('male','female');
# 3.修改字段
alter table [<数据库名>.]<表名> modify <字段名> <数据类型> [约束条件];
alter table <表名> change <旧字段名> <新字段名> <数据类型> [约束条件];
# 4.删除字段
alert table [<数据库名>.]<表名> drop <字段名>;
# 表的查询
describe table [<数据库名>.]<表名>; # 查看表的结构
show tables; # 查看数据库中的数据表
show create table [<数据库名>.]<表名>; # 查看数据表信息
对表中数据的操作
# 增加记录
insert into
{<数据库名>.]<表名> | [<数据库名>.]<表名>(<字段名1>,...<字段名n>)]}
values
({表中所有字段对应的值|[字段1的值,...字段n的值]});
# 例如:
# 存在表
t1(id,name,sex)
# 插入
insert into t1 values(1,"Tom","male");
insert into t1(name,sex) values("Lee","male"),('Jack','female');
# 删除记录
delete from [<数据库名>.]<表名> [其他];
# 例如:
delete from t1; # 删除t1表所有记录
delete from t2 where id=3; #删除t2中id为3的记录
# 更改记录
update [<数据库名>.]<表名>[,... [<数据库名>.]<表名>] set
[[<数据库名>.]<表名>.]<字段名>=xx
[ ,...[[<数据库名>.]<表名>.]<字段名>=xx [其他];
# 例如:
update t1 set age=18 where id=3;
# 查询记录
select
{
[[<数据库名x>.]<表名x>.]<字段名x>,...[[<数据库名n>.]<表名n>.]<字段名n>
| *
}
from
[<数据库名n>.]<表名x>[,...[<数据库名x>.]<表名n>] [其他];
# 例如:
# 存在表
t1(id,name,age);
t2(id,job);
select * from t1 where id=2;
select db1.t1.id,name from db1.t1,db2.t2;
# 注意t1和t2表中不能同时存在“name”字段
更进一步的sql
# select语句关键字
select DISTINCT <字段名1>...
from
<左表名>
连接类型 join
<右表名>
on 连接条件
where 约速条件
group by 分组(分类)条件
having 过滤条件
order by 排序方式
limit 限制条数
# 顺序:
1. from <左表名>
2. on 连接条件
3. 连接类型 join <右表名>
4. where 约速条件
5. group by 分组(分类)条件
6. having 过滤条件
7. select
8. distinct
9. order by 排序方式
10. limit 限制条数
# 一点小解释
# 连接类型有3种(有的数据库有四种)inner,left,right ,下面的连表查询里解释
# 正则查询
# 使用正则表达式筛选
select * from <表名> where <字段名> regexp '^ab.*(d|e)$';
# 匹配字段中所有以“ab”开头,且以“d”或者“e”结尾的纪录
# 连表查询
# 内连接 仅仅取两表的共同部分,及条件成立的纪录
select * from <表名1> inner join <表名2> on 条件;
# 如:
slect * from t1 inner join t2 on t1.id = t2.did;
# 左连接 取两表的共同部分和左表不符合条件的部分
select * from <表名1> left join <表名2> on 条件;
# 如:
slect * from t1 left join t2 on t1.id = t2.did;
# 右连接 取两表的共同部分和右表不符合条件的部分
select * from <表名1> right join <表名2> on 条件;
# 如:
slect * from t1 right join t2 on t1.id = t2.did;
# 子查询
# 一个查询结果作为另一个查询的条件
select <字段名1>... from <表名1> where 子查询;
# 如
select * from t1 where id in (
select id from t2 where age > 30
);
# 将t2中age大于30的纪录的id作为t1表的“id=”的条件
select * from t1 where name in (
select name from t2
);
# 查找t1表中所有name在t2表中相同的纪录
常见数据类型
# 整数
tinyint
int
bigint
# 浮点型
float
double
decimal # 精准
# 字符串
char # 定长字符串
varchar # 变长字符串
# 时间
date # YYYY-MM-DD
time # HH:MM:SS
datetime # YYYY-MM-DD HH:MM:SS
year # YYYY
timestamp # 时间戳 YYYY-MM-DD HH:MM:SS
# 枚举
enum
enum('a','b') # 选择其一
# 集合
set
set('a','b','c') # 任意选择n个
常见完整性约束
# 主键
primary key
# 指定默认值
default
# 表明唯一
unique
# 表明不为空
not null
# 填充0
zerofill
# 无符号
unsigned
# 外键
foreign key
# 自增加
auto_increment