I'm trying to modify a row, marketing_schedule, in my database to change the column to NULL to NO.
I've tried the command ALTER TABLE permissions MODIFY marketing_schedule tinyint(1) NOT NULL; as given in How to add not null constraint to existing column in MySQL5.1 but I get the error shown in the screenshot above. Any idea on why I'm getting this error and how I can go about fixing my problem?
Update rows which have NULL's in marketing_schedule to have a value and run the ALTER TABLE command again.
If your are providing the default not null then you have to provide default value
in query
These query run after the update , as answered by #slaakso
ALTER TABLE `table_name` Modify `column_name` TINYINT(4) DEFAULT 1 NOT
NULL;
OR
ALTER TABLE `table_name` CHANGE `column_name` `column_name` TINYINT(4)
DEFAULT 1 NOT NULL;
Here 1 is default value
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.
ALTER TABLE `pages` MODIFY `views` INT(11) NOT NULL DEFAULT 0
Trying to alter a column in a table which is currently allows NULL and has no default.
I want it to be NOT NULL and have a default of 0.
The Error message I get is
Invalid use of NULL value
This is because the table already has a row or more with null value, you might need to update those to 0 before executing ALTER table, e.g.:
UPDATE test SET views = 0 WHERE views IS NULL;
ALTER TABLE test MODIFY COLUMN views int NOT NULL DEFAULT 0;
Here's the SQL Fiddle (commenting out update statement will result in the same error).
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
How do I change my column's default value from None to something else? For example, I want my dates to have a default value of 0000-00-00 if I don't specify one when I create the row.
I understand this in phpMyAdmin, but I'm not sure how to do it via command prompt.
I also understand how to do this when adding a column. But all of my columns are made and have data in some of them.
ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;
From searching, I found this line, but I'm not sure if that's what I want?
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
Use ALTER TABLE to CHANGE or MODIFY the DEFAULT value of column. Check this link ALTER TABLE SYNTAX
ALTER TABLE `tableName` CHANGE `columnName` `columnName` DATE DEFAULT '0000-00-00';
ALTER TABLE `tableName` MODIFY `columnName` DATE DEFAULT '0000-00-00';
try this
ALTER TABLE foobar_data CHANGE COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
user CHANGE to alter a existing column
see Link
In my case, it worked
ALTER TABLE `table_name` CHANGE `col_name` `col_name_again` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'anydefault_text';
Hope it helps you too.
I got the following error while trying to alter a column's data type and setting a new default value:
ALTER TABLE foobar_data ALTER COLUMN col VARCHAR(255) NOT NULL SET DEFAULT '{}';
ERROR 1064 (42000): 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 SET DEFAULT '{}'' at line 1
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
A second possibility which does the same (thanks to juergen_d):
ALTER TABLE foobar_data CHANGE COLUMN col col VARCHAR(255) NOT NULL DEFAULT '{}';
As a follow up, if you just want to set a default, pretty sure you can use the ALTER .. SET syntax. Just don't put all the other stuff in there. If you're gonna put the rest of the column definition in, use the MODIFY or CHANGE syntax as per the accepted answer.
Anyway, the ALTER syntax for setting a column default, (since that's what I was looking for when I came here):
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 'literal';
For which 'literal' could also be a number (e.g. ...SET DEFAULT 0). I haven't tried it with ...SET DEFAULT CURRENT_TIMESTAMP but why not eh?
If you want to add a default value for the already created column,
this works for me:
ALTER TABLE Persons
ALTER credit SET DEFAULT 0.0;
For DEFAULT CURRENT_TIMESTAMP:
ALTER TABLE tablename
CHANGE COLUMN columnname1 columname1 DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHANGE COLUMN columnname2 columname2 DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
Please note double columnname declaration
Removing DEFAULT CURRENT_TIMESTAMP:
ALTER TABLE tablename
ALTER COLUMN columnname1 DROP DEFAULT,
ALTER COLUMN columnname2 DROPT DEFAULT;
In case the above does not work for you (i.e.: you are working with new SQL or Azure) try the following:
1) drop existing column constraint (if any):
ALTER TABLE [table_name] DROP CONSTRAINT DF_my_constraint
2) create a new one:
ALTER TABLE [table_name] ADD CONSTRAINT DF_my_constraint DEFAULT getdate() FOR column_name;
Accepted Answer works good.
In case of Invalid use of NULL value error, on NULL values, update all null values to default value in that column and then try to do the alter.
UPDATE foobar_data SET col = '{}' WHERE col IS NULL;
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
Try this
ALTER TABLE `table_name` CHANGE `column_name` `column_name` data_type NULL DEFAULT '';
like this
ALTER TABLE `drivers_meta` CHANGE `driving_license` `driving_license` VARCHAR(30) NULL DEFAULT '';
Use this approach:-
ALTER TABLE foobar_data
CHANGE COLUMN col VARCHAR(255) NOT NULL SET DEFAULT '{}';