oceanbase与mysql模式下兼容性序
在当今的大数据时代,数据库技术的选择对于企业的信息化发展至关重要。oceanbase作为一种高性能、高可用的分布式关系数据库,在与mysql模式的兼容性方面展现出了显著的优势,为企业数据迁移、整合与升级提供了极大的便利。
oceanbase与mysql的兼容性不仅体现在数据类型、sql语法等基本面,更深入到了存储引擎、优化器等多个层次。这种高度的兼容性使得企业在不改变原有业务逻辑的情况下,能够轻松地将数据和应用从mysql迁移到oceanbase,从而享受到oceanbase带来的高性能和可扩展性。
本文将探讨oceanbase与mysql模式下的兼容性和oceanbase的mysql 模式下的 sql 基本操作。
目录
前言:oceanbase与 mysql 兼容性对比
oceanbase与mysql模式下的兼容性可以从以下几个方面进行简述:
数据类型兼容性
oceanbase的mysql模式兼容mysql 5.7的绝大部分数据类型,包括数值类型(如int、bigint、float、double等)、日期和时间类型(如datetime、timestamp等)、字符串类型(如char、varchar等)以及其他复杂数据类型(如enum、set、json等)。此外,oceanbase还支持空间数据类型,这在某些特定应用场景中非常有用。
oceanbase 数据库支持的数据类型有:
数值类型
整数类型:bool/boolean/tinyint、smallint、mediumint、int/integer 和 bigint。
定点类型:decimal 和 numeric。
浮点类型:float 和 double。
bit-value 类型:bit。
日期时间类型:datetime、timestamp、date、time 和 year。
字符类型:char、varchar、binary 和 varbinary。
大对象类型:tinyblob、blob、mediumblob 和 longblob。
文本类型:tinytext、text、mediumtext 和 longtext。
枚举类型:enum。
集合类型:set。
json 数据类型
空间数据类型
sql语法兼容性
oceanbase支持mysql 5.7版本的大部分sql语法,包括select、insert、update、delete等基本操作,以及复杂的sql查询,如子查询、联接操作、聚合函数等。此外,oceanbase还支持对json数据进行查询和操作,提供了丰富的json函数。
select
支持大部分查询功能,包括支持单、多表查询;支持子查询;支持内联接、半联接以及外联接;支持分组、聚合;支持常见的概率、线性回归等数据挖掘函数等。
支持对多个 select 查询的结果进行 union、union all、minus、except 或 intersect 等集合操作。
支持使用 explain 语法查看执行计划。
insert
支持单行和多行插入数据,同时支持指定分区插入数据。
支持 insert into … select … 语句。
update
支持单列和多列更新数据。
支持使用子查询更新数据。
支持集合更新数据。
delete
支持单表和多表删除。
truncate
支持完全清空指定表。
然而,需要注意的是,oceanbase在某些方面与mysql存在不兼容的情况。例如,oceanbase不支持select…for share语法,也不支持部分json类型和空间数据类型。此外,在函数支持方面,oceanbase也有一些与mysql不同的地方。
存储引擎和分区功能
oceanbase本质上是一个基线加增量的存储引擎,采用lsm树(log-structured merge tree)作为存储机制,这与传统的关系数据库存储引擎有所不同。尽管如此,oceanbase仍然提供了与mysql相似的分区功能,支持分区表和二级分区,可以完全取代mysql常用的分库分表方案。
优化器和执行计划
oceanbase的优化器在执行查询时会生成与mysql相似的执行计划。然而,两者在执行计划的表示和细节上可能存在差异。此外,oceanbase还支持使用explain语法查看执行计划,这有助于用户理解和优化查询性能。
备份与恢复
与mysql相比,oceanbase在备份与恢复方面有所不同。例如,oceanbase不支持冷备份、数据库和表级的备份恢复以及备份数据的有效性验证。因此,在使用oceanbase时需要考虑这些差异并采取相应的措施来确保数据的完整性和可用性。
总的来说,oceanbase在mysql模式下与mysql具有较高的兼容性,但仍然存在一些差异和限制。在使用oceanbase时,建议仔细了解其与mysql的兼容性和差异,并根据实际需求进行相应的调整和优化。
oceanbase 数据库的 mysql 模式兼容 mysql 5.7/8.0 的绝大部分功能和语法。
1. 创建数据库
1.1 语法
create table [if not exists] table_name
(table_definition_list) [table_option_list] [partition_option] [as] select;
create table [if not exists] table_name
like table_name;
table_definition_list:
table_definition [, table_definition ...]
table_definition:
column_definition
| [constraint [constraint_name]] primary key index_desc
| [constraint [constraint_name]] unique {index | key}
[index_name] index_desc
| [constraint [constraint_name]] foreign key
[index_name] index_desc
references reference_definition
[match_action][opt_reference_option_list]
| [spatial] {index | key} [index_name] [index_type] (key_part,...)
[index_optionn_list]
| [constraint [constraint_name]] check(expression) constranit_state
column_definition_list:
column_definition [, column_definition ...]
column_definition:
column_name data_type
[default const_value] [auto_increment]
[null | not null] [[primary] key] [unique [key]] comment
| column_name data_type
[generated always] as (expr) [virtual | stored]
[opt_generated_column_attribute]
index_desc:
(column_desc_list) [index_type] [index_option_list]
match_action:
match {simple | full | partial}
opt_reference_option_list:
reference_option [,reference_option ...]
reference_option:
on {delete | update} {restrict | cascade | set null | no action | set default}
key_part:
{index_col_name [(length)] | (expr)} [asc | desc]
index_type:
using btree
index_option_list:
index_option [ index_option ...]
index_option:
[global | local]
| block_size
| compression
| storing(column_name_list)
| comment
table_option_list:
table_option [ table_option ...]
table_option:
[default] {charset | character set} [=] charset_name
| [default] collate [=] collation_name
| table_tablegroup
| block_size
| compression
| auto_increment [=] int_value
| comment
| row_format [=] redundant|compact|dynamic|compressed|default
| pctfree [=] num
| parallel_clause
| duplicate_scope [=] 'none|cluster'
parallel_clause:
{noparallel | parallel integer}
partition_option:
partition by hash(expression)
[subpartition_option] partitions partition_count
| partition by key([column_name_list])
[subpartition_option] partitions partition_count
| partition by range {(expression) | columns (column_name_list)}
[subpartition_option] (range_partition_list)
| partition by list {(expression) | columns (column_name_list)}
[subpartition_option] partitions partition_count
subpartition_option:
subpartition by hash(expression)
subpartitions subpartition_count
| subpartition by key(column_name_list)
subpartitions subpartition_count
| subpartition by range {(expression) | columns (column_name_list)}
(range_subpartition_list)
| subpartition by list(expression)
range_partition_list:
range_partition [, range_partition ...]
range_partition:
partition partition_name
values less than {(expression_list) | maxvalue}
range_subpartition_list:
range_subpartition [, range_subpartition ...]
range_subpartition:
subpartition subpartition_name
values less than {(expression_list) | maxvalue}
expression_list:
expression [, expression ...]
column_name_list:
column_name [, column_name ...]
partition_name_list:
partition_name [, partition_name ...]
partition_count | subpartition_count:
int_value
1.2 参数说明
参数 | 描述 |
---|---|
primary key | 为创建的表指定主键。如果不指定,则使用隐藏主键。 特别地,oceanbase 数据库不支持修改表的主键或通过 alter table 语句为表添加主键,因此推荐您在创建表时指定好表的主键。 |
foreign key | 为创建的表指定外键。如果不指定外键名,则会使用表名 + obfk + 创建时间命名。(例如,在 2021 年 8 月 1 日 00:00:00 为 t1 表创建的外键名称为 t1_obfk_1627747200000000)。 |
key , index | 为创建的表指定键或索引。 如果不指定索引名,则会使用索引引用的第一列作为索引名,如果命名存在重复,则会使用下划线(_)+ 序号的方式命名。(例如,使用 c1 列创建的索引如果命名重复,则会将索引命名为 c1_2。) 您可以通过 show index |
key_part | 定义所创建的索引。 |
index_col_name | 指定索引的列名,每个列名后都支持 asc(升序),不支持 desc(降序)。默认为升序。 建立索引的排序方式为:首先以 index_col_name 中第一个列的值排序;该列值相同的记录,按下一列名的值排序;以此类推。 |
expr | 表示合法的函数索引表达式,且允许是布尔表达式,例如 c1=c1。oceanbase 数据库当前版本禁止创建生成列上的函数索引。 |
row_format | 指定表是否开启 encoding 存储格式。redundant:不开启 encoding 存储格式。compact:不开启 encoding 存储格式。dynamic:encoding 存储格式。compressed:encoding 存储格式。default:等价 dynamic 模式。 |
[generated always] as (expr) [virtual . stored] | 创建生成列,expr 为用于计算列值的表达式。virtual:列值不会被存储,而是在读取行时,在任何 before 触发器之后立即计算 。虚拟列不占用存储空间。stored:在插入或更新行时评估和存储列值。存储列确实需要存储空间并且可以被索引。 |
block_size | 指定表的微块大小。 |
compression | 指定表的压缩算法,取值如下:none:不使用压缩算法。lz4_1.0: 使用 lz4 压缩算法。zstd_1.0: 使用 zstd 压缩算法。snappy_1.0: 使用 snappy 压缩算法。 |
charset , character set | 指定表中列的默认字符集,可使用的字符集请参见 字符集。 |
collate | 指定表中列的默认字符序,可使用的字符序请参见 字符序。 |
table_tablegroup | 指定表所属的 tablegroup。 |
auto_increment | 指定表中自增列的初始值。oceanbase 数据库支持使用自增列作为分区键。 |
comment | 注释。 |
pctfree | 指定宏块保留空间百分比。 |
parallel_clause | 指定表级别的并行度:noparallel:并行度为 1,默认配置parallel integer:指定并行度,integer 取值大于等于 1。duplicate_scope 指定复制表的属性,取值如下: |
使用 create database 语句创建数据库。
示例:创建数据库 db1,指定字符集为 utf8mb4,并创建读写属性。
obclient> create database db1 default character set utf8mb4 read write;
query ok, 1 row affected
创建完成后,可以通过 show databases 命令查看当前数据库服务器中所有的数据库。
obclient> show databases;
+--------------------+
| database |
+--------------------+
| oceanbase |
| db1 |
| test |
+--------------------+
3 rows in set
2. 表操作
在 oceanbase 数据库中,表是最基础的数据存储单元,包含了所有用户可以访问的数据,每个表包含多行记录,每个记录由多个列组成。本节主要提供数据库中表的创建、查看、修改和删除的语法和示例。
2.1 创建表
使用 create table 语句在数据库中创建新表。
示例:在数据库 db1 中创建表 test。
obclient> use db1;
database changed
obclient> create table test (c1 int primary key, c2 varchar(3));
query ok, 0 rows affected
更多 create table 语句相关的语法说明,请参见 create table 章节。
2.2 查看表
使用 show create table 语句查看建表语句。
示例:
查看表 test 的建表语句。
obclient> show create table test\g
*************************** 1. row ***************************
table: test
create table: create table `test` (
`c1` int(11) not null,
`c2` varchar(3) default null,
primary key (`c1`)
) default charset = utf8mb4 row_format = dynamic compression = 'zstd_1.3.8' replica_num = 1 block_size = 16384 use_bloom_filter = false tablet_size = 134217728 pctfree = 0
1 row in set
使用 show tables 语句查看 db1 数据库中的所有表。
obclient> show tables from db1;
+---------------+
| tables_in_db1 |
+---------------+
| test |
+---------------+
1 row in set
2.3 修改表
使用 alter table 语句来修改已存在的表的结构,包括修改表及表属性、新增列、修改列及属性、删除列等。
示例:
将表 test 的字段 c2 改名为 c3,并同时修改其字段类型。
obclient> describe test;
+-------+------------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+------------+------+-----+---------+-------+
| c1 | int(11) | no | pri | null | |
| c2 | varchar(3) | yes | | null | |
+-------+------------+------+-----+---------+-------+
2 rows in set
obclient> alter table test change column c2 c3 char(10);
query ok, 0 rows affected
obclient> describe test;
+-------+----------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+----------+------+-----+---------+-------+
| c1 | int(11) | no | pri | null | |
| c3 | char(10) | yes | | null | |
+-------+----------+------+-----+---------+-------+
2 rows in set
在表 test 中增加、删除列。
obclient> describe test;
+-------+----------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+----------+------+-----+---------+-------+
| c1 | int(11) | no | pri | null | |
| c3 | char(10) | yes | | null | |
+-------+----------+------+-----+---------+-------+
2 rows in set
obclient> alter table test add c4 int;
query ok, 0 rows affected
obclient> describe test;
+-------+----------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+----------+------+-----+---------+-------+
| c1 | int(11) | no | pri | null | |
| c3 | char(10) | yes | | null | |
| c4 | int(11) | yes | | null | |
+-------+----------+------+-----+---------+-------+
3 rows in set
obclient> alter table test drop c3;
query ok, 0 rows affected
obclient> describe test;
+-------+---------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+---------+------+-----+---------+-------+
| c1 | int(11) | no | pri | null | |
| c4 | int(11) | yes | | null | |
+-------+---------+------+-----+---------+-------+
2 rows in set
2.4 删除表
使用 drop table 语句删除表。
示例:删除表 test。
obclient> drop table test;
query ok, 0 rows affected
3. 索引操作
索引是创建在表上并对数据库表中一列或多列的值进行排序的一种结构。其作用主要在于提高查询的速度,降低数据库系统的性能开销。本节主要介绍数据库中索引的创建、查看、删除的语法和示例。
3.1 创建索引
使用 create index 语句创建表的索引。
示例:在表 test 中创建索引。
obclient> describe test;
+-------+----------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+----------+------+-----+---------+-------+
| c1 | int(11) | no | pri | null | |
| c2 | char(3) | yes | | null | |
+-------+----------+------+-----+---------+-------+
2 rows in set
obclient> create index test_index on test (c1, c2);
query ok, 0 rows affected
3.2 查看索引
使用 show index 语句查看表的索引。
示例:查看表 test 中的索引信息。
obclient> show index from test\g
*************************** 1. row ***************************
table: test
non_unique: 0
key_name: primary
seq_in_index: 1
column_name: c1
collation: a
cardinality: null
sub_part: null
packed: null
null:
index_type: btree
comment: available
index_comment:
visible: yes
*************************** 2. row ***************************
table: test
non_unique: 1
key_name: test_index
seq_in_index: 1
column_name: c1
collation: a
cardinality: null
sub_part: null
packed: null
null:
index_type: btree
comment: available
index_comment:
visible: yes
*************************** 3. row ***************************
table: test
non_unique: 1
key_name: test_index
seq_in_index: 2
column_name: c2
collation: a
cardinality: null
sub_part: null
packed: null
null: yes
index_type: btree
comment: available
index_comment:
visible: yes
3 rows in set
3.3 删除索引
使用 drop index 语句删除表的索引。
示例:删除表 test 中的索引。
obclient> drop index test_index on test;
query ok, 0 rows affected
4. 插入数据
使用 insert 语句在已经存在的表中插入数据。
示例:
创建表 t1 并插入一行数据。
obclient> create table t1(c1 int primary key, c2 int) partition by key(c1) partitions 4;
query ok, 0 rows affected
obclient> select * from t1;
empty set
obclient> insert t1 values(1,1);
query ok, 1 row affected
obclient> select * from t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
+----+------+
1 row in set
向表 t1 中插入多行数据。
obclient> insert t1 values(2,2),(3,default),(2+2,3*4);
query ok, 3 rows affected
records: 3 duplicates: 0 warnings: 0
obclient> select * from t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | null |
| 4 | 12 |
+----+------+
4 rows in set
更多 insert 语句相关的语法,请参见 insert 章节。
5. 删除数据
使用 delete 语句删除数据,支持单表删除和多表删除数据。
示例:
通过 create table 创建表 t2 和 t3。删除 c1=2 的行,其中 c1 列为表 t2 中的 primary key。
/表 t3
为 key
分区表,且分区名由系统根据分区命令规则自动生成,即分区名为 p0
、p1
、p2
、p3
/
obclient> create table t2(c1 int primary key, c2 int);
query ok, 0 rows affected
obclient> insert t2 values(1,1),(2,2),(3,3),(5,5);
query ok, 4 rows affected
records: 4 duplicates: 0 warnings: 0
obclient> select * from t2;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 5 | 5 |
+----+------+
4 rows in set
obclient> create table t3(c1 int primary key, c2 int) partition by key(c1) partitions 4;
query ok, 0 rows affected
obclient> insert into t3 values(5,5),(1,1),(2,2),(3,3);
query ok, 4 rows affected
records: 4 duplicates: 0 warnings: 0
obclient> select * from t3;
+----+------+
| c1 | c2 |
+----+------+
| 5 | 5 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
4 rows in set
obclient> delete from t2 where c1 = 2;
query ok, 1 row affected
obclient> select * from t2;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
| 3 | 3 |
| 5 | 5 |
+----+------+
3 rows in set
删除表 t2 中按照 c2 列排序之后的第一行数据。
obclient> delete from t2 order by c2 limit 1;
query ok, 1 row affected
obclient> select * from t2;
+----+------+
| c1 | c2 |
+----+------+
| 3 | 3 |
| 5 | 5 |
+----+------+
2 rows in set
删除表 t3 的 p2 分区的数据。
obclient> select * from t3 partition(p2);
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
3 rows in set
obclient> delete from t3 partition(p2);
query ok, 3 rows affected
obclient> select * from t3;
+----+------+
| c1 | c2 |
+----+------+
| 5 | 5 |
+----+------+
1 row in set
删除 t2、t3 表中 t2.c1 = t3.c1 的数据。
obclient> select * from t2;
+----+------+
| c1 | c2 |
+----+------+
| 3 | 3 |
| 5 | 5 |
+----+------+
2 rows in set
obclient> select * from t3;
+----+------+
| c1 | c2 |
+----+------+
| 5 | 5 |
+----+------+
obclient> delete t2, t3 from t2, t3 where t2.c1 = t3.c1;
query ok, 3 rows affected
/*等价于
obclient> delete from t2, t3 using t2, t3 where t2.c1 = t3.c1;
*/
obclient> select * from t2;
+----+------+
| c1 | c2 |
+----+------+
| 3 | 3 |
+----+------+
1 row in set
obclient> select * from t3;
empty set
6. 更新数据
使用 update 语句修改表中的字段值。
示例:
通过 create table 创建表 t4 和 t5,将表 t4 中 t4.c1=10 对应的那一行数据的 c2 列值修改为 100。
obclient> create table t4(c1 int primary key, c2 int);
query ok, 0 rows affected
obclient> insert t4 values(10,10),(20,20),(30,30),(40,40);
query ok, 4 rows affected
records: 4 duplicates: 0 warnings: 0
obclient> select * from t4;
+----+------+
| c1 | c2 |
+----+------+
| 10 | 10 |
| 20 | 20 |
| 30 | 30 |
| 40 | 40 |
+----+------+
4 rows in set
obclient> create table t5(c1 int primary key, c2 int) partition by key(c1) partitions 4;
query ok, 0 rows affected
obclient> insert t5 values(50,50),(10,10),(20,20),(30,30);
query ok, 4 rows affected
records: 4 duplicates: 0 warnings: 0
obclient> select * from t5;
+----+------+
| c1 | c2 |
+----+------+
| 20 | 20 |
| 10 | 10 |
| 50 | 50 |
| 30 | 30 |
+----+------+
4 rows in set
obclient> update t4 set t4.c2 = 100 where t4.c1 = 10;
query ok, 1 row affected
rows matched: 1 changed: 1 warnings: 0
obclient> select * from t4;
+----+------+
| c1 | c2 |
+----+------+
| 10 | 100 |
| 20 | 20 |
| 30 | 30 |
| 40 | 40 |
+----+------+
4 rows in set
将表 t4 中按照 c2 列排序的前两行数据的 c2 列值修改为 100。
obclient> update t4 set t4.c2 = 100 order by c2 limit 2;
query ok, 2 rows affected
rows matched: 2 changed: 2 warnings: 0
obclient> select * from t4;
+----+------+
| c1 | c2 |
+----+------+
| 10 | 100 |
| 20 | 100 |
| 30 | 100 |
| 40 | 40 |
+----+------+
4 rows in set
将表 t5 中 p1 分区的数据中 t5.c1 > 20 的对应行数据的 c2 列值修改为 100。
obclient> select * from t5 partition (p1);
+----+------+
| c1 | c2 |
+----+------+
| 10 | 10 |
| 50 | 50 |
+----+------+
2 rows in set
obclient> update t5 partition(p1) set t5.c2 = 100 where t5.c1 > 20;
query ok, 1 row affected
rows matched: 1 changed: 1 warnings: 0
obclient> select * from t5 partition(p1);
+----+------+
| c1 | c2 |
+----+------+
| 10 | 10 |
| 50 | 100 |
+----+------+
2 rows in set
对于表 t4 和表 t5 中满足 t4.c2 = t5.c2 对应行的数据,将表 t4 中的 c2 列值修改为 100,表 t5 中的 c2 列值修改为 200。
obclient> update t4,t5 set t4.c2 = 100, t5.c2 = 200 where t4.c2 = t5.c2;
query ok, 1 row affected
rows matched: 4 changed: 1 warnings: 0
obclient> select * from t4;
+----+------+
| c1 | c2 |
+----+------+
| 10 | 100 |
| 20 | 100 |
| 30 | 100 |
| 40 | 40 |
+----+------+
4 rows in set
obclient> select * from t5;
+----+------+
| c1 | c2 |
+----+------+
| 20 | 20 |
| 10 | 10 |
| 50 | 200 |
| 30 | 30 |
+----+------+
4 rows in set
7. 查询数据
使用 select 语句查询表中的内容。
示例:
通过 create table 创建表 t6。从表 t6 中读取 name 的数据。
obclient> create table t6 (id int, name varchar(50), num int);
query ok, 0 rows affected
obclient> insert into t6 values(1,'a',100),(2,'b',200),(3,'a',50);
query ok, 3 rows affected
records: 3 duplicates: 0 warnings: 0
obclient> select * from t6;
+------+------+------+
| id | name | num |
+------+------+------+
| 1 | a | 100 |
| 2 | b | 200 |
| 3 | a | 50 |
+------+------+------+
3 rows in set
obclient> select name from t6;
+------+
| name |
+------+
| a |
| b |
| a |
+------+
3 rows in set
在查询结果中对 name 进行去重处理。
obclient> select distinct name from t6;
+------+
| name |
+------+
| a |
| b |
+------+
2 rows in set
从表 t6 中根据筛选条件 name = ‘a’ ,输出对应的 id 、name 和 num。
obclient> select id, name, num from t6 where name = 'a';
+------+------+------+
| id | name | num |
+------+------+------+
| 1 | a | 100 |
| 3 | a | 50 |
+------+------+------+
2 rows in set
8. 提交事务
使用 commit 语句提交事务。
在提交事务(commit)之前:
- 您的修改只对当前会话可见,对其他数据库会话均不可见。
- 您的修改没有持久化,您可以通过 rollback 语句撤销修改。
在提交事务(commit)之后:
- 您的修改对所有数据库会话可见。
- 您的修改持久化成功,不能通过 rollback 语句回滚修改。
示例:通过 create table 创建表 t_insert。使用 commit 语句提交事务。
obclient> begin;
query ok, 0 rows affected
obclient> create table t_insert(
id number not null primary key,
name varchar(10) not null,
value number,
gmt_create datetime not null default current_timestamp
);
query ok, 0 rows affected
obclient> insert into t_insert(id, name, value, gmt_create) values(1,'cn',10001, current_timestamp),(2,'us',10002, current_timestamp),(3,'en',10003, current_timestamp);
query ok, 3 rows affected
records: 3 duplicates: 0 warnings: 0
obclient> select * from t_insert;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | cn | 10001 | 2022-08-22 16:19:26 |
| 2 | us | 10002 | 2022-08-22 16:19:26 |
| 3 | en | 10003 | 2022-08-22 16:19:26 |
+----+------+-------+---------------------+
3 rows in set
obclient> insert into t_insert(id,name) values(4,'jp');
query ok, 1 row affected
obclient> commit;
query ok, 0 rows affected
obclient> exit;
bye
obclient> obclient -h127.0.0.1 -ur**t@mysql -p2881 -p****** -ddb1
obclient> select * from t_insert;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | cn | 10001 | 2022-08-22 16:19:26 |
| 2 | us | 10002 | 2022-08-22 16:19:26 |
| 3 | en | 10003 | 2022-08-22 16:19:26 |
| 4 | jp | null | 2022-08-22 16:21:39 |
+----+------+-------+---------------------+
4 rows in set
9. 回滚事务
使用 rollback 语句回滚事务。
回滚一个事务指将事务的修改全部撤销。可以回滚当前整个未提交的事务,也可以回滚到事务中任意一个保存点。如果要回滚到某个保存点,必须结合使用 rollback 和 to savepoint 语句。 其中:
如果回滚整个事务,则:
-
事务会结束
-
所有的修改会被丢弃
-
清除所有保存点
-
释放事务持有的所有锁
如果回滚到某个保存点,则: -
事务不会结束
-
保存点之前的修改被保留,保存点之后的修改被丢弃
-
清除保存点之后的保存点(不包括保存点自身)
-
释放保存点之后事务持有的所有锁
示例:回滚事务的全部修改。
obclient> select * from t_insert;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | cn | 10001 | 2022-08-22 16:19:26 |
| 2 | us | 10002 | 2022-08-22 16:19:26 |
| 3 | en | 10003 | 2022-08-22 16:19:26 |
+----+------+-------+---------------------+
3 rows in set
obclient> begin;
query ok, 0 rows affected
obclient> insert into t_insert(id, name, value) values(4,'jp',10004),(5,'fr',10005),(6,'ru',10006);
query ok, 3 rows affected
records: 3 duplicates: 0 warnings: 0
obclient> select * from t_insert;
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | cn | 10001 | 2022-08-22 16:19:26 |
| 2 | us | 10002 | 2022-08-22 16:19:26 |
| 3 | en | 10003 | 2022-08-22 16:19:26 |
| 4 | jp | 10004 | 2022-08-22 16:26:23 |
| 5 | fr | 10005 | 2022-08-22 16:26:23 |
| 6 | ru | 10006 | 2022-08-22 16:26:23 |
+----+------+-------+---------------------+
6 rows in set
obclient> rollback;
query ok, 0 rows affected
+----+------+-------+---------------------+
| id | name | value | gmt_create |
+----+------+-------+---------------------+
| 1 | cn | 10001 | 2022-08-22 16:19:26 |
| 2 | us | 10002 | 2022-08-22 16:19:26 |
| 3 | en | 10003 | 2022-08-22 16:19:26 |
+----+------+-------+---------------------+
3 rows in set
发表评论