Oracle12c Learning Series-identitycolumn

Source: Internet
Author: User
In addition to the reprinted content, this article is original site: reposted from lovewifelovelife-Roger's Oracle technology blog this article URL: Oracle12c Study Series identitycolumn before 12c, if you want to achieve automatic column growth, it is usually achieved through sequence. However, this problem has been solved in 12c, Oracl

In addition to the reprinted content, this article is original site: reposted from love wife love life-Roger's Oracle technology blog this article link address: Oracle 12c Learning Series of identity column before 12c, if you want to achieve automatic column growth, it is usually achieved through sequence. However, this problem has been solved in 12c, Oracl

In addition to the reposted content, this site is original:Reposted from the Oracle technology blog of love wife & love life-Roger

Link:Oracle 12c Learning Series-identity column

Before 12c, if you want to achieve automatic column growth, it is usually achieved through sequence. However, this problem has been solved in 12c, and Oracle has postponed the identity column function, this function perfectly solves previous problems. The following is my simple test for your reference!

SQL> alter pluggable database all open;Pluggable database altered.SQL> select name,open_mode from V$pdbs;NAME                           OPEN_MODE------------------------------ ----------PDB$SEED                       READ ONLYPDBORCL                        READ WRITESQL> conn roger/roger@pdborclConnected.SQL> show con_nameCON_NAME------------------------------PDBORCLSQL> create table test(id number generated by default as identity ,  2  name varchar2(20));Table created.SQL> insert into test(name) values('roger');1 row created.SQL> insert into test(name) values('killdb.com');1 row created.SQL> commit;Commit complete.SQL> select * from test;        ID NAME---------- --------------------         1 roger         2 killdb.comSQL>SQL> insert into test(id,name) values(null,'killdb.com');insert into test(id,name) values(null,'killdb.com')                                 *ERROR at line 1:ORA-01400: cannot insert NULL into ("ROGER"."TEST"."ID")

We can see that the id column can automatically grow by default. Before 12c, Oracle can only achieve this function through sequence. In addition, we can also see that in this case, null values cannot be inserted.

SQL> alter table test modify (id default null); alter table test modify (id default null)                          *ERROR at line 1:ORA-30674: identity column cannot have a default value

So for the column of identity, is it true that the null value cannot be inserted? Actually, it works, but you have to do this:

SQL> create table test1 (id number generated by default on NULL as identity ,name varchar2(20));Table created.SQL>SQL> insert into test1 values(1,'killdb.com');1 row created.SQL> insert into test1 values(2,'baidu.com');1 row created.SQL> insert into test1 values(null,'google.com');1 row created.SQL> commit;Commit complete.SQL> select * from test1;        ID NAME---------- --------------------         1 killdb.com         2 baidu.com         1 google.com

However, we can see that when the id column of the inserted 3rd pieces of data is null, the actual oracle value is 1 .? How did this 1 come from? The answer will be provided later.

SQL> update test1 set id=100 where id=2;1 row updated.SQL> commit;Commit complete.SQL> select * from test1;        ID NAME---------- --------------------         1 killdb.com       100 baidu.com         1 google.com

In this case, you can perform a normal dml. Because identity column is null by default, the following test continues.

SQL> create table test2 (id number generated always as identity  ,name varchar2(20));Table created.SQL> insert into test2(name) values('killdb.com');1 row created.SQL> insert into test2(name) values('baidu.com');1 row created.SQL> insert into test2(name) values('google.com');1 row created.SQL> commit;Commit complete.SQL> select * from test2;        ID NAME---------- --------------------         1 killdb.com         2 baidu.com         3 google.comSQL>  update test2 set id=4  where id=2; update test2 set id=4  where id=2                  *ERROR at line 1:ORA-32796: cannot update a generated always identity columnSQL> update test2 set id=1 where id=2;update test2 set id=1 where id=2                 *ERROR at line 1:ORA-32796: cannot update a generated always identity columnSQL> delete from test2 where id=2;1 row deleted.SQL> commit;Commit complete.SQL> select * from test2;        ID NAME---------- --------------------         1 killdb.com         3 google.com

We can see that the update is not allowed. Here we should know that Oracle won't allow you to perform the update.

SQL>SQL> select TABLE_NAME,  2         COLUMN_NAME,  3         DEFAULT_ON_NULL,  4         IDENTITY_COLUMN,  5         DATA_DEFAULT  6    from user_tab_columns  7    where IDENTITY_COLUMN='YES';TABLE_NAME           COLUMN_NAME          DEF IDE DATA_DEFAULT-------------------- -------------------- --- --- ------------------------------------------------------------TEST                 ID                   NO  YES "ROGER"."ISEQ$$_91820".nextvalTEST1                ID                   YES YES "ROGER"."ISEQ$$_91822".nextvalTEST2                ID                   NO  YES "ROGER"."ISEQ$$_91824".nextvalSQL> SQL> set pagesize 200 long 9999SQL> select dbms_metadata.get_ddl('TABLE','TEST') from dual;DBMS_METADATA.GET_DDL('TABLE','TEST')--------------------------------------------------------------------------------  CREATE TABLE "ROGER"."TEST"   (    "ID" NUMBER GENERATED BY DEFAULT AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER  NOCYCLE  NOT NULL ENABLE,        "NAME" VARCHAR2(20)   ) SEGMENT CREATION IMMEDIATE  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)  TABLESPACE "USERS"SQL> select dbms_metadata.get_ddl('TABLE','TEST1') from dual;DBMS_METADATA.GET_DDL('TABLE','TEST1')--------------------------------------------------------------------------------  CREATE TABLE "ROGER"."TEST1"   (    "ID" NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER  NOCYCLE  NOT NULL ENABLE,        "NAME" VARCHAR2(20)   ) SEGMENT CREATION IMMEDIATE  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)  TABLESPACE "USERS"

Finally, we can see from the table definition that the ID column is defined as sequence by default. The attribute is similar to the attribute created in our previous version. By default, start with is 1. This is why id = 1 is inserted with null.

Related posts:

  1. About partiton column with date or varchar2?
In addition to the reprinted content, this article is original site: reposted from the Oracle technology blog of love wife & love life-Roger. Link: Oracle 12c Learning Series-identity column before 12c, if you want to achieve automatic column growth, it is usually achieved through sequence. However, this problem has been solved in 12c, and Oracle has postponed the identity column function, this function perfectly solves previous problems. The following is my simple test for your reference! SQL> alter pluggable database all open; Pluggable database altered. SQL> select name, open_mode from V $ pdbs; NAME OPEN_MODE ------------------------------ ---------- PDB $ seed read only pdborcl read write SQL> conn roger/roger @ pdborcl Connected. SQL> show con_name CON_NAME ---------------------------- pdborcl SQL> create table test (id number generated by [...]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.