Error in SQL statement - mysql

I cannot find this error in the sql, could anyone help?
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 'ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT, datetimeTIMESTAMP, title TEXT NOT' at line 1
CREATE TABLE tableName1 (
ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
timestamp TIMESTAMP NOT NULL,
title TEXT NOT NULL,
titleEdited TEXT,
description TEXT NOT NULL,
descriptionEdited TEXT,
URL VARCHAR(255) NOT NULL,
URLEdited VARCHAR(255),
imgPath VARCHAR(255) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY (URL, imgPath)
)
CREATE TABLE tableName2 (
ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
linkID MEDIUMINT(9) NOT NULL,
timestamp TIMESTAMP NOT NULL,
comment TEXT NOT NULL,
userID BIGINT(20) NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(linkID) REFERENCES tableName1(ID),
FOREIGN KEY(userID) REFERENCES users(ID)
)

I think the only thing that is wrong here is that mySQL needs a semicolon after each execution. You should just have to end each create table with a ; and this should work
Here is a SQL Fiddle to prove that. (Minus the FK to users since that table is not in your example)

Are you sure you are creating this tables and getting error?
I created them succesfuly.
And with looking error, it seems you are trying to create another field named datetime which type is timestamp? datetime is reserverd word. You need to use backticks.
'ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT, datetimeTIMESTAMP, title TEXT NOT' at line 1

Related

where is my error in this area, can you edit the code?

DROP TABLE IF EXISTS lang;
CREATE TABLE lang (
ID int( 11 ) NOT NULL auto_increment,
key varchar(255) NOT NULL,
code varchar(8) NOT NULL,
value text NOT NULL,
PRIMARY KEY (ID),
KEY key (key)
);
MySQL said:
Documentation #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 'varchar(255) NOT NULL, code varchar(8) NOT NULL,
value text NOT NULL, ' at line 3
key is a reserved word in mysql it must be escaped:
DROP TABLE IF EXISTS lang;
CREATE TABLE lang (
ID int( 11 ) NOT NULL auto_increment,
`key` varchar(255) NOT NULL,
code varchar(8) NOT NULL,
value text NOT NULL,
PRIMARY KEY (ID),
KEY `key` (`key`)
);
http://sqlfiddle.com/#!9/3b6cd5

Created table with generated columns throw an error

I am trying to build my table in MySQL following this answer. Hower i keep getting errors in MySQL even launching the same query used as example in the answer
this query is from documentation and works correctly
CREATE TABLE triangle (
sidea DOUBLE,
sideb DOUBLE,
sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb)) stored
);
this query comes from the answer i've linked and it gives me an error
CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
this is the query i am trying to execute based on the answer which is giving me also an error
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) NOT NULL AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique,
PRIMARY KEY (id),
INDEX (first));
can you help me figuring out why it's not working?
Note that SHA2(concat_ws(',',first, second, third, fourth), 256) this works in a normal select statement
EDIT
this is the error i am getting
Query execution failed
Reason:
Errore SQL [1064] [42000]: (conn:28) 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 'AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT ' at line 5
Query is : CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8
You have to add the NOT NULL after the AS part (computed column definition) of the column like this:
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique NOT NULL,
PRIMARY KEY (id),
INDEX (first)
);
Also have a look at the official document of CREATE TABLE. There you can find the description of the datatype syntax. The definition of the computed column is part of the datatype.

Multiple Errors in Uploading Database Columns MySQL

I am new to MySQL and am uploading a database for the first time. I am not uploading the data, just the columns for now. The issue is that I get an error for every line of code provided:
#1064 - You have an error in your SQL syntax;
I can understand if I was getting the message for just one or two lines of the code, but I am beginning to think I misformatted all the database file since I'm getting that error message for every line.
I previously changed the primary/foreign keys
CONSTRAINT `PurchasePK` PRIMARY KEY (`P_ORDERNO`)
CONSTRAINT `PurchaseFK` FOREIGN KEY (`SUPPLY_CODE`)
to: P_ORDERNO int(5) NOT NULL auto_increment PRIMARY KEY,
And still get errors.
I would appreciate if someone took a look at the contents of the .sql file below and let me know if there is something I am missing that is giving me consistent errors.
-- Table structure for table `Purchase`
CREATE TABLE IF NOT EXISTS `Purchase` (
`P_ORDERNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_CODE` int(5) NOT NULL auto_increment FOREIGN KEY,
`P_ORDER_DATE` timestamp NOT NULL,
`P_ORDER_AMT` int(5) NOT NULL,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Vendor`
CREATE TABLE IF NOT EXISTS `Vendor` (
`VENDORNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`VENDOR_NAME` varchar(50) NULL,
`VENDOR_STREET` varchar(50) NULL,
`VENDOR_CITY` varchar(50) NULL,
`VENDOR_STATE` varchar(2) NULL,
`VENDOR_ZIP` varchar(3) NULL,
`VENDOR_AREA_CODE` varchar(5) NULL,
`VENDOR_PHONE` varchar(10) NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Supply`
CREATE TABLE IF NOT EXISTS `Supply` (
`SUPPLY_CODE` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
You have trailing commas
`SUPPLY QTY` int(5) NOT NULL,
^---here
);
in each of the table definitions. That makes the DB server expect another field definition, but instead it runs into );

I'm trying to create a database but i keep getting the same error, and according to me and my fellow students the code is fine

This is just an example, from the first 2 tables but i get the same error:
MySQL said: Documentation
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 10
CREATE TABLE Tallas
(
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla),
)
CREATE TABLE Modelo
(
IDModelo int NOT NULL AUTO_INCREMENT,
IDTalla int NOT NULL,
modeloNombre varchar(45) NOT NULL,
precio varchar(45) NOT NULL,
informacion varchar(45) NOT NULL,
PRIMARY KEY (IDModelo),
FOREIGN KEY (IDTalla) REFERENCES Modelo(IDTalla),
)
Remove the coma at PRIMARY KEY (IDTalla), to make it PRIMARY KEY (IDTalla)
and the same with REFERENCES Modelo(IDTalla),
Remove the comma -> (IDTalla),) (IDTalla))
Looks like you put an extra comma in your create table statement just after primary key.
CREATE TABLE Tallas (
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla),
)
This should be:
CREATE TABLE Tallas (
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla)
)
Same with the second statement.

I keep getting an error when trying to make a table in phpmyadmin

when I was trying to run this code:
USE user;
CREATE TABLE IF NOT EXISTS 'posts' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'body' text NOT NULL,
'date_added' date NOT NULL,
'added_by' varchar(255) NOT NULL,
'user_posted_to' varchar(255) NOT NULL,
PRIMARY KEY ('id')
)ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10;
It gave me this 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 ''posts' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'body' text NOT NULL,
'dat' at line 1
how do I solve it? By the way, I already selected a database? Any help would be appreciated.
Get rid of all the "'" marks.
CREATE TABLE IF NOT EXISTS posts (
id int(11) NOT NULL AUTO_INCREMENT,
body text NOT NULL,
date_added date NOT NULL,
added_by varchar(255) NOT NULL,
user_posted_to varchar(255) NOT NULL,
PRIMARY KEY (id)
)ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10;
Here is a fiddle with a working creation.