Adding Auto Increment and Unique to Table ID Failing - mysql

I am using mysql workbench to alter a table, and I have never had trouble with this before. I am trying to alter my table to have a unique table ID that auto increments. I get the following from my error screen:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
ALTER TABLE `madewix5_lindsey_website_data`.`products`
CHANGE COLUMN `product_id` `product_id` INT(3) NOT NULL AUTO_INCREMENT ,
ADD UNIQUE INDEX `product_id_UNIQUE` (`product_id` ASC) VISIBLE;
;
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
SQL Statement:
ALTER TABLE `madewix5_lindsey_website_data`.`products`
CHANGE COLUMN `product_id` `product_id` INT(3) NOT NULL AUTO_INCREMENT ,
ADD UNIQUE INDEX `product_id_UNIQUE` (`product_id` ASC) VISIBLE
It looks good to me syntactically, so I am not sure why it is saying there is an issue. There is ONE row of data in the table.
EDIT
According to mysql --version, my xampp server is currently using version Ver 15.1 Distrib 10.1.36-MariaDB, for Win32 (AMD64) so the suggested duplicate in the comments probably isn't applicable.

According to the help in the comments, the suggestion from other answers has been given but to clarify, on the specific case of an XAMPP server that is up to date with its Maria-DB software, the word "visible" may not be supported at this time. Removing the work "visible" allowed the script to run and added the unique and auto increment attributes to the primary key.
ALTER TABLE `madewix5_lindsey_website_data`.`products`
CHANGE COLUMN `product_id` `product_id` INT(3) NOT NULL AUTO_INCREMENT ,
ADD UNIQUE INDEX `product_id_UNIQUE` (`product_id` ASC);

Related

MY SQL ERROR: Error 1064: You have an error in your SQL syntax;

Executing SQL script in server
ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '
INDEX fk_film_actor_actor_id_idx (actor_id ASC) VISIBLE,
CONSTRAINT `fk' at line 10
SQL Code:
-- -----------------------------------------------------
-- Table `baza_filmova`.`film_actor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `baza_filmova`.`film_actor` (
`film_actor_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`film_id` INT(10) UNSIGNED NOT NULL,
`actor_id` INT(10) UNSIGNED NOT NULL,
`role` VARCHAR(45) NOT NULL,
PRIMARY KEY (`film_actor_id`),
INDEX `fk_film_actor_film_id_idx` (`film_id` ASC) VISIBLE,
INDEX `fk_film_actor_actor_id_idx` (`actor_id` ASC) VISIBLE,
CONSTRAINT `fk_film_actor_film_id`
FOREIGN KEY (`film_id`)
REFERENCES `baza_filmova`.`film` (`film_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_film_actor_actor_id`
FOREIGN KEY (`actor_id`)
REFERENCES `baza_filmova`.`actor` (`actor_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB
SQL script execution finished: statements: 7 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
I am using MySQL Workbench 8.0.18. I start making model and when I click to forward engineer it makes only to tables bud doesn't make this film_actor. Its middle table between film table and actor table.
Do somebody have same error before and how can I fix it.
PS: I doesn't make syntax error, I check my tables and foreign keys multiple times.
The error message really make it looks like the VISIBLE keyword is not supported in your version of MySQL/Maria DB.
This feature was introduced in MySQL 8.0. In MariaDB, it is not available as of now.
Note that VISIBLE is the default setting when not specified, so you don't actually need it in your query: the following is equivalent (and should work):
INDEX `fk_film_actor_film_id_idx` (`film_id`),
INDEX `fk_film_actor_actor_id_idx` (`actor_id`),

Appending primary key index to mysql (Using Mysql workbench) causing "GENERATED ALWAYS AS" error

I have an existing MySQL database table running on AWS, and that I'm connected through using MySQL Workbench.
I just tried to append an auto-generated primary key to the table using the GUI, and the script that gets generated is the following:
ALTER TABLE `participantData`.`resources`
ADD COLUMN `id` INT GENERATED ALWAYS AS () FIRST,
ADD PRIMARY KEY (`id`),
ADD UNIQUE INDEX `id_UNIQUE` (`id` ASC);
However, upon execution, I get the following error:
Executing:
ALTER TABLE `participantData`.`resources`
ADD COLUMN `id` INT GENERATED ALWAYS AS () FIRST,
ADD PRIMARY KEY (`id`),
ADD UNIQUE INDEX `id_UNIQUE` (`id` ASC);
Operation failed: There was an error while applying the SQL script to the database.
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 'GENERATED ALWAYS AS () FIRST,
ADD PRIMARY KEY (`id`),
ADD UNIQUE INDEX `id_UNIQU' at line 2
I'm not the most eloquent with MySQL, so I'm relying entirely on the GUI for this. Any help is appreciated!
Have you tried checking what is the current primary key, and dropping that first?
(Sorry this is posted as an answer and not a comment, I do not have enough reputation yet)

error in applying auto increment by 1 on specific column

I have a table name article and a column name articleid. I want that every time i enter the data in the table then value in column of articleid should be incremented to 1. I am trying to do like this.
ALTER TABLE article MODIFY COLUMN articleid INT auto_increment
But it generates this error statement:
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 '=1' at line 1
Your error message is
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 '=1' at line 1
But the query you've shown us is
ALTER TABLE article MODIFY COLUMN articleid INT auto_increment
Note =1 is not in the query you've shown us, so you are running some code that you are not intentionally trying to run. The syntax for your ALTER TABLE query looks correct.
Update: To ensure your MySQL increment by 1, set the MySQL configuration setting of auto_increment_increment = 1. Note in MySQL, this is a global setting and not a table or column setting.
http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment
A working SQL Statement to accomplish this is:
ALTER TABLE article CHANGE articleid articleid INT NOT NULL AUTO_INCREMENT
Please make sure that you've assigned the Primary key to the column 'articled'
If not, run this statement:
ALTER TABLE article ADD PRIMARY KEY(articled)
Because auto_increment can only be applied to the column that is the primary key of your table.
If it's not working at all, you could just start over by dropping your table (all data is lost!)
DROP TABLE article
And re-create it with all properties and keys assigned properly.
CREATE TABLE article (
articleid int NOT NULL AUTO_INCREMENT,
articletitle varchar(100) NOT NULL,
PRIMARY KEY (articleid)
)

how to add an auto Increment to a table which has composite key means two primary keys

I have two primary keys in one table. First one is PropertyID and second one is ImageID. I want to to add an auto increment with starting = 1000 to the PropertyID. I tried to use the command below, but get this error message:
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 'AUTO_INCREMENT = 1000' at line 3`
Here is the command:
Alter Table Properties
Alter Column `PropertyID` AUTO_INCREMENT = 1000;
You don't need to add ALTER COLUMN
ALTER TABLE `Properties` AUTO_INCREMENT =1000

"#1064 - You have an error in your SQL syntax" when I alter a table

When I try to alter a table I got this 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 'PRIMARY KEY (id)' at line 1
Here is my query:
ALTER TABLE gk
ADD COLUMN id MEDIUMINT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id);
How can I resolve this?
In an ALTER TABLE statement, a single ADD specification can't add both a column and a constraint; you'll need to put them in separate specifications:
ALTER TABLE gk ADD COLUMN id MEDIUMINT NOT NULL AUTO_INCREMENT,
ADD PRIMARY KEY (id);
(See ยง13.1.6 "ALTER TABLE Syntax" in the MySQL 5.6 Reference Manual.)
ALTER TABLE gk ADD id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY