The SQL Server DEFAULT constraint is a constraint in a SQL Server database, and the following describes how SQL Server default constraints, column constraints, and table constraints are defined for your reference.
SQL Server Default Constraints
The SQL Server DEFAULT constraint specifies the default value for a column by defining the default value for the column or by using the column of the database's default object binding table. SQL Server recommends using default constraints instead of specifying the default values for the columns by using the default values defined.
The syntax for defining SQL Server default constraints is as follows:
CONSTRAINT constraint_name
DEFAULT constant_expression [For column_name]
Cases:
Constraint de_order_quantity default for?? Order_quantity
Note: You cannot define a DEFAULT constraint when you create a table, you can only add a default constraint to a table that has already been created.
SQL Server column constraints and table constraints
For a database, constraints are divided into column constraints (columns Constraint) and table constraints (table Constraint).
A column constraint acts only on the column itself as part of the column definition. Table constraints, as part of the table definition, can be used to
multiple columns.
The following example illustrates the difference between a column constraint and a table constraint.
Cases:
- Create?table?products? ( ?
- P_id?char (8)?,?
- P_name?char (?,?)
- price?money?default?0.01?,?
- Quantity?smallint?check? (quantity>=10)?,?/* column constraints? */?
- Constraint?pk_p_id?primary?key? (p_id,?p_name)?/* Table constraint? */??
SQL Server default constraints, column constraints, and table constraints