Create table error with MySQL - mysql

I try to create a table in MySQL:
mysql> create table order ( ID varchar(30) not null,
-> Cname varchar(100) not null,
-> name varchar(30),
-> Type varchar(30),
-> primary key(ID, Cname));
but an error occured:
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 varchar(30) not null, Cname
varchar(100) not null, name varchar(30), ' at line 1
I have checked for thousand time and I still find no error here.
Can anyone help me?

Its is because of the table name order.
There is an order by reserved word. Change the table name and it will work fine.
If you want order as table name use back ticks around the table name. It will workfine

Related

MYSQL (WAMP SERVER): Error 1064 when using AUTO_INCREMENT=1000 to create table column?

I have this code:
CREATE TABLE Company ( Comp_Code INT(5) NOT NULL AUTO_INCREMENT=1000, Comp_Name VARCHAR(15) NOT NULL, PRIMARY KEY (Comp_Code) );
but when i run it in MYSQL from WAMPServer it gives an 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 '=1000,
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
)' at line 2
Why is this happening? I can't seem to find any solution to this particular issuer.
AUTO_INCREMENT is column option.
Initial AUTO_INCREMENT=1000 value is table option.
CREATE TABLE Company (
Comp_Code INT(5) NOT NULL AUTO_INCREMENT, -- specify that the column is AI
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
) AUTO_INCREMENT=1000; -- specify initial AI value

Error to create a brand new table

I tried to create a brand new table with codes:
MySQL [distributor]> create table order
-> (
-> order_num integer not null,
-> order_date datetime not null,
-> cust_id chat(10) not null
-> );
It generates errors:
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
(
order_num integer not null,
order_date datetime not null,
cust_id chat(' at line 1
I have checked multiple times to ensure there no problems in my codes since SQL is case-insensitive.
What's the bug with my code?
There are two issues with your query: Firstly, order is a MySQL reserved word. Secondly, you have chat which should be char. Try this instead (renaming table to orders):
CREATE TABLE orders
(
order_num integer not null,
order_date datetime not null,
cust_id char(10)
);
order it's a reserved word and put char instead of chat. You can name the table orders

SQL syntax error 'GENERATED ALWAYS AS IDENTITY'

I am trying to upload this file to netbeans however I am unable to create the authors table and continue to receive the following error:
CREATE TABLE authors (
authorID INT NOT NULL GENERATED ALWAYS AS IDENTITY,
firstName varchar (20) NOT NULL,
lastName varchar (30) NOT NULL,
PRIMARY KEY (authorID)
);
INSERT INTO authors (firstName, lastName)
VALUES
('Paul','Deitel'),
('Harvey','Deitel'),
('Abbey','Deitel'),
('Dan','Quirk'),
('Michael','Morgano');
[Warning, Error code 1,064, SQLState 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 'GENERATED ALWAYS AS IDENTITY,
firstName varchar (20) NOT NULL,
lastName va' at line 2
[Exception, Error code 1,064, SQLState 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 'GENERATED ALWAYS AS IDENTITY,
firstName varchar (20) NOT NULL,
lastName va' at line 2
Line 2, column 1
[Warning, Error code 1,146, SQLState 42S02] Table 'books.authors' doesn't exist
I have been searching all over the web for hours and have yet to find an answer! I am new to SQL so I apologize if this is a duplicate question. I am using MYSQL version 5.7.18
There are seval things wrong:
identity is SQL Server syntax, MySQL uses auto_increment
<name> <type> generated always as <calculation> applies to calculated columns.
Try:
CREATE TABLE authors (
authorID INT NOT NULL AUTO_INCREMENT,
firstName varchar (20) NOT NULL,
lastName varchar (30) NOT NULL,
PRIMARY KEY (authorID)
);

mysql error referring to Null

$sql1= "CREATE TABLE bookapp(
id int NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
pnumber int(11),
emailID VARCHAR(50),
reg_date datetime()
PRIMARY KEY(id)
)";
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 ''', NULL)' at line 1
You can use the following to create the table:
CREATE TABLE bookapp(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
pnumber INT(11),
emailID VARCHAR(50),
reg_date DATETIME,
PRIMARY KEY(id)
);
Explanation:
( and ) on DATETIME is invalid. DATETIME doesn't need a length.
You have to seperate the column definitions with ,. You forgot this after column reg_date.
Hint (to your error message):
Your given CREATE TABLE throws another error message, that is different to yours:
Error Code: 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 ') PRIMARY KEY(id) )' at line 6

MySQL Create Table Error Syntax

Hello i am having an issue when trying to create a table inside of my database webhostc_MyRadContactForm
When i try to execute the statement below in phpMyAdmin i get this error
CREATE TABLE Contacts (
-> ContactID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ContactName VARCHAR(100),
-> ContactEmail VARCHAR(100),
-> ContactLeastFavoriteColor VARCHAR(10)
-> ContactDateCreated DATETIME
-> );
#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 '-> ContactID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ContactName VARCHA' at line 2
Also phpMyAdmin flags these lines:
My server is running: 10.0.22-MariaDB
You are missing a comma just after ContactLeastFavoriteColor VARCHAR(10) and those arrows, ->, are not supposed to be there. The following is the correct syntax for creating your table:
CREATE TABLE Contacts (
ContactID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ContactName VARCHAR(100),
ContactEmail VARCHAR(100),
ContactLeastFavoriteColor VARCHAR(10),
ContactDateCreated DATETIME
);
Good luck!!
There are two problems:
Those -> symbols aren't part of SQL syntax. They're the prompts that the MySQL monitor prints when you enter a multi-line query. You can't copy them into PhpMyAdmin.
You're missing a comma at the end of the ContactLeastFavoriteColor line.