Oracle Create Sequence

Oracle’da otomatik olarak Primary Key ( Birincil Anahtar ) oluşturmak için Sequence objesinden faydalanıyoruz. Microsoft SQL Server’daki “IDENTITY(1,1)” Oracle karşılığı Sequence’dir veya 12c ile gelen “GENERATED BY DEFAULT ON NULL AS IDENTITY” dir. 2 kullanımı da örneklerle aşağıda göstereceğim.

We can use Sequence to generate Primary Key (PK) automatically. In Microsoft SQL Server “IDENTITY(1,1)” refers to Sequence or “GENERATED BY DEFAULT ON NULL AS IDENTITY” that is released in Oracle 12c. I will demonstrate below examples.

Geleneksel Yol (conventional way) :

--DROP TABLE t_test_table;
CREATE TABLE t_test_table 
(
    id    NUMBER,
    ack   VARCHAR2(100) NOT NULL
);

--DROP SEQUENCE seq_id_for_test_table;
CREATE SEQUENCE seq_id_for_test_table
  START WITH 1
  MAXVALUE 999999
  INCREMENT BY 1
  CACHE 20
  NOCYCLE
  NOORDER;

INSERT INTO t_test_table (id,ack) VALUES ( seq_id_for_test_table_m.NEXTVAL,'a');
INSERT INTO t_test_table (id,ack) VALUES ( seq_id_for_test_table_m.NEXTVAL,'b');
INSERT INTO t_test_table (id,ack) VALUES ( seq_id_for_test_table_m.NEXTVAL,'c');
COMMIT;

SELECT * FROM t_test_table;
/* sonuç (result):
ID    ACK
 1    a
 2    b
 3    c
*/

START WITH 1 : başlangıç değeri. (starting value).
MAXVALUE 999999 : Maksimum alacağı değer. Bu satır olmassa Default değeri “9999999999999999999999999999” dur. ( It is the maximum value. If we don’t specify this value, default value will be “9999999999999999999999999999”
INCREMENT BY 1 : Birer birer artacağı anlamına gelir ( The value will increase one by one. )
CACHE 20 : Oracle SGA alanındaki cache’lenen adet. MAXVALUE’dan büyük olamaz. Database shutdown olursa, cache’deki değerler kaybolur. Bu sebeple default değeri olan 20’yi değiştirmedim. Veri ambarı gibi çok fazla insert alan ortamlarda, performansı iyileştirmek için bu değer arttırılabilir. (Number of the cached value in SGA. The value can not be bigger than MAXVALUE. Cache value. If the database shut down, cached values will be lost. This is why I did’nt change the default value which is 20. In datawarehouse environment this value can be increased for performans. As you know in DWH environment, there are lots of bulk insert operation. )
NOCYCLE : Primary Key değerimiz maxvalue değerine ulaştıktan sonra; CYCLE olursa tekrar aynı değerleri üretir. Bu da duplike primary key problemine yol açabilir. Duplike sorunu ile uğraşmamak için, bu alanı default değer olan NOCYCLE olarak seçili bırakıyorum. ( If we set this option as CYCLE and the primary key value reach maxvalue, the key value will start from beginning. It may cause duplicate key value. So I prefer to set NOCYCLE which is default option to avoid duplicate key value. )
NOORDER : ORDER seçilirse, değerlerin sıralı olmasını garanti eder. Default olan değer NOORDER değeri olduğunda, cache’deki bir değeri alabilir. Yani sıralı olmayabilir. ( If you choose ORDER option, Oracle guarantees that values will be ordered. If you choose NOORDER property, number order might be random between cache values. )

12c ile gelen alternatif yeni yöntem ( the new alternative way which is released in 12c ):

--DROP TABLE t_test_table_12c;
CREATE TABLE t_test_table_12c 
(
    id   NUMBER  GENERATED ALWAYS AS IDENTITY
                 START WITH 1
                 INCREMENT BY 1
                 CACHE 20,
    ack  VARCHAR2(100) NOT NULL
);

INSERT INTO t_test_table_12c (ack) VALUES ('a');
INSERT INTO t_test_table_12c (ack) VALUES ('b');
INSERT INTO t_test_table_12c (ack) VALUES ('c');

COMMIT;

SELECT * FROM t_test_table_12c;
/* sonuç (result):
ID    ACK
 1    a
 2    b
 3    c
*/

GENERATED BY DEFAULT ON NULL : ID sütununa null değer atmak istediğimizde, kendisi sıradaki değeri verir. Ama tabloya öncesinde elle verilen bir değer var ise, ID alanı çoklayabilir. Bu sebeple benim çok tercih ettiğim bir seçenek değil. (When you insert “NULL” to ID column, PK number automatically generates next value. If we insert PK value manually, ID column might be duplicated. So I don’t prefer this option. )
GENERATED ALWAYS : ID sütununa null ataması bile yapsanız hata verir. Kendi otomatik olarak değerleri oluşturur. ( If you specify a value even if it is NULL, it will raise an error. With this option, key numbers are generated automatically.
START WITH, INCREMENT BY, CACHE : özelliklerine yukarda değindiğim için oraya bakabilirsiniz. (I have already explained these options at conventional PK generating way. So I won’t rewrite it.

Leave a Reply

Your email address will not be published. Required fields are marked *