Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Self-referencing constraint?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Self-referencing constraint?
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    Post subject:
Reply with quote

Shreejita,
You can refer this.

http://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    Post subject:
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: 788
Location: Chennai, India

PostPosted: Wed Mar 10, 2010 12:21 pm    Post subject:
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,

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

http://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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Command UNIQUE CONSTRAINT on any tabl... rohanthengal DB2 2 Thu Aug 18, 2016 3:48 pm
No new posts Summing up child-values in self-refer... Auryn DB2 1 Sun Feb 14, 2016 6:02 pm
No new posts How to skip foriegn Key constraint in... Arunkumar.chinnappa DB2 5 Fri Feb 07, 2014 2:17 pm
No new posts Use referencing in Eazytrieve krunalbafna CA Products 8 Thu Feb 02, 2012 2:34 pm
No new posts DB2 Triggers - cant put NOT NULL cons... ashishsr123 DB2 6 Thu Apr 30, 2009 12:08 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us