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;
Related
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 want to add a new NOT NULL column to an existing table which has data in MySQL 5.7. I have seen this question and I am using the solution suggested there.
I am adding the new column as NULL, populate data for the new column and then change the column from NOT NULL to NULL.
-- 1. add new column as null
ALTER TABLE `mytable` ADD COLUMN newCol BIT NULL AFTER curCol;
-- 2. populate default data for new column
SET sql_safe_updates = 0;
UPDATE `mytable` SET newCol = 0;
SET sql_safe_updates = 1;
-- 3. change the column to NOT NULL
ALTER TABLE `mytable` ALTER COLUMN newCol BIT NOT NULL;
But I am getting the following error on the last command:
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 'BIT NOT NULL:' at line 1
This piece of SQL is not valid in MySQL:
ALTER TABLE `mytable` ALTER COLUMN newCol BIT NOT NULL;
Instead, consider :
ALTER TABLE `mytable` MODIFY newCol BIT NOT NULL;
Reference : MySQL ALTER TABLE syntax
You can do this in three steps:
Add your new column (initially, let it have NULL values)
ALTER TABLE my_table ADD COLUMN new_col datatype NULL AFTER cur_col;
Update the table so that there are no NULL in our new column
UPDATE my_table SET new_col = 0 WHERE new_col IS NULL;
Modify our new column to NOT NULL
ALTER TABLE my_table MODIFY COLUMN new_col datatype NOT NULL;
Reference: StackOverflow- Altering a column: null to not null
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
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;
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';