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