MYSQL Sintax Error in the sentence with the attribute default - mysql

Hello stackoverflows friends.
My problem in sql is this sintaxis:
ALTER TABLE user MODIFY COLUMN (role INT(10)) DEFAULT 2 NOT NULL;
I want to modify a column in this case role but my default should be 2.
Thanks.

This syntax should work:
alter table user modify column role int default 2;
Here is a SQL Fiddle.

Try this - For SQL
ALTER TABLE USER ALTER COLUMN ROLE SET DEFAULT(2);
For MySql :
ALTER TABLE USER ALTER ROLE SET DEFAULT(2);

Related

Spring boot migration 1.5.x to 2.x, H2 database 1.4.200 version throwing syntax error in alter command

I have upgraded my spring boot version from 1.5.x to 2.0.0 and updated h2 DB version to 1.4.200. In the process, h2 is throwing error with my alter column command.
Actual alter command
--ALTER TABLE log_activity
--CHANGE COLUMN name name VARCHAR(255) NOT NULL,
--CHANGE COLUMN address address TEXT NOT NULL,
--CHANGE COLUMN status status TEXT NOT NULL;
Modified alter command (ref: https://www.h2database.com/html/commands.html#alter_table_alter_column)
ALTER TABLE log_activity
ALTER COLUMN name SET NOT NULL,
ALTER COLUMN address SET NOT NULL,
ALTER COLUMN status SET NOT NULL;
Still I am facing syntax issues with the above commands.
Migration V1_2__REFINE_FIELD_CHANGE_FIELDS.sql failed
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "ALTER TABLE log_activity
ALTER COLUMN name SET NOT NULL,[*]
ALTER COLUMN address SET NOT NULL,
ALTER COLUMN status SET NOT NULL"; SQL statement:
ALTER TABLE log_activity
ALTER COLUMN name SET NOT NULL,
ALTER COLUMN address SET NOT NULL,
ALTER COLUMN status SET NOT NULL [42000-200]
.
.
.
Line : 6
Statement : ALTER TABLE log_activity
ALTER COLUMN name SET NOT NULL,
ALTER COLUMN address SET NOT NULL,
ALTER COLUMN status SET NOT NULL
I am could see there is [*] gets appended after the ALTER COLUMN name SET NOT NULL,
I am kinda stuck with this issue since two days.
Can you please tell me where I am going wrong.
Neither CHANGE COLUMN nor standard ALTER COLUMN can be used to alter multiple columns at once in H2, you need to write three separate commands, for example, with ALTER COLUMN:
ALTER TABLE log_activity ALTER COLUMN name SET NOT NULL;
ALTER TABLE log_activity ALTER COLUMN address SET NOT NULL;
ALTER TABLE log_activity ALTER COLUMN status SET NOT NULL;
The SQL Standard also doesn't have multi-column DDL operations, only some DBMS support them with various vendor-specific syntaxes.
Please note that 1.4.200 is an old unsupported version of H2 Database.
ALTER TABLE … CHANGE COLUMN is accepted by newer versions of H2 only in MySQL and MariaDB compatibility modes, if you want to use CHANGE COLUMN in H2 2.*.*, you need to enable one of these modes first:
https://h2database.com/html/features.html#compatibility

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'

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.

SQL alter table works in MySQL but not in Oracle

I have created a table users
create table users (
id int,
name varchar(40)
);
Now i want a default value for name
This query works in MYSQL but not in Oracle database 11g XE
alter table users alter name set default 'user';
Can anyone explain why ?
The syntax for adding a default to an existing column is different in Oracle, viz:
alter table users
modify (name default 'user');
SqlFiddle here
I think the correct query will be like this:
ALTER TABLE users MODIFY name VARCHAR(40) NOT NULL DEFAULT 'user';

Rename a MySQL column containing ` in the column name

So, the other guy at work created a table with a column called:
Max(`abs_spg_20090430`.`ID`)
this is giving me an error now that I am trying to run a dump of the database on a different server.
I am trying to rename it, but
ALTER TABLE abs_spgID_20090504 CHANGE Max(`abs_spg_20090430`.`ID`) id bigint default null;
as well as
ALTER TABLE abs_spgID_20090504 CHANGE `Max(`abs_spg_20090430`.`ID`)` id bigint default null;
give me an error. Does any of you friendly people have a hint? Many thanks!
you need to quote your quotes and the column too, e.g:
ALTER TABLE abs_spgID_20090504 CHANGE `Max(``abs_spg_20090430``.``ID``)` id BIGINT DEFAULT NULL;