Take the maximum value of oracle note as id

Source: Internet
Author: User

The boss assigns a task to perform CRUD operations on the Identification table and identification table. There is no difficulty in coding, but there are two places I want to record.
 
First, give the table structure.
REPOT_BS ):
 
Name Type Nullable Default Comments
----------------------------------------------
Id number (12) id of the identification type
NAME VARCHAR2 (128) y id type NAME
PARENT_ID NUMBER (12) Y identification type parent id
MARK VARCHAR2 (2000) Y remarks
 
 
REPOT_CATEGORY ):
 
Name Type Nullable Default Comments
--------------------------------------------------------
Id number (12) id
NAME VARCHAR2 (128) Y NAME
MARK VARCHAR2 (2000) Y remarks
CATEGORY_ID NUMBER (12) Y type id
State char (1) Y 1 enabled 0 disabled cannot be deleted
 
Well, the first point is that when inserting data, take the maximum value + 1 of the table id as the id. SQL:
 
Insert into REPOT_BS (ID, NAME, MARK, CATEGORY_ID, STATE) VALUES (select nvl (MAX (ID), 0) + 1 FROM REPOT_BS), # name #, # mark #, # categoryId #, # state #)
 
 
The red part indicates the maximum value + 1 of the table as the id, max indicates the maximum value function, and NVL (A, B) indicates that the value of A is null, then B is returned, otherwise, return. Both max and nvl are oracle system functions.
Note: oracle is case-insensitive. -_-
 
Second, determine whether a startup ID exists under a certain identification type. SQL:
 
Select count (*) from (select 1 from ZY_SYS_REPOT_BS B where B. state = '1' and B. category_id = # categoryId #)
 
If the returned value is greater than 0, a startup identifier is available for this identifier type. If the returned value is less than 0, no startup identifier is available. This SQL statement must be pre-determined when the ID type is deleted.
 
Haha, I have declared it is easy to remember. It may be of little help to you. I hope it will be slower...
 
From Scott's column

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.