SQL Server default constraints, column constraints, and table constraints

Source: Internet
Author: User
Tags table definition

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:

    1. Create?table?products? ( ?
    2. P_id?char (8)?,?
    3. P_name?char (?,?)
    4. price?money?default?0.01?,?
    5. Quantity?smallint?check? (quantity>=10)?,?/* column constraints? */?
    6. Constraint?pk_p_id?primary?key? (p_id,?p_name)?/* Table constraint? */??

SQL Server default constraints, column constraints, and table constraints

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.