Oracle database to create a primary key ID from the growing list

A. To build the table

CREATE TABLE 表
(
ID INTEGER not null,
NAME NVARCHAR2(256),
DEL_FLG int
,CONSTRAINT "PF_表" PRIMARY KEY ("ID")
)

II. Creating a Sequence

CREATE SEQUENCE Table _ID
the INCREMENT BY. 1 - a designated interval between the sequence number, this value may be positive or negative integers, but not to zero. Ascending sequence. When this clause is omitted, the default value of 1

START WITH 1 - specifies a sequence number generated first. When ascending, the sequence may begin with a value larger than the minimum value, the minimum value is the default sequence. For values ​​in descending order, the sequence may be less than the maximum starts, the maximum default value of the sequence.

NOMAXVALUE - Specify a maximum of 1027 for the ascending, descending specified maximum value -1.

ORDER
NOCYCLE
CACHE 10;             

III. Creating Triggers

CREATE OR REPLACE TRIGGER trigger name
BEFORE INSERT ON table
the FOR EACH the ROW
the BEGIN
the SELECT yqjyMaintain_ID.nextval the INTO: the FROM NEW.ID the DUAL; - trigger conditions (sql statement)
the END trigger name;

 

Trigger name: The name of the trigger object. As the trigger is a database automatically, so the name is just a name, there is no real purpose.
Trigger Time: when specified trigger execution, the desirable value:
before: indicates that the trigger action is performed before database;
After: mean do it after the database operation.
Triggering Event: specify which database trigger action will trigger this:
INSERT: insert database triggers this flip-flop;
Update: database modifications will trigger the trigger;
the Delete: Delete the database triggers this trigger.
Table Name: Table database triggers located.
for each row: execute once for each row in the table trigger. Without this option, only be performed once for the entire table.

Triggers can achieve the following functions:

Function :

1, to allow / restrict modification to the table
2, generated automatically derived columns, such as the self-energizing field
3, force data consistency
. 4, auditing and logging
5, to prevent invalid transaction
6, enable complex business logic

 

Guess you like

Origin www.cnblogs.com/xqfk/p/11097879.html