How to resolve the syntax error in sql query - mysql

I am getting error for the query:
ALTER TABLE `cms_users` ADD `show_on_web` TINYINT(4)  NOT NULL  DEFAULT '1';
And i am getting 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 '  NOT NULL  DEFAULT '1'' at line 1
I am not sure what the error is.
To verify the Query - http://sqlfiddle.com

You can try below - you need to add column and also default will be 1 not '1' because your data type is tinyint
ALTER TABLE `cms_users`
ADD column `show_on_web` TINYINT(4)  NOT NULL DEFAULT 1

Change it to:
ALTER TABLE `cms_users` ADD COLUMN `show_on_web` TINYINT(4)  NOT NULL  DEFAULT 1;
I've added a COLUMN word and removed quotes of default value.
Because it's TINYINT - Integer, you need to use 1 instead of '1'.
Because '1' is string.

As fa06 posted, you are missing COLUMN after the ADD.
Also, there is a type mismatch with your DEFAULT clause (quotes imply VARCHAR and not TINYINT).
Try this :
ALTER TABLE `cms_users` ADD COLUMN `show_on_web` TINYINT(4)  NOT NULL  DEFAULT 1;
PS : For some reason, SQLFiddle does not work for me today. However, I juste tried the following :
CREATE TABLE cms_users (
id INT
);
ALTER TABLE `cms_users` ADD COLUMN `show_on_web` TINYINT(4) NOT NULL DEFAULT 1;
on https://paiza.io/en/languages/mysql , and it shows no compilation error.

Related

I need to create a null value in SQL

I am tasked to create a column with a null value but I got an error.
SELECT * FROM test.Persons
ALTER TABLE Persons
ADD mark_percentage FLOAT
This is my error.
12:32:47 use database test ALTER TABLE PERSONS ADD MARKS float SELECT * FROM test.Persons 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 'database test ALTER TABLE PERSONS ADD MARKS float SELECT * FROM test.Persons' at line 1 0.063 sec
Try this :
ALTER TABLE Persons
ADD mark_percentage FLOAT

MySQL Error 1366 when trying to change column data type to INT

I can't figure out why I can't change a column's data type from TEXT to INT. Here is the script I'm using.
ALTER TABLE covidworlddata.coviddeaths
ALTER COLUMN total_deaths INT;
Error 1366: Incorrect integer value: '' for column 'total_deaths' at row 1 SQL Statement: ALTER TABLE covidworlddata.coviddeaths CHANGE COLUMN total_deaths total_deaths INT(255) NULL DEFAULT NULL
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 'INT' at line 2
Is it because the first row of the csv is the column names (e.g. total_deaths) as seen below?
Source data csv
ALTER COLUMN does not allow to change the datatype. It only allows you to set/drop DEFAULT values and change the visibility of the column. You must use either CHANGE COLUMN or MODIFY COLUMN. See ALTER TABLE Statement.
This problem causes #1064.
Your column which you want to alter have string datatype now and contains empty string as a value. When you modify the column definition the server must convert the datatype for existing values. Empty string cannot be converted to numeric datatype correctly. You must update your table prevously and set the column values to the value which can be converted correctly (for example, this can be '0' or NULL).
This problem causes #1366.
You have a syntax error, so check the syntax. As a minimum you need this
ALTER TABLE covidworlddata.coviddeaths
CHANGE COLUMN total_deaths total_deaths INT;
That's old column name, new column name (which is the same here) and then the rest of the column definition.
Reference: https://dev.mysql.com/doc/refman/8.0/en/alter-table.html

Change type of a column in mysql

I want to change the DataType of a mySQL table from float to DECIMAL:
ALTER TABLE t_tapes ALTER COLUMN price DECIMAL(15,6);
but I got an 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 'DECIMAL(15,6)' at line 1
You Have to change only ALTER before COLUMN TO MODIFY
LIKE
ALTER TABLE t_tapes MODIFY COLUMN price DECIMAL(15,6);
need Modify instead of Alter
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
ALTER TABLE t_tapes
MODIFY COLUMN price DECIMAL(15,6);
Use the SQl Query for Change data type of Mysql Column
ALTER TABLE `db-name`.`table-name`
CHANGE COLUMN `column-name` `column-name` DECIMAL(15,6);

How to create random number for a column in mysql with DEFAULT Constraint?

The DEFAULT constraint has no problem in accepting string or current date values. What i need is an constraint that will create an random 4 digit number every time an entity is created. I tried the following code but it returns an syntax error.
ALTER TABLE client_number ADD(code INT(4) DEFAULT FLOOR(RAND()*9999)+1111);
The above statement returns following error:
#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 'floor(rand()*9999)+1111)' at line 1
Need solution.
The documentation is quite clear:
The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression.
The expression you have is not a constant, so it doesn't work. You would need to use a trigger instead.
Run the below query
ALTER TABLE table_name ADD field_name INT(4) NOT NULL DEFAULT (rand() * (9999 - 1000) + 1000) AFTER some_feild;
The result gives a random number between 1000-9999

Error in creatinng a field in the table

I have problem in creation a field name staus which type-BOOLEAN. and length is 1.
When I press the go button then this Massage Arrived
SQL query:
ALTER TABLE `abcd` ADD `status` BOOLEAN( 1 ) BINARY NOT NULL DEFAULT NULL
MySQL said: Documentation
#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 '(1) BINARY NOT NULL DEFAULT NULL' at line 1
I can't solve the problem. I don't know where the error occurring.
Please Help me to solve the problem.
Thank you.
The correct syntax for ALTER TABLE requires COLUMN after ADD
ALTER TABLE `abcd` ADD COLUMN ...
ALTER TABLE `abcd`
ADD COLUMN `status` BIT NOT NULL DEFAULT 0
If you want to create only a single bit field use BIT. You had 2 data types in your statement.
Your default value was null but you don't want to allow that: NOT NULL. Use 0 as default value instead.
For MySQL 5.0.3 and higher, you can use BIT. The manual says:
As of MySQL 5.0.3, the BIT data type is used to store bit-field values. A type of BIT(M) enables storage of M-bit values. M can range from 1 to 64.
Otherwise, according to the MySQL manual you can use bool and boolean which are at the moment aliases of tinyint(1):
Bool, Boolean: These types are synonyms for TINYINT(1). A value of zero is considered false. Non-zero values are considered true.