Auto increment column - mysql

I want to make an AUTO_INCREMENT column in a database table,here is the syntax i write:
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment
);
and i get the following error:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
then i made it as a primary key:
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int primary_key auto_increment
);
and i get the following 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 'primary_key auto_increment,name varchar(20),mail varchar(30),comment varchar(100' at line 1
what is wrong???

It is PRIMARY KEY without the underscore.
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int primary key auto_increment
);
or
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment,
primary key(`com_no`)
);

create table comments(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment,
PRIMARY KEY (com_no)
);
(as per on-line MySQL manual).

create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (com_no)
);
ref.

Use primary key in place of primary_key

The proper syntax goes like this for example:
CREATE TABLE `admin` (
`id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`userid` VARCHAR(50) NULL DEFAULT '0',
`pwd` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)

In MySQL, there can be only one auto increment column (that is generally known as identity column) and it should be also defined as a unique key. For example:
create table comments
(
com_no int NOT NULL AUTO_INCREMENT,
name varchar(20),
mail varchar(30),
comment varchar(100),
PRIMARY KEY (com_no)
);
Please see MySQL auto increment documentation for more details.

Related

MySQL concat with special characters [duplicate]

Here is my failing MySQL code:
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
'item-name' VARCHAR(255) NOT NULL,
'item-description' TEXT,
'listing-id' VARCHAR(50),
PRIMARY KEY (id)
)
The error is:
Error creating table: 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
'item-name' VARCHAR(255) NOT NULL, 'item-description' TEXT, 'listing-id'' at line 3
The documentation says to use quotes... What is wrong?
Use ` instead of ':
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
You should use the back-quote (`) to quote column names, not the single-quote ('). Look above the tilde key (~).
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
Why do you use quotes? You should use backticks. Try this:
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
Please, use normal character in table definition. You can use underscore "_" instead "-".
When you'll query your db, you must always use quote. Impossible for me! :)
If you want follow this road, use the quote
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
maybe it is coming late. but this worked for me.
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
[item-name] VARCHAR(255) NOT NULL,
[item-description] TEXT,
[listing-id] VARCHAR(50),
PRIMARY KEY (id)
)

Why am I getting an error in my SQL command?

mysql>
CREATE TABLE twitter(
username varchar(255),
created_at varchar(45),tweet text,
retweet_count int(11),
location varchar(100),
place varchar(100),
PRIMARY KEY int(11));
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 'int(11))' at line 1mys
The PRIMARY KEY clause is wrong.
You need to specify a name for your primary key column.
So, that would be ... id int(11), PRIMARY KEY (id));
You can not define PRIMARY KEY like that. The syntax for PRIMARY KEY is
PRIMARY KEY(Id)
Where you'll also have to create a column named Id
Id int(11)
try this
CREATE TABLE twitter (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(255),
created_at varchar(45),
tweet text,
retweet_count int(11),
location varchar(100),
place varchar(100),
PRIMARY KEY (id)
);

Creating table within mysql db, ERROR 1064 (42000)?

**Am working with MySQL 8.0 Command Line Client
I am trying to create a general table in mysql db but keep getting the following 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 'AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am I' at line 2"
I have look through the manual but haven't found anything that helps with this issue; have also tried removing the index (id) column and assigning it as the KEY
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO),
);
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (id)
);
Primary key should be id
Updated answer as per you comment
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO),
UNIQUE KEY (id)
);
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO), <--- remove comma from here
);
and make PRIMARY KEY(id);
I had the same error message, after a long time I realized that I was trying to create a table by name "order" and that was the problem.As soon as I changed the name it worked!!!

Cannot add foreign key constraints, Mysql

please could you please tell me what I am doing wrong?
I have MySQl script but finally I got this
Error 1215: Cannot add foreign key constraints
Here is code:
CREATE TABLE IF NOT EXISTS profile_public_data
(
idProfile INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
login VARCHAR(64),
name VARCHAR(45),
lastName VARCHAR(45),
location VARCHAR(45),
is_active BOOLEAN DEFAULT false,
age TINYINT,
photo VARCHAR(45),
PRIMARY KEY(idProfile)
);
CREATE TABLE IF NOT EXISTS profile_private_data
(
idProfile INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
CONSTRAINT fk_idProfile
FOREIGN KEY(idProfile)
REFERENCES `profile_public_data`(idProfile)
ON DELETE SET NULL
ON UPDATE SET NULL,
email VARCHAR(45),
password VARCHAR(45),
PRIMARY KEY(idProfile)
);
You idProfile column in the dependent table is set as auto increment, this is not correct. It should be just INT(11).
Its value must be specified and exists in the source table when when inserting a new record, instead of auto incremented.

How to use special characters in MySQL column names?

Here is my failing MySQL code:
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
'item-name' VARCHAR(255) NOT NULL,
'item-description' TEXT,
'listing-id' VARCHAR(50),
PRIMARY KEY (id)
)
The error is:
Error creating table: 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
'item-name' VARCHAR(255) NOT NULL, 'item-description' TEXT, 'listing-id'' at line 3
The documentation says to use quotes... What is wrong?
Use ` instead of ':
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
You should use the back-quote (`) to quote column names, not the single-quote ('). Look above the tilde key (~).
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
Why do you use quotes? You should use backticks. Try this:
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
Please, use normal character in table definition. You can use underscore "_" instead "-".
When you'll query your db, you must always use quote. Impossible for me! :)
If you want follow this road, use the quote
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
maybe it is coming late. but this worked for me.
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
[item-name] VARCHAR(255) NOT NULL,
[item-description] TEXT,
[listing-id] VARCHAR(50),
PRIMARY KEY (id)
)