How to create an auto-incrementing field _ MySQL under MySQL & Oracle

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.