obclient -h192.168.3.14 -P2881 -uroot@sys -p'xxx'
show databases;
use fengmysql
-- 创建表时定义自增列(默认 ORDER 模式)
CREATE TABLE t_user (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
) AUTO_INCREMENT = 1;
-- 显式指定 NOORDER 模式
CREATE TABLE t_order (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
order_no VARCHAR(20)
) AUTO_INCREMENT_MODE = 'NOORDER';
执行截图:
此时,已创建 t_user,t_order 两张表
2)插入实验
复制代码
INSERT INTO t_user (name) VALUES ('A'),('B'),('C');
select * from t_user;
commit;
-------
INSERT INTO t_order (order_no) VALUES('order1'),('order2'),('order3');
select * from t_order ;
commit;
截图:
发现,都从1开始增长。再插入不同的条数,观察:
3)再插入不同条数
复制代码
INSERT INTO t_order (order_no) VALUES('ord1'),('ord2');
INSERT INTO t_user (name) VALUES ('Ad');
select * from t_user;
select * from t_order;
commit;
截图:
4)删除某行,看是否连续
复制代码
delete from t_order where id=5;
commit;
select * from t_order;
--删除后,再插入1条
insert into t_order(order_no) values(222);
commit;
select * from t_order;
--再插入2条
INSERT INTO t_order (order_no) VALUES('ord2'),('ord4');
select * from t_order;
截图:
看到删除的那条就没有了,序号依次往下排,从6开始。
5)truncate 再观察 1开始
复制代码
truncate table t_order;
insert into t_order(order_no) values('new1'),('new2');
commit;
select * from t_order;
截图:
发现 truncate 表之后,自增序列重新开始。
2.2 实验:oracle模式下 自增序列
1)切换背景
oracle 数据库中建表t_log, 可以实现自增模式:
复制代码
create table T_LOG
(
logid NUMBER generated always as identity (start with 1 maxvalue 9999999999999 cycle order),
message VARCHAR2(400)
)
如何切换oceanbase数据库呢?
2)oracle的语法测试
复制代码
obclient -h192.168.3.14 -P2881 -ufeng@fengoracle -p'xxx'
select user_name from user_tables;
create table T_LOG
(
logid NUMBER generated always as identity (start with 1 maxvalue 9999999999999 cycle order),
message VARCHAR2(400)
);
---改为 ok
create table T_LOG ( logid NUMBER , message VARCHAR2(400) );
截图:发现 直接用oracle创建语句,不可以
3)测试 auto_increment, 不可
复制代码
create table T_LOG1 (
logid NUMBER not null AUTO_INCREMENT,
message VARCHAR2(400)
);
CREATE SEQUENCE sequence_name
[
MINVALUE value -- 序列最小值
MAXVALUE value -- 序列最大值
START WITH value -- 序列起始值
INCREMENT BY value -- 序列增长值
CACHE cache -- 序列缓存个数
CYCLE | NOCYCLE -- 序列循环或不循环
]
--语句
CREATE TABLE T_SEQ(
ID NUMBER NOT NULL PRIMARY KEY,
NAME VARCHAR2(400));
CREATE SEQUENCE seq_t_seq START WITH 1 INCREMENT BY 1;
insert into t_seq(id,name) values(seq_t_seq.nextval,'xiaohua'),(seq_t_seq.nextval,'xiaoming');
截图:
5)类oracle自增方式 generated by default
复制代码
--oracle下:
create table T_LOG
(
logid NUMBER generated always as identity (start with 1 maxvalue 9999999999999 cycle order),
message VARCHAR2(400)
)
--oceanbase oracle模式下:
create table T_LOG(
id number generated by default as identity maxvalue 21 start with 1 cycle order,
name varchar2(400)
);
截图: ok 创建成功。
注意:如果设置cycle 循环,且没有指定 cache 则maxvalue 必须大于一个 one cycle 默认20
否则 提示 OBE-04013: number to CACHE must be less than one cycle
指定cache也可以,建表语句修改如下:
复制代码
CREATE SEQUENCE seq1 START WITH 1 MINVALUE 1 MAXVALUE 5 INCREMENT BY 2 CYCLE CACHE 2;
--oceanbase oracle模式下:
create table T_LOG1(
id number generated by default as identity maxvalue 4 start with 1 cycle order cache 3,
name varchar2(400)
);
截图: generated by default as identity maxvalue x start with 1 cycle order cache 3
插入t_log1 (最大4,循环)
复制代码
insert into t_log1(name) values('hello1'),('hello3'),('hello4'),('hello_hell');
insert into t_log1(name) values('oo1'),('oo2'),('003'),('oo4');
select * from t_log1;
截图:
发现id自增,且循环。ok
6)创建表时 字段默认序列
创建表,字段默认值为序列的值
或者
后面改为默认值为序列值也可以
复制代码
create table t_log3(
id number default seq_t_seq.nextval,
name varchar2(400)
);
insert into t_log3(name) values('小花'),('小孩'),('小东');
或者:
create table t_log2(id number,name varchar2(400));
alter table t_log2 modify id DEFAULT seq_t_seq.nextval;
insert into t_log2(name) values('小花2'),('小孩2'),('小东2');