0

I can´t understand why this query is not working to create this table structure in PHPMyAdmin.

I always receive this error message:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

near 'MAX), FOREIGN KEY (form_id) REFERENCES form (Id) )' at line 4

CREATE TABLE form(
    Id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    Title VARCHAR(500),
    Is_Active BIT,
    Is_Trash BIT,
    Date_Created DATETIME
);
CREATE TABLE form_data(
    Id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    form_id INT,
    formdata VARCHAR(MAX),
    FOREIGN KEY (form_id) REFERENCES form (Id)
);

3 Answers 3

3

varchar(max) is MS SQL Server's syntax. MySQL doesn't have an equivalent, so you'll just have to use a very long size. Additionally, once you'll solve this problem, you'll encounter a problem where form_data.form_id is not of the the same type as form.id (one is unsigned and the other isn't), so it cannot reference it. In short:

CREATE TABLE form_data(
    Id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    form_id INT UNSIGNED, -- Second issue
    formdata VARCHAR(4000), -- First issue 
    FOREIGN KEY (form_id) REFERENCES form (Id)
);
Sign up to request clarification or add additional context in comments.

Comments

2

There are 2 problems in table form_data:

  1. type of form_id, it should be INT UNSIGNED to match type of Id type of table form
  2. Using formdata VARCHAR(MAX), you should change MAX to real integer, 255 for example

Try this code:

CREATE TABLE form(
    Id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    Title VARCHAR(500),
    Is_Active BIT,
    Is_Trash BIT,
    Date_Created DATETIME
);
CREATE TABLE form_data(
    Id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    form_id INT UNSIGNED,
    formdata VARCHAR(255),
    FOREIGN KEY (form_id) REFERENCES form (Id)
);

Comments

1

Your error come from :

formdata VARCHAR(MAX),

Change MAX to a value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.