Search code examples
phpmysqlddlcreate-table

MySQL - PhpMyadmin - Create Multiple Tables


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)
);

Solution

  • 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)
    );