Alter to varchar - mysql

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

Related

need to alter column type to NOT NULL AUTO_INCREAMENT using ALTER COLUMN in MySQL

I want to change the column "ID" which is currently type INTEGER but have NULL values into type INTEGER NOT NULL AUTO_INCREMENT. But I am getting error. My SQL syntax is as follows.
ALTER TABLE users
ALTER COLUMN ID INTEGER NOT NULL AUTO_INCREMENT;
The above code is giving me syntax error.
Then I tried,
ALTER TABLE users
ALTER COLUMN ID INTEGER NOT NULL;
This is also giving me syntax error like
1064 - 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 'INTEGER NOT NULL' at line 2
Use MODIFY COLUMN:
ALTER TABLE users
MODIFY COLUMN ID INTEGER NOT NULL AUTO_INCREMENT;
There's also an ALTER COLUMN type of command, but it's only for specifying the DEFAULT for a column. Refer to https://dev.mysql.com/doc/refman/8.0/en/alter-table.html for examples.

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'

How can I change my existing column in MariaDB to Not Null?

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;

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

Adding new enum column to an existing table

I'm trying to add a gender column to my table with this query:
ALTER TABLE QRCodeUser ADD gender CHAR(1) enum('M','F') NOT NULL;
I get this 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 'enum('M','F') NOT NULL' at line 1
What's my mistake?
Try this (you dont need to specify the size, char(1) ) :
ALTER TABLE QRCodeUser ADD gender enum('M','F') NOT NULL;
Correct usage of syntax:
ALTER TABLE table_name ADD column_name enum(`field1`,`field2`,...);