文章目录
1. DML - 数据操纵语言
1.1 update - 更新
# 单表更新
update 表名
set 列=值
where 筛选条件
# 多表更新
update 表名 【连接类型 join on 连接条件】
set 列=值
where 筛选条件
示例
// 修改两个表之前的列记录 - 等值连接,雇员42号的姓名、以及部门名被修改
update emp1
inner join dept1 on emp1.deptno = dept1.deptno
set ename='lrc1', dname='研发中心'
where empno = 42;
1.2 delete、truncate - 删除
语法
# 自增长不受影响 - 删除有返回值
# 单表
delete from 表名 where 筛选条件
# 指定删除的记录数
delete from 表名 limit 删除数
# 多表删除
delete 表1,表2
from 表1,表2
where 连接、筛选条件
delete 表1,2
from 表1【连接类型 join 表2 on 连接条件】
where 筛选条件
# 不可回滚删除 - 效率高 - 自增长从0开始 - 删除无返回值
truncate table 表名
举例
# 删除部门、员工关联的记录
delete emp1,dept1
from emp1 inner join dept1 on emp1.deptno = dept1.deptno
where empno = 42
delete emp1,dept1
from dept1,emp1
where emp1.deptno = dept1.deptno and emp1.empno = 50; ;
1.3 insert - 插入
语法
# 插入单行
insert into 表( 列名 ) values ( 列值 )
# 插入多行 方式1
insert into 表( 列名 )
select 列值 union
select 列值 union
select 列值 union
# 插入多行 方式2
insert into emp1(ename)
values ('eee'), ('ttt');
举例
# 插入两行数据
insert into emp1(ename)
select 'qqq' union
select 'ccc'
2. DDL - 数据定义语言
2.1 库的操纵
- 创建库
create database if not exists 库名
- 库名修改
rename database 旧库名 to 新库名
- 库的删除
drop database 库名
//通用写法
drop database if exists 库名
- 修改库的字符集
alter database 库名 character set 字符集
2.2 表的操纵 - 表创建、删除、列类型修改
- 表的修改
# 语法
alter table 表名 add/drop/modify/change column 列名 【列类型 约束】
# ①修改列名
alter table 表名 change column 旧列名 新列名 数据类型
# ②修改列类型、约束
alter table 表名 modify column 列名 新数据类型
# ③添加新列
alter table 表名 add column 列名 数据类型
# ④删除列
alter table 表名 drop column 列名;
# ⑤修改表名
alter table 旧表名 rename to 新表名
- 表的删除
# 表的删除
drop table 表名
#通用写法
drop table if exists 表名
- 表的创建
# 表创建
create table 表名(
列名 数据类型,
)
#通用写法
create table if exists 表名(
列名 数据类型,
)
4. 复指表
# 复制表结构
create table if not exists 新表名 like 库内的某个表名
# 复制表结构加数据
create table if not exists 新表名
select * from 表
2.3 约束 - check约束可定义但不支持
查看表中的 主键、外键、唯一键
show index from 表名
2.3.1 创表时添加约束
列级约束定义
create table if not exists book2(
id int primary key,
`name` varchar(100) not null,
# 无效果
authorSex char(1) check(authorSex='男' or authorSex='女'),
author varchar(100) UNIQUE,
price double(5,2) default 0.00,
#没用 - 无效果
typeId int REFERENCES type(id)
);
表级约束定义
#语法
# constraint 约束名 约束(列名)
create table if not exists book2(
id int,
`name` varchar(100) not null,
authorSex char(1) ,
author varchar(100),
price double(5,2) default 0.00,
typeId int,
constraint pk_id primary key(id),
constraint author_sez_check check(authorSex='男' or authorSex='女'),
constraint author unique(author),
constraint typeId_fk foreign key(typeId) references type(id)
);
2.3.2 表创建完后添加、修改约束
- 添加列级约束 - 相当于把列删除重新创建新列、所以必须指定类型
alter table 表名 modify column 列名 新列类型 新约束
- 添加表级约束 - 相当于在原来列的基础上修改列约束
alter table 表名 add 【constraint 约束名 】约束类型( 列名 ) 【外键引用】;
# 外键约束添加 - 建议显示指定外键约束名
alter table 表名1 add constraint 名字 foreign key( 列名 ) references key 表名2( 列名 );
# 普通约束添加
alter table 表名1 add unique( 列名 );
2.3.2 删除约束
# 1. 普通约束
alter table 表名 modify 列名 列类型;
# 2. 唯一约束删除
① 查看索引
show index from 表名;
② 通过约束名进行删除约束
alter table 表名 drop index 唯一约束名;
# 3. 主键约束删除
alter table 表名 drop primary key 外键约束名;
2.4 自增长increment
- 查看系统内置的自增长变量
show variables like '%auto_increment%'
- 修改自增长的步长 - 但起始索引不可被修改
set auto_increment_increment = 步长
2.5 索引 - index
2.5.1 创建、删除索引
普通索引创建、删除 - 语法
# 配套1 - 普通索引
# 添加索引
create index 索引名 on 表名( 索引列 )
# 删除索引
drop index 索引名 on 表名
----------------------------
# 配套2 - 普通索引
# 添加索引
alter table 表名 add index 索引名 ( 索引列 )
# 删除索引
alter table 表名 drop index 索引名;
----------------------------
# 配套3 - 全文索引
# 添加索引
alter table 表名 add fulltext 索引名( 索引列 )
----------------------------
# 配套4 - 主键、唯一索引
# 添加索引
alter table 表名 add primay key ( 列名 );
alter table 表名 add unique 索引名 ( 列名 );
2.5.2 强制、忽略使用索引
# 强制使用索引查询 -( 强制使用可以使用到的索引 - 如果查询中压根跟索引不搭边,强制使用索引也没用)
select * from 表 force/use index(索引名) where 查询条件
# 不使用索引查询
select * from 表 ignore index(索引名) where 查询条件
3. TCL - 事务控制语言 - 默认Repeatable Read级别
事务使用步骤
# ① 关闭当前连接的事务自动提交功能
set autocommit = 0;
# ② 开启事务
start transaction;
# ③sql语句
# ④设置事务回滚点
savepoint 回滚点名;
# ④结束事务
commit; #提交事务
rollback; #回滚事务 - 无回滚点
rollback to 回滚点 #回滚事务 - 有回滚点
- 查看当前数据库连接的事务隔离级别
select @@tx_isolation
- 设置当前连接或者所有数据库连接的事务隔离级别
set 【global】 transaction isolation level 事务隔离级别名;