Table of Contents
MySQL 触发器 索引
Home Database Mysql Tutorial MySQL 触发器 目录

MySQL 触发器 目录

Jun 07, 2016 pm 04:15 PM
mysql Table of contents index trigger

MySQL 触发器 索引 MySQL 触发器 索引 ? 博文目录 ?触发器 索引 ? 触发器是MySQL响应insert,update,delete语句时自动执行的一条SQL语句,只有表支持触发器,视图不支持。 触发器需要的信息 唯一的触发器名称(一个表中唯一) 触发器关联的表 触发器应该响应的

MySQL 触发器 索引

MySQL 触发器 索引

?

博文目录

  1. ?触发器
  2. 索引

?

触发器是MySQL响应insert,update,delete语句时自动执行的一条SQL语句,只有表支持触发器,视图不支持。

触发器需要的信息

  • 唯一的触发器名称(一个表中唯一)
  • 触发器关联的表
  • 触发器应该响应的事件(insert?update?delete?)
  • 触发器何时执行(处理之前或处理之后)
  • 一个表的一个事件最多只能有两个触发器,所以一个表最多有6个触发器
  • 如果响应之前的触发器执行失败,响应则不会执行;响应之前的触发器或响应执行失败,响应之后的触发器不会执行


insert触发器

create trigger tr_insert_tableA 
after insert on t_tableA
for each row 
insert into t_tableB(val) 
values 
(new.val);
Copy after login



在insert触发器内,可引用一个名为new的虚拟表,访问被插入的行
在before insert触发器中,new中的值也可以被更新(new可以取到插入的值)
对于自动增长的列,new在insert执行之前的值为0,在执行之后是新的自动生成的值

获取刚刚插入的自动生成的主键值

create trigger t_insert_pk_tableA 
after insert on t_tableA 
for each row 
select new.id into @id;
Copy after login



insert into t_tableA(val) values("abc");
select @id;
Copy after login



delete触发器

delimiter //
create trigger t_delete_tableA 
after delete on t_tableA 
for each row 
begin 
  insert into t_tableB(val) values(old.val);
end //
delimiter ;

delete from t_tableA where id=2;
Copy after login



在delete触发器代码中,可以引用了一个old的虚拟表,访问被删除的行
old表中值全是只读的,不能更新

update触发器

将A表中修改后的名字都改为大写

delimiter //
create trigger t_update_tableA 
before update on t_tableA 
for each row 
begin 
  set new.val=upper(new.val);
end //
delimiter ;

update t_tableA set val='xyz' where id=1;
select * from t_tableA;
Copy after login



在update触发器代码中,可以引用一个名为old的虚拟表访问以前的值,引用new表访问新的值
在before update触发器中,new中值允许被更新
old表中的值是只读的,不能更改

删除触发器

drop trigger tr_insert_tableA;
Copy after login



索引

索引是优化数据库查询速度的重要途径

索引类型
普通索引:最基本的索引,没有唯一性之类的限制

create index valindex on t_tableA(val(20));
create index products_index on products(prod_name(25),prod_price);
Copy after login



唯一索引:所有的索引列只能出现一次,保持唯一性

create unique index valindex2 on t_tableB(val(20));
Copy after login



主键索引:主键索引是一种特殊的唯一索引,在主键建立时自动创建
全文索引:全文所以可以在varchar或text类型上创建

索引的缺点

  • 虽然索引大大提高了查询速度,但会降低更新表的速度,比如对表的insert,update,delete操作,因为更新表时,MySQL不仅仅要保存数据,还要保存索引文件
  • 建立索引会占用磁盘空间。如果在一个大表上创建了多种索引组合,索引文件会膨胀的很快。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve mysql cannot be started How to solve mysql cannot be started Apr 08, 2025 pm 02:21 PM

There are many reasons why MySQL startup fails, and it can be diagnosed by checking the error log. Common causes include port conflicts (check port occupancy and modify configuration), permission issues (check service running user permissions), configuration file errors (check parameter settings), data directory corruption (restore data or rebuild table space), InnoDB table space issues (check ibdata1 files), plug-in loading failure (check error log). When solving problems, you should analyze them based on the error log, find the root cause of the problem, and develop the habit of backing up data regularly to prevent and solve problems.

Does mysql need the internet Does mysql need the internet Apr 08, 2025 pm 02:18 PM

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

Does mysql optimize lock tables Does mysql optimize lock tables Apr 08, 2025 pm 01:51 PM

MySQL uses shared locks and exclusive locks to manage concurrency, providing three lock types: table locks, row locks and page locks. Row locks can improve concurrency, and use the FOR UPDATE statement to add exclusive locks to rows. Pessimistic locks assume conflicts, and optimistic locks judge the data through the version number. Common lock table problems manifest as slow querying, use the SHOW PROCESSLIST command to view the queries held by the lock. Optimization measures include selecting appropriate indexes, reducing transaction scope, batch operations, and optimizing SQL statements.

How to use SUBSTRING_INDEX in MySQL How to use SUBSTRING_INDEX in MySQL Apr 08, 2025 pm 02:09 PM

In MySQL database operations, string processing is an inevitable link. The SUBSTRING_INDEX function is designed for this, which can efficiently extract substrings based on separators. SUBSTRING_INDEX function application example The following example shows the flexibility and practicality of the SUBSTRING_INDEX function: Extract specific parts from the URL For example, extract domain name: SELECTSUBSTRING_INDEX('www.mysql.com','.',2); Extract file extension to easily get file extension: SELECTSUBSTRING_INDEX('file.pdf','.',-1); Processing does not exist

The primary key of mysql can be null The primary key of mysql can be null Apr 08, 2025 pm 03:03 PM

The MySQL primary key cannot be empty because the primary key is a key attribute that uniquely identifies each row in the database. If the primary key can be empty, the record cannot be uniquely identifies, which will lead to data confusion. When using self-incremental integer columns or UUIDs as primary keys, you should consider factors such as efficiency and space occupancy and choose an appropriate solution.

Does mysql need a server Does mysql need a server Apr 08, 2025 pm 02:12 PM

For production environments, a server is usually required to run MySQL, for reasons including performance, reliability, security, and scalability. Servers usually have more powerful hardware, redundant configurations and stricter security measures. For small, low-load applications, MySQL can be run on local machines, but resource consumption, security risks and maintenance costs need to be carefully considered. For greater reliability and security, MySQL should be deployed on cloud or other servers. Choosing the appropriate server configuration requires evaluation based on application load and data volume.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Can mysql return json Can mysql return json Apr 08, 2025 pm 03:09 PM

MySQL can return JSON data. The JSON_EXTRACT function extracts field values. For complex queries, you can consider using the WHERE clause to filter JSON data, but pay attention to its performance impact. MySQL's support for JSON is constantly increasing, and it is recommended to pay attention to the latest version and features.

See all articles