Click here to Skip to main content
15,883,870 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I keep on getting errors when trying to insert values into my table that has a composite foreign key that consists of the primary key as well as another attribute. Here is my Query:

SQL
CREATE TABLE `User` (
  `IC` varchar(50),
  `Name` varchar(50),
  `PhNo` int(12),
  `Email` varchar(50),
  `UserType` varchar(50),
  CONSTRAINT UNIQUE (IC,UserType),
  PRIMARY KEY (`IC`)
);

CREATE TABLE `Patient` (
  `IC` varchar(50),
  `Case` varchar(50),
  `UserType` varchar(50),
  FOREIGN KEY (IC,UserType) REFERENCES USER (IC,UserType),
  PRIMARY KEY (`IC`)
);


When I tried to manually insert using a query, an syntax error pops up.

What I have tried:

This is the insert query that I had tried

SQL
INSERT INTO PATIENT(IC,Case,UserType) VALUES ("123456789101","Non-Urgent","Patient");
Posted
Updated 10-Jan-23 22:02pm

You haven't told us what the error is, so all we can do is guess.

At a guess, you have the ANSI_QUOTES[^] SQL mode enabled, which means:
If the ANSI_QUOTES SQL mode is enabled, string literals can be quoted only within single quotation marks because a string quoted within double quotation marks is interpreted as an identifier.
Therefore, you would need to fix your string literals:
SQL
INSERT INTO PATIENT (IC, Case, UserType) VALUES ('123456789101', 'Non-Urgent', 'Patient');
 
Share this answer
 
USER is a MySql Keyword and shouldn't be used as a table name - if it is, it probably needs to be enclosed in backticks:
FOREIGN KEY (IC,UserType) REFERENCES `USER` (IC,UserType),
Fix that, and your problem may go away.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900