Home > database >  How to default a table column value with text and auto generated numbers
How to default a table column value with text and auto generated numbers

Time:08-12

create sequence test_sq;
create sequence test_sq1;

create table test
(
    col1 number default test_sq.nextval,
    col2 number default lpad('req-'||test_sq1.nextval,5,'0'),
    col3 varchar2(20)
);

I need to create a table for which col1 and col2 columns should be as per below

  • col1 should be auto generated - I will use test_sq.nextval for this
  • col2 should be auto generated with the format REQ-00001 and this column will have 5 digits only starting from REQ-. Like REQ-00001, REQ-00002,... , REQ-00025 etc

How can I achieve this?

DB Version: Oracle 19c

CodePudding user response:

Hm, not like that. col1 should then be an identity column (so you'd let Oracle create & use sequence it creates in the background. For col2, create a trigger.

Something like this:

SQL> CREATE SEQUENCE test_sq1;

Sequence created.

SQL> CREATE TABLE test
  2  (
  3     col1   NUMBER GENERATED ALWAYS AS IDENTITY,
  4     col2   VARCHAR2 (10),
  5     col3   VARCHAR2 (20)
  6  );

Table created.

SQL> CREATE OR REPLACE TRIGGER trg_bi_test
  2     BEFORE INSERT
  3     ON test
  4     FOR EACH ROW
  5  BEGIN
  6     :new.col2 := 'req-' || LPAD (test_sq1.NEXTVAL, 5, '0');
  7  END;
  8  /

Trigger created.

Testing:

SQL> INSERT INTO test (col3) VALUES ('Little');

1 row created.

SQL> INSERT INTO test (col3) VALUES ('Foot');

1 row created.

SQL> SELECT * FROM test;

      COL1 COL2       COL3
---------- ---------- --------------------
         1 req-00001  Little
         2 req-00002  Foot

SQL>

If you'd still like to use your own sequence for col1, you can - don't generate it as identity and add yet another line into the trigger:

SQL> CREATE SEQUENCE test_sq;

Sequence created.

SQL> CREATE SEQUENCE test_sq1;

Sequence created.

SQL> CREATE TABLE test
  2  (
  3     col1   NUMBER,
  4     col2   VARCHAR2 (10),
  5     col3   VARCHAR2 (20)
  6  );

Table created.

SQL> CREATE OR REPLACE TRIGGER trg_bi_test
  2     BEFORE INSERT
  3     ON test
  4     FOR EACH ROW
  5  BEGIN
  6     :new.col1 := test_sq.NEXTVAL;
  7     :new.col2 := 'req-' || LPAD (test_sq1.NEXTVAL, 5, '0');
  8  END;
  9  /

Trigger created.

Testing:

SQL> INSERT INTO test (col3) VALUES ('Little');

1 row created.

SQL> INSERT INTO test (col3) VALUES ('Foot');

1 row created.

SQL> SELECT * FROM test;

      COL1 COL2       COL3
---------- ---------- --------------------
         1 req-00001  Little
         2 req-00002  Foot

SQL>

Without a trigger:

SQL> CREATE TABLE test
  2  (
  3     col1   NUMBER DEFAULT test_sq.NEXTVAL,
  4     col2   VARCHAR2 (30) DEFAULT 'req-' || LPAD (test_sq1.NEXTVAL, 5, '0'),
  5     col3   VARCHAR2 (20)
  6  );

Table created.

SQL> INSERT INTO test (col3) VALUES ('Little');

1 row created.

SQL> INSERT INTO test (col3) VALUES ('Foot');

1 row created.

SQL> SELECT * FROM test;

      COL1 COL2       COL3
---------- ---------- --------------------
         8 req-00008  Little
         9 req-00009  Foot

SQL>
  • Related