I have below two parent-(UserLogins,UserSecurityQuestions) and one child table - UserSecurityAnswers
#Parent Table
CREATE TABLE UserLogins
(
UserLoginID SMALLINT NOT NULL IDENTITY(1,1),
UserLogin VARCHAR(50) NOT NULL,
UserPassword VARCHAR(20) NOT NULL,
CONSTRAINT pk_UL_UserLoginID PRIMARY KEY(UserLoginID)
);
CREATE TABLE UserSecurityQuestions
(
UserSecurityQuestionID TINYINT NOT NULL IDENTITY(1,1),
UserSecurityQuestion VARCHAR(50) NOT NULL,
CONSTRAINT pk_USQ_UserSecurityQuestionID PRIMARY KEY(UserSecurityQuestionID)
);
#Child Table
CREATE TABLE UserSecurityAnswers
(
UserLoginID SMALLINT NOT NULL IDENTITY(1,1),
UserSecurityAnswers VARCHAR(25) NOT NULL,
UserSecurityQuestionID TINYINT NOT NULL,
CONSTRAINT pk_USA_UserLoginID PRIMARY KEY(UserLoginID),
CONSTRAINT fk_UL_UserLoginID FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID),
CONSTRAINT fk_USQ_UserSecurityQuestionID FOREIGN KEY(UserSecurityQuestionID) REFERENCES UserSecurityQuestions(UserSecurityQuestionID)
);
#INsert value in to parent table
#UserLogins
insert into UserLogins values('User1', 'Pass1');
insert into UserLogins values('User2', 'Pass2');
insert into UserLogins values('User3', 'Pass3');
insert into UserLogins values('User4', 'Pass4');
insert into UserLogins values('User5', 'Pass5');
#UserSecurityQuestions
insert into UserSecurityQuestions values('What is your favourite food?');
insert into UserSecurityQuestions values('What is your favourite food?');
insert into UserSecurityQuestions values('What is your favourite food?');
insert into UserSecurityQuestions values('What is your favourite food?');
insert into UserSecurityQuestions values('What is your favourite food?');
Now i am updating table UserSecurityAnswers with below value
insert into UserSecurityAnswers values('Apples');
insert into UserSecurityAnswers values('Spiderman');
insert into UserSecurityAnswers values('School1');
insert into UserSecurityAnswers values('Ram');
insert into UserSecurityAnswers values('Toyota');
But i am getting below error
Column name or number of supplied values does not match table definition.
i expected child table would inherited values from parent table in this case. but not sure what wrong with this implementation
Any help on this?
Only the Primary keys will be taken automatically during insert if its given as increment. All other columns you have to supply, if you just given "Apply" the db doesnt know to map it against the parent FK id.
In your insert query into Answers table, include the PK value from other two tables.
btw- you are not updating the securityanswer table, you are trying to insert fresh. So the FK columns are mandatory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.