I am trying to create a table with only two foreign key

Shivangi Gupta

I have searched a lot but I could get only the concept that this is used for many to many linking. Or separate syntax for foreign key and primary key. But could not correct syntax as a whole.

CREATE TABLE cart
(
Customer varchar(40) FOREIGN KEY REFERENCES users(UserName),
Product varchar(40)  FOREIGN KEY REFERENCES products(PID),
CONSTRAINT combination PRIMARY KEY (Customer,Product)
);

I am getting the error

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FOREIGN KEY REFERENCES users(UserName), Product varchar(40) FOREIGN KEY RE' at line 3

pilsetnieks

Foreign key definitions go after all of the column definitions, they aren't defined inside the column definition. In your case, it would rather be:

CREATE TABLE cart
(
    Customer varchar(40),
    Product varchar(40),
    FOREIGN KEY (Customer) REFERENCES users(UserName),
    FOREIGN KEY (Product) REFERENCES products(PID),
    PRIMARY KEY (Customer,Product)
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

i am trying to create foreign key constraint in laravel 5.8

I'm trying to create Two Tables in sql.js but there is an error regarding foreign key constraint. there is no problem in creating the first table

I get ER_PARSE_ERROR when trying to CREATE a TABLE with PRIMARY KEY and FOREIGN KEY

SQL: I am trying to add a foreign key but it give me error

Why can I not create a foreign key for this table?

How can I create a Table with two Foreign Key References to one other Table via UCanAccess?

I am trying to create a table with sqlite in Kotlin

Error when trying to create foreign key for table in SQL?

mysql error 1822 when trying to create new table with a FOREIGN KEY

how to join two table only when foreign key value is not null

Cant create a foreign key for a table that has two primary keys set

I keep getting errors when trying to add a foreign key, what am I doing wrong?

Why can't I properly create a table with a foreign key?

How do i create a foreign key on table creation

How do I create a table with a foreign key reference?

I am trying to create a table (Order) using schema with mutable FK

Why am I getting a syntax error when trying to create a table?

I am trying to create a table in SQL and keep getting an error

Unable to create table with foreign key

pymysql - FOREIGN KEY CREATE TABLE

i am trying to create foreign keys but i got error 1822 .. please see my code below

Keep getting "IntegrityError FOREIGN KEY constraint failed" when i am trying to delete a post

Why am I getting 'Foreign Key Mismatch' when trying to save a model form in Django?

How do I programmatically create two foreign keys in mysql table

SQL syntax error 1064 when trying to create a foreign key constraint in a create table query

Syntax error while trying to create a foreign key

Error (1005) when trying to create foreign key

I am gettng the error code 1170 when I try to alter a table to add a foreign key

MySQL Failed to initialize JPA EntityManagerFactory. I want to create two table in which users table is main table which has foreign key for courses