How to represent float values in SQL - mysql

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

Related

Maria DB Server

I land up in an error stating:
Error Code 1064: You have an error in your sqlsyntax; check the manual that corresponds to your MariaDB Server syntax for the right syntax to use
CREATE TABLE 'company'.'employee'(
'fname' VARCHAR (10) NOT NULL,
'mname' VARCHAR(2) NULL,
'lname' VARCHAR(10) NOT NULL,
'ssn' CHAR(9) NOT NULL,
'bdate' DATE,
'address' VARCHAR(20) NOT NULL,
'sex' CHAR NULL,
'salary' DECIMAL (10, 2) NULL,
'super_ssn' CHAR(9) NULL,
'd_no' INT NOT NULL,
PRIMARY KEY ('ssn'));
Use ` instead of ' (single quote),
You should write it like the following :
CREATE TABLE `company`.`employee`(
`fname` VARCHAR (10) NOT NULL,
`mname` VARCHAR(2) NULL,
`lname` VARCHAR(10) NOT NULL,
`ssn` CHAR(9) NOT NULL,
`bdate` DATE,
`address` VARCHAR(20) NOT NULL,
`sex` CHAR NULL,
`salary` DECIMAL (10, 2) NULL,
`super_ssn` CHAR(9) NULL,
`d_no` INT NOT NULL,
PRIMARY KEY (`ssn`)
);
Actually, the ` symbol is optional, but it is used if the field names, tables, or databases are the same as keywords or MySQL clauses whose purpose is that MySQL is not confused with what you mean in the query.
For example :
SELECT column FROM `char`
I use ` symbol because the name of the table is the same as keyword on MySQL i.e CHAR() (but naming like that is a bad way), so keep in mind that if you write SQL Query you must decide to use the ` symbol
hope this can help you.

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

Syntax error in MySQL CREATE TABLE

I want to make a table in phpmyadmin and I am using SQL command for that
CREATE TABLE userdetail(
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP
)
I am getting this error:
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 '
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT ' at line 2
It should be like this
CREATE TABLE userdetail(
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP);
You are missing KEY after PRIMARY:
CREATE TABLE userdetail (
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP
)
Note that int(255) really doesn't make sense. Are you familiar with the integer data types and what the value in parentheses means? You can review the documentation here.

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 ;

What's wrong with my query

Following is my query:
CREATE TABLE report_invoice(
'ID' INT(10) NOT NULL AUTO_INCREMENT,
'Invoice_No' VARCHAR(30) NOT NULL,
'Invoice_Type' INT(10) NOT NULL,
'Operator' VARCHAR(50) NOT NULL,
'Customer' VARCHAR(50) NOT NULL,
'Invoice_Date' DATE NOT NULL,
'Total' DECIMAL('10,2'),
PRIMARY KEY ('ID'));
I keep getting this 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'
INT(10) NOT NULL AUTO_INCREMENT,
'Invoice_No' VARCHAR(30) NOT NULL,
'Invoic' at line 2
You're using single quotes around your field names, use backticks instead:
CREATE TABLE report_invoice(
`ID` INT(10) NOT NULL AUTO_INCREMENT,
`Invoice_No` VARCHAR(30) NOT NULL,
`Invoice_Type` INT(10) NOT NULL,
`Operator` VARCHAR(50) NOT NULL,
`Customer` VARCHAR(50) NOT NULL,
`Invoice_Date` DATE NOT NULL,
`Total` DECIMAL(10,2),
PRIMARY KEY (`ID`));
Don't use simple quotes
You may replace them with backticks, or suppress them.
They would be usefull if you want to use reserved keywords as column names (bad idea anyway), or completely numeric column names, or special characters in column names.
So not in your case.
Don't put quotes around decimal scale and precision, too.
So this would do the job.
CREATE TABLE report_invoice(
ID INT(10) NOT NULL AUTO_INCREMENT,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT(10) NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL(10,2),
PRIMARY KEY (ID));
Remove the ticks or replace them with back ticks. Same for numbers.
CREATE TABLE report_invoice(
ID INT(10) NOT NULL AUTO_INCREMENT,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT(10) NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL(10,2),
PRIMARY KEY (ID));
CREATE TABLE report_invoice(
Id INT IDENTITY PRIMARY KEY,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL );