MySQL外键和参照完整性的实现步骤
我们大家都知道MySQL参照完整性一般是通过MySQL外键(foreign key)的具体使用而随之应用。总的来说,流行工具开源 RDBMS MySQL(与PHP搭配之最佳组合)并不支持外键,原因是这种支持将会降低RDBMS的速度和性能。 然而,由于很多用户对参照完整性的优点倍感兴趣
我们大家都知道MySQL参照完整性一般是通过MySQL外键(foreign key)的具体使用而随之应用。总的来说,流行工具开源 RDBMS MySQL(与PHP搭配之最佳组合)并不支持外键,原因是这种支持将会降低RDBMS的速度和性能。
然而,由于很多用户对参照完整性的优点倍感兴趣,最近MySQL(和PHP搭配之最佳组合)的不同版本都通过新InnoDB列表引擎支持外键。由此,在数据库组成的列表中保持参照完整性将变得非常简单。
为了建立两个MySQL(和PHP搭配之最佳组合)表之间的一个MySQL外键关系,必须满足以下三种情况:
两个表必须是InnoDB表类型。
使用在外键关系的域必须为索引型(Index)。
使用在MySQL外键关系的域必须与数据类型相似。
例子是理解以上要点的最好方法。如表A所示,建立两个表,其中一个列出动物种类及相应的代码(表名为:species),另一表列出动物园中的动物(表名为:zoo)。现在,我们想通过species关联这两个表,所以我们只需要接受和保存zoo表中包含species表中的合法动物的入口到数据库中。
表A
MySQL(和PHP搭配之最佳组合)> CREATE TABLE species (id TINYINT NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, Prima(最完善的虚拟主机管理系统)RY KEY(id)) ENGINE=INNODB;
<ol class="dp-xml"> <li class="alt"><span><span>Query OK, 0 rows affected (0.11 sec) </span></span></li> <li>MySQL<span>(和PHP搭配之最佳组合)</span><span class="tag">></span><span> INSERT INTO species VALUES (1, 'orangutan'), (2, 'elephant'), (3, 'hippopotamus'), (4, 'yak'); </span> </li> <li class="alt"><span>Query OK, 4 rows affected (0.06 sec) </span></li> <li><span>Records: 4 Duplicates: 0 Warnings: 0 </span></li> </ol>
MySQL(和PHP搭配之最佳组合)> CREATE TABLE zoo (id INT(4) NOT NULL, name VARCHAR(50) NOT NULL, FK_species TINYINT(4) NOT NULL, INDEX (FK_species), FOREIGN KEY (FK_species) REFERENCES species (id), Prima(最完善的虚拟主机管理系统)RY KEY(id)) ENGINE=INNODB;
注意:对于非InnoDB表, FOREIGN KEY 语句将被忽略。
现在,fieldszoo.species与species.id 之间存在一个MySQL外键关系。只有相应的zoo.specie与species.idfield的一个值相匹配,动物表中的入口才可被访问。以下的输出即演示了当你想输入一个Harry Hippopotamus记录,而使用到不合法的species代码:
<ol class="dp-xml"> <li class="alt">MySQL<span>(和PHP搭配之最佳组合)</span><span>> INSERT INTO zoo VALUES (1, 'Harry', 5); </span> </li> <li><span>ERROR 1216 (23000): Cannot add or update a child row: a foreign key constraint fails </span></li> </ol>
这里,MySQL(和PHP搭配之最佳组合)核查species表以查看species代码是否存在,如果发现不存在,就拒绝该记录。当你输入正确代码的,可以与以上做比较。
<ol class="dp-xml"> <li class="alt">MySQL<span>(和PHP搭配之最佳组合)</span><span>> INSERT INTO zoo VALUES (1, 'Harry', 3); </span> </li> <li><span>Query OK, 1 row affected (0.06 sec) </span></li> </ol>
这里,MySQL(和PHP搭配之最佳组合)核查species表以查看species代码是否存在,当发现存在,允许记录保存在zoo表中。
为了删除一个MySQL外键关系,首先使用SHOW CREATE TABLE找出InnoDB的内部标签,如表B所示:
表 B
<ol class="dp-xml"> <li class="alt"><span><span>+-------+---------------------------------------------------+ </span></span></li> <li><span>| Table | Create Table | </span></li> <li class="alt"><span>+-------+---------------------------------------------------+ </span></li> <li><span>| zoo | CREATE TABLE `zoo` ( </span></li> <li class="alt"><span>`id` int(4) NOT NULL default '0', </span></li> <li><span>`name` varchar(50) NOT NULL default '', </span></li> <li class="alt"><span>`FK_species` tinyint(4) NOT NULL default '0', </span></li> <li><span>KEY `FK_species` (`FK_species`), </span></li> <li class="alt"><span>CONSTRAINT `zoo_ibfk_1` FOREIGN KEY (`FK_species`) </span></li> <li><span>REFERENCES `species` (`id`) </span></li> <li class="alt"> <span>) </span><span class="attribute">ENGINE</span><span>=</span><span class="attribute-value">InnoDB</span><span> DEFAULT </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">latin1</span><span> | </span> </li> <li><span>+-------+----------------------------------------------------+ </span></li> </ol>
然后使用带有DROP FOREIGN KEY 语句的ALTER TABLE命令,如以下:
<ol class="dp-xml"> <li class="alt">MySQL<span>(和PHP搭配之最佳组合)</span><span>> ALTER TABLE zoo DROP FOREIGN KEY zoo_ibfk_1; </span> </li> <li><span>Query OK, 1 row affected (0.11 sec) </span></li> <li class="alt"><span>Records: 1 Duplicates: 0 Warnings: 0 </span></li> </ol>
为了将一个外键添加到一个现成的表中,使用ADD FOREIGN KEY的 ALTER TABLE语句指定合适的域作为一个外键:
<ol class="dp-xml"> <li class="alt">MySQL<span>(和PHP搭配之最佳组合)</span><span>> ALTER TABLE zoo ADD FOREIGN KEY (FK_species) REFERENCES species (id); </span> </li> <li><span>Query OK, 1 rows affected (0.11 sec) </span></li> <li class="alt"><span>Records: 1 Duplicates: 0 Warnings: 0 </span></li> </ol>
如以上例子解释的,MySQL外键在捉摸数据入口错误上起着重要的作用,由此可建立更为强健更加集成的数据库。另一方面值得提到的是,执行外键核实是内部资料处理的过程,且不同表之间指定复杂的内部关系可以导致数据库的性能下降。所以,在参照完整性与性能考虑之间找到平衡点相当重要,而使用外键就是能够确保性能与稳健之间的最优结合。
我期望本期的有关MySQL外键的介绍对你有所好处,你将会在下回的MySQL(和PHP搭配之最佳组合)数据库设计中感受到外键的好处。编程快乐!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

WeChat is one of the social media platforms in China that continuously launches new versions to provide a better user experience. Upgrading WeChat to the latest version is very important to keep in touch with family and colleagues, to stay in touch with friends, and to keep abreast of the latest developments. 1. Understand the features and improvements of the latest version. It is very important to understand the features and improvements of the latest version before upgrading WeChat. For performance improvements and bug fixes, you can learn about the various new features brought by the new version by checking the update notes on the WeChat official website or app store. 2. Check the current WeChat version We need to check the WeChat version currently installed on the mobile phone before upgrading WeChat. Click to open the WeChat application "Me" and then select the menu "About" where you can see the current WeChat version number. 3. Open the app

Having issues with the Shazam app on iPhone? Shazam helps you find songs by listening to them. However, if Shazam isn't working properly or doesn't recognize the song, you'll have to troubleshoot it manually. Repairing the Shazam app won't take long. So, without wasting any more time, follow the steps below to resolve issues with Shazam app. Fix 1 – Disable Bold Text Feature Bold text on iPhone may be the reason why Shazam is not working properly. Step 1 – You can only do this from your iPhone settings. So, open it. Step 2 – Next, open the “Display & Brightness” settings there. Step 3 – If you find that “Bold Text” is enabled

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Can't enable Wi-Fi calling on iPhone? Call quality is improved and you can communicate even from remote locations where cellular networks are not as strong. Wi-Fi Calling also improves standard call and video call quality. So, if you can't use Wi-Fi calling on your phone, these solutions might help you fix the problem. Fix 1 – Enable Wi-Fi Calling Manually You must enable the Wi-Fi Calling feature in your iPhone settings. Step 1 – For this, you have to open Settings. Step 2 – Next, just scroll down to find and open the “Phone” settings Step 3 – In the phone settings, scroll down and open the “Wi-Fi Calling” setting. Step 4 – In the Wi-Fi Calling page, change “This iPhone

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

Is the Clear History button gray in Safari on iPhone? If this is the case, you won't be able to clear history in Safari at all. Stored cookies and website data may cause unsolicited events in your browser. However, if you follow the steps below, you can easily resolve the issue and delete stored user history from Safari. Fix 1 – Disable Content Restrictions Content restrictions on iPhone may limit the correct use of Safari browser. Step 1 – Open iPhone settings. Step 2 – Next, go to Screen Time settings. Step 3 – In Screen Time settings, turn on Content & Privacy Restrictions
