SQL Enum Error 1064 - mysql

I am trying to create a table in a MySQL database, I am not sure where I am going wrong with my syntax. In my query it is returning a 1064 error in the line where I use ENUM. Here is part of my query:
CREATE TABLE General
(
pId varchar(30) NOT NULL UNIQUE,
Hometown varchar(30) NOT NULL,
Year char(2) NOT NULL ENUM("FR","SO","JR","SR")
Position varchar(2) NOT NULL ENUM("PG","SG","SF","PF","C"),
Season char(4) NOT NULL DEFAULT 2016,
Date_Of_Birth DATE NOT NULL,
CONSTRAINT PRIMARY KEY (pId));

missing comma , enum don't need type, season is a string '2016' and remove constraint for primary key
CREATE TABLE General
(
pId varchar(30) NOT NULL UNIQUE,
Hometown varchar(30) NOT NULL,
Year ENUM("FR","SO","JR","SR"), <---- here missing commma
Position ENUM("PG","SG","SF","PF","C"),
Season char(4) NOT NULL DEFAULT '2016',
Date_Of_Birth DATE NOT NULL,
PRIMARY KEY (pId)
);

Related

How to represent float values in SQL

I'm trying to create a database in Ubuntu using MySQL in the command line, I need to create a table with the following data:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
Condition VARCHAR(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
It just returns an error that says "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 'Condition varchar(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle' at line 6"
What is wrong with my code? The "Price" and "Engine_Size" columns need to only be a float/decimal values so they can't be varchar because I want to only be able to insert numbers.
This works:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size int NOT NULL,
Vehicle_Condition VARCHAR(255) NOT NULL,
Price numeric(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Notes:
For Price you seem to want numeric, not float, because you are specifying the precision and scale.
For Engine_Size, I have no idea what float(1, 1) is supposed to mean. I am guessing that int is an appropriate type.
Condition is a reserved word in MySQL, so I changed the name of the column.
The only apparent problem in your code is the use of 'condition' as a column name as condition is a reserved word in MySQL.
You can fix it in 2 ways:
Don't use 'condition' as a column name:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
V_Condition VARCHAR(255) NOT NULL, //Just an example feel free to use any another name
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Put 'condition' inside backticks (``)
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
`Condition` VARCHAR(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Hope this helps

When creating a table using SQL, a variable student-type can be either post-grad or under-grad. What data type should I use?

This is what I have now. I have done a check for DECIMAL() using BETWEEN AND. Was wondering if it is possible to do the same for VARCHAR()
CREATE TABLE STUDENT(
student_number CHAR(4) NOT NULL,
first_name VARCHAR(50) NULL,
last_name VARCHAR(50) NULL,
date_of_birth DATE NULL,
student_type VARCHAR(20) NULL,
CONSTRAINT student_pkey PRIMARY KEY (student_number),
CONSTRAINT student_check1 CHECK (student_type POSTGRADUDATE_STUDENT OR UNDERGRADUATE_STUDENT));

Error creating table in MySQL 5.7

Can some one help me in correcting the syntax according to MySQL 5.7?
Table :
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
PRIMARY KEY (ID)
);
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 '(ID)
You forgot comma at the end of this line:
ADDRESS VARCHAR(20) NOT NULL
You dont have a column named ID, you may want EMPID?
Replace
ADDRESS VARCHAR(20) NOT NULL
By
ADDRESS VARCHAR(20) NOT NULL,
There are two problems here:
A primary key clause is it's own clause, and needs to be separated from the previous column definition by a comma.
You don't have an id column - your primary key should (probably) be empid:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
PRIMARY KEY (EMPID)
);
I guess this is what you are looking for:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
);

CREATE Table Execution issues

I cannot figure out what the problem is with my table..
CREATE TABLE PLAYER
(FirstName VARCHAR(15) NOT NULL,
LastName VARCHAR(15) NOT NULL,
BirthDate DATE,
Address VARCHAR(30),
EmailAddress VARCHAR(30) NOT NULL,
Sex CHAR,
TeamName VARCHAR(30) NOT NULL,
ClubName VARCHAR(30) NOT NULL,
AgeGroup VARCHAR(5) NOT NULL,
PRIMARY KEY (LastName),
FOREIGN KEY (TeamName)
**);**
CREATE TABLE TEAMS
(TeamName VARCHAR(30) NOT NULL,
AgeGroup VARCHAR(5) NOT NULL,
Sex CHAR,
CoachFName VARCHAR(20) NOT NULL,
CoachLName VARCHAR(20),
ClubName VARCHAR(30) NOT NULL,
PRIMARY KEY (TeamName),
FOREIGN KEY (AgeGroup)
**FOREIGN KEY (ClubName)**
);
Below is the message I keep receiving.. I honestly have no idea how to get this table to execute! I highlighted in bold where the message said the error was near.
*Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ')'.
Msg 156, Level 15, State 1, Line 24
Incorrect syntax near the keyword 'FOREIGN'.*
You need to specify what table and field the foreign keys reference. For example, for the first error, you need to specify foreign key (teamname) references teams(teamname), if that is what you mean.

Error creating table: You have an error in your SQL syntax near 'order( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1 [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 8 years ago.
I am trying to create 2 tables in the same MySQL database with a PHP-script:
table 'user' with primary key 'user_id' and table 'order' with primary key 'order_id' and foreign key 'user_id' from the 'user' table (1 to many relationship).
Table user creates successfully without problems:
$sql="CREATE TABLE user(
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
type ENUM('member','admin') NOT NULL,
username VARCHAR(30) NOT NULL,
email VARCHAR(80) NOT NULL,
pass VARBINARY(32) NOT NULL,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
date_expires DATE NOT NULL,
date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_modified TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (user_id),
UNIQUE (username),
UNIQUE (email)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
However, I am not able to create table order:
$sql="CREATE TABLE order(
order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
transaction_id VARCHAR(19) NOT NULL,
payment_status VARCHAR(15) NOT NULL,
payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id),
FOREIGN KEY (user_id) REFERENCES user (user_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
I get the following error:
Error creating table: 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_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1
Already checked the syntax and cannot find the mistake. Could you please advise what went wrong? Thanks a lot.
You need to escape reserved words like order with backticks
CREATE TABLE `order` ( ...
or better use another name instead.
order is keyword used by mysql like (select from tbl_name order by id ASC) so for escaping from using keywords you have to use quotes `` to avoid my sql error
so your query should by
$sql="CREATE TABLE `order` (
order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
transaction_id VARCHAR(19) NOT NULL,
payment_status VARCHAR(15) NOT NULL,
payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id),
FOREIGN KEY (user_id) REFERENCES user (user_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
enjoy :D