Background introduction:
SQL Server can add an identity (1) After a field of type int, and the field will be increased by +1, and this field will be set as the primary key, enabling us to insert the data. You can use "auto_increment" in MySQL. But Oracle is a bit cumbersome and needs to use sequences and triggers to achieve its purpose.
--School Table
CREATE TABLE School (SID number (4), sname varchar2 (), check_status number (1) Default 0 check (Check_status in (0,1)), Accountant VARCHAR2 (+ char), pwd varchar2 (+ char), email varchar2 (+ char), Photo_path varchar2 (+ char),
Constraint Pk_t_school primary KEY (SID));
--Sequence (sequence and trigger to implement automatic growth of Sid field in school table)
Create sequence Shool_sid_autoinc
MinValue 1
MaxValue 9999999999999999999999999999
Start with 1
Increment by 1
NoCache
--Triggers (sequence and trigger implementations school the SID field in the table)
Create or Replace Trigger Insert_shool_sid_autoinc
Before insert on school
For each row
Begin
Select Shool_sid_autoinc.nextval Into:new.sid from dual;
End
/
--Test results
Sql> INSERT into school values (+, ' a ', 0,001,001, ' [email protected] ', 66666);
1 row created.
Sql> INSERT into school values (+, ' aaaaaaaaaaaaaaaaaaaaa ', 0,001,001, ' [email protected] ', 66666);
1 row created.
Sql> select Sid from School;
Sid
----------
1
2
Ok, end. Reprint please indicate the source
How to specify table field self-increment in Oracle