Create a primary key auto-increment table in Oracle

Source: Internet
Author: User

-- Create a table and Set primary key auto-Increment
Create Table Stu (
Uerid number (4) not null primary key,
Username varchar (20 ),
Userpass varchar (20 ),
Email varchar2 (30 ),
Grade number (5)
);
Commit;
-- Check it.
Select * from Stu;
-- Create an auto-incrementing sequence of the primary key
Create sequence emp_sequence
Increment by 1
Start with 1
Nomaxvalue
Nocycle
Nocache;
Commit;
-- Create a trigger
Create trigger "trigger_on_stu" before
Insert on Stu for each row when (New. uerid is null)
Begin
Select emp_sequence.nextval into: New. uerid from dual;
End;
-- Insert data
Insert into Stu (username, userpass, email, grade) values ('admin', 'admin', 'admin @ admin.com ', 1 );
Insert into Stu (username, userpass, email, grade) values ('dhy', 'dhy', 'dhy @ admin.com ', 1 );
Insert into Stu (username, userpass, email, grade) values ('ns', 'ns', 'ns' @ admin.com ', 1 );
Insert into Stu (username, userpass, email, grade) values ('adamin', 'addmin', 'admdin @ admin.com ', 5 );
Insert into Stu (username, userpass, email, grade) values ('dhay', 'dhay', 'dhay @ admin.com ', 3 );
Insert into Stu (username, userpass, email, grade) values ('ansr', 'ansr', 'ansr @ admin.com ', 4 );
Commit;
-- View data
Select * from Stu;

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.