Do a primary key and a foreign key referencing it must have the same column size in sql server 2008 ? could someone indicate me a link to a documentation explaining this ?
Thank you for help.
foreign keyprimary-keysql-server-2008
Do a primary key and a foreign key referencing it must have the same column size in sql server 2008 ? could someone indicate me a link to a documentation explaining this ?
Thank you for help.
Best Answer
Yes. It's in the MSDN documentation pages: Foreign Key relationships
That page does not provide much more details but testing reveals that "same datatype" means same type and same size. Also, if one tries to make a
FOREIGN KEY
constraint between for example, aVARCHAR(20)
and aVARCHAR(30)
column,we get the explanatory error:
Also note that some DBMS (like Oracle, MySQL, Postgres) allow foreign keys between columns of same datatype (i.e.
VARCHAR
orCHAR
) but different size.Postgres is even less strict. It allows foreign keys between columns of
CHAR
,VARCHAR
orTEXT
datatypes in any combination.