Getting an error while creating a Foreign Key - mysql

I was following W3Schools' guide on how to create a Foreign Key but every time I try to run my code it gives me an error.
My code
CREATE TABLE students(
`Student Number` INT NOT NULL PRIMARY KEY,
`First Name` VARCHAR(255) NOT NULL,
`Last Name` VARCHAR(255) NOT NULL,
Address VARCHAR(255) NOT NULL,
City VARCHAR(255) NOT NULL,
State VARCHAR(255) NOT NULL,
Zip INT NOT NULL,
`Date of Birth` DATE NOT NULL,
`Major ID` INT,
Advisor VARCHAR(255),
`Enroll Date` DATE,
`Total Credits Complete` INT,
FOREIGN KEY `Major ID` REFERENCES Major(`Major ID`)
);
I'm trying to give 'Major ID' a Foreign Key but instead I get this error code
Error SQL query:
CREATE TABLE students(
Student Number INT NOT NULL PRIMARY KEY,
First Name VARCHAR(255) NOT NULL,
Last Name VARCHAR(255) NOT NULL,
Address VARCHAR(255) NOT NULL,
City VARCHAR(255) NOT NULL,
State VARCHAR(255) NOT NULL,
Zip INT NOT NULL,
Date of Birth DATE NOT NULL,
Major ID INT,
Advisor VARCHAR(255),
Enroll Date DATE,
Total Credits Complete INT,
FOREIGN KEY Major ID REFERENCES Major(Major ID) ) MySQL said:
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 'REFERENCES Major(Major ID) )' at line 14
I would really appreciate it if someone could gear me in the right direction.
Thank you for taking your time to help me.

Related

Ways to resolve mysql #1064 error in the following code?

I tried using EVERSQL to validate the following code but its also showing error
You have an error in your SQL syntax; it seems the error is around: 'NOT NULL, DepartmentID int NOT NULL, CategoryID int NOT NULL, CourseName V' at line 2
this is my SQL query,
CREATE TABLE Course (
CourseID varchar NOT NULL,
deptID int NOT NULL,
catID int NOT NULL,
CourseName Varchar NOT NULL,
Credit varchar NOT NULL,
PRIMARY KEY (CourseID),
FOREIGN KEY (deptID) REFERENCES department(deptID),
FOREIGN KEY (catID) REFERENCES coursecategory(catID)
);
and this is the error I'm getting
#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 'NOT NULL,
deptID int NOT NULL,
catID int NOT NULL,
CourseName ...' at line 2
You need to specify the lengths of your varchar columns. E.g.:
CREATE TABLE Course (
CourseID varchar(10) NOT NULL,
deptID int NOT NULL,
catID int NOT NULL,
CourseName varchar(10) NOT NULL,
Credit varchar(10) NOT NULL,
PRIMARY KEY (CourseID),
FOREIGN KEY (deptID) REFERENCES department(deptID),
FOREIGN KEY (catID) REFERENCES coursecategory(catID)
);

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

Mariadb syntax error 1064 (42000)

So I'm getting an error when trying to run this script in MariaDB that reads as follows: "ERROR 1064 (42000):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
CREATE TABLE customers (
customer_id int NOT NULL,
customer_f
at line 1
The weird thing is that MariaDB seems to be reading the first line of the command and then a little bit of the next line as 1 line. The whole script is below: if anyone could help I would greatly appreciate it.
DROP TABLE customers;
DROP TABLE orders;
DROP TABLE products;
DROP TABLE orderitem;
​
CREATE TABLE customers (
customer_id INT NOT NULL AUTO_INCREMENT,
customer_firstname VARCHAR(20) NOT NULL,
customer_lastname VARCHAR(40) NOT NULL,
customer_phone CHAR(10) NOT NULL,
customer_email VARCHAR(60) NOT NULL,
customer_address VARCHAR(40) NOT NULL,
customer_city VARCHAR(40) NOT NULL,
customer_state CHAR(2) NOT NULL,
customer_zip VARCHAR(10) NOT NULL,
customer_aptnum VARCHAR(5) NOT NULL,
customer_pass CHAR(40) NOT NULL,
customer_type VARCHAR(10) NOT NULL,
PRIMARY KEY (customer_id),
INDEX customer_fullname (customer_firstname, customer_lastname),
UNIQUE (customer_email)
);
​
CREATE TABLE orders (
order_id INT NOT NULL AUTO_INCREMENT,
order_datetime DATETIME NOT NULL,
order_trackingnumber VARCHAR(20) NOT NULL,
order_shipdate DATETIME NOT NULL,
order_shipmethod VARCHAR(10) NOT NULL,
order_shipcarrier VARCHAR(10) NOT NULL,
order_totalprice DECIMAL,
customer_id INT NOT NULL,
PRIMARY KEY (order_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
UNIQUE (order_trackingnumber)
);
​
​
CREATE TABLE products (
product_id VARCHAR(30) NOT NULL AUTO_INCREMENT,
product_beginningstockdate DATETIME NOT NULL,
product_endstockdate DATETIME,
product_category VARCHAR(15) NOT NULL,
product_name VARCHAR(60) NOT NULL,
product_availablequantity SMALLINT NOT NULL,
product_totalquantity SMALLINT NOT NULL,
product_price DECIMAL NOT NULL,
product_taxable DECIMAL NOT NULL,
product_itemstatus VARCHAR(15) NOT NULL,
product_discountpercent DECIMAL,
product_soldinstore char(3),
product_soldonwebsite char(3),
PRIMARY KEY (product_id),
UNIQUE (product_name)
);
​
/*INSERT INTO products (product_description, product_beginningstockdate, product_endstockdate, product_category, product_name, product_availablequantity, product_totalquantity, product_price, product_taxable, product_itemstatus, product_discountpercent, product_soldinstore, product_soldonwebsite)
VALUES
(...),
(...),
........ */
​
CREATE TABLE orderitem (
orderitem_id INT NOT NULL AUTO_INCREMENT,
order_id INT NOT NULL,
product_id VARCHAR(30) NOT NULL,
orderitem_priceperunit DECIMAL NOT NULL,
orderitem_quantityordered TINYINT NOT NULL,
PRIMARY KEY (orderitem_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES orders(product_id)
);
Copy&pasting your code into NotePad++ and then viewing it in the hex editor shows that you have 80 8b 0a in each of those empty lines between the statements.
That byte sequence is the UTF-8 encoded form of the zero-width space character.
See to it that you remove those – then it should work.
(If you’re using NotePad++ and the hex editor plugin, then in hex view you can simply replace e2 80 8b with an empty string. Otherwise, in any other text editor, going to the end of the previous line, selecting everything from there over the empty line until the beginning of the next line, and then replacing the selection by pressing enter should also work.)
I had a similar frustration and discovered I needed to change my delimiter around my procedure. Perhaps it'll work here too.
DELIMITER //
CREATE PROCEDURE
....
BEGIN
END //
DELIMITER ;

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
);

MySQL Syntax errors

CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
),
I have this as part of a database I'm setting up, yet whenever I try to run the .sql file that this is included in, I get the following 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 Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(' at line 8
Any ideas what's wrong? Thanks.
Remove the , from the end CREATE TABLE statement
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)
Or use semicolon instead comma:
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
);
You have comma at the end if you remove that it is working fin for me.
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)
We don't use "," at the end to separate two sql queries, you have to use ";" and if you are running a single query then you don't need.
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)