Lou.rn
Lou.rn

Reputation: 105

mysql, Create Column with a default value

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.

Upvotes: 2

Views: 2353

Answers (2)

bimal sharma
bimal sharma

Reputation: 169

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');

Upvotes: 1

D-Shih
D-Shih

Reputation: 46219

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:

Upvotes: 3

Related Questions