MySQL & amp; Oracle database creation auto-incrementing field operations

Source: Internet
Author: User
The following articles teach you how to create an auto-incrementing field in the MySQLOracle database. We all know Oracle (large website database platform) the following figure shows the 100 questions that database beginners must know. Oracle (large website database platform) PLSQL language basic Oracle (number of large websites

The following articles teach you how to create an auto-incrementing field in the MySQLOracle database. We all know Oracle (large website database platform) the following figure shows the 100 questions that database beginners must know. Oracle (large website database platform) PL/SQL language basic Oracle (number of large websites

The following articles teach you how to create an auto-incrementing field in MySQL and Oracle databases. We all know Oracle (large website database platform) the following figure shows the 100 questions that database beginners must know.

Oracle (large website database platform) PL/SQL language basics

Basic knowledge of Oracle (large website database platform) architecture (Entry level)

How to create a simple Oracle (large website database platform) Stored Procedure

Detailed introduction to various data types in Oracle (large website database platform)

Oracle (large website database platform) trigger details

How to create a simple Oracle (large website database platform) Stored Procedure

Oracle (large website database platform) Beginners

Usage of nvl functions of Oracle (large website database platform)

Use of sequences in Oracle (large-scale website database platform) development

How to Create auto-incrementing fields in MySQL and Oracle (large website database platform)

Create an auto-incrementing field in MySQL (the best combination with PHP:

Create table article

Create a table first.

(

Id int Prima (the most perfect VM management system) ry key auto_increment,

Set this field to auto increment.

 
 
  1. title varchar(255)
  2. );
  3. insert into article values (null,'a');

Insert data into the database.

Select * from article; the result is as follows:

 
 
  1. Id
  2. Title
  3. 1
  4. a
  5. insert into article values (null,’b’);
  6. insert into article values (null,'c');
  7. insert into article (title) values ('d');

Select * from article; the result is as follows:

 
 
  1. Id
  2. Title
  3. 1
  4. a
  5. 2
  6. b
  7. 3
  8. c
  9. 4
  10. d

However, Oracle (a large-scale website database platform) 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:

 
 
  1. create sequence seq_test_ids
  2. minvalue 1
  3. maxvalue 99999999
  4. start with 1
  5. increment by 1
  6. nocache
  7. order;

The code of the producer is:

 
 
  1. create or replace trigger tri_test_id
  2. before insert on test_table
  3. for each row
  4. declare
  5. nextid number;
  6. begin
  7. IF :new.id IS NULLor :new.id=0 THEN
  8. select seq_test_id.nextval
  9. into nextid
  10. from sys.dual;
  11. :new.id:=nextid;
  12. end if;
  13. end tri_test_id;

OK. The above code can be used to implement the auto-increment function.

The above content is an introduction to creating auto-incrementing fields in MySQL and Oracle. I hope you will find some gains.

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.