sql server 2008 r2 - Invalid Columns in referenced table -
im setting couple tables in new database. have users
table, companies
table, usercompanies
table (so user can work many different companies), , userpay
table.
create table tblusers ( [usertableid] int identity not null primary key, [useremail] varchar(50) not null, [userfirstname] varchar(20) not null, [userlastname] varchar(20) not null, [useraddress1] varchar(45) null, [useraddress2] varchar(45) sparse null, [usercity] varchar(45) null, [userstate] varchar(2) null, [userzipcode] varchar(10) null, [userphone] varchar(13) null, ) create table tblcompanies ( [companyid] int identity not null primary key, [companyname] varchar(45) not null, [companyaddress1] varchar(45) null, [companyaddress2] varchar(45) sparse null, [companycity] varchar(45) null, [companystate] varchar(2) null, [companyphone] varchar(13) null, [companyphoneextension] varchar(8) null, [companycontactfirstname] varchar(20) not null, [companycontactlastname] varchar(20) not null ) create table tblusercompanies ( [usertableid] int not null, [companyid] int not null, primary key (usertableid, companyid), foreign key (usertableid) references tblusers(usertableid), foreign key (companyid) references tblcompanies(companyid) ) create table tbluserpay ( [userpayid] int identity not null primary key, [userid] int not null references tblusercompanies(usertableid), [usercompany] int not null references tblusercompanies(companyid), [netpay] money not null, [paydate] date not null )
the problem error states.. "there no primary or candidate keys in referenced table tblusercompanies
match referencing column list in tbluserpay
.
i dont understand why im getting error. same data type, foreign keys in tact, primary keys established. thing can think of throwing error because have foreign key linking foreign key. please if can! thanks
when linking composite key, need have 1 foreign key reference includes both columns, like:
create table tbluserpay ( [driverpayid] int identity not null primary key, [userid] int not null, [usercompany] int not null, [netpay] money not null, [paydate] date not null, foreign key (userid, usercompany) references tblusercompanies(usertableid, companyid) )
Comments
Post a Comment