Search code examples
mysqlsqlstringmysql-workbenchcreate-table

Why is this MySQL CREATE TABLE statement failing?


This create statement is failing, and I can't see the reason. MySQL reports:

Error Code: 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 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, objObjectType VARCHAR(100) ' at line 3*

CREATE TABLE `my_object_attribute_map` (
`objID` INT(10) UNSIGNED auto_increment NOT NULL primary key,
`objForeignKey` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, 
`objObjectType` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, 
`objTypeName` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, 
`objLabel` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, 
`objValue` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, 
`objDateCreated` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, 
`objCreatorID` CHAR(32) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci        
);

Solution

  • The not null constraint goes after the character set and collation:

    CREATE TABLE `my_object_attribute_map` (
        `objID` INT(10) UNSIGNED auto_increment NOT NULL primary key,
        `objForeignKey` VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
        `objObjectType` VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, 
        ...
    );