8

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 '{}';
3
  • what is your previous column type? Commented Jan 4, 2014 at 5:01
  • I'm trying to modify DATE and INT columns. Commented Jan 4, 2014 at 5:02
  • Careful about the 0000-00-00 as that is not within the valid date range. dev.mysql.com/doc/refman/5.7/en/datetime.html Commented Jul 2, 2016 at 23:24

3 Answers 3

13

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'; 
Sign up to request clarification or add additional context in comments.

2 Comments

Will this apply 0000-00-00 to all my old rows that currently have None, or do I need to run a query to change those?
@macdonjo It will apply 0000-00-00 to all rows where the field is null in your column.
0

try this

ALTER TABLE foobar_data CHANGE COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';

user CHANGE to alter a existing column

see Link

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.