MySQL外键应用
MySQL外键应用,所有tables必须是InnoDB型,它们不能是临时表.因为在MySQL中只有InnoDB类型的表才支持外键.
MySQL版本:5.5.28
系统平台:RHEL 5.8 32位
(1) 外键的使用:
外键的作用,主要有两个:
一个是让数据库自己通过外键来保证数据的完整性和一致性
一个就是能够增加ER图的可读性
有些人认为外键的建立会给开发时操作数据库带来很大的麻烦.因为数据库有时候会由于没有通过外键的检测而使得开发人员删除,插入操作失败.他们觉得这样很麻烦
其实这正式外键在强制你保证数据的完整性和一致性.这是好事儿.
例如:
有一个基础数据表,用来记录商品的所有信息。其他表都保存商品ID。查询时需要连表来查询商品的名称。单据1的商品表中有商品ID字段,单据2的商品表中也有商品ID字段。如果不使用外键的话,当单据1,2都使用了商品ID=3的商品时,,如果删除商品表中ID=3的对应记录后,再查看单据1,2的时候就会查不到商品的名称。
当表很少的时候,有人认为可以在程序实现的时候来通过写脚本来保证数据的完整性和一致性。也就是在删除商品的操作的时候去检测单据1,2中是否已经使用了商品ID为3的商品。但是当你写完脚本之后系统有增加了一个单据3 ,他也保存商品ID找个字段。如果不用外键,你还是会出现查不到商品名称的情况。你总不能每增加一个使用商品ID的字段的单据时就回去修改你检测商品是否被使用的脚本吧,同时,引入外键会使速度和性能下降。
(2) 添加外键的格式:
说明:
on delete/on update,用于定义delete,update操作.以下是update,delete操作的各种约束类型:
CASCADE:
外键表中外键字段值会被更新,或所在的列会被删除.
RESTRICT:
RESTRICT也相当于no action,即不进行任何操作.即,拒绝父表update外键关联列,delete记录.
set null:
被父面的外键关联字段被update ,delete时,子表的外键列被设置为null.
而对于insert,子表的外键列输入的值,只能是父表外键关联列已有的值.否则出错.
外键定义服从下列情况:(前提条件)
1)
所有tables必须是InnoDB型,它们不能是临时表.因为在MySQL中只有InnoDB类型的表才支持外键.
2)
所有要建立外键的字段必须建立索引.
3)
对于非InnoDB表,FOREIGN KEY子句会被忽略掉。
注意:
创建外键时,定义外键名时,不能加引号.
如: constraint 'fk_1' 或 constraint "fk_1"是错误的
(3) 查看外键:
可以查看到新建的表的代码以及其存储引擎.也就可以看到外键的设置.
删除外键:
注意:
只有在定义外键时,用constraint 外键名 foreign key .... 方便进行外键的删除.
若不定义,则可以:
先输入:alter table tb_name drop foreign key -->会提示出错.此时出错信息中,会显示foreign key的系统默认外键名.--->
用它去删除外键.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.
