MySQL 如何将表字段的默认值设置为 0.00?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7481473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:07:21  来源:igfitidea点击:

How to set Default value of table field to 0.00?

mysqldatabasedefault

提问by aslamdoctor

I have created a table named "salary_mst" in MySql database. Table fields are

我在 MySql 数据库中创建了一个名为“salary_mst”的表。表字段是

id -> auto increment
name -> varchar(50)
salary -> double

Now if someone don't insert value in salary, it should store default 0.00 How can I do that ?

现在如果有人不在工资中插入值,它应该存储默认值 0.00 我该怎么做?

回答by Emil

ALTER TABLE `table`  ADD COLUMN `column` FLOAT(10,2) NOT NULL DEFAULT '0.00'

回答by Bohemian

create table salary_mst (
    id int not null primary key auto_increment,
    name varchar(50),
    salary double not null default 0
);

To test:

去测试:

insert into salary_mst (name) values ('foo');
select * from salary_mst;
+----+------+--------+
| id | name | salary |
+----+------+--------+
|  1 | foo  |      0 |
+----+------+--------+