Oracle creates the primary key self-increasing representation example code _oracle

Source: Internet
Author: User

Objective

This article gives you a major introduction to the method of creating a self-growing table in Oracle, which uses a sequence. Here's a few words, let's look at the sample code.

Sample code

CREATE TABLE Tb_student
(
  ID number         () not      null,
  createtime DATE     not         null,
  Constraint Pk_tb_student primary key (ID)
);

Comment on table "tb_student" is
' student table ';

Comment on column "Tb_student". Id ' is
' primary key ID ';

Comment on column "Tb_student". Createtime ' is
' creation time ';


--Create sequence created
sequence seq_tb_student
minvalue 1
nomaxvalue start with
1 increment by
1
nocycle  --cumulative, not cyclic
nocache; 


--Creates a trigger if the INSERT statement does not specify an ID to automatically insert the growth value
create OR REPLACE TRIGGER tr_tb_student 
before insert on tb_student for each ROW when (new.id are null)
begin
Select Seq_tb_student.nextval into:new.id from dual;
End

NOTE: triggers are not mandatory and can specify the insertion value from a strict business requirement.

Note that the Oracle Limit object name cannot be longer than 30 characters long, so the table name must be controlled at a certain length or the following creation sequence may exceed the limit, and it is recommended that the table name be controlled below 27 characters.

Summarize

The above is the Oracle to create the main table of the entire content, I hope the content of this article for everyone to learn or use Oracle to bring some help, if there are questions you can message exchange, small series will be as soon as possible to reply.

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.