I’m new to Oracle, and new to Oracle Application Express.
I’m working through a manual and I’m trying to create a table with 3 fields:
USER_ID: Number, Primary Key, Autonumber
USER_NAME: VARCHAR2, Unique
PASSWORD: VARCHAR2
I used the APEX SQL Workshop wizard to create the table, but when I try to do something like the following:
INSERT INTO Schema.USERS (USER_NAME,PASSWORD) VALUES ('planet','password');
…I get told that a unique constraint on the Primary Key is violated.
A trigger was automatically created by the Table Creation wizard. It looks like this:
create or replace trigger "BI_USERS"
before insert on "USERS"
for each row
begin
if :NEW."USER_ID" is null then
select "USERS_SEQ".nextval into :NEW."USER_ID" from sys.dual;
end if;
end;
I was under the impression this would automatically find the highest numeric value in the USER_ID field of USERS and automatically assign an incremented value to each new INSERT, but this doesn’t appear to be happening.
Can anyone suggest what I might be doing wrong?
Many thanks
pt
Sequences: http://www.techonthenet.com/oracle/sequences.php
You said:
No. A sequence will simply generate the next value based on the last value and the increment, and is not tied to your data.
Is something that would look at your data and increment the highest
USER_IDwith1.If right now you are getting constraint violations due to the primary key on
USER_ID, this might mean you have inserted data into your users table, and this data contained values for theUSER_IDcolumn.For instance, say created 3 records and manually assigned a value to
USER_IDwithout using a sequenceWhen you then start using the sequence (or create it only then), the sequence will start at the specified start with value =
1.So doing
would generate a constraint violation because sequence.nextval would be 1.