コンパイル方法
-c -> コンパイルを行う。-w -> -cとセットで使うことでファイルを保存するたびに自動でコンパイルを行うことが出来る
-- 常に生成した値を設定する(明示的に値を設定しようとするとエラーとなる) id number generated as identity not null -- generatedの後にalwaysを指定すると、常に生成した値を使用する -- デフォルト動作なので、上のオプション無しと同じ動きとなる id number generated always as identity not null -- by defaultを指定すると値の設定がない場合のみ、生成した値が設定されます。 -- on nullをby defaultの後に設定すると、挿入(更新)対象の値がnullの場合にも、生成した値が設定されます。 id number generated by default as identity not null
id number generated as identity( minvalue 100 maxvalue 999 cycle ) not null,
-- テーブル定義
create table test_table
(
  id number generated as identity(
  minvalue 100
  maxvalue 999
  cycle
  ) not null,
  name NVARCHAR2 (100) not null,
  primary key (id)
);
-- INSERTの実行結果
SQL> insert into test_table (name) values ('なまえ');
1 row created.
SQL> select id from test_table;
 ID
----------
       100
SQL> insert into test_table (name) values ('なまえ');
1 row created.
SQL> select id from test_table;
 ID
----------
       100
       101
SQL> insert into test_table (id, name) values (999, 'name');
insert into test_table (id, name) values (999, 'name')
                        *
ERROR at line 1:
ORA-32795: cannot insert into a generated always identity column
create table on_null_test ( id number not null, char_col char(1) default on null '0', -- nullの場合は、「0」を代入する number_col number default on null 100, -- nullの場合は、「100」を代入する primary key (id) );
-- 主キー以外は指定しない INSERT INTO ON_NULL_TEST (id) values (1); -- nullを代入 INSERT INTO ON_NULL_TEST (id, char_col, number_col) values (2, null, null); -- null以外を代入 INSERT INTO ON_NULL_TEST (id, char_col, number_col) values (3, '1', 999);
ID CHAR_COL NUMBER_COL 1 0 100 2 0 100 3 1 999