2

I've a table item that has some columns that are nullable. To one of them type, I'd like to automatically insert a default value (instead of a NULL) whenever a new record in inserted in the table and do not specify a value for that column.

Can it be done without affecting the existing data? The type column is a varchar.

I can update the current nulls.

2 Answers 2

3

You can try to ALTER column set a default value.

ALTER TABLE `T` MODIFY `type` varchar(50) DEFAULT 'default'; 

then insert by DEFAULT keyword:

INSERT INTO T (type) VALUES (DEFAULT); 

Results:

Sign up to request clarification or add additional context in comments.

Comments

1

This query will work for you.

For update table.

ALTER TABLE `column_name` CHANGE `tab` `my_id` INT(11) NOT NULL DEFAULT '0'; 

For insert table

CREATE TABLE `db_name`.`Tbale_name` ( `demo` INT NOT NULL DEFAULT '0'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.