Use DB2 sequence to automatically generate a primary key

Source: Internet
Author: User

You can use DB2 sequence to automatically generate a primary key. The following describes how to use DB2 sequence to automatically generate a primary key. If you are interested in this, take a look.

Write a tracing program to record the operations performed on the data.

It is assumed that Access has an automatic number type, which can automatically generate a primary key for the field.

After checking db2, I feel that using DB2 Sequence is a bit like this.

-- Create a DB2 Sequence to generate an id
Create sequence xixi. id_log
AS bigint
Start with 1
Increment by 1
NO MAXVALUE
CYCLE
No cache;

Detailed difference description see ibm description http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.doc/admin/r0004201.htm? Resultof = % 22% 73% 65% 71% 75% 65% 6e % 63% 65% 22% 20)

-- Query the previous id
Values prevval for xixi. id_log;

-- Generate id
Values nextval for xixi. id_log;

-- Insert record

Insert into xixi. id_log (id) values
(Nextval for xixi. id_log)

-- Delete sequence
Drop sequence userid. ID_PDALOG restrict

It is convenient to automatically generate a primary key without using max.

However, it is a pity that the table is not defined as Access.
 

How to Create a tablespace in DB2

Introduction to the DB2 user group in Linux

Top 10 considerations for DB2 Performance Optimization

Connect SQL SERVER to the DB2 database

DB2 index types

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.