在建表的时候时候,可以对于删除delete、修改update设置为级联。用一个例子先说明外键级联级联的概念
假如数据库中本以存在一张usertable如下:
此user表非常简单,id为主键。
下面我将新建一张cascade_test表如下,这里的user_id与usertable的主键id形成参照完整性,并同时建立删除与修改的级联:
如果用SQL语句建立上图的表则如下:
CREATE TABLE `test`.`cascade_test` ( `id` INTEGER UNSIGNED NOT NULL auto_increment, `content` TEXT, `user_id` INTEGER UNSIGNED, -- 由于usertable的主键是INTEGER UNSIGNED,这里必须同为这样类型,键值对应 PRIMARY KEY (`id`), CONSTRAINT `FK_cascade_test_1` FOREIGN KEY `FK_cascade_test_1` (`user_id`) REFERENCES `test`.`usertable`(`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE = InnoDB; -- 数据库引擎设置,可以没有比起普通设置外键的语句,在其下多出了两个级联的声明,ON DELETE CASCADE、ON UPDATE CASCADE
设置级联与不设置级联,在参照完整性上是没有区别的。
user的id类型是怎么cascade_test的user_id类型就应该怎么样,这里需要严格照抄,由于user的id设置为无符号整形,这里cascade_test的user_id还不能是整形,一定要特意声明为无符号整形,否则会出现如下的[Err] 1005 - Can't create table 'xx' (errno: 150),其实150页就是参照完整性错误。
之后,在新建出来的cascade_test插入如下一些数据:
同样地,由于参照完整性的存在,cascade_test的user_id的取值范围还需要在user的id现有的值来取。
设置级联与不设置级联唯一的区别,就是我们在删除user的id中数据的同时,同样会删除cascade_test的user_id的所有有关字段。修改级联同样如此。
比如将user中id为6的项删除,也就是执行如下的语句:
delete from usertable where id=6
运行效果如下图:

usertable中id=6的项被删除是肯定的,
由于删除级联的存在:cascade_test中user_id=6的项通通被删除。
如果不设置级联,在usertable中id=6的项被删除的同时,cascade_test中user_id=6的项是不会删除的。造成了一定参照完整性的缺失。
下面说说级联的劣势:
这看来似乎很强大的样子,尤其是在网页编程的时候,就不用写这么多删除语句了,仅仅是删除一个主键,就能把所有涉及的项删除
但是,在你的工程足够的时候,这样的删除会很慢,实质上造成了表与表之间的耦合。远远比不删新增加一个isDel的项,使用标志删除的方式,查表的时候仅查询isDel=false的项。其实delete语句在网页的编程的时间根本就是可以扔掉的。这样还有个好处,出现在需要找被删除的旧数据的时候,绝对可以找回来。
这样你的网页的运行速度会大大加快,否则如果一旦执行级联删除的语句,会涉及的表足够多的时候,执行起来将会足够慢。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor