Unique sequence for a column in oracle table - sql

I am trying to load a table where i have a PK column called E_id. I want to generate a unique ID for this column every time a row is loaded into this table at informatica level. But i want this e_id column to generate a unique value at table level. Can i achieve this by writing a query to this column at table create script itself?
Currently i tried this method of defaulting the value with this
e_id NUMBER DEFAULT TO_NUMBER(TO_CHAR(SYSTIMESTAMP, 'YYYYMMDDHH24MISSFF9')) NOT NULL
Although at times i am getting the same value for two records.
Can someone help how to go about this?
TIA.

one of the most common way to do this is using sequences
1 create sequence
2 before insert row trigger that will populate value into the pk column
create sequence GSEQUENCE
minvalue 1000
maxvalue 99999999999999999
start with 93581
increment by 1
cache 20;
CREATE OR REPLACE TRIGGER BI_DOCUMENTS
BEFORE INSERT
on DOCUMENTS
for each row
declare numrows INTEGER;
begin
select gsequence.nextval
into :new.id_DOCUMENT
from dual;
end;
/

If you are using 12c the easiest way is to use an IDENTITY column. Oracle will automatically generate a unique value for the column whenever you insert a record.
In prior versions you can define a sequence and generate it via a trigger or reference it directly in the insert statement.
You mention Informatica. Its a long time since I used it but I seem to remember there being a simple way to generate a unique ID using Informatica too.

You can use Globally Unique ID (GUID) which is unique for all practice purpose. Lot of systems use GUID to generate unique id.
select sys_guid() from dual
Mote into here
While GUID gives truly random unique ids, you can always use Oracle sequence if you need sequential unique values.
Oracle doesn't have "auto increment" columns, but you can achive similar results by a trigger.
EDIT: You can refer this link too How to generate a GUID in Oracle?

Related

How to create view with extra column called Id (primary key) in Db2?

Could anyone help me to create a view from the table? I am using IBM Db2 on cloud, I have a table with below columns:
I want to create a view having an extra column id as below:
I need to this column be unique so I think adding a new column as identity. I must mention this view returned a large of data so I need a way with good performance. I have seen some related posts on Stack Overflow, but I found those are for different databases.
Any suggestions will be appreciated.
You can create a sequence and call the sequence in as id.
Create a sequence in the database
Call the sequence in a view and alias it as ID
Creating sequence:
CREATE SEQUENCE sequence_name
INCREMENT BY 1 START WITH 1
MAXVALUE 30 MINVALUE 0 -- As you required
NOCYCLE CACHE 10 ORDER; -- As you required
Calling in query:
select sequence_name.NEXTVAL, f_name, i_name, city, country from table;
If you do not want to use sequence, you can also so use ROW_NUMBER() in your query

Adding a computed column that uses MAX

I need to create a sequential number column for record number proposes
I am OK with losing sequence if I delete a row from the middle of the table
For example
1
2
3
If I delete 2, I am ok with new column been 4.
I tried to alter my table to
alter table [dbo].[mytable]
add [record_seq] as (MAX(record_seq) + 1)
but I am getting An aggregate may not appear in a computed column expression or check constraint.
Which is a bit confusing? do I need to specify an initial value? is there a better way?
If you're looking to allocate a sequence number even in cases where the table doesn't get a record inserted, I would handle it in the process responsible for performing those inserts. Create another table, in this table keep track of the max identity value of that sequence. Each time you want to perform an insert, reserve the sequence number you want by updating that table first. If you rely on selecting the max existing value, you could be at risk of multiple sessions getting the same "new" sequence number before inserting. Even if the insert fails, you will have incremented that control table so nothing else uses that value that has been reserved.
Its not supported in MsSql. You can use identity column:
ALTER TABLE [dbo].[mytable]
ADD [record_seq] INT IDENTITY
Or use trigger to update your seq column after insert and/or delete

In SQL, how to populate number in sequence in column without alter the table structure?

populating number in a sequence for a column without altering table structure--need a query
if you don't want to change or alter the database table structure, you have limited solutions actually.
First option might be using a sequence object
create sequence mySequence increment by 1;
After you have a sequence object, you can read from this sequence whenever you need this field value
select mySequence.nextval from dummy;
Unfortunately, each read will increment the current value of the sequence.
This means if you read but did not insert the value into the table, next time you insert a new row there will be a gap in your table column.
Or if you insert two rows from two different executions, one might read the sequence first but insert value later than the other thread; then the sequence will be like 11,12,14,13,15
Second option is creating a database table trigger AFTER INSERT statement.
Then you can the max value from table column (or can store in a separate table) or from sequence, and update the column field with this sequence data

Filling incremental unqiue values for a newly created PK

I currently have a table with many rows, but no PK at all. I now require to have a unique, non-null, > 0 PK for every row.
I'm created the column for PK, but how I can I quickly fill in the column with an incremental value starting from 1?
Any method, a single SQL line, or a SQL line to be executed as many times as are rows are good enough for me.
Something like
update sometable set newkeyfield = Row_Number() Over();
should do not a DB2 bloke, but should be close.
Try using a DB2 Sequence object. They are designed for creating unique sequences of numbers. Pick your data type.
CREATE SEQUENCE mySeq as int;
You retrieve and increment the sequence in one step, using a sequence reference
NEXT VALUE FOR mySeq
You expression can use this in an INSERT, UPDATE, or MERGE, or most places you can use an expression.

Universal SQL construct to retrieve the last row inserted

What would be the correct universal SQL construct to get the last row inserted (or it's primary key). The ID might be autogenerated by a sequence but I do not want to deal with the sequence at all! I need to get the ID by querying the table. Alternatively, INSERT might be somehow extended to return the ID. Assume I am always inserting a single row. The solution should work with most RDBMS!
the best way is to depend on the sequence like:
select Max(ID) from tableName
but If you don't want to deal with it, you can add new timestamp column to your table and then select max from that column.
like this way
select Max(TimestampField) from tableName