Mssqlserver constraints

Source: Internet
Author: User
Tags mssqlserver one table

There are several constraints:

Not null: the content used to control fields must NOT be NULL ).


UNIQUE: The control field content cannot be repeated. A table can have multiple Unique constraints.

Primary key: it is also used to control the content of fields that cannot be repeated, but it can only appear in one table.

Foreign key: the foreign key constraint is used to prevent the action of damaging the connection between tables. The foreign key constraint can also prevent illegal data insertion into the foreign key column, because it must be one of the values in the table to which it points.

CHECK: used to control the value range of a field.

DEFAULT: set the DEFAULT value of the new record.


1. not null: the content used to control the field must not be NULL ).

Usage: Create table MyTable
               (
Id varchar (32) not null,
Name varchar (32)
               )
2. Unique: the control field content cannot be repeated. A table can have multiple Unique constraints.

NEW:
Add the Unique syntax supported by SQL Server, Orcale, and MS Access:
Create table MyTable
                (
Id varchar (32) not null unique,
Name varchar (32)
                )
Add the Unique syntax supported by SQL Server and My SQL: you can add several fields as constraints at the same time, such as: unique (id, name)
Create table MyTable
                (
Id varchar (32) not null,
Name varchar (32 ),
Unique (id ,.....)
                 )
Add the Unique syntax supported by SQL Server, Orcale, MS Access, and My SQL:
Create table MyTable
                  (
Id varchar (32) not null,
Name varchar (32 ),
Constraint uniqueName unique (UniqueColumn ,.....)
                   )

Delete:
Delete the Unique constraint syntax in SQL Server, Oracle, and MS Access: drop constraint UniqueName;
Delete Unique constraint syntax in My SQL: drop index UniqueName;

Modify:

Alter table EPlatform
Add constraint Unique_EPlatform
Unique ([UserId], [Platform]);

 

3. Primary Key: the content of the control field cannot be repeated, but only one table can appear.
Add the Primary Key syntax supported by SQL Server, Orcale, and MS Access:
Create table myTB1
             (
Id nvarchar (32) not null primary key,
Name nvarchar (32)
              )
Supported in SQL Server and My SQL

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.