Thread: Foreign key
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial">Hello,</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial">When i add table with foreign key in my database, this error return : « </span></font><tt><font color="black"face="Courier New" size="2"><span style="font-size:10.0pt;color:black">number of referencing and referencedcolums for foreign key disagree</span></font></tt><font face="Arial" size="2"><span style="font-size:10.0pt;font-family:Arial">».</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial">How resolve this problem ?</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial">Thanks</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Times New Roman" size="1"><span style="font-size: 8.0pt">alain SAKALALA</span></font><p class="MsoNormal"><font face="Times New Roman" size="1"><span style="font-size: 8.0pt">Mailto:asakalal@bouyguestelecom.fr</span></font><p class="MsoNormal"><font face="Times New Roman" size="3"><span style="font-size: 12.0pt"> </span></font></div>
On Fri, Mar 25, 2005 at 04:31:16PM +0100, ASAKALAL@bouyguestelecom.fr wrote: > > When i add table with foreign key in my database, this error return : < > number of referencing and referenced colums for foreign key disagree>. Apparently the referencing key (the foreign key specification) has a different number of columns than the referenced key (the primary key or other unique key in the referenced table). Here's an example that illustrates the problem: CREATE TABLE foo ( pk1 integer NOT NULL, pk2 integer NOT NULL, PRIMARY KEY (pk1, pk2) -- 2-column primarykey ); CREATE TABLE bar ( fk integer NOT NULL REFERENCES foo -- 1-column foreign key ); ERROR: number ofreferencing and referenced columns for foreign key disagree In the above example we need a 2-column foreign key: CREATE TABLE bar ( fk1 integer NOT NULL, fk2 integer NOT NULL, FOREIGN KEY (fk1, fk2) REFERENCES foo ); Here's another example that references a 1-column unique key that isn't a primary key: CREATE TABLE foo ( pk1 integer NOT NULL, pk2 integer NOT NULL, x integer NOT NULL, PRIMARY KEY (pk1,pk2), UNIQUE (x) ); CREATE TABLE bar ( fk integer NOT NULL REFERENCES foo (x) ); If these examples don't help, then please post the table definitions you're working with and explain what you'd like to do. -- Michael Fuhr http://www.fuhr.org/~mfuhr/
On Fri, Mar 25, 2005 at 16:31:16 +0100, ASAKALAL@bouyguestelecom.fr wrote: > > When i add table with foreign key in my database, this error return : < > number of referencing and referenced colums for foreign key disagree>. > > How resolve this problem ? Besides what Mike said, one other thing to remember is that if you don't specify columns in the referenced table, the primary key of that table is used, NOT columns with names matching those of the referencing table. In cases like this it have helped if you had copied and pasted an example displaying the problem in addition to the error message.