How to Create Auto-incrementing fields in MySQLamp; Oracle how to create auto-incrementing fields in MySQL & Oracle
Create an auto-increment field in MySQL:
Create table article // create a table first.
(
Id int primary key auto_increment, // set this field to auto increment.
Title varchar (255)
);
Insert into article values (null, 'A'); // insert data to the database.
Select * from article; the result is as follows:
Id Title
1
Insert into article values (null, 'B ');
Insert into article values (null, 'C ');
Insert into article (title) values ('D ');
Select * from article; the result is as follows:
Id Title
1
2 B
3 c
4 d
However, oracle does not have such a function, but it can be implemented through trigger and sequence.
Assume that the keyword segment is id, create a sequence, and the code is:
Create sequence seq_test_ids
Minvalue 1
Max value 99999999
Start with 1
Increment by 1
Nocache
Order;
The code of the producer is:
Create or replace trigger tri_test_id
Before insert on test_table
For each row
Declare
Nextid number;
Begin
IF: new. id IS NULLor: new. id = 0 THEN
Select seq_test_id.nextval
Into nextid
From sys. dual;
: New. id: = nextid;
End if;
End tri_test_id;
OK. the above code can be used to implement the auto-increment function.