IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Self-referencing constraint?


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
shreejita

New User


Joined: 16 Aug 2009
Posts: 60
Location: chennai

PostPosted: Tue Mar 09, 2010 5:29 pm
Reply with quote

Hi

Question-Can anybody explain What is the self-referencing constraint?

i understand the following answer

Self-referencing constraint makes the table both the parent
and dependent table in the same referential constraint. The
foreign key to the table is defined after the table is
created.

Can anybody throw some logic of creating this???

Thanks in advance
shreejita
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Tue Mar 09, 2010 5:42 pm
Reply with quote

Shreejita,
You can refer this.

publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.intro/db2z_integrity.htm
Back to top
View user's profile Send private message
shreejita

New User


Joined: 16 Aug 2009
Posts: 60
Location: chennai

PostPosted: Wed Mar 10, 2010 12:04 pm
Reply with quote

Hi Srihari,

Appreciate your answer but my question is

Is it possible to have both primary key and foreign key from one single table?

Thanks
Back to top
View user's profile Send private message
Gnanas N

Active Member


Joined: 06 Sep 2007
Posts: 792
Location: Chennai, India

PostPosted: Wed Mar 10, 2010 12:21 pm
Reply with quote

Hi,

Quote:
Is it possible to have both primary key and foreign key from one single table?
Yes.

Here it is,
Create a table named DSN8910.DEPT ...
Code:
CREATE TABLE DSN8910.DEPT
     (DEPTNO   CHAR(3)     NOT NULL,
      DEPTNAME VARCHAR(36) NOT NULL,
      MGRNO    CHAR(6)             ,
      ADMRDEPT CHAR(3)     NOT NULL,
      LOCATION CHAR(16)            ,
      PRIMARY KEY(DEPTNO)          )
     IN DSN8D91A.DSN8S91D;

Define ADMRDEPT as the foreign key of a self-referencing constraint on DSN8910.DEPT.

Code:
   ALTER TABLE DSN8910.DEPT
     FOREIGN KEY(ADMRDEPT) REFERENCES DSN8910.DEPT ON DELETE CASCADE;


From manual,
Quote:
A table can be a dependent of itself; this is called a self-referencing table. For example, the DEPT table is self-referencing because the value of the administrative department (ADMRDEPT) must be a department ID (DEPTNO). To enforce the self-referencing constraint, DB2 requires that a foreign key be defined.


I got them from,

publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sql_createtable.htm

publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sql_altertable.htm

Please let me know if I'm wrong.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Difference between VALIDPROC and CHEC... DB2 3
No new posts Command UNIQUE CONSTRAINT on any tabl... DB2 2
No new posts Summing up child-values in self-refer... DB2 1
No new posts How to skip foriegn Key constraint in... DB2 5
No new posts Use referencing in Eazytrieve CA Products 8
Search our Forums:

Back to Top