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.
Related
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
An existing MySQL table has a DateTime field which is not null and having a Default Value set as '0001-00-00 00:00:00'. Is it possible to Alter this table to remove the default value for the DateTime field ?
Yes, you can drop the default using an ALTER TABLE statement like this:
alter table your_table
alter column your_column drop default;
To drop the default from multiple datetime columns in a table:
ALTER TABLE your_table
ALTER COLUMN columnname1 DROP DEFAULT,
ALTER COLUMN columnname2 DROP DEFAULT,
ALTER COLUMN columnname3 DROP DEFAULT,
....
You can simply change the column and exclude default clause using following query. Here T1 is table containing default value for column updated and subject. You can simply remove default value as given below:
ALTER TABLE `T1`
CHANGE `updated` `updated` DATETIME NOT NULL,
CHANGE `subject` `subject` VARCHAR(100)
Is there a way to add a new column and set its default value in MySQL? When I run this command I get a syntax error:
ALTER TABLE tableName ADD newColumn varchar(20) SET DEFAULT 'test';
The documentation I've found doesn't really help me.
The SET shouldn't be necessary:
ALTER TABLE tableName ADD newColumn varchar(20) DEFAULT 'test';
try this
ALTER TABLE tableName ADD COLUMN newColumn VARCHAR(20) DEFAULT 'test';
When I create a new table
create table test(rId int auto_increment, createtimestamp timestamp not null, PRIMARY KEY (rId));
it creates a table with default schema for
createtimestamp as CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
Expected behavior was no default set. But it's getting set to default: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP. Why is this happening?
I can alter this after the creation with ALTER TABLE test CHANGE COLUMN createtimestamp createtimestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ; but can't alter it to ALTER TABLE test CHANGE COLUMN createtimestamp createtimestamp TIMESTAMP NOT NULL ;.
It looks like a default has to be specified but this is exactly what I don't want. How can I achieve a behavior where the default is not set and schema is not null, just after creation of table and without any alteration required?
MySql Version: 5.5.49
The initial answer is correct, but if it's not acceptable to have a default or a NULL in the definition, one might consider either using datetime instead, or upgrading to MySQL 5.6 which allows behaviour change http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp.
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 '{}';