进入正题
再用PLSQL创建自动增长的字段时需要 :
1.创建序列 2.创建触发器
上代码
/**创建一个学生表**/
create table students(
StudentID number(3) not null primary key,
StudentName Char(50) not null,
Grender number(1),
Age number(2)
Class Char(50),
Score number(3)
)tablespace StudentDb;
1.创建序列
(名字最好规范好记就行)
/**设置 StudentID 自动增长 */
--创建序列
create sequence students_StudentID_seq
start with 1
increment by 1
nomaxvalue
nocache;
2.创建触发器
之前看了一大堆,按着标准来结果报‘触发器无效且未通过重新验证’ 此问题原因是 new.自动增长的字段
,这个就是你创建表时的那个
--创建触发器
CREATE OR REPLACE TRIGGER insert_students_StudentID
BEFORE INSERT ON students
FOR EACH ROW
BEGIN
SELECT students_StudentID_seq.NEXTVAL INTO:NEW.StudentID FROM DUAL;
END;
其实也就这么回事。。。。。。
----------------把代码写熟悉之后---------
也可以直接创建
点击确定—>