MySQL table creation error in workbench - mysql

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:

Related

In the MySQL WorkBench, when executing a Forward Engineering, an error 1064 appears in the log. Help me to understand

Executing SQL script in server
ERROR: 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(1000) NULL,
`place_of_birth` VARCHAR(1000) NULL,
' at line 5
SQL Code:
-- -----------------------------------------------------
-- Table `myBank`.`Clients`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `myBank`.`Clients` (
`id` INT GENERATED ALWAYS AS () VIRTUAL,
`name` VARCHAR(1000) NULL,
`place_of_birth` VARCHAR(1000) NULL,
`date_of_birth` DATE NULL,
`address` VARCHAR(1000) NULL,
`passport` VARCHAR(100) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
SQL script execution finished: statements: 6 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
MySQL supports generated columns, but you must give an expression in the parentheses. It's not valid to say GENERATED ALWAYS AS ().
If you intended to use ANSI SQL:2003 syntax for an identity column, MySQL does not support that syntax.
You can't make a generated column also be auto-increment.
https://dev.mysql.com/doc/refman/8.0/en/create-table-generated-columns.html says:
The AUTO_INCREMENT attribute cannot be used in a generated column definition.
An AUTO_INCREMENT column cannot be used as a base column in a generated column definition.
For an identity primary key, you should use:
CREATE TABLE IF NOT EXISTS `myBank`.`Clients` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(1000) NULL,
`place_of_birth` VARCHAR(1000) NULL,
`date_of_birth` DATE NULL,
`address` VARCHAR(1000) NULL,
`passport` VARCHAR(100) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB

Error 1064 with MySQL Workbench, with CREATE TABLE function

I'm new to MySQL and I'm trying to create tables inside a Database,
I used the 'Create a new table' function that appears on my SQL Workbench, instead of manually trying to add them. It writes this :
CREATE TABLE `Bory`.`Produits` (
`idProduits` NOT NULL,
`Marque` VARCHAR(45) NULL,
`NomProduit` VARCHAR(45) NULL,
PRIMARY KEY (`idProduits`));
So I go and click on "Apply" but I receive this message :
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE TABLE `Bory`.`Produits` (
`idProduits` NOT NULL,
`Marque` VARCHAR(45) NULL,
`NomProduit` VARCHAR(45) NULL,
PRIMARY KEY (`idProduits`));
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 'NOT NULL,
Marque VARCHAR(45) NULL,
NomProduit VARCHAR(45) NULL,
PRIMAR' at line 2
So now I am lost, because why is does function that is native to the Workbench itself keep making mistakes and doesn't let me add tables?
as mentioned in comments, You did't specified the data type for idProduits. for instance use int like below:
CREATE TABLE `Bory`.`Produits` (
`idProduits` int NOT NULL,
`Marque` VARCHAR(45) NULL,
`NomProduit` VARCHAR(45) NULL,
PRIMARY KEY (`idProduits`));

How do I debug SQL query with the following error message

This is my query.
CREATE TABLE `requirements`
(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`requirement_id` VARCHAR NOT NULL,
`state` VARCHAR NOT NULL,
`city` VARCHAR NOT NULL,
`salary_per_anum` FLOAT NOT NULL,
`is_closed` TINYINT(1) NOT NULL,
`created_updated` DATETIME NOT NULL,
CONSTRAINT `pk_requirements` PRIMARY KEY(`id`)
);
and this is the error message
ERROR 1064 (42000): 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 'NOT NULL, `state` VARCHAR NOT NULL, `city` VARCHAR NOT NULL, `salary_per_anum` F' at line 1
how can I debug what seems to be the issue?
You need to define a length of the string colum like
`requirement_id` VARCHAR(100)
and the same with column state and city.
But since requirement_id looks like a number id column it must be of the same type as id in table requirements, probably int.
Also your constraint seems to be defined wrong. It should refer to the table requirements.

Throwing MySQLSyntaxErrorException regarding syntax of SQL in Struts

I am making a project on Struts framework.
I am getting a exception from server while inserting data in a table using SQL and I am unable to find the solution.
The error is as following:
javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
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 ',,,,,,,'yes','yes',,,,'admin')' at line 1
My code which is generating the error is as following:
sql="Insert into field_visit_details (visit_date,block,village,flw_category,
flw_name,flw_contact_no,aware_zinc,
aware_ors,past_zinc,past_ors,qty_avail_zinc_ten,qty_avail_zinc_twe,qty_avail_zinc_syr,
qty_avail_ors,qty_disp_zinc_ten,qty_disp_zinc_twe,qty_disp_zinc_syr,qty_disp_ors,
stockout_zinc,stockout_ors,diar_cases_seen,diar_cases_reff,diar_deaths_less_than_five,
added_by)
values
('"+field_visit_date+"','"+block_row_one+"','"+village_row_one+"','"+flw_category_row_one
+"','"+flw_name_row_one+"','"+flw_contact_no_row_one+"','"+aware_zinc_row_one+"','"+
aware_ors_row_one+"','"+past_zinc_row_one+"','"+past_ors_row_one+"','"+
qty_avail_zinc_ten_row_one+"','"+qty_avail_zinc_twe_row_one+"','"+
qty_avail_zinc_syr_row_one+"','"+qty_avail_ors_row_one+"','"+qty_disp_zinc_ten_row_one
+"','"+qty_disp_zinc_twe_row_one+"','"+qty_disp_zinc_syr_row_one+"','"+
qty_disp_ors_row_one+"','"+stockout_zinc_row_one+"','"+stockout_ors_row_one+
"','"+diar_cases_seen_row_one+"','"+diar_cases_reff_row_one+"','"+diar_deaths_row_one
+"','"+loginid+"')";
System.out.println(sql);
int x=stmt.executeUpdate(sql);
if(x>0)
SUCCESS="admin";
Table Structure is as following:
CREATE TABLE IF NOT EXISTS `field_visit_details` (
`field_visit_id` int(11) NOT NULL auto_increment,
`visit_date` date NOT NULL,
`block` varchar(100) NOT NULL,
`village` varchar(100) NOT NULL,
`flw_category` varchar(45) NOT NULL,
`flw_name` varchar(100) NOT NULL,
`flw_contact_no` varchar(13) NOT NULL,
`aware_zinc` varchar(10) NOT NULL,
`aware_ors` varchar(10) NOT NULL,
`past_zinc` varchar(10) NOT NULL,
`past_ors` varchar(10) NOT NULL,
`qty_avail_zinc_ten` int(11) NOT NULL,
`qty_avail_zinc_twe` int(11) NOT NULL,
`qty_avail_zinc_syr` int(11) NOT NULL,
`qty_avail_ors` int(11) NOT NULL,
`qty_disp_zinc_ten` int(11) NOT NULL,
`qty_disp_zinc_twe` int(11) NOT NULL,
`qty_disp_zinc_syr` int(11) NOT NULL,
`qty_disp_ors` int(11) NOT NULL,
`stockout_zinc` varchar(11) NOT NULL,
`stockout_ors` varchar(11) NOT NULL,
`diar_cases_seen` int(11) NOT NULL,
`diar_cases_reff` int(11) NOT NULL,
`diar_deaths_less_than_five` int(11) NOT NULL,
`added_by` varchar(100) NOT NULL,
PRIMARY KEY (`field_visit_id`) )
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The query when I printed in the console was :
Insert into field_visit_details (visit_date,block,village,flw_category,flw_name,
flw_contact_no,aware_zinc,aware_ors,past_zinc,past_ors,qty_avail_zinc_ten,
qty_avail_zinc_twe,qty_avail_zinc_syr,qty_avail_ors,
qty_disp_zinc_ten,qty_disp_zinc_twe,qty_disp_zinc_syr,qty_disp_ors,stockout_zinc,
stockout_ors,diar_cases_seen,diar_cases_reff,diar_deaths_less_than_five,added_by)
values
('2014-07-02','ASDASD','','asha','asd+','99','yes','yes','yes','yes',
'12','12','12','12','12','12','12','12','yes','yes','12','12','12','admin')
But the thing is that the query is inserting the data successfully in the table but still server is showing the above error.
I even used the above printed query directly in phpmyadmin and worked well without any problem.
You have a syntax exception in the SQL statement. The error message you have posted point to it.
So, you need to change SQL statement and fix syntax errors. For this purpose you should rewrite it to use PraparedStatement like in the example JDBC PreparedStatement example – Insert a record.
And use appropriate to the database column types set methods when setting parameters to the prepared statement.

MySQL 5.6.13 not executing create table properly

I have the following SQL to create a table on a MySQL 5.6.13 instance:
CREATE TABLE 'exchange' (
'id' int NOT NULL AUTO_INCREMENT,
'abbrev' varchar(32) NOT NULL,
'name' varchar(255) NOT NULL,
'city' varchar(255) NULL,
'country' varchar(255) NULL,
'currency' varchar(128) NULL,
'time_zone_offset' time NULL,
'created_date' datetime NOT NULL,
'last_updated_date' datetime NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
However, I keep getting the following unhelpful error:
ERROR 1064 (42000): 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
''exchange' ( 'id' int NOT NULL AUTO_INCREMENT,
'abbrev' varchar(32) NOT NULL, 'n' at line 1
I must be missing something glaringly obvious...
Any ideas where I'm going wrong?
Try :
CREATE TABLE exchange (
Ref:
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
Remove all quotes, this helped me on a windows machine command line