简体   繁体   中英

MySQL table creation error in workbench

I'm new to MySQL and I'm running version 5.7.19-log (At least thats what I'm getting from command line client) with Workbench 6.3.9 Community edition. My problem is I'm trying to create a simple table with auto-generated id using the UI, however when I execute the generated script it throws me the following error:

  Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE TABLE `travelportaldb`.`user` (
  `user_id` INT GENERATED ALWAYS AS () VIRTUAL,
  `name` VARCHAR(45) NOT NULL,
  `account_id` VARCHAR(15) NULL,
  `account_currency` CHAR(3) NULL,
  `account_balance` DECIMAL NULL,
  PRIMARY KEY (`user_id`));

ERROR 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 ') VIRTUAL,
  `name` VARCHAR(45) NOT NULL,
  `account_id` VARCHAR(15) NULL,
  `ac' at line 2
SQL Statement:
CREATE TABLE `travelportaldb`.`user` (
  `user_id` INT GENERATED ALWAYS AS () VIRTUAL,
  `name` VARCHAR(45) NOT NULL,
  `account_id` VARCHAR(15) NULL,
  `account_currency` CHAR(3) NULL,
  `account_balance` DECIMAL NULL,
  PRIMARY KEY (`user_id`))

This is the script that it generated:

CREATE TABLE `travelportaldb`.`user` (
  `user_id` INT GENERATED ALWAYS AS () VIRTUAL,
  `name` VARCHAR(45) NOT NULL,
  `account_id` VARCHAR(15) NULL,
  `account_currency` CHAR(3) NULL,
  `account_balance` DECIMAL NULL,
  PRIMARY KEY (`user_id`));

I have no idea what's wrong. I'm from the oracle background and the query seems to be fine except the auto generated apart which I have no idea about.

Please help.

Thanks.

When you switch on the generated flag in MySQL Workbench the column details fields will change a bit to allow setting the required options for a generated column:

在此处输入图片说明

Most important here is the Expression field. For a generated column you have to specify an expression which is used for generation. Obviously you didn't do that and hence in your generated SQL the column definitions are wrong (empty parentheses, where an expression should be).

PS: You can also see the used server version in the Session tab page:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM