Tuesday, 3 September 2013

How to generated default values for the PK's

If we want to generate the default values for the PK's, there are many options to do that. I will show in 2 ways to generate the PK's


1) Open the Entity Object, and select the unique option and Primary Key option as show in the below image
It will generate the Random numbers, starting from 1.


2) If you want to generate the numbers sequentially not randomly, use sequences. Follow below steps to generate default values for PK using sequences.

i) Go to the SQL Developer, expand the DB and right click on the Sequncee to create new sequence as in shown below image.
ii) In the opened sequence pop-up, specify the values as you want . Press OK to complete.







iii) Now create a trigger fro the sequence which you have created.

CREATE OR REPLACE TRIGGER department_pk
       BEFORE INSERT
        ON Department
              FOR EACH ROW
                  BEGIN
                        SELECT SAMPLE_SEQ.nextval
                        INTO :new.Department_id
                  FROM dual;
                  END;



department_pk is the trigger name, Department is the table name , SAMPLE_SEQ is the sequence we have created. Department_id is the PK column name

iv) Now go to the Entity Object. open the attribute, and select the data type as DBSequence, then you can see the option to enter the Sequence name which you have created at the DB side. Enter the DB sequence name and press Ok.




And you can start using it.


No comments:

Post a Comment