Why am I getting an error in my SQL command? - mysql

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

Related

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.

SQL foreign key error?

DROP TABLE IF EXISTS userRole;
DROP TABLE IF EXISTS account;
CREATE TABLE account (
lionID VARCHAR(50) NOT NULL,
firstName VARCHAR(50) NOT NULL,
hashedpass VARCHAR(255) NOT NULL,
PRIMARY KEY (lionID)
);
CREATE TABLE roles (
roleID INT UNSIGNED AUTO_INCREMENT NOT NULL,
lionID VARCHAR(50) NOT NULL,
administrator BOOLEAN NOT NULL DEFAULT False,
qRole VARCHAR(255) NOT NULL,
PRIMARY KEY (roleID),
FOREIGN KEY (lionID) REFERENCES account(lionID)
);
I am having trouble understanding why I am getting an error.. the foreign key lionID in roles is referencing the primary key lionID in account but it doesn't seem to like it. Any help is greatly appreciated.
ERROR 1064 (42000) at line 76: 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 'FOREIGN KEY lionID REFERENCES account lionID'
)' at line 8
I think the problem is in create statement:
You need to use Syntax:
CONSTRAINT `fk_account`
FOREIGN KEY (lionID) REFERENCES account(lionID)

Syntax error when creating a foreign key constraint

I am creating a table where it has a foreign key so that it would be linked to another table, but it kept me giving this error, I already checked the syntax on w3schools, but I still keep getting errors any idea why? here's my SQL script
CREATE TABLE user_profile
(
user_Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
accnt_id INT,
first_name varchar(255),
last_name varchar(255),
biography TEXT,
date_joined DATETIME,
date_of_birth DATE,
email varchar(255),
gender varchar(255),
screenname varchar(255)
country varchar(255),
FOREIGN KEY (accnt_Id) REFERENCES accounts(accnt_Id)
)
Here's 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 'country varchar(255), FOREIGN KEY (accnt_Id) REFERENCES accounts(accnt_Id) )' at line 13
You are missing , behind screenname varchar(255) change it to
screenname varchar(255),
and it should works.
You are missing a comma after the 'screenname' column
CREATE TABLE user_profile
(
user_Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
accnt_id INT,
first_name varchar(255),
last_name varchar(255),
biography TEXT,
date_joined DATETIME,
date_of_birth DATE,
email varchar(255),
gender varchar(255),
screenname varchar(255),
country varchar(255),
FOREIGN KEY (accnt_Id) REFERENCES accounts(accnt_Id)
)

Auto increment column

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.