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