Set up column automatic growth in Oracle

Source: Internet
Author: User

The steps for creating columns in Oracle automatically grow as follows:

1 Create a table first, such as: Create TABLE "DEPARTMENT"
("department_id" number,
"Department_name" VARCHAR2 not NULL ENABLE,
"MANAGER_ID" number (6, 0),
"LOCATION_ID" number (4, 0),
PRIMARY KEY ("department_id") ENABLE
)
/

2 Creating a sequence for the department_id column: Create SEQUENCE "Department_seq" MinValue 1 MAXVALUE 9999 INCREMENT by 1 START with 1 NoCache noorder nocycle

3 Create a trigger for this table: Create OR REPLACE TRIGGER "Insert_department"
Before
Insert on "DEPARTMENT"
For each row
Begin
Select "Department_seq". Nextval into:new. DEPARTMENT_ID from dual;
End;
/
ALTER TRIGGER "Insert_department" ENABLE
/

4 over, then if you need to insert data into the table, the department_id can grow automatically. The column minimum value is 1, the maximum is 9999, and the cardinality is 1, which you can modify according to your own needs.

5 Oracle Another way to achieve automatic growth is as follows (still, for example, the table built above):

First create sequence: Drop sequence department_seq;
Create sequence Department_seq
Increment by 1
Start with 1
MaxValue 9999999999
NoCache;

You can then write the INSERT statement directly, as follows: Insert   into  department  values (Department_seq.nextval, ' R & D ', 8, 10); This shows that the way to complete a function is varied, frankly, I do not know whether the two methods are good or bad, but I am more inclined to use the first method.

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.