Oracle Virtual Column with Regex & Primary Key

I wrote this trivial piece of test code.

I wanted it to work.

It is Oracle. I am surprised it worked.

No triggers, stored procedures, packages, or functions.

I’m not sure what else I can say.

SQL>
SQL>
SQL>
SQL>
SQL> create table tblTest(
 2                          fullName varchar2(40),
 3                          osUser varchar2(40)
 4                            generated always as (upper(regexp_substr(fullName, '\w*$')))
 5                             primary key
 6                       );
Table created.

SQL>
SQL>
SQL> insert into tblTest(fullName)
2       values('domain.com\Roger');

1 row created.

SQL>
SQL> insert into tblTest(fullName)
2       values('Rick');

1 row created.

SQL>
SQL> insert into tblTest(fullName)
2       values('domain.co.uk\Nick');

1 row created.

SQL>
SQL> insert into tblTest(fullName)
2       values('domain.com\David');

1 row created.

SQL>
SQL> insert into tblTest(fullName)
2       values('domain.com\david');
insert into tblTest(fullName)
*
ERROR at line 1:
ORA-00001: unique constraint (MDO.SYS_C007576) violated
SQL>
SQL> select *
2      from tblTest;

FULLNAME                       OSUSER
------------------------------ ------------------------------
domain.com\Roger               ROGER
Rick                           RICK
domain.co.uk\Nick              NICK
domain.com\David               DAVID

SQL>
SQL> select * from v$version;

BANNER                                                                           CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production    0
PL/SQL Release 12.1.0.2.0 - Production                                           0
CORE 12.1.0.2.0 Production                                                       0
TNS for Solaris: Version 12.1.0.2.0 - Production                                 0
NLSRTL Version 12.1.0.2.0 - Production                                           0

— Published by Mike, 11:21 01 January 2017

Leave a Reply