Programmer's SQL Classic Note 1_ auto-grow field

Source: Internet
Author: User
Tags table definition

Auto Grow Field
When designing a database, it is sometimes necessary to automatically grow a field of a table, the primary key of the table is the most common use of the autogrow field, and the auto-grow field can simplify the generation of the primary key. There are different implementations of autogrow fields in different DBMS, as described below.

    • It is very simple to set a field in MYSQL as an auto-grow field, as long as you specify auto_increment in the table definition.
      Example:

CREATE TABLE t_student (t_id int primary key auto_increment,t_name varchar (), t_age int);
Insert into t_student (t_name,t_age) VALUES ("King", "n"), insert into t_student (t_name,t_age) VALUES ("Hong", 22);
    • Auto-grow fields in Oracle

Unlike MySQL and MSSQLSERVER, Oracle does not specify an auto-Grow column, but the autogrow field can be implemented in Oracle through the sequence sequence.
Before using SEQUENCE, you need to first define a SEQUENCE, and the syntax for defining SEQUENCE is as follows:

CREATE SEQUENCE sequence_nameincrement by Stepstart with Startvalue;

Where Sequence_name is the name of the sequence, each sequence must have a unique name, the Startvalue parameter value is the starting number, and the step parameter value is the step size, which is the increment value for each auto-increment.
Once you have defined sequence, you can use currval to get the current value of sequence, or you can add sequence by Nextval , and then return a new sequence value, such as:

Sequence_name. Currvalsequence_name. Nextval

If the sequence is not needed, you can delete it:

DROP SEQUENCE Sequence_name;

Here is an example of using the sequence sequence to automate growth.
First create a sequence named Seq_personid:

Create sequence seq_t_personincrement by 1start with 1;

Then create the T_person table:

CREATE TABLE T_person (FId number PRIMARY KEY, FName VARCHAR2 (), Fage number (10));

After executing the above SQL statement, create a successful T_person table, and then execute the following SQL statement to
Insert some data into the T_person table:

INSERT into T_person (FID, FName, Fage) VALUES (seq_personid.nextval, ' Tom ', +); INSERT into T_person (FID, FName, Fage) VALUES (seq_personid.nextval, ' Jim ', Bayi); INSERT into T_person (FId, FName, Fage) VALUES (seq_personid.nextval, ' Kerry ') , 33);


Programmer's SQL Classic Note 1_ auto-grow field

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.