MYSQL development-five constraints, automatic growth, deletion and emptying of table data, indexes, and views

MYSQL development-constraints, automatic growth, delete and empty table data, indexes, views

1. Five types of database integrity constraints

①Null constraint (not null) : the column value cannot be NULL, but it can be an empty string''

  1. The field allows null by default. At this time, additional storage space is required to record whether the field value is null, and if the field constraint is not null, the field value cannot be null, so no additional storage space is needed, so the efficiency of not null is higher than null.
  2. The difference between ``empty string'' and NULL: the
    column can be null, and extra space will be allocated to mark each NULL value. If the column value is an empty string ”, then the empty string ”does not occupy space. If the column value is NULL, you need to record this value as NULL in the extra space , so space is required.
    Insert picture description here
    Insert picture description here

②The primary key constraint (primary key) : the column value is unique and cannot be NULL

create table student(id int not null primary key,name char(20),grade int);
desc student;

Insert picture description here
③Check constraints (check) : control the range of inserted values. Generally not applicable in mysql.
In mysql, the input value range is small, such as male and female. You can use the enumeration type enum() to limit. The input value has a large range, such as a>0, which can be controlled by a trigger. Here only use enumerated types as examples.
Insert picture description here
The default character set utf8sentence is used here , that is, using utf8 encoding. If not used, the following error will be reported:
Insert picture description here
Use enumerated fields after creating the table:
Insert picture description here

④Foreign key constraint (foreign key) :
Create test data:
Insert picture description here
Insert picture description here
You can see that the test data has emp table and dept table. Where emp.deptno and dept.id are both department numbers.
The foreign key constraint is also to protect the integrity of the data.
① When a record with a deptno of 30 is inserted into the emp table, but the department information cannot be found in the dept table, the data is incomplete at this time.
② When the record with id=10 is deleted in the dept table, the employee with deptno=10 in the emp table cannot find the corresponding department information, and the data is also incomplete.

Adding foreign key constraints to the deptno field on the emp table can ensure data integrity. It is not allowed to insert records where deptno does not belong to dept in emp. If you delete the data in dept, the corresponding records in the emp table will also be deleted.

-- 创建表后添加索引
alter table table_name
add constraint con_name 
foreign key(fk_col_name) 
references 外键表名(外键表对应字段)
[on delete cascade]   -- 级联删除
[on update cascade]   -- 级联更新
) [ENGINE=innodb];    -- 修改存储引擎为innodb

-- 创建表时添加索引
create table table_name(字段 数据类型...,
[constraint con_name]
foreign key(fk_col_name) 
references 外键表名(外键表对应字段)
[on delete cascade]
[on update cascade]
) [ENGINE=innodb];

-- mysql5.7默认存储引擎就是innodb

Add foreign key constraints after table creation:
Insert picture description here
Test cascading delete:
Insert picture description here
Check the foreign key constraints of the
Insert picture description here
table creation
statement: Add foreign key constraints when creating a table: First create a foreign key table, and then create a table with foreign key constraints (reference table).
Insert picture description here
Insert picture description here
First delete the reference table, and then delete the foreign key table.
Insert picture description here
Delete foreign key constraint:
Insert picture description here
The value of the foreign key column (emp.deptno) has a stipulation: it can be NULL, but it cannot be absent.
⑤Default constraint (default)

-- 创建一个表,当插入的某条记录的字段grade未指定值时,为字段grade指定默认值
create table student(id int not null primary key,name char(20),grade int default 0);
INSERT INTO student(id,NAME) VALUES(1,'haha');    -- grade默认填充0
select * from student;

Insert picture description here

2. Automatic growth auto_increment

-- auto_increment所在列必须是主键列
create table student(
id int not null auto_increment primary key,
name char(20),
grade int default 0);

Test ID automatically grows, you can see that ID starts from 1.

Insert picture description here
If the maximum current id is 5, then the next id that automatically increases will be 6. [Ie 当前最大ID+1]
Insert picture description here

3. delete和truncate

idListed as auto_incrementautomatic growth. The current value is 6.
Insert picture description here
Insert picture description here
deleteThe statement only deletes the data, and auto_incrementthe value not reset is 1. You can see that the new id value is 7.
Insert picture description here
Insert picture description here
truncateNot only will the data be cleared, but auto_incrementthe value will be reset to 1.
Insert picture description here

4. Index

An index is a special file that contains reference pointers to all records in the data table. It can be regarded as a directory, and the purpose is to speed up the query speed of the database .
Disadvantages:
Indexes are stored in files, too many indexes, taking up a lot of disk space, and will affect the execution time of insert, update, and delete.
② The data in the index must be synchronized with the data in the data table. There are too many indexes. When the data in the table is updated, the index must be updated simultaneously, which reduces efficiency.

Index type:
① Ordinary index: The field value can be non-unique, that is, it can be repeated, and NULL values ​​are allowed to speed up the query.

index关键字可以与key关键字等价替换。
index [index_name](col_name)
key [key_name](col_name)

-- 创建表时建立索引
-- index [index_name](col_name)
-- index_name可以省略,此时字段名即索引名。
create table test1(id int,name char(20),index(name));
create table test1(id int,name char(20),index index_name(name));

-- 创建完表后建立索引
alter table 表名 add index index_name(col_name);

Insert picture description here

②Unique index: The field value must be unique and can only appear once, but NULL values ​​are allowed.

-- unique index index_name(col_name)
-- 创建表时建立索引
create table test2(id int,name char(20), unique index(id));

-- 创建完表后建立索引
alter table test2 add unique index(id);

Insert picture description here

③ Primary key index: The primary key index specifies which field is the primary key column, and the field value is non-empty and unique.

-- 创建表时建立索引
create table test3(id int,name char(20), primary key(id));

-- 创建完表后建立主键索引,生产环境中不推荐,在数据库设计的时候就应该指定主键列。
create table test4(id int,name char(20));
alter table test4 add primary key index_id(id);

Insert picture description here
Insert picture description here
Check the index on a table:
Insert picture description here
④Composite index: Index can not only be built on one column, but also on two or more columns. This kind of index is called a composite index.

-- 在ip和port上创建复合主键索引
create table test5(ip char(50),port char(10),hostname char(50),primary key(ip,port));

Insert picture description here
Insert picture description here

⑤Full-text index: In order to solve the problem of full-table scan under the condition of large amount of data, which causes great database pressure, mysql provides a solution, namely full-text index. Makes fuzzy query not to scan the full table, but to query through the full-text index.

-- fulltext index index_name(col_name)
-- 全文索引所在列的数据类型只能是varchar和test类型。
-- 创建表时建立索引
create table test6(id int primary key,name varchar(50),fulltext index(name));

-- 创建完表后建立索引
create table test7(id int primary key,name varchar(50));
alter table test7 add fulltext index index_name(name);

Insert picture description here
Insert picture description here
MySQL's built-in full-text index can only be used for data tables whose database engine is MyISAM. If it is another data engine, the full-text index will not take effect, and it is generally handed over to third-party software for full-text indexing.

Summary: The main purpose of the index is to speed up the query speed. The establishment of the index when the data table is not frequently updated and the amount of data is large, which can maximize the query efficiency of the database.

Rules for indexing: [You can refer to the directory settings of the Chinese dictionary]
①Limit the number of
indexes
②Create indexes for fields that are often used as query conditions ③Create indexes for fields that often require sorting, grouping, and joint operations
④Select unique indexes
⑤Try your best Use indexes with a small amount of data
⑥ Delete indexes that are no longer used or rarely used
⑦ Try to use prefixes to index
⑧ Index columns cannot participate in the calculation
⑨ Try to expand the index as much as possible, do not create a new index.

5. View

Create a view : After the create view view_name as select查询
view is created, it can be accessed just like the basic table.
Insert picture description here
Insert picture description here
Modify view: alter view view_name as select查询
Insert picture description here
delete view :drop view view_name
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_36522099/article/details/108495294