why isnt it working as it should? it should do something with the foreign key... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why isnt it working as it should? it should do something with the foreign key...

CREATE TABLE User(customer_id INT , firstname char(20) , lastname char(20) , PRIMARY KEY(customer_id)); INSERT INTO User VALUES (1 , "YAHEL" , "BRAUN"); INSERT INTO User VALUES (2 , "CHARRLS" , "CARMIKEL"); CREATE TABLE Pay(customer_id INT NOT NULL, PRICE INT , FOREIGN KEY (customer_id) REFERENCES User(customer_id)); INSERT INTO Pay VALUES(3 , 23); SELECT * FROM Pay;

10th Sep 2020, 1:03 PM
Yahel
Yahel - avatar
3 Answers
+ 2
1. VARCHAR or char type values is expressed by single quotes, you used double quotes in your INSERT command for table 'User'. 2. You only have two records in 'User' table, having 'customer_id' value of 1 and 2. The referential connection between 'User.customer_id' and 'Pay.customer_id' should normally prevent insertion of value into 'Pay' table when such a 'customer_id' value about to be inserted does not exist in 'User' table
10th Sep 2020, 1:33 PM
Ipang
+ 1
You haven't create a user with customer_id 3, the foreign key error means that there isn't a customer with customer_id 3, you only inserted 1 & 2
10th Sep 2020, 1:26 PM
Heng Jun Xi
Heng Jun Xi - avatar
+ 1
Heng Jun Xi Ipang ok, thanks!
10th Sep 2020, 3:00 PM
Yahel
Yahel - avatar