MySQL Syntax Error - mysql

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.

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

Can´t create a column age from a column birthday_year in mysql

so I have a simple create table animal, for which I pretend to fill the age column by inserting only the birtday_year.
I tried de getdate(), but can´t use a non-deterministic function, and even with other options, I end up with the simple one(from my point of view). But i can´t accomplish the requirements of this task.
Here is the code and one of the error:
create table animal
( nome varchar(255),
VAT integer,
species_name varchar(255),
color varchar(255),
gender varchar(255),
birth_year integer,
age generated always as ( 2018 - birth_year),
primary key (nome,VAT),
foreign key (VAT) references cliente(VAT) ,
foreign key (species_name) references species(nome) );
---Error Code: 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 'generated always as ( 2018 - birth_year), primary key (nome,VAT), foreign ke' at line 8
I tried also:
create table animal
( nome varchar(255),
VAT integer,
species_name varchar(255),
color varchar(255),
gender varchar(255),
birth_year integer,
age integer as ( 2018 - birth_year),
primary key (nome,VAT),
foreign key (VAT) references cliente(VAT) ,
foreign key (species_name) references species(nome) );
--Error Code: 3105. The value specified for generated column 'age' in table 'animal' is not allowed.
Thanks for the help.
Best regards.
In MySQL, you need to specify the type:
create table animal (
nome varchar(255),
VAT integer,
species_name varchar(255),
color varchar(255),
gender varchar(255),
birth_year integer,
age int generated always as ( 2018 - birth_year),
primary key (nome,VAT),
foreign key (VAT) references cliente(VAT) ,
foreign key (species_name) references species(nome)
);
Your version with:
age integer as ( 2018 - birth_year),
Should also work, assuming your version of MySQL supports generated columns.
Note that the definition of age is rather specific. Ideally, you would want:
year(curdate()) - birth_year
or something like that.
MySQL does not allow a volatile function such as curdate() to be used for a generated column. To implement this functionality, you need to use a view:
create table v_animal as
select a.*, (year(curdate()) - birth_year) as age
from animal;

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

foreign key not creating in table

first table:
create table login(
id int auto_increment,
user varchar(20),
pass varchar(20),
primary key(id)
);
first table is created with primary key..
second table:
create table check(
cid int,
rid int,
usname varchar(20),
word varchar(20),
FOREIGN KEY(rid) REFERENCES login(id)
);
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 'check
(cid int, rid int, usname varchar(20), word varchar(20), FOREIGN KEY(rid) R' at
line 1
The problem isn't the foreign key. check is a reserved word in MySQL. So you need to choose another name or escape it:
create table `check`(
cid int,
rid int,
usname varchar(20),
word varchar(20),
FOREIGN KEY(rid) REFERENCES login(id)
);
Ironically, although it is a reserved word, it is not actually used. Alas, I do wish that MySQL supported check constraints.
By the way, here is a SQL Fiddle.
check is a key word in MySQL; either surround it with backticks, `, or choose a different name.
Change the name of your table check. Check is a reserved word in MySQL

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