PhpMyAdmin - SQL Creating tables Error #1064 - mysql

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

Related

MYSQL Error 1064: Can't seem to find the error exactly [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.
Hello I'm new to SQL and coding in general, and I'm seeing an error in my code and I can't seem to figure it out.
DROP TABLE IF EXISTS record, artist;
CREATE TABLE artist (
id INT AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
PRIMARY KEY (id)
);
CREATE TABLE record (
id INT AUTO_INCREMENT,
title VARCHAR(50),
artist_id INT,
genre TINYTEXT,
year YEAR(4),
price DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (artist_id)
REFERENCES artist (id)
);
CREATE TABLE order (
id INT AUTO_INCREMENT,
record_id INT,
quantity INT unsigned,
total DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (record_id)
REFERENCES record (id)
);
The error that shows up is:
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 (
id INT AUTO_INCREMENT,
record_id INT,
quantity INT unsigned,
total D' at line 1
I know it's telling me exactly where the error is, and I know it's a syntax error, but for some reason I just can't seem to understand where or what the error is exactly. I've tried adding 'order' with the DROP TABLE IF EXISTS but that just comes up with more errors. I know it's probably something really obvious so I'm sorry for asking but I'm so lost.
order is a reserved word in SQL. You could either protect it by surrounding it with forward quotes:
CREATE TABLE `order` (
id INT AUTO_INCREMENT,
record_id INT,
quantity INT unsigned,
total DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (record_id)
REFERENCES record (id)
);
or better yet, just find a name that ins't a reserved word, e.g., orders:
CREATE TABLE orders (
id INT AUTO_INCREMENT,
record_id INT,
quantity INT unsigned,
total DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (record_id)
REFERENCES record (id)
);

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

Can't create table <tablename> errno 150

Here is my SQL script
CREATE TABLE tracks(
track_id int NOT NULL AUTO_INCREMENT,
account_id int,
track_name varchar(255),
track_path varchar(255),
track_art_path varchar(255),
track_desc text,
primary key(track_id),
FOREIGN KEY (account_id) REFERENCES accounts_profile(accnt_id)
)
I don't see any syntax errors. Everything looks fine. My Database Engine is innoDB. but how come I keep on receiving this error?
#1005 - Can't create table 'beatbeast.tracks' (errno: 150)
It's not showing what line where the error is.
Errno 150 is generally the result of a mismatch between the exact data types of the main table's referenced column, and the referencing column. In your case, tracks.account_id is a signed INT, but the column it references accounts_profile.accnt_id is INT UNSIGNED. So you must create the tracks table using INT UNSIGNED for account_id as well:
CREATE TABLE tracks(
track_id int NOT NULL AUTO_INCREMENT,
account_id int UNSIGNED,
track_name varchar(255),
track_path varchar(255),
track_art_path varchar(255),
track_desc text,
primary key(track_id),
FOREIGN KEY (account_id) REFERENCES accounts_profile(accnt_id)
)
From the documentation:
If MySQL reports an error number 1005 from a CREATE TABLE statement,
and the error message refers to error 150, table creation failed
because a foreign key constraint was not correctly formed.
Check that the datatype of accounts_profile.accnt_id matches tracks.account_id exactly. Currently one is an int, so the other must also be an int.
Furhter, the documentation suggests to call:
SHOW ENGINE INNODB STATUS
after you get the error message for a more detailed explanation.

MySQL Syntax Error

This is my SQL Script
CREATE TABLE account_profile
(
accnt_id int NOT NULL ,
first_name varchar(255),
last_name varchar(255),
biography varchar(255),
date_joined DATE,
country varchar(255),
gender varchar(255),
screename varchar(255),
path varchar(255),
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
)
it kept giving me this error
SQL query:
CREATE TABLE ac
MySQL said:
#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 '' at line 1
What does this specifically mean? what should I do? from my point of view the script is quite okay,I just can't pin point where's the error
Make sure you're using the InnoDB engine. The other engines do not support foreign keys.
CREATE TABLE account_profile
(
...
) ENGINE = INNODB;
Also check if the column account_profile.accnt_id matches the data type of accounts.id exactly. The second column should have an index defined on it (a primary key will do.)
I have tested your query and it works with me too. Do you have a query before creating the table account_profile? because if you do, try to check if the query before has been terminated by a semi-colon. like this:
Create table TableName
(
-- fields list
); -- <== don't forget this before creating another table again.
CREATE TABLE account_profile
(
accnt_id int NOT NULL ,
first_name varchar(255),
last_name varchar(255),
biography varchar(255),
date_joined DATE,
country varchar(255),
gender varchar(255),
screename varchar(255),
path varchar(255),
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
); -- <== and also this.
the error says near line 1.
Probably your table account does not exists :
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
That's why you've got an error, the request is correct otherwise.

Why is my SQL statement throwing a syntax error?

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