Database study notes (four, table constraints)

The real constraint on the field is the data type, but the data type constraint is very single, and some additional constraints are needed to better ensure the legitimacy of the data and ensure the correctness of the data from the perspective of business logic. For example, if one field is email, the requirement is unique. There are many constraints on the table, mainly including: null/not null, default, comment, zerofill, primary key, auto_increment, unique key.


1. Empty attributes

Two values: null (default) and not null (not empty). The
database default fields are basically empty fields, but in actual development, ensure that the fields are not empty as much as possible, because the data is empty and there is no way to participate in the operation.

2. Default value

Default value: A certain type of data will often appear a specific value, which can be specified at the beginning. When real data is needed, the user can selectively use the default value. When data is inserted, do not assign a value to the field, and use the default value.

create table tt10 (
-> name varchar(20) not null,
-> age tinyint unsigned default 0,
-> sex char(2) default '男'
-> );

3. Column description

Column description: comment, which has no actual meaning. It is specifically used to describe the field. It will be saved according to the table creation statement for the programmer or DBA to understand.

create table tt12 (
-> name varchar(20) not null comment '姓名', 
-> age tinyint unsigned default 0 comment '年龄', 
-> sex char(2) default '男' comment '性别' 
-> );

4. Primary key
: The primary key is used to uniquely constrain the data in the field. It cannot be repeated or empty. There can only be one primary key in a table; the column where the primary key is located is usually an integer type.
When creating the table, after all the fields, use the primary key (primary key field list) to create the primary key. If there are multiple fields as the primary key, you can use a composite primary key.

create table Tab( 
 -> id int unsigned,
 -> course char(10) comment '代码', 
 -> score tinyint unsigned default 60 comment '成绩', 
 -> primary key(id, course) -- id和course为复合主键
 -> );

When the table is created, you can add the primary key again

alter table 表名 add primary key(字段列表)

Primary key constraint: The field corresponding to the primary key cannot be repeated. Once repeated, the operation will fail.
Delete the primary key:

alter table 表名 drop primary key;

5. Self-increment
auto_increment: When the corresponding field does not give a value, it will be automatically triggered by the system, and the system will get a new and different value from the maximum value already in the current field by +1 operation. Usually used with the primary key as a logical primary key.
The characteristics of self-growth:
1) Any field should be self-growth, provided that it is an index (the key column has a value)
2) The self-growth field must be an integer
3) A table can only have one self-growth
6. Index
In a relational database, an index is a single, physical storage structure that sorts the values ​​of one or more columns in a database table. It is a collection of one or more column values ​​in a table and corresponding points A list of logical pointers to data pages that physically identify these values ​​in the table. The function of the index is equivalent to the catalog of books, you can quickly find the content you need according to the page number in the catalog.
The index provides pointers to the data values ​​stored in the specified columns of the table, and then sorts these pointers according to the sort order you specify. The database uses the index to find a specific value, and then moves forward to find the row that contains that value. This can make the SQL statement corresponding to the table execute faster, and can quickly access specific information in the database table.
7. Unique key
There are often many fields in a table that need uniqueness, and data cannot be repeated, but there can only be one primary key in a table: a unique key can solve the problem of multiple fields in the table that require uniqueness constraints.
The essence of the unique key is similar to that of the primary key. The unique key is allowed to be empty, and multiple empty fields can be empty. The empty fields are not compared for uniqueness.
8. Foreign keys
Foreign keys are used to define the relationship between the primary table and the secondary table: foreign key constraints are mainly defined on the secondary tables, and the primary table must have primary key constraints or unique constraints. When the foreign key is defined, the data of the foreign key column must exist in the primary key column of the main table or be null.

foreign key (字段名) references 主表(列)

Guess you like

Origin blog.51cto.com/14289397/2545064