About automatic growth of primary keys in the database __ database

Source: Internet
Author: User
MySQL, SQL Server, Oracle primary key automatic growth 1, define the primary key as an automatic growth identifier type in MySQL, if you set the table's primary key to the Auto_increment type, the database automatically assigns the primary key. Example: CREATE TABLE Customers (ID int auto_increment PRIMARY key NOT NULL, name varchar (15)); INSERT into customers (name) VALUES ("Name1"), ("name2");
2. In MS SQL Server, if the primary key of the table is set to the identity type, the database is automatically assigned the primary key. For example:
CREATE TABLE Customers (ID int identity (1,1) primary key NOT NULL, name varchar (15));
INSERT into customers (name) VALUES ("Name1"), ("name2");
The identity contains two parameters, the first parameter represents the starting value, and the second parameter represents an increment.

3. Get automatic growth identifier in Oracle column
In Oracle, you can create a separate sequence for the primary key of each table, and then get the automatically incremented identifier from the sequence, assigning it to the primary key.
For example, the statement creates a sequence named Customer_id_seq, which starts with a value of 1 and an increment of 2.

Method One,
Create sequence Customer_id_seq
INCREMENT by 1--add a few at a time
Start with 1-counting from 1
Nomaxvalue--Do not set maximum value
Nocycle--Cumulative, not cyclic
CACHE 10;
Once you have defined the CUSTOMER_ID_SEQ sequence, you can access the Curval and Nextval properties of the sequence.
Curval: Returns the current value of a sequence
Nextval: First increment the value of the sequence, then return the sequence value
CREATE TABLE Customers (ID int primary key NOT NULL, name varchar (15));
INSERT into customers values (Customer_id_seq.curval, "name1"), (Customer_id_seq.nextval, "name2");
Method Two, or through stored procedures and triggers:
1. Generate sequences and triggers by adding stored procedures:
Create or replace PROCEDURE "Pr_createidentitycolumn"
(TableName varchar2,columnname VARCHAR2)
As
strSQL varchar2 (1000);
Begin
strSQL: = ' Create sequence seq_ ' | | tablename| | ' MinValue 1 maxvalue 999999999999999999 start with 1 increment by 1 NoCache ';
Execute immediate strSQL;
strSQL: = ' Create or replace trigger Trg_ ' | | tablename| | ' before insert on ' | | tablename| | ' For each row begin select Seq_ ' | | tablename| | '. Nextval into:new. ' | | columnname| | ' from dual; end; ';
Execute immediate strSQL;
End
2. Execute stored procedure on the table
exec pr_createidentitycolumn (' Xs_audit_record ', ' audit_record_id ');
The previous scenario is for each table, and the following are generated in batches by the user
Select A.table_name, B.column_name from Dba_constraints A, dba_cons_columns b
where a.constraint_name = B.constraint_name
and A.constraint_type = ' P '
and A.owner=user;

3, add role permissions to execute stored procedures, modify stored procedures, add authid current_user when stored procedures can use role permissions.
Create or replace Procedeate_ ure P_CR table Authid Current_User is
Begin
Execute Immediate ' CREATE TABLE create_table (id int) ';
End p_create_table;

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.