Reputation: 496
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
);
Upvotes: 0
Views: 1668
Reputation: 222442
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,
...
);
Upvotes: 3
Reputation: 98388
The syntax of column definitions has some warts; see https://dev.mysql.com/doc/refman/8.0/en/create-table.html and https://dev.mysql.com/doc/refman/8.0/en/string-type-syntax.html.
Basically, the CHARACTER SET is part of the data type, and must come before the NOT NULL, which is an extra attribute that applies to any type. COLLATE I believe in principle is also part of the data type, and should also come before NOT NULL, but if you do specify it afterwards it still works (or is ignored, for non-string types), presumably for backwards compatibility.
Since the collation set also determines the character set, you could just leave character set out and specify COLLATE wherever you want.
Upvotes: 0