This is my first time using SQL Server and I have created these tables:
create table Department
(
D_Number int not null,
D_Name varchar(20),
D_MgrSSN int,
D_MgrDate datetime,
primary key(D_Number),
unique(D_Name),
)
create table Empolyee
(
E_SSN int not null,
E_FName varchar(20),
E_LName varchar(20),
E_Sex varchar(6),
E_BDate datetime,
E_Salary decimal(10,2),
E_Address varchar(50),
E_Department varchar(20),
E_SuperSSN int,
primary key(E_SSN),
foreign key(E_Department) references Department(D_Number),
)
and when I run I get this error:
Column ‘Department.D_Number’ is not the same data type as referencing column ‘Empolyee.E_Department’ in foreign key ‘FK_Empolyee_E_Depa__0CBAE877′.
Personally I think SQL Server did an excellent job with the error message, it tells you exactly what needs to be fixed.
Look at the two fields that you have defined, one is a
varchar(20)the other is aninteger.In order for a foreign key to match it’s primary key, it must be of the same type.
Either change both to
intor both tovarchar. Whatever makes sense for your business model.