在SQL中使用DEFAULT约束

DEFAULT constraint is used to insert default value into a column on a table and if no any value is stored in any place of a column then default value will be added into it.

DEFAULT约束用于将默认值插入到表的列中,如果列的任何位置均未存储任何值,则将默认值添加到其中。

How to use DEFAULT with example?

如何在示例中使用DEFAULT?

CREATE TABLE EMployees
(E_Id int NOT NULL,E_Name varchar(25) NOT NULL,
Contact number(10),Address varchar(50),
City varchar(20) DEFAULT 'Gwaloir');

We can also achieve DEFAULT value property by using GETDATE() function:

我们还可以通过使用GETDATE()函数来实现DEFAULT值属性:

CREATE TABLE Employees
(E_Id int NOT NULL,E_name varchar(50),
E_Join_Date date DEFAULT GETDATE());

USE of ALTER to ADD DEFAULT constraint when table is already created:

已创建表时,使用ALTER来添加DEFAULT约束:

There is different way to add DEFAULT constraint in different database language:

有不同的方法可以用不同的数据库语言添加DEFAULT约束

  • MYSQL

    MySQL数据库

    ALTER table EmployeesALTER E_name SET DEFAULT 'Bharti';

  • SQL

    SQL

  •     ALTER TABLE Employees ALTER COLUMN City SET DEFAULT 'Gwalior';
    
    
  • Oracle

    Oracle

  •     ALTER TABLE Employees MODIFY Contact DEFAULT '0000';
    
    
    

    How to DROP DEFAULT constraint from a table?

    如何从表中删除默认约束?

        ALTER TABLE Employees ALTER COLUMN City DROP DEFAULT;
    
    

    Conclusion:

    结论:

    In this article we have learnt what DEFAULT constraint is, how to use it with example and how we can alter and drop DEFAULT on a table? If you have any doubt/query, feel free to ask in comment section.

    在本文中,我们了解了什么是DEFAULT约束 ,如何在示例中使用它,以及如何在表上更改和删除DEFAULT ? 如果您有任何疑问/疑问,请随时在评论部分提问。

    翻译自: https://www.includehelp.com/sql/DEFAULT-Constraint.aspx

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值