How to set auto-increment for ID in Oracle?
In Oracle, auto-increment ID can be achieved by creating sequences and triggers. Here is an example:
- Create a sequence:
CREATE SEQUENCE id_sequence
START WITH 1
INCREMENT BY 1
NOCACHE;
- Create a trigger.
CREATE OR REPLACE TRIGGER id_trigger
BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
SELECT id_sequence.NEXTVAL INTO :NEW.id FROM dual;
END;
In the example above, a sequence named id_sequence is first created, followed by a trigger named id_trigger. The trigger automatically assigns a value to the id field when inserting a new record into the your_table table, achieving the setting of an auto-incrementing ID.
More tutorials
BroadcastReceiver Example Tutorial on Android(Opens in a new browser tab)
Tutorial on how to set up a Hibernate Tomcat JNDI DataSource.(Opens in a new browser tab)
QR code generator in Java using zxing.(Opens in a new browser tab)
Java thread ensuring Java code is thread-safe(Opens in a new browser tab)
Spring MVC HandlerInterceptorAdapter and HandlerInterceptor.(Opens in a new browser tab)
The main method in Java(Opens in a new browser tab)
How to automatically generate an ID based on time in C++?(Opens in a new browser tab)
What are the parameters of the RPAD function in Oracle?(Opens in a new browser tab)
What things should be considered when starting the Oracle listener?(Opens in a new browser tab)
How to set the cache size in Linux?(Opens in a new browser tab)