Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Oracle:keunikan selektif indeks berbasis fungsi

Anda akan membutuhkan sesuatu seperti

CREATE UNIQUE INDEX fn_unique_idx 
    ON table1 (CASE WHEN is_deleted='N' THEN id ELSE null END,
               CASE WHEN is_deleted='N' THEN name ELSE null END,
               CASE WHEN is_deleted='N' THEN type ELSE null END);

Contoh kendala dalam tindakan

SQL> create table table1 (
  2    id number,
  3    name varchar2(10),
  4    type varchar2(10),
  5    is_deleted varchar2(1)
  6  );

Table created.

SQL> CREATE UNIQUE INDEX fn_unique_idx
  2      ON table1 (CASE WHEN is_deleted='N' THEN id ELSE null END,
  3                 CASE WHEN is_deleted='N' THEN name ELSE null END,
  4                 CASE WHEN is_deleted='N' THEN type ELSE null END);

Index created.

SQL> insert into table1 values( 1, 'Foo', 'Bar', 'N' );

1 row created.

SQL> insert into table1 values( 1, 'Foo', 'Bar', 'Y' );

1 row created.

SQL> insert into table1 values( 1, 'Foo', 'Bar', 'Y' );

1 row created.

SQL> insert into table1 values( 1, 'Foo', 'Bar', 'N' );
insert into table1 values( 1, 'Foo', 'Bar', 'N' )
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.FN_UNIQUE_IDX) violated


SQL> insert into table1 values( 1, 'Foo', 'Zee', 'N' );

1 row created.


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Jumlah hari Jumat di antara dua tanggal

  2. 50 Nuansa Ujian Sertifikasi Database Oracle

  3. Permintaan parameter dalam masalah Oracle

  4. Contoh Oracle FOR LOOP REVERSE

  5. Cara menanyakan kolom CLOB di Oracle