Basic operation of MySQL database - constraints (concept + syntax)

what is constraint

Concept: A constraint is actually a restriction on the data in a table.
Function: Guarantee the integrity and validity of the data in the table
Classification:
primary key constraint (handling)
self-growth constraint,
non-null constraint,
unique constraint,
default constraint
, zero fill constraint,
foreign key constraint

1. Primary key constraints

1.1 Concept:
A Mysql primary key constraint is a column or a combination of multiple columns whose value can uniquely identify each row in the table, making it easy to find a certain row as soon as possible in a relational database management system (1) The primary key constraint is equivalent to a unique constraint
+ The combination of non-null constraints, the primary key constraint column does not allow duplicates, and does not allow null values.
(2) Each table only allows at most one primary key
(3) The keyword of the primary key constraint is: primary key
(4) When creating the primary key When constraining, the system will create a corresponding unique index on the column and column combination by default.

1.2 Adding a single-column primary key:
Syntax 1:

create table table name(
...
field name type primary key,
...
);

Grammar two:

create table table name(
...
primary key(field name)
);

Example:

create table test1 (
id int primary key,
name varchar(20),
address varchar(20),
birthday date
); 
-- 方法一
create table test2 (
id int,
name varchar(20),
address varchar(20),
birthday date,
primary key(id)
);
-- 方法二

Result:
created successfullySelect the table, click the design table
design tablegolden key to represent the primary key
1.3 Add joint primary key

The so-called joint primary key is that the primary key is composed of multiple fields in a table.
Note:
(1) When the primary key is composed of multiple fields, the primary key constraint cannot be declared directly after the field name.
(2) A table can only have one primary key, and the joint primary key is also a primary key

grammar:

Create table table name(
...
Primary key(field name 1, field name 2...) )
;

Example:

use mydb1;
create table test3(
id int,
name varchar(10),
address varchar(10),
birthday date,
primary key(id,name)
);

Result:
click on the design table
test3You can see that there are two keys, but there is only one primary key, because a table can only have one primary key
1.4 Add primary key
syntax by modifying table structure

Create table table name (
...
);
Alter table table name add primary key (field list);

Example:

use mydb1;
create table test4(
id int,
name varchar(10),
address varchar(10),
birthday date
);
---新建表
alter table test4 add primary key(id,name);
---指定联合主键

You can add either a single-column primary key or a combined primary key
Validation:
test41.5 Delete Primary Key
Syntax:

Alter table table name drop primary key;
— Both single-column and multi-column primary keys can be deleted with this statement

2. Self-growth constraints (auto_increment)

2.1 Self-growth constraint
In MySQL, when the primary key is defined as self-growth, the value of the primary key no longer needs to be entered by the user, but is automatically assigned by the database system according to the definition. Every time a record is added, the primary key will automatically be the same The step size is increased.
By adding the auto_increment attribute to the field to achieve primary key self-growth

grammar:

Field name type primary key auto_increment

Features of self-growth constraints
(1) By default, the initial value of auto_increment is 1, and every time a new record is added, the field value is automatically increased by 1.
(2) Only one field in a table can use the auto_increment constraint, and the field must have a unique index to avoid duplication of serial numbers (that is, the primary key or part of the primary key).
(3) The fields constrained by auto_increment must have the NOT NULL attribute.
(4) The field constrained by auto_increment can only be of integer type (TINYINT, SMALLINT, INT, BIGINT, etc.).
(5) The maximum value of the constrained field of auto_increment is constrained by the data type of the field. If it reaches the upper limit, auto_increment will fail.

2.2 Specifying the initial value of the auto-increment field
If the initial value of the field is set for the first record, then the newly added record will auto-increment from this initial value. For example, if the id value of the first record inserted in the table is set to 5, then when the record is inserted again, the id value will increase from 5 Syntax
:


Method 1, specify the Create tabele table name when creating a table (
Id int primary key auto_increment,
Name varchar(20)
) auto_increment=5;

Method 2: After creating the table, specify
the Create table table name (
Id int primary key auto_increment,
Name varchar(20)
);
Alter table table name auto_increment=5;

3. Unique constraint (unique)

The unique constraint (Unique Key) means that the value of the field in all records cannot be repeated. For example, after adding a unique constraint to the id field, the id value of each record is unique and cannot be duplicated.
grammar:

Method 1:
field name type unique
method 2:
alter table table name add constraint constraint name unique (field name);
Note: method 1 is practical when creating a table, method 2 is used when the table has already been created

The unique constraint cannot be repeated, but it can be empty
Alter table table name drop index unique constraint name
If there is no name, the constraint name is the field name

4. Default constraint (default)

Role: used to specify the default value of a column
Syntax:

Create:
Method 1:
field name type default default value
Method 2:
Alter table table name modify column name type default default value;
delete the default constraint (just understand, rarely used)
set the default value of creation method 2 to null
Alter table table name modify column name type default null;

5. Zero fill constraint (zonefill)

When inserting data, when the length of the value of the field is less than the defined length, the corresponding 0 will be added in front of the value. Zerofill defaults
to int (10)
. When using zerofill, the unsigned (unsigned) attribute will be automatically added by default , after using the unsigned attribute, the value range is twice the original value, for example, signed is -128~+127, and unsigned is 0-256.
grammar:

field name type zonefill

Guess you like

Origin blog.csdn.net/caolongbin/article/details/127026477