Why am I getting error at line 1 on Create Table - mysql

First time working with MySQL. Using MySQL Workbench with a new instance installed on my Windows 10 desktop. I've created a test test db/schema, and now trying to create a table.
CREATE TABLE kcplusers (
'user_barcode' varchar(255),
'first_name' varchar(255),
'middle_name' varchar(255),
'last_name' varchar(255),
'address_street' varchar(500),
'city_state' varchar(500),
'zip' varchar(100),
'user_profile' varchar(500),
'birth_date' DATE
);
Workbench shows a red x at line 1 and red slashes under the open-parenth (
I get the error:
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 ''user_barcode' varchar(255), 'first_name' varchar(255), 'middle_name' varc' at line 2
What am I missing?

Don't use single quotes ' for the fields names, use backticks ` or nothing at all:
CREATE TABLE kcplusers (
`user_barcode` varchar(255),
`first_name` varchar(255),
`middle_name` varchar(255),
`last_name` varchar(255),
`address_street` varchar(500),
`city_state` varchar(500),
`zip` varchar(100),
`user_profile` varchar(500),
`birth_date` DATE
);

You're using apostrophes around the column names - they should be removed in this case.
If you must specify certain characters in a column name you can use ascii cd 96 shown as ` - usually just to the left of the number "1" on your keyboard.
But to keep it simple as you're starting out - just remove the apostrophes.
And let us know how it turns out (good or bad)
Cheers!

Related

SQL syntax error; "check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Order INT NOT NULL' [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 1 year ago.
I'm using MariaDB and MySQL and I'm trying to create a table using the command prompt but I'm getting a syntax error and I am unsure what is wrong. Below is the error:
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 'Order INT NOT NULL,
FName VARCHAR(255),
LName VARCHAR(255),
PhoneNum VARCH...' at line 3
Here is what I have:
CREATE DATABASE projecttestDB;
USE projecttestDB;
DROP TABLE IF EXISTS Staff;
CREATE TABLE Staff (
Year INT NOT NULL,
Order INT NOT NULL,
FName VARCHAR(255),
LName VARCHAR(255),
PhoneNum VARCHAR(255),
Salary INT,
CHECK(Salary>=0),
PRIMARY KEY (Year, Order)
);
order is a reserved word in SQL. If you absolutely have to use it, you can escape the column name using backticks:
CREATE TABLE Staff (
Year INT NOT NULL,
`Order` INT NOT NULL, -- Here!
FName VARCHAR(255),
LName VARCHAR(255),
PhoneNum VARCHAR(255),
Salary INT,
CHECK(Salary>=0),
PRIMARY KEY (Year, `Order`) -- And here
);
But it's probably a better idea to rename it to something that isn't a reserved word, such as order_num:
CREATE TABLE Staff (
Year INT NOT NULL,
Order_Num INT NOT NULL,
FName VARCHAR(255),
LName VARCHAR(255),
PhoneNum VARCHAR(255),
Salary INT,
CHECK(Salary>=0),
PRIMARY KEY (Year, Order_Num)
);
Order is a system key word. If you change it to something else it will work.

Error 1064 when creating table in MySQL Workbench [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 1 year ago.
I'm trying to create a table in my schema (mydb) called employee, but whenever I try running the script to create the table I get error #1064.
I'm new to SQL so I was looking at examples on how to create tables and went off those examples to create my own until I bumped into this error. Could anyone explain why I am getting this error message please?
MySQL Workbench Community 8.0 Version 8.0.26:
CREATE TABLE 'mydb'.'employee'
(
'emp#' INT PRIMARY KEY,
'firstname' VARCHAR(20) NOT NULL,
'surname' VARCHAR(20) NOT NULL,
'salary' INT default 0,
'city' VARCHAR(20),
UNIQUE(firstname, surname)
);
Error:
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 ''mydb'.'employee' ( 'emp#' INT Primary key, 'firstname' VARCHAR(20) NOT NULL, 's' at line 1
Your issue is the because you are adding ' single quotes in your CREATE TABLE statement, you need to do it like this:
CREATE TABLE mydb.employee
(
emp INT PRIMARY KEY,
firstname VARCHAR(20) NOT NULL,
surname VARCHAR(20) NOT NULL,
salary INT default 0,
city VARCHAR(20),
UNIQUE(firstname, surname)
);
If you need the quotes in Mysql you need to use the curly single quote like this:
CREATE TABLE `mydb`.`employee`
(
`emp_id` INT PRIMARY KEY,
`firstname` VARCHAR(20) NOT NULL,
`surname` VARCHAR(20) NOT NULL,
`salary` INT default 0,
`city` VARCHAR(20),
UNIQUE(firstname, surname)
);
Also instead of using #, use _id or no you can check the following reference about MySQL Naming Rules

How to fix this mysql create table syntax error [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 1 year ago.
I'm getting a mysql create table syntax error and hoping someone can spot it before I loose my sanity.
create table buyer_representations (
Key int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (Key)
);
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 'int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Star' at line 2
mysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapper
I tried adding 'primary key' at the end of line 2, as well a default value, and switching up 'not null' with 'auto_increment' .. but nothing is working.
Edit:
I also tried changing Key to KeyId, and putting Key in quotes .. but the error remains the same.
Final Edit:
putting Key inside backquotes finally worked
create table buyer_representations (
`Key` int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (`Key`)
);
Key is a reserve keyword, rename that and you should be golden.
A complete list of keywords and reserved words can be found in section 10.3 Keywords and Reserved Words. Below
https://dev.mysql.com/doc/refman/8.0/en/keywords.html
in your case, this should work (I name it test, but rename it to whatever fits your situation)
create table buyer_representations (
test int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (test)
);
The problem is the column name you have used in your table.
The first column name is Key.
Key is considered as a reserved word in Mysql and it has a special meaning. That is why there is an error.
If you want to use the same column name you can do it by writing the column name inside ``.
For eg.
create table buyer_representations (
`Key` int not null auto_increment,
This will work fine.

SQL query doesn't work?

I want to create a table in the database and below is the query that I wrote; does anyone have any idea why it doesn't work?
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 'sname varchar(255), city varchar(255), avg int(20), clg# int(20) )' at line 4`
CREATE TABLE stud
(
s# int,
sname varchar(255),
city varchar(255),
avg int(20),
clg# int(20)
);
The # is not a valid character for an identifier. Just remove it:
CREATE TABLE stud (
s int,
sname varchar(255),
city varchar(255),
avg int(20),
clg int(20)
);
You can review the rules for identifiers here. Note you could also put the name in backticks:
`s#` int,
However, I strongly discourage you from using names that need to be escaped.
You cannot use the '#' character in column names.
you cant have # in your table names, change your script to this one and try again.
CREATE TABLE stud ( s int,
sname varchar(255),
city varchar(255),
avg int(20),
clg int(20) );
It's not recommended to use '#' to name your identifiers. This is ambiguous for you and other developers on your team. Use this instead
CREATE TABLE stud (
sNo int,
sname varchar(255),
city varchar(255),
avg int(20),
clgNo int(20)
);
By naming your identifiers descriptively it easier for everyone to understand. However mysql qouted identifier naming rules allow ASCII: U+0001 .. U+007F
see here.
MySQL Schema object naming Rules

error 10064 mysql 4200 while trying to create table

CREATE TABLE DIAMOND(
RAPNETSELLERCODE VARCHAR(30) ,
SHAPE VARCHAR(15) ,
WEIGHT INT(5) ,
COLOR VARCHAR(3) ,
CLARITY VARCHAR(6) ,
CUTGRADE VARCHAR(10),
POLISH VARCHAR(13),
SYMMENTRY VARCHAR(10),
FLUORESCE VARCHAR(10),
MEASUREMENTS INT(100),
MEASLENGTH INT(30),
MEASWIDTH INT(30),
MEASDEPTH INT(30),
RATIO INT(12),
LAB VARCHAR(10),
ID INT(15),
STOCK# VARCHAR(14),
RAPNETPRICE INT(15),
RAPNETDISCOUNTPRICE INT(15),
RAPTOTALPRICE INT(16),
DEPTH % INT(12),
TABLE % INT(10),
GIRDLE VARCHAR(10),
CULET VARCHAR(10),
CERTIFICATEURL VARCHAR(300),
RAPNETLOT # INT(38);
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 ')
CREATE TABLE DIAMOND(
RAPNETSELLERCODE VARCHAR(30) ,
SHAPE VARCHAR(15) ,' at line 1
Evidently there is more to the query than what you are showing us. Seems like you have something like
CREATE TABLE PLATINUM (
/* fields */
)
CREATE TABLE DIAMOND (
You need a semicolon after the ) before CREATE TABLE DIAMOND.
You have other errors in this CREATE TABLE statement as well.
STOCK # -- invalid syntax
`STOCK #` -- desired syntax
Same is true of DEPTH %, TABLE %, and RAPNETLOT #
Finally, you are missing the closing paren for the entire CREATE TABLE statement.
Several things I can see: you have a % symbol in 2 lines of code, you have a # symbol in 2 lines of code, you have DEPTH and TABLE as column names although they are keywords, and you never closed your parenthesis inside the semicolon. At least one of these things is causing the error message. Start with the ) you are missing.