Why is my SQL statement throwing a syntax error? - mysql

I tried to create a table like so:
CREATE TABLE Persons
(
id int PRIMARY KEY AUTOINCREMENT,
metalId varchar(255),
colorId varchar(255)
)
but it get an 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 'AUTOINCREMENT, metalId varchar(255), colorId varchar(255) )' at
line 3
Anyone know what's wrong with my code?

You missed an underscore. AUTOINCREMENT should be AUTO_INCREMENT.
Using AUTO_INCREMENT

Try
CREATE TABLE Persons
(
id int PRIMARY KEY AUTO_INCREMENT,
metalId varchar(255),
colorId varchar(255)
)
Here is main source.
Syntax for MySQL
The following SQL statement defines the "P_Id" column to be an
auto-increment primary key field in the "Persons" table:
CREATE TABLE Persons
(
P_Id int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment
feature.
By default, the starting value for AUTO_INCREMENT is 1, and it will
increment by 1 for each new record.
To let the AUTO_INCREMENT sequence start with another value, use the
following SQL statement: ALTER TABLE Persons AUTO_INCREMENT=100

Related

[42000][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 [duplicate]

This question already has answers here:
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Closed 3 months ago.
I have created tables like before, with the given primary and foreign keys. However I get this error when I try to create a new table with the code below.
create table Order (
oid int(255),
sid int(255),
sku int(255),
quantity int(255),
foreign key (sid) references Suppliers(sid),
foreign key (sku) references Parts(sku),
primary key(sid,sku)
)
and I have created Suppliers and Parts tables with the code below
create table Parts(
sku int(255) auto_increment primary key,
pname varchar(255),
stock_level int(255),
color varchar(255)
)
create table Suppliers (
sid int(255) auto_increment primary key,
sname varchar(255),
city varchar(255),
street varchar(255)
)
sid and sku already exist in their respective tables. I do not understand why I get such an error.
The complete output is:
[42000][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 'Order( oid int (255), sid int (255), sku int(255), quantity
i' at line 1
I solved the problem with creating the table with the name "Orders" instead of just Order. I think it got confused with the order by keyword hence the syntax error.
I suppose it chokes on INT(255).
INT is a signed four-byte integer. Its max positive value is 2147483647.
So, just use INT and not INT(255), and it will work.
Check this page on the topic, for example:
https://blog.devart.com/mysql-int-data-type.html#what_is_mysql_integer

mysql create a composite key with different data types

So let's say I am trying to create a table in MYSQL that contains 3 columns, Id, Name and Price. How would I go about creating a composite key between the Id and Name, given that they are different data types? I have tried the following query but am presented with an error.
CREATE TABLE name_price (
id int NOT NULL,
name Varchar NOT NULL,
price int,
PRIMARY KEY (id, name)
);
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,
price int,
PRIMARY KEY (id, name)
)' at line 3
Any help would be appreciated.
You are simply missing the varchar length for the name. E.g.:
CREATE TABLE name_price (
id int NOT NULL,
name varchar(100) NOT NULL,
price int,
PRIMARY KEY (id, name)
);

Error in CREATE table statement in MySQL [duplicate]

This question already has answers here:
Error creating table: You have an error in your SQL syntax near 'order( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1 [duplicate]
(2 answers)
Closed 5 years ago.
I wanted to create a table ORDER in database called Projects and which has another table CUSTOMER.
Here are the statements I used :
mysql> Create Table ORDER(
-> OrderNo Integer(5) Primary Key,
-> CustNo Integer(7),
-> ItemName Varchar(30),
-> Qty Integer(5),
-> Price Decimal(6,2) Not Null,
-> Foreign Key (CustNo) references CUSTOMER(CustID));
but I am getting this 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 'ORDER(
OrderNo Integer(5) Primary Key,
CustNo Integer(7),
ItemName Varchar(30),
' at line 1
What is wrong here?
As the comments mentioned, ORDER among others are reserved in MySQL. The complete list is in the documentation here.
You have three options, either to change the table name (my personal recommendation), escape the name, or qualify the name with the database such as: CREATE TABLE mydb.ORDER ..... If you do choose to stick with a keyword named table (or column), you run the risk of confusion later if you forget to escape it in another query. If this is tied to customers, perhaps it could be the 'CustomerOrder' table.
CREATE TABLE `ORDER`(
OrderNo Integer(5) Primary Key,
CustNo Integer(7),
ItemName Varchar(30),
Qty Integer(5),
Price Decimal(6,2) Not Null,
Foreign Key (CustNo) references CUSTOMER(CustID)
);
Given that you have a primary key, you may also want it to auto increment so you don't have to create the keys manually.
CREATE TABLE `ORDER`(
OrderNo Integer(5) Primary Key AUTO_INCREMENT,
CustNo Integer(7),
ItemName Varchar(30),
Qty Integer(5),
Price Decimal(6,2) Not Null,
Foreign Key (CustNo) references CUSTOMER(CustID)
);

PhpMyAdmin - SQL Creating tables Error #1064

I'm trying to create some tables, with a one-to-many relationship between them. I get this error, I have read other threads, and suspect it is something to do with the FK. Any help would be great! :D Thanks
I'm using PhpMyAdmin on a XAMPP server. I created the database, clicked SQL tab, and put my code it. I got the error below.
Error:
Error
Static analysis:
2 errors were found during analysis.
Unexpected beginning of statement. (near "userID" at position 307)
Unrecognized statement type. (near "INT" at position 314)
SQL query:
CREATE TABLE reportTable ( reportID INT AUTO_INCREMENT, submitDate DATETIME, submitBy VARCHAR(255), reportInformation VARCHAR(255), reviewedBy VARCHAR(255), PRIMARY KEY(reportID), FOREIGN KEY(userID) REFERENCES userTable(userID) ) CREATE TABLE userTable ( userID INT AUTO_INCREMENT, username VARCHAR(255), PRIMARY KEY(userID) )
MySQL said: Documentation
#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 'CREATE TABLE userTable (
userID INT AUTO_INCREMENT,
username VARCHAR(2' at line 11
Code:
CREATE TABLE reportTable (
reportID INT AUTO_INCREMENT,
userID INT,
submitDate DATE,
submitBy VARCHAR(255),
reportInformation VARCHAR(255),
reviewedBy VARCHAR(255),
PRIMARY KEY(reportID),
FOREIGN KEY(userID) REFERENCES userTable(userID)
);
CREATE TABLE userTable (
userID INT AUTO_INCREMENT,
username VARCHAR(255),
PRIMARY KEY(userID)
);
As I mentioned in my comment, semi-colons are usually used to end statements. Otherwise, MySQL doesn't know when you mean to stop. When doing one query in PHPMyAdmin, it will interpret it properly, but not when you do more than one statement. This will fix your run-on statements.
CREATE TABLE reportTable (
reportID INT AUTO_INCREMENT,
submitDate DATE,
submitBy VARCHAR(255),
reportInformation VARCHAR(255),
reviewedBy VARCHAR(255),
PRIMARY KEY(reportID),
FOREIGN KEY(userID) REFERENCES userTable(userID)
);
CREATE TABLE userTable (
userID INT AUTO_INCREMENT,
username VARCHAR(255),
PRIMARY KEY(userID)
);

#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

Hello I am new to sql and only really using it for JDBC, i am trying to create a database call SuperMarket and create the tables with this sql script
CREATE TABLE item (
ID INT NOT NULL CONSTRAINT pk_item PRIMARY KEY,
name VARCHAR(30),
price DOUBLE PRECISION,
quantity INT
);
CREATE TABLE customer (
ID INT NOT NULL CONSTRAINT pk_customer PRIMARY KEY,
name VARCHAR(30)
);
CREATE TABLE employee (
ID INT NOT NULL CONSTRAINT pk_employee PRIMARY KEY,
name VARCHAR(30),
password VARCHAR(15)
);
CREATE TABLE trans (
ID INT NOT NULL CONSTRAINT pk_transaction PRIMARY KEY,
employee INT,
customer INT,
is_open INT
);
CREATE TABLE acquisition (
ID INT NOT NULL CONSTRAINT pk_acquisition PRIMARY KEY,
trans INT,
item INT,
quantity INT
);
When i try to import it in phpMyAdmin using xampp i get the following error:
Number 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 'CONSTRAINT pk_item PRIMARY KEY,
name VARCHAR(30),
price DOUBLE PRECISION,
qua' at line 2
I still cannot find any syntax error can anyone tell me what I am doing wrong?
Try this:
CREATE TABLE item (
ID INT NOT NULL,
name VARCHAR(30),
price DOUBLE PRECISION,
quantity INT,
CONSTRAINT pk_item PRIMARY KEY (ID)
);
Your CONSTRAINT keyword was in the wrong spot. Same goes for the rest of the tables as well.
If you simply want ID to be the primary key, you can do:
CREATE TABLE item (
ID INT NOT NULL PRIMARY KEY,
name VARCHAR(30),
price DOUBLE PRECISION,
quantity INT
);