SQL database constraint behavior---Prevent data from being completely duplicated

Source: Internet
Author: User

Prevent the same piece of data from being completely duplicated:

Primary keyword constraint: PRIMARY KEY constraint.
1. Be able to uniquely differentiate each row of data. --Don't weigh
2. The data in the table is sorted by primary key word. --Orderly
3. Primary key cannot be empty--not empty
4. A table can have only one primary key, but it is possible to set a combined primary key.
Action: In the table Design interface (in the table to add the primary key to the right mouse button selection design, such as), on the corresponding column, right-click-"Set Primary key"/"Delete primary key". Or click the top left corner key icon

Code implementation:

CREATE TABLE Car1 (

Code varchar (primary key),

Name varchar (50),

Brand varchar (50)

)

When there are two columns of elements as primary keys

CREATE TABLE Car2 (

Code varchar (50),

Name varchar (50),

Brand varchar (50),

Primary KEY (Code,name)

)

Two, unique key constraints. The candidate key.
1. No weight, can be empty.
2. You can have multiple unique keys
3. Unique keys are not automatically sorted.

Action: On the table design interface, right-click--"Index/Key" in the pop-up window, click "Add" in the lower left corner, the right property list, modify type-unique key, set the column

Code implementation:

CREATE TABLE Car2 (

Code varchar (50),

Name varchar (50),

Brand varchar (unique),

)

Iii. Self-growth columns: Identity column, identity column
1. You cannot manually fill in the values. If you are writing an INSERT statement, the statement should ignore this column
2. Self-growing columns are mainly used for integer, long Integer, decimal type.
3. Do not arbitrarily use the self-growth column as the primary key.

Action: In the table design interface, select the appropriate column, in the following property panel--"Identity Specification"--Display is

Code implementation

CREATE TABLE work (

IDS int identity (first) not null-(1) increased by 1 each time

)

SQL database constraint behavior---Prevent data from being completely duplicated

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.