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