creating a table in the database with a foreign key

Viewed 23

I am getting this error whenever I try to create my table:

#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 'NOT NULL, hehi VARCHAR NOT NULL, adds VARCHAR NOT NULL, aob VA...' at line 3

This is my code:

CREATE TABLE patients (
    patientId int NOT NULL,
    problem VARCHAR NOT NULL,
    hehi VARCHAR NOT NULL,
    adds VARCHAR NOT NULL,
    aob VARCHAR NOT NULL,
    PRIMARY KEY (patientID),
    CONSTRAINT FK_userspatients FOREIGN KEY (user_id)
    REFERENCES users(user_id)
);

What might be the problem?

1 Answers

You need to specify the lengths of your VARCHARs, e.g. if the column problem is supposed to hold 255 characters max.:

problem VARCHAR(255) NOT NULL

see https://mariadb.com/kb/en/varchar/ for more info

Update: As P.salmon mentioned in the comments, your foreign key definition should be updated to

CONSTRAINT FK_userspatients FOREIGN KEY (patientId)

If patientId refers to the corresponding field user_id of table users.

Related