Generate Alphanumber field and Increment - oracle

i have a transaction form in oracle apex within that form has a page item called transactioncode.
what i will like, is to generate a alphanumeric code on page load for eg. AA110 which will increment everytime they create a new transaction.
table: transactioncode, transactiondate, productcode, productname.
I have not tried any sql or pl/sql or trigger

You can use a SEQUENCE and assign the sequence.nextval to the field transactioncode or define the field as IDENTITY if you are using 12.1 onwards. If your version is prior to 12.1, then you need a trigger to associate the sequence.
Example below will show you column c1 as identity, column c2 using sequence
SQL> create sequence my_example start with 1000 increment by 1 maxvalue 9999999999999999 ;
Sequence created.
SQL> create table my_test ( c1 NUMBER GENERATED BY DEFAULT AS IDENTITY , c2 number default my_example.nextval , c3 varchar2(1) ) ;
Table created.
SQL> insert into my_test ( c3 ) values ( 1 ) ;
1 row created.
SQL> commit;
Commit complete.
SQL> select * from my_test ;
C1 C2 C
---------- ---------- -
1 1000 1
The following restrictions applied to IDENTITY fields
One identity column per table.
Identity columns must be numeric types, and can't be user-defined
data types.
Identity columns can't have a default clause.
Identity columns are implicitly have NOT NULL and NOT DEFERRABLE constraints.
They can't be explicitly alter to anything else. From the doc, "If an
identity column is encrypted, then the encryption algorithm may be
inferred. Oracle recommends that you use a strong encryption
algorithm on identity columns."
The CREATE TABLE ... AS SELECT will not inherit the identity property on a column. This is true for several structural definitions. If you care about structure, you
should always CREATE TABLE, then use INSERT INTO ... SELECT to
populate it.

Related

Is there any way to perform md5 hashing in oracle sql developer on every field of an column, and store the result hash in corresponding column(md5)

select standard_hash(‘text’,’MD5’) from dual.
The above script provides md5 hashing in oracle sql developer
But i want to do hashing on my table column and store the result in corresponding field.
I have also tried:
INSERT INTO TABLENAME(MD5)
SELECT standard_hash(TEXT,’MD5’) from TABLENAME
but it is giving error: ORA-01400 cannot insert into null
Normally, the standard_hash will give you an output which you perhaps can't store in the same column you had, because the length won't be the same. To avoid this, I recommend you use raw as data type for the column where you want to store the hash value.
MD5 processes an arbitrary-length message into a fixed-length output of 128 bits, typically represented as a sequence of 32 hexadecimal digits.
SQL> create table test_normal ( c1 varchar2(20) ) ;
Table created.
SQL> insert into test_normal values ( 'AbdiRSDAloPdi8978' ) ;
1 row created.
SQL> select standard_hash(c1) from test_normal ;
STANDARD_HASH(C1)
----------------------------------------
BD6062214847DB31BB40A0185E3F89B0FB6980BF
SQL> alter table test_normal add ( c2 raw(256) );
Table altered.
SQL> update test_normal set c2=standard_hash(c1) ;
1 row updated.
SQL> select * from test_normal ;
C1
--------------------
C2
--------------------------------------------------------------------------------
AbdiRSDAloPdi8978
BD6062214847DB31BB40A0185E3F89B0FB6980BF
SQL> select standard_hash(c1,'MD5') from test_normal ;
STANDARD_HASH(C1,'MD5')
--------------------------------
E404D2867EB21AD65827E2858A07CA65

How to increment the value of the unique constraint column value in ORACLE

How to increment the value of the unique constraint column value in ORACLE, in the select statement.
For example, in a table 'BILLING_TABLE' - column BLNG_Sk is the unique key (Autoincremented).
So while inserting a new record into the BILLING_TABLE, for the column BLNG_SK we need to give the value (Which is the increment by 1 from the present max value.)
For example, if BLNG_SK max value is 12321.
new record should be 12322.
how to achieve this in Oracle?
Oracle has a SEQUENCE object which provides the functionality you require.
You create one using the CREATE SEQUENCE SQL statement.
The Oracle documentation provides all the required information and the documentation is available via Oracle's Web site.
Assuming you are on Oracle 12.1 or later, define it as an identity column and do not pass any value when inserting:
create table testtable
( test_id number generated always as identity
constraint testtable_pk primary key
, othercol varchar2(10) );
insert into testtable (othercol) values ('Demo');
select * from testtable;
TEST_ID OTHERCOL
---------- ----------
1 Demo
insert into testtable (othercol) values ('Demo #2');
select * from testtable;
TEST_ID OTHERCOL
---------- ----------
1 Demo
2 Demo #2
Try creating a sequence and a trigger. This is the case when you provide the value manually.
CREATE SEQUENCE dept_seq START WITH 12322;
Trigger definition:
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON BILLING_TABLE
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
/

Data Type issue in one of the filelds in Table

One of my field has Data Type as Numeric and the size is (16,8). If I give the value as 0.000000012 will it take and load it in the table? As there are 9 places after the decimal.
It will accept and round the values (at least in 11g):
SQL> CREATE TABLE tst (c1 NUMBER(10,2));
Table created
SQL> INSERT INTO tst VALUES (9.123);
1 row inserted
SQL> INSERT INTO tst VALUES (9.129);
1 row inserted
SQL> SELECT * FROM tst;
C1
------------
9.12
9.13

How to generate alphanumeric id in Oracle

In my vb application I want an autogenerated id of alphanumeric characters, like prd100. How can I increment it using Oracle as backend?
Any particular reason it needs to be alphanumeric? If it can just be a number, you can use an Oracle sequence.
But if you want just a random string, you could use the dbms_random function.
select dbms_random.string('U', 20) str from dual;
So you could probably combine these 2 ideas (in the code below, the sequence is called oid_seq):
SELECT dbms_random.string('U', 20) || '_' || to_char(oid_seq.nextval) FROM dual
There are two parts to your question. The first is how to create an alphanumeric key. The second is how to get the generated value.
So the first step is to determine the source of the alpha and the numeric components. In the following example I use the USER function and an Oracle sequence, but you will have your own rules. I put the code to assemble the key in a trigger which is called whenever a row is inserted.
SQL> create table t1 (pk_col varchar2(10) not null, create_date date)
2 /
Table created.
SQL> create or replace trigger t1_bir before insert on t1 for each row
2 declare
3 n pls_integer;
4 begin
5 select my_seq.nextval
6 into n
7 from dual;
8 :new.pk_col := user||trim(to_char(n));
9 end;
10 /
Trigger created.
SQL>
The second step requires using the RETURNING INTO clause to retrieve the generated key. I am using SQL*PLus for this example. I confess to having no idea how to wire this syntax into VB. Sorry.
SQL> var new_pk varchar2(10)
SQL> insert into t1 (create_date)
2 values (sysdate)
3 returning pk_col into :new_pk
4 /
1 row created.
SQL> print new_pk
NEW_PK
--------------------------------
APC61
SQL>
Finally, a word of warning.
Alphanumeric keys are a suspicious construct. They reek of "smart keys" which are, in fact, dumb. A smart key is a value which contains multiple parts. At somepoint you will find yourself wanting to retrieving all rows where the key starts with 'PRD', which means using SUBSTR() or LIKE. Even worse someday the definition of the smart key will change and you will have to cascade a complicated update to your table and its referencing foreign keys. A better ides is to use a surrogate key (number) and have the alphanumeric "key" defined as separate columns with a UNIQUE composite constraint to enforce the business rule.
SQL> create table t1 (id number not null
2 , alpha_bit varchar2(3) not null
3 , numeric_bit number not null
4 , create_date date
5 , constraint t1_pk primary key (id)
6 , constraint t1_uk unique (alpha_bit, numeric_bit)
7 )
8 /
Table created.
SQL>

Setting the primary key of a object type table in Oracle

I have two Oracle questions.
How can I set the primary key of a table when the table is made up of an object type? e.g.
CREATE TABLE object_names OF object_type
I have created a Varray type,
CREATE TYPE MULTI_TAG AS VARRAY(10) OF VARCHAR(10);
but when I try to do
SELECT p.tags.count FROM pg_photos p;
I get an invalid identifier error on the "count" part. p.tags is a MULTI_TAG, how can I get the number of elements in the MULTI_TAG?
First of all I wouldn't recommend storing data in Object tables. Objects are a great programmatic tool but querying Object tables leads to complicated SQL. I would advise storing your data in a standard relationnal model and using the objects in your procedures.
Now to answer your questions:
A primary key should be immutable, so most of the time an Object type is inappropriate for a primary key. You should define a surrogate key to reference your object.
You will have to convert the varray into a table to be able to query it from SQL
For example:
SQL> CREATE TYPE MULTI_TAG AS VARRAY(10) OF VARCHAR(10);
2 /
Type created
SQL> CREATE TABLE pg_photos (ID number, tags multi_tag);
Table created
SQL> INSERT INTO pg_photos VALUES (1, multi_tag('a','b','c'));
1 row inserted
SQL> INSERT INTO pg_photos VALUES (2, multi_tag('e','f','g'));
1 row inserted
SQL> SELECT p.id, COUNT(*)
2 FROM pg_photos p
3 CROSS JOIN TABLE(p.tags)
4 GROUP BY p.id;
ID COUNT(*)
---------- ----------
1 3
2 3
1)
A primary key is a constraint, to add constrains on object tables check this link:
http://download-west.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjdes.htm#i452285
2)
The COUNT method can't be used in a SQL statement:
REF LINK IN COMMENTS
So in my case I had to do
SELECT p.pid AS pid, count(*) AS num_tags FROM pg_photos p, TABLE(p.tags) t2 GROUP BY p.pid;

Resources