Oracle12c新特性之自增列的实现

一.Mysql自增列众所周知,mysql有自增列功能。Mysql中每张表只能有一个自增列,并且自增列必须是主键或者唯一键。...
摘要由CSDN通过智能技术生成

一. Mysql 自增列

众所周知, mysql 有自增列功能。

Mysql 中每张表只能有一个自增列,并且自增列必须是主键或者唯一键。

mysql> create table test2(id int not   null auto_increment);

ERROR 1075 (42000): Incorrect table   definition; there can be only one auto column and it must be defined as a key

mysql> create table test2(id int not   null auto_increment primary key);

Query OK, 0 rows affected (0.01 sec)

 

Mysql 插入空值,可以看到是允许的,默认会用自增列值插入。

mysql> insert into test2   values();    

Query OK, 1 row affected (0.01 sec)

 

mysql> select * from test2;

+----+

| id |

+----+

|    1 |

+----+

1 row in set (0.00 sec)

 

Mysql 也是允许插入的值不连续

mysql> insert into test2 values(3);

Query OK, 1 row affected (0.00 sec)

 

mysql> select * from test2;       

+----+

| id |

+----+

|    1 |

|    3 |

+----+

2 rows in set (0.00 sec)

 

表加一列,显式插入新增列,自增列会隐式地从当前最大值自增。

mysql> insert into test2(b) values(4);

Query OK, 1 row affected (0.00 sec)

 

mysql> select * from test2;

+----+------+

| id | b    |

+----+------+

|    1 | NULL |

|    3 | NULL |

|    4 |    4 |

+----+------+

3 rows in set (0.00 sec)

 

二. Oracle 自增列

Oracle 12c 之前的版本,自增列功能只能通过序列 + 触发器的方式实现。 12c 版本终于迎来了这个新功能。

 

自增列语法

 

建表

SQL> CREATE TABLE test1 (

    2  id int GENERATED ALWAYS AS IDENTITY

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值