Auto-increment column in Oracle

Source: Internet
Author: User

Step 1: Create a new test table as follows:

Create Table king
(
ID number (10 ),
Name nvarchar2 (2000)
)

Step 2: Create a sequence
Create sequence king_tb_seq
Minvalue 1
Max value 9999999999
Start with 21
Increment by 1
Cache 20;

Step 3: Create a trigger

Create or replace trigger king_tb_tri
Before insert on King
For each row
Begin
Select king_tb_seq.nextval into: New. ID from dual;
End;

Step 4: perform a test under PL/SQL

SQL test statement:

Insert into King (name) values ('andy Lau ');

Stored Procedure test:

Create or replace procedure insertking
(
K_name varchar2
)
Is
Begin
Insert into King (name) values (k_name );
Commit;
End;

Test:

Begin

Insertking ('andy Lau ');

End;

 

Related Article

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.