Home Database Mysql Tutorial 【Mysql】外键级联与级联的劣势_MySQL

【Mysql】外键级联与级联的劣势_MySQL

Jun 01, 2016 pm 12:59 PM
Disadvantages

在建表的时候时候,可以对于删除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; -- 数据库引擎设置,可以没有
Copy after login
比起普通设置外键的语句,在其下多出了两个级联的声明,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
Copy after login

 

运行效果如下图:

\

usertable中id=6的项被删除是肯定的,

由于删除级联的存在:cascade_test中user_id=6的项通通被删除。

如果不设置级联,在usertable中id=6的项被删除的同时,cascade_test中user_id=6的项是不会删除的。造成了一定参照完整性的缺失。

下面说说级联的劣势:

这看来似乎很强大的样子,尤其是在网页编程的时候,就不用写这么多删除语句了,仅仅是删除一个主键,就能把所有涉及的项删除

但是,在你的工程足够的时候,这样的删除会很慢,实质上造成了表与表之间的耦合。远远比不删新增加一个isDel的项,使用标志删除的方式,查表的时候仅查询isDel=false的项。其实delete语句在网页的编程的时间根本就是可以扔掉的。这样还有个好处,出现在需要找被删除的旧数据的时候,绝对可以找回来。

这样你的网页的运行速度会大大加快,否则如果一旦执行级联删除的语句,会涉及的表足够多的时候,执行起来将会足够慢。

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 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

In-depth analysis: jQuery's advantages and disadvantages In-depth analysis: jQuery's advantages and disadvantages Feb 27, 2024 pm 05:18 PM

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

What are the advantages and disadvantages of deploying PHP applications using serverless architecture? What are the advantages and disadvantages of deploying PHP applications using serverless architecture? May 06, 2024 pm 09:15 PM

Deploying PHP applications using Serverless architecture has the following advantages: maintenance-free, pay-as-you-go, highly scalable, simplified development and support for multiple services. Disadvantages include: cold start time, debugging difficulties, vendor lock-in, feature limitations, and cost optimization challenges.

What are the advantages and disadvantages of vr What are the advantages and disadvantages of vr Dec 20, 2023 pm 05:31 PM

The advantages of VR: 1. Immersive experience; 2. Interactivity; 3. Diversity; 4. Safety; 5. Convenience. Disadvantages of VR: 1. High technical threshold; 2. Dizziness; 3. Dependence; 4. Risk of privacy leakage; 5. High cost. VR technology is an advanced interactive technology that creates a completely virtual scene by simulating human audio-visual perception, allowing users to interact and experience it. In the future development, with the continuous advancement of technology and the continuous expansion of application fields, VR technology will be more widely used and developed.

What are the advantages and disadvantages of developing with golang framework? What are the advantages and disadvantages of developing with golang framework? Jun 02, 2024 pm 07:30 PM

The advantages of Go framework development include: efficient performance, excellent concurrency, simple syntax, rich standard library, strongly typed language and cross-platform support. Disadvantages include: lack of generics, inexperience of novices, external library dependencies, cumbersome error handling, and limited HTTP routing performance.

Improve the shortcomings and solutions of static relocation technology Improve the shortcomings and solutions of static relocation technology Jan 28, 2024 am 09:28 AM

Static relocation is a technique used to load software or applications into memory and assign memory addresses. Its purpose is to enable programs to run in different environments without modifying the source code. However, static relocation technology also has some disadvantages. This article will explore these disadvantages and suggest some ways to improve them. First, static relocation techniques may cause memory fragmentation. When a program runs, it allocates a contiguous memory space to store instructions, data, and stack. However, as the program runs longer, some memory blocks may be freed.

Advantages and disadvantages of go language Advantages and disadvantages of go language Oct 26, 2023 am 09:46 AM

As a modern programming language, Go language has the advantages of simplicity and ease of learning, strong concurrency performance, efficient memory management, and cross-platform support. However, it also has disadvantages such as a relatively small ecosystem, incomplete generic support, limited error handling, and lack of some advanced features. When developers choose to use the Go language, they need to make trade-offs and choices based on specific application scenarios and needs.

In-depth analysis of the disadvantages and improvement directions of Go language In-depth analysis of the disadvantages and improvement directions of Go language Mar 27, 2024 pm 05:33 PM

In the field of computer programming, Go language has received widespread attention and application as a programming language with high development efficiency and strong concurrency performance. However, just like any programming language, Go language also has some disadvantages and needs continuous improvement and optimization. This article will deeply explore the disadvantages and improvement directions of the Go language, and analyze it with specific code examples. 1. Disadvantages Analysis: Exception handling is not flexible enough. In the Go language, the error handling mechanism is mainly implemented by returning error values. Although this mechanism is simple and efficient, it is not suitable for complex error handling.

A deep dive into the strengths and weaknesses of the Go programming language A deep dive into the strengths and weaknesses of the Go programming language Mar 09, 2024 pm 05:03 PM

A Deep Dive into the Advantages and Disadvantages of the Go Programming Language The Go language, also known as Golang, is an open source programming language developed by Google. Since its release in 2009, the Go language has attracted much attention in the field of software development and has been widely used in many fields such as network programming, cloud computing, and big data processing. This article will delve into the advantages and disadvantages of the Go language and illustrate it with specific code examples. Advantages 1. Strong concurrent programming capabilities. Go language has built-in goroutine and channel, making concurrent programming simple and efficient.

See all articles