error in applying auto increment by 1 on specific column - mysql

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)
)

Related

Adding Auto Increment and Unique to Table ID Failing

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);

My MYSQL code won't run through a syntax checker

Online SQL syntax checkers have been giving me a lot of guff about my simple SQL code, and I don't understand why...
CREATE DATABASE IF NOT EXISTS DB ;
USE DB ;
CREATE TABLE IF NOT EXISTS teachers(
ID INT NOT NULL AUTO INCREMENT,
LAST_NAME TEXT,
FIRST_NAME TEXT,
COMPUTER_ID TEXT);
I pretty much copied this code from some SQL examples. I'm using mySQL and testing on SQLfiddle.com which is set to use MYSQL 5.6. It says Access denied for user 'user_9_79940c'#'%' to database 'db' if I have the first two lines there and if I remove them it says about the create table that I have a syntax error near AUTO INCREMENT
First of all, you are not allowed to create or switch databases on SQLFiddle. On your own MySQL instance, you might be able to, depending on what permissions you have. This is why the first two lines do not work in the SQLFiddle; the database is pre-created and pre-selected for you, and you are sandboxed in it.
The CREATE TABLE command will throw a syntax error complaining about AUTO INCREMENT - that is not a keyword, it should be written AUTO_INCREMENT.
There is still an error there: "there can be only one auto column and it must be defined as a key". There is only one auto column, but it is not a key yet. So, we'll make it one, by appending PRIMARY KEY. This works:
CREATE TABLE IF NOT EXISTS teachers(
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
LAST_NAME TEXT,
FIRST_NAME TEXT,
COMPUTER_ID TEXT);
Note that PRIMARY KEY will imply NOT NULL, so NOT NULL is unnecessary, and can be deleted without consequence.

MySQL not dropping index for unique constraint

I created a table and assigned a UNIQUE CONSTRAINT to a specific column.
... column_name INT UNSIGNED NOT NULL UNQUE
Now I no longer want the column to have the unique constraint, and so I tried modifying that:
ALTER TABLE mytable DROP INDEX column_name
like I saw here.
The above query executes successfully the first time. But when I try inserting duplicate values in the column_name column, I still get the error
#1062 Duplicate entry '10' for key column_name_2
Which, I presume, means the Constraint still remains on the table. (Also funny how _2 gets appended to the column name). But if I repeat the above ALTER statement, I get
#1091 - Can't DROP 'column_name'; check that column/key exists
I also tried
ALTER TABLE mytable DROP INDEX UNIQUE
and it gives me the 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 'UNIQUE' at line 1
Don't know if I'm missing something, but how do I remove the UNIQUE constraint from this column?
The issue came up because I had earlier ran the following query, in trying to remove the UNIQUE constraint.:
ALTER TABLE mytable CHANGE column_name column_name INT UNSIGNED NOT NULL
This somehow created a new column called column_name_2 and assigned it the UNIQUE constraint. So when I ran SHOW CREATE TABLE like #Paolof76 suggested, I found among the columns:
UNIQUE KEY `column_name_2` (`column_name`)
which i presume got created due to the ALTER statement above.
So I ran
ALTER TABLE mytable DROP INDEX column_name_2
which eliminated the unique key and solved my problem.
Thanks for the heads-up #Paolof76!

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

SQL Query error (create table)

I want to make a new table but it says my query is wrong, and I have no idea what's wrong with the query because I exported the database from SQLite Database browser.
Here's the query,
CREATE TABLE sqlite_sequence(name,seq);
and it says
#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 'seq)' at line 1
and if i can create that table i wanna insert into that table with this,
INSERT INTO sqlite_sequence VALUES('objek',55);
I hope it wont do another error.
Please tell me your opinion.
The only missing on your DDL is that it lacks datatypes on the columns,
CREATE TABLE sqlite_sequence
(
name VARCHAR(20),
seq INT
);
SQLFiddle Demo
UPDATE 1
MySQL Data Types
The sqlite_sequence table gets generated automatically when you assign a data field of a table to autoincrement (e.g. _id integer primary key autoincrement)
It is impossible to create table called sqlite_sequence manually because "Table Name cannot begin with sqlite_ as the object name reserved for internal use: SQLITE_SEQUENCE"
You need to specify the data types for name and seq.
Try:
create table SQLITE_SEQUENCE ( name varchar(255), seq int );
define data type for fields(columns)
CREATE TABLE sqlite_sequence(name varchar(200),seq int(8));
Here is a normal create table command for reference:
CREATE TABLE demo (id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE DEFAULT 0,name text);
And yeah there's no problem with the INSERT command.
You have to define data type after each table field Like this:
CREATE TABLE sqlite_sequence (
name VARCHAR(45),
seq INT
);