Syntax error in SQL create table - mysql

Hi I am trying to get more familiat with SQL and I am using MySql to test my SQL queries.I seem to be getting a sintax error in this statement:
CREATE TABLE dog
(
id int(11) NOT NULL auto_increment,
name varchar(255),
descr text,
size enum('small','medium','large'),
date timestamp(14),
PRIMARY KEY (id)
)ENGINE = InnoDB;
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 '(14), PRIMARY KEY (id) )ENGINE = InnoDB' at line 7
What am I doing wrong here?

try mentioning like this
CREATE TABLE dog
(
id int NOT NULL auto_increment,
name varchar(255),
descr text,
size enum('small','medium','large'),
date timestamp,
PRIMARY KEY (id)
)
ENGINE = InnoDB;

'TIMESTAMP(14)' is deprecated; use 'TIMESTAMP' instead

Try this :
CREATE TABLE dog
(
id int NOT NULL auto_increment PRIMARY KEY,
name varchar(255),
descr text,
size enum('small','medium','large'),
date timestamp
)
ENGINE = InnoDB;
PRIMARY KEY statement has to be precised with the field declaration

Related

I don't understand why this code is throwing an Error?

I'm running the following code in MySQL Workbench:
CREATE TABLE beer_type ( -- create beer table
type_id INTEGER NOT NULL PRIMARY KEY, -- PK "type_id"
beer_name VARCHAR(30) NOT NULL,
beer_type VARCHAR(30) NOT NULL,
beer_id INTEGER NOT NULL,
FOREIGN KEY (beer_id) REFERENCES beer (beer_id));
I'm getting:
0 5 21:27:10 CREATE TABLE beer_type ( -- create beer table 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 '' at line 1 0.000 sec
I'm new to SQL so this is a bit confusing to me and my professor isn't the best of help...
Any way anyone can help would be greatly appreciated!!
Copy this and paste :
CREATE TABLE beer_type (
type_id INTEGER NOT NULL PRIMARY KEY,
beer_name VARCHAR(30) NOT NULL,
beer_type VARCHAR(30) NOT NULL,
beer_id INTEGER NOT NULL,
FOREIGN KEY (beer_id) REFERENCES beer (beer_id));

MySQL query not getting executed

I am new to SQL databases. I am using MySQL for my current project
Here is my query
CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID VARCHAR(50) NOT NULL FOREIGN KEY REFERENCES users(U_ID),
USER2_ID VARCHAR(50) NOT NULL FOREIGN KEY REFERENCES users(U_ID),
TIME_OF_FRIENDSHIP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
When I run this query I get this error.
>[Error] Script lines: 1-22 -------------------------
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 'CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID V' at line 17
Warnings: --->
W (1): 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 'CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID V' at line 17
<---
[Executed: 18/11/2020 8:14:46 AM] [Execution: 0ms]
Can someone help me out with this problem.
I guess you are using MS SQL Server syntax and you try to run it on MySQL. Here is MySQL syntax for creating table with FK.
CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID VARCHAR(50) NOT NULL,
USER2_ID VARCHAR(50) NOT NULL,
TIME_OF_FRIENDSHIP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (USER2_ID) REFERENCES USERS(U_ID),
FOREIGN KEY (USER1_ID) REFERENCES USERS(U_ID)
);
Reference https://www.w3schools.com/sql/sql_foreignkey.asp

I got an error #1064 during saving files ,data into my sqli database?

Im trying to creat a database for storing Photos in my sqli with the following code:
CREATE TABLE `photos`.`images` (
`id` INT NOT NULL AUTO_INCREMENT ,
`file` BLOB BINARY NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
so when i try to save it i got the following error . The error is the this :
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 'BINARY NOT NULL , PRIMARY KEY (id)) ENGINE = InnoDB' at line 1
does someone whats the error about !
Just remove the BINARY keyword:
CREATE TABLE `photos.images` ( `id` INT NOT NULL AUTO_INCREMENT , `file` BLOB NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;

SQL Expression wrong

I try to create a table but I keep getting an SQL Error but I can't figure out why.
This is the error:
check the manual that corresponds to your MySQL server version for the
right syntax to use near '+491634170770 (Id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,Type VARCHAR(20),Conte' at line 1 ERROR 1064 (42000):
You have an error in your SQL syntax;
This is my statement:
CREATE TABLE +491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I already tried to find a solution here, but my syntax seems to be correctly. I think its because of the name of the table. But using backticks like this ´+491234175789´ didn't work.
This is the backtick `:
CREATE TABLE `+491234175789` (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
However, don't create a table name that requires backticks. It is just bad form and makes queries harder to read and write -- you are creating problems for the future. Call it something like:
CREATE TABLE t_491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);

what is wrong with the sql query?

CREATE TABLE 1
(text longtext(4,294,967,295),
date VARCHAR(50),
time VARCHAR(50),
id INT(11 ) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY ( `id` )
What is wrong with the above SQL statement? I keep getting an error
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 '(4,294,967,295), date VARCHAR(50), time VARCHAR(50), id INT( 11 ) NOT NULL AU' at line 3
1 is a lousy name for a table. If you use it, you need to escape the name. Also, commas aren't allow in lengths for character fields and longtext doesn't need a length anyway:
CREATE TABLE `1`
(text longtext,
date VARCHAR(50),
time VARCHAR(50),
id INT(11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY
)
And, you don't "add" a primary key. You just declare it.