SQL query doesn't work? - mysql

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

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.

Getting error in my mysql code: You have an error in your SQL syntax

I am trying to solve error but i dont have any idea how to solve please help me.
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 jobseeker(j_id first_name varchar(20),last_name varchar(20),phone_' at line 5
create table admin(primary key,admin_name varchar(20),admin_password varchar(20));
create table jobseeker(j_id int primary key,first_name varchar(20),last_name varchar(20),phone_no int(11),email_id varchar(20),address1 varchar(30), address2 varchar(30),qualification varchar(20),birthday_date date,gender varchar(10),field varchar(50),year of passing int(11), board varchar(20),zip int(11),city varchar(20),state varchar(20),username varchar(20),password varchar(20));
create table job seeker job(id int primary key,j_id int(10),c_name varchar(20),post varchar(20),foreign key references(j_id int foregin key references job seeker(id)));
create table company news(n_id int primary key,c_id int(11)primary key,information varchar(50),link varchar(50));
create table company(c_id int primary key,c_name varchar(20),phone_no int(11),address varchar(20),email_id varchar(20),password varchar(20));
Well, you have some errors, but as Bill Karwin said, run SQL statemets one at a time.
In your first statement you dont have a name and a type for your primary key.
create table admin(id int primary key,admin_name varchar(20),admin_password varchar(20));
In your second statement you have a column named 'year of passing'. It's not allowed, you should use underscore.
create table jobseeker(j_id int primary key,first_name varchar(20),last_name varchar(20),phone_no int(11),email_id varchar(20),address1 varchar(30), address2 varchar(30),qualification varchar(20),birthday_date date,gender varchar(10),field varchar(50),year_of_passing int(11), board varchar(20),zip int(11),city varchar(20),state varchar(20),username varchar(20),password varchar(20));
Again, check every statement and I recommend you to use some editor with syntax highlighting.
Because j_id did not have the data type. Which is int.
Also, the year of passing int(11) can not have spaces. Well, it can f that is what you really want to do. I will recomend you do not. Rather use underscore "_"
Write it like below if you need your spaces for your column name: year of passing` int(11)
create table jobseeker(
j_id int primary key,
first_name varchar(20),
last_name varchar(20),
phone_no int(11),
email_id varchar(20),
address1 varchar(30),
address2 varchar(30),
qualification varchar(20),
birthday_date date,
gender varchar(10),
field varchar(50),
`year of passing` int(11),
board varchar(20),
zip int(11),
city varchar(20),
state varchar(20),
username varchar(20),
password varchar(20)
);
notice that I also have spaces for year of passing, this is not recommended. I recommend that you one of the following for that column.
year_of_passing int(11)
or
yearOfPassing int(11)

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.

Why am I getting error at line 1 on Create Table

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!

Error in Create Table Syntax MYSQL

This is my create table syntax:
Create table Recipe(
Recipename varchar(30),
Cuisine varchar(20),
Skilllevel varchar(13),
Ingredients varchar(400),
Procedure varchar(1000),
Cookingtime int(30),
Category varchar(10),
Restrictions varchar(15)
);
I'm not able to identify the error in this. Please do help. Thanks
Procedure is a reserved word. Need another name or surround it with backticks.
INT doesn't need a (30) value + you cant use word Procedure. Changed it to Procedures
Create table Recipe(
Recipename varchar(30),
Cuisine varchar(20),
Skilllevel varchar(13),
Ingredients varchar(400),
Procedures varchar(1000),
Cookingtime int,
Category varchar(10),
Restrictions varchar(15));
Looks like Isaiah answered your question but I wanted to add: depending on what kind of format you want the Cookingtime to be in, you could use the time datatype for long cook times (several hours) or keep using int if you are using minutes.