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
Related
I can't figure out why my alter table query throws an error. Currently, this is a (DATETIME) column with default value NULL.
My wish is to alter it so datetime value gets automatically populated when I update a row. I'm trying to write an alter statement, but I can't figure out why mine is throwing the error.
My alter statement
ALTER TABLE `mydb`.`orders` CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}';
And this is the error that I'm getting
16:28:34 ALTER TABLE `mydb`.`orders` CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}' 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 'NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}'' at line 1 0.00041 sec
I'm using MySQL version 5.7
The CHANGE COLUMN alteration is used when you might want to change the name of the column, and requires you to provide the new name after the old name. If you're not renaming the column, you have to provide the name twice. Your command tries to rename the date_u column to DATETIME, and it's missing the datatype before the NULL keyword.
Use MODIFY COLUMN instead. It's the same, but doesn't allow renaming, so doesn't require you to give the column name twice.
ALTER TABLE `mydb`.`orders` MODIFY COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
I'm also not sure what you intended with '{}' at the end, but I don't think it's valid syntax, either, so I've removed it.
You're missing to name the new column.
change this:
ALTER TABLE `mydb`.`orders`
CHANGE COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP '{}';
into this:
ALTER TABLE `mydb`.`orders`
CHANGE COLUMN `date_u` `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
Notice the column name is typed twic, because you want the column name to stay the same, formerly here's the change column syntax:
ALTER TABLE `table_name`
CHANGE COLUMN `column_name` `column_new_name` (...);
Or, you can just you modify column syntax:
ALTER TABLE `mydb`.`orders` MODIFY COLUMN `date_u` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;
Ps: I don't get what you mean by '{}' so I removed it because I think it's not a valid syntax.
Hope I pushed you further.
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
Is it possible to set a default value for new records only in a mysql column?
I want all rows before today to be null if nothing has been entered, but any new rows added after today to be a specific string if nothing has be entered for that column.
Is this possible?
Search in all rows in the table and check if the specific column is empty, if yes set it to null.
UPDATE tablename SET column = CASE column WHEN '' THEN NULL ELSE column END
For future entries you can add to your table a Default Constraint like this:
ALTER TABLE tablename ALTER column SET DEFAULT 'specific string'
This is an old post but I do think this answer might help other folks.
ALTER TABLE object_table_name ADD `value` INT NULL;
ALTER TABLE object_table_name MODIFY COLUMN `value` INT DEFAULT 0;
Doing it this way will only impact newly inserted records, this code did not work for me
ALTER TABLE object_table_name ADD `value` INT DEFAULT 0
I'm using MySQL 5.6
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';