简体   繁体   中英

create a column with default value as null in mysql database

我想创建一个默认值为null的列,并且在执行任何操作时应将其更改为0。如何在mysql数据库中执行此操作?

Here example how to add colum in existing table with default value

ALTER TABLE `test1` ADD `no` INT NULL DEFAULT NULL ;

When you call function then you have to write following query

UPDATE test1 SET `no` = '0' WHERE `test1`.`id` =your_id;
CREATE TABLE test 
(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
test_id INT,
cost FLOAT(5,2) DEFAULT NULL,
);

each time when you do some operation on that you need to update it as @Sadikhasan

or write a trigger that will update it to zero automatically.

if the operation you want to perform is read then write trigger on ON SELECT

if the operation you want to perform is update then write trigger on ON UPDATE

like wise for others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM