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'
Related
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'm trying to alter a table from int to varchar using
ALTER TABLE shares
ALTER COLUMN link VARCHAR(255) NOT NULL;
Currently getting the error message
#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 'VARCHAR(255) NOT NULL' at line 2
If you're using MySQL, then the syntax should be:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
so:
ALTER TABLE shares MODIFY COLUMN link VARCHAR(255) NOT NULL;
The syntax you were using was for SQL Server.
ALTER TABLE table_name ALTER COLUMN column_name datatype;
Try with following query :
ALTER TABLE shares MODIFY COLUMN link VARCHAR(255) NOT NULL;
ALTER TABLE `shares`
MODIFY COLUMN `link` VARCHAR(255) NOT NULL;
That's work only if the old table definition have data that will be compatible with the new definition of the column
Message : 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 ''country_of_residence_id' INTEGER NOT NULL' Statement :
ALTER TABLE address ALTER COLUMN 'country_of_residence_id' INTEGER NOT
NULL
In my table 'address' I want to set an already existing column 'country_of_residence_id' to NOT NULL.
I tried it this way:
ALTER TABLE address
ALTER COLUMN 'country_of_residence_id' INTEGER NOT NULL;
My IDE underlines INTEGER and says: DROP or SET expected, got "INTEGER"
When I add SET before INTEGER it doesn't work either.
I found it here:
https://mariadb.com/kb/en/mariadb/alter-table/
alter table address modify country_of_residence_id bigint unsigned NOT NULL;
First of all, make all existing NULL values of rows disappear:
UPDATE [Table_Name] SET [Column_Name]=0 WHERE [Column_Name] IS NULL;
Then, update(alter) the table definition to reject NULLs:
ALTER TABLE [Table_Name] MODIFY [Column_Name] BIGINT UNSIGNED NOT NULL;
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';
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';