Is it possible to create a casted index on MySQL? - mysql

So I tried to create a casted index on a table (The data type of my column is char(64)), my query is like that:
CREATE INDEX idx_column_name ON table (CAST(column_name AS INTEGER));
But it didn't work, It gives the error SQL Error [1064] [42000] which means that I have an error in my syntax, I tried so many syntaxes but still doesn't work, Can anyone have a clear idea about what exactly the issue is?

You have 2 errors in your statement.
INTEGER is not correct datatype which can be used in CAST(). Use SIGNED or UNSIGNED instead.
The expression must be enclosed with separate parenthesis.
So correct syntax is:
CREATE TABLE table_name (column_name VARCHAR(64));
CREATE INDEX idx_column_name ON table_name ( (CAST(column_name AS UNSIGNED)) );
SHOW CREATE TABLE table_name;
Table
Create Table
table_name
CREATE TABLE `table_name` (  `column_name` varchar(64) DEFAULT NULL,  KEY `idx_column_name` ((cast(`column_name` as unsigned)))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
fiddle

Related

I am getting Error (1064) with this

I am trying to make tables from a lua script (on a fiveM server) however I get the 1064 error when it tries to run the lines below. I don't know where the problem lies. I will post some of the lines as they are all similar. I am running the latest version of mysql database.
ALTER TABLE vrp_user_vehicles ADD IF NOT EXISTS veh_type varchar(255) NOT NULL DEFAULT 'default' ;
ALTER TABLE vrp_user_vehicles ADD IF NOT EXISTS vehicle_plate varchar(255) NOT NULL;
ALTER TABLE vrp_user_vehicles ADD IF NOT EXISTS vehicle_colorprimary varchar(255) DEFAULT NULL;
ALTER TABLE vrp_user_vehicles ADD IF NOT EXISTS vehicle_turbo varchar(255) NOT NULL DEFAULT 'off';
If you check out the mysql manual on alter table statement, you can see that it does not support IF NOT EXISTS clause. You need to remove them from your statements.
You must use the columns view in the information_schema if you want to check if a field exists.
select count(*) from information_schema.columns
where table_schema='your db name' and table_name='vrp_user_vehicles' and column_name='veh_type'

Failed to set default null value for column

I already have a table, simplified example:
CREATE TABLE t (c INT NOT NULL);
And I need to change column default value to NULL, so I tried:
ALTER TABLE t ALTER COLUMN c SET DEFAULT NULL;
but I got the error "Error Code: 1067. Invalid default value for 'c'".
It looks really strange, because query conforms with official docs.
I even tried to:
ALTER TABLE t ALTER COLUMN c DROP DEFAULT;
and after it to make a 'SET DEFAULT NULL' query, but the same error occurred.
It's interesting, that query like:
ALTER TABLE t ALTER COLUMN c SET DEFAULT 1;
executed without errors.
I know, that it is possible to change column default value to NULL in my case using:
ALTER TABLE t MODIFY COLUMN c INT NULL;
but this query is really slow on big tables (it is much slower, than queries like 'SET DEFAULT 1')
So, how to just change default value to NULL?
I mean, without any overhead caused by 'MODIFY COLUMN' command.
Details: MySQL x64 version 5.7.10, Win8. Tested using MySQL Workbench.
By creating column as NOT NULL you have created a CONSTRAINT - declaring that values entered into that column may never be NULL.
A default value of NULL (set to null is value not present during INSERT) would create invalid data.
As sadly nullability constraint is part of the datatype in mysql the only way to make the column nullable will be
ALTER TABLE t MODIFY COLUMN c INT NULL;

MySQL: How to solve syntax error on ALTER TABLE statement during change type of column

When running
ALTER TABLE my_table modify column my_column int(10) NOT NULL DEFAULT 0;
I've got the error message:
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 'int(10) NOT NULL DEFAULT 0' at line 1.
How can this issue being fixed?
ALTER TABLE ... MODIFY COLUMN ... does not allow renaming the column; this is why the name of the column must be provided only once (the current name).
In order to rename the column (besides other changes you may want to operate on it, like changing its type) you have to use ALTER TABLE ... CHANGE COLUMN ... and provide the current and the new name of the column.
See the documentation page of the ALTER TABLE statement for more explanation and examples.
Try this code
ALTER TABLE my_table CHANGE mycolumn my_column INT( 10 ) NOT NULL DEFAULT '1';

converting mysql scripts to postgresql script

I have the following line in a .sql file from a mysql db:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50) DEFAULT NULL;
I would like to convert it into syntax that postgresql would understand. In my personal tests, I was only able to get it to work by breaking it down into two separate statements, like so:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50);
ALTER TABLE lcr_gw ALTER COLUMN ip_addr SET DEFAULT NULL;
Just wondering if there's a way to consolidate the two statements back into one, but one that postgresql will be happy with?
Thanks!
The statement you posted is not valid syntax at all:
SQL Fiddle
To change the type in MySQL, you would use CHANGE or MODIFY.
To change the default you would use DROP DEFAULT or SET DEFAULT NULL.
If the intention was to change the type and reset the column default:
Like in MySQL, you can pack multiple actions into a single ALTER TABLEstatement in Postgres .
ALTER TABLE lcr_gw ALTER COLUMN ip_addr SET DEFAULT NULL
,ALTER COLUMN ip_addr TYPE VARCHAR(50);
Per documentation:
The main reason for providing the option to specify multiple changes
in a single ALTER TABLE is that multiple table scans or rewrites can
thereby be combined into a single pass over the table.
But if there was a DEFAULT on the column that is incompatible with the new type, you have to run two separate statements:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr SET DEFAULT NULL;
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50);
Doesn't matter in this case anyway.
As #Gordon Linoff states in the comments, postgreSQL by default sets a value to null unless a value is given or the default is changed to something else;
therefore, all you'll need is:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50);
The PostgreSQL ALTER TABLE syntax diagram doesn't show any way to combine changing a data type and changing a default value in a single SQL statement. You can't simply omit set default null in the general case. For example,
create table test (
column_1 char(10) not null default 'a'
);
alter table test alter column column_1 type varchar(50);
insert into test values (default);
select * from test;
column_1
--
a
Instead, either rewrite as two independent statements (which you already know how to do), or as two statements in a single transaction.

Add the NOT NULL constraint to a column

I'm using PHPMyAdmin and I try to add the NOT NULL constraint to a column of my table.
PHPMyAdmin accepts my following query :
ALTER TABLE `wall` MODIFY `token_message` varchar(40) NOT NULL;
But I can still insert empty strings (=NULL), I don't understand why.
PS : If you're going to give me some other queries to add this constraint, note I've have tried these 3 which don't work in my PHPMyAdmin (kind of error : #1064 - You have an error in your SQL syntax; check the manual) :
ALTER TABLE `wall` ALTER COLUMN `token_message` SET NOT NULL;
ALTER TABLE `wall` ALTER COLUMN `token_message` varchar(40) NOT NULL;
ALTER TABLE `wall` MODIFY `token_message` CONSTRAINTS token_message_not_null NOT NULL;
You wrote, "I can still insert empty strings (=NULL)," which sounds like a misunderstanding. In SQL, an empty string does not evaluate to NULL, or vice versa. Try inserting an empty string and doing SELECT from wall where token_message is NULL. You should get zero rows back. Then try doing an insert where you specify NULL (unquoted) as the value for your column, and you should get the expected error message.
If those tests work as expected, then everything is fine, and your problem is actually that you want to prevent blank strings from being inserted. Check out this question for suggestions, or just check for blank strings during validation, before the query.
MySQL's column alter syntax requires you to completely re-specify the column. You can't just change one attribute of a column, you have to re-define it completely:
ALTER TABLE wall MODIFY token_message varchar(40) NOT NULL default ''
The only 'SET' version allowed is to change the default value.
ref: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
I think this is a matter of scrubbing your inputs. As octern mentioned, an empty string ('') is not a NULL value in sql. The best way to handle this is to only allow updates through a store procedure which strips out empty strings, even space characters:
CREATE PROC InsertIntoMyDb (#MyVarChar VARCHAR(2000)) AS
SET #MyVarChar = NULLIF(RTRIM(LTRIM(#MyVarChar)), '')
INSERT INTO [TBL] (MyVarChar)
VALUES #MyVarChar
This will truncate any number of spaces to an empty string, turn an empty string into a NULL, and then it will not allow the NULL value to be inserted based on the constraint you already have in place.
Try to use this query
Alter table table_name
change column_name column_name datatype(length) definition
ie,
Alter table wall
change tocken_message tocken_message varchar(40) NOT NULL DEFAULT