Home Database Mysql Tutorial Oracle中用表外键来保证系统参照完整性

Oracle中用表外键来保证系统参照完整性

Jun 07, 2016 pm 03:07 PM
oracle ensure integrity system

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 Oracle中表的外键是保证系统参照完整性的手段,而参照完整性是指分布在两个表中的列所满足的具有主从性质的约束关系。外键涉及到两个表,其中一个称之为父项表,另一个称之为子项表。 父项表 ( par

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入

   Oracle中表的外键是保证系统参照完整性的手段,而参照完整性是指分布在两个表中的列所满足的具有主从性质的约束关系。外键涉及到两个表,其中一个称之为父项表,另一个称之为子项表。

    父项表( parent table )是参照约束的基础,即通过检查这张表的有效数据情况来判断约束是否成立,它是参照约束的条件,影响约束,而不受约束的任何影响。

    子项表( child table )是参照约束的对象,当其发生变化,如有新数据输入时,通过比较父项表中的有效数据状况,来判断这些变化是否符合约束条件,若不符合,则拒绝要发生的变化。

    在实际应用系统中,开发者为了保证系统的完整性,一般要定义大量的外键。然而,如果外键的命名不规范,如采用系统自动生成的名称,则在以后的系统运行维护中会造成很大的麻烦。如在系统运行后,加载大量数据或者进行一些数据转换操作等时,出现外键错误时,根据系统提示的外键错误,根本不可能直接定位到那两个表间的外键发生错误,需要浪费很多时间查找造成错误发生的外键的父项表和子项表,然后才能进一步确定是那条记录违反了外键约束条件。通常,我们采用这样的命名规则来命名外键 FK_Child_table name_Parent_table name 。由于外键名称的最大长度限制在 30 个字符之内,对 child_table_name 和 Parent_table name 不一定和原表一模一样,可以采取一些简写的办法,但名称一定要能反映出约束的两个表。这里的命名, Child_table name 指子项表,也就是约束表, Parent_table name 是指父项表,也就是被约束的表。

    下面来详细讨论如何将应用系统中不规范的外键命名修改为规范的外键名称。在讨论之前,需要提醒读者注意的是,完成下面的操作需要花费较长的时间,所以一定要规划在系统空闲时来完成。同时这里的外键更名,采用的方法是首先删除然后重建,涉及到删除应用系统对象的操作,所以在操作之前,为安全起见,应该备份应用系统。

一、生成系统目前的外键报告单

首先生成系统目前模式下的所有外键情况报告单, SQL 脚本如下:

脚本 1 :列出当前模式下所有外键的报告表,可以将其 spool 到某个文件中


SELECT RPAD(child.TABLE_NAME,25,' ') Child_Tablename,

RPAD(cp.COLUMN_NAME,17,' ') Referring_Column,

RPAD(parent.TABLE_NAME,25,' ') Parent_Tablename,

RPAD(pc.COLUMN_NAME,15,' ') Referred_Column,

RPAD(child.CONSTRAINT_NAME,25,' ') Constraint_Name

FROM USER_CONSTRAINTS child,

USER_CONSTRAINTS parent,

USER_CONS_COLUMNS cp,

USER_CONS_COLUMNS pc

WHERE child.CONSTRAINT_TYPE = 'R' AND

child.R_CONSTRAINT_NAME = PARENT.CONSTRAINT_NAME AND

child.CONSTRAINT_NAME = cp.CONSTRAINT_NAME AND

parent.CONSTRAINT_NAME = pc.CONSTRAINT_NAME AND

cp.POSITION = pc.POSITION

ORDER BY child.OWNER,

child.TABLE_NAME,

child.CONSTRAINT_NAME,

cp.POSITION;
该脚本生成所在模式下的所有外键情况,包括外键名称,父项表名称,子项表名称以及引用的列名称等。在 SQL/PLUS 下运行该脚本,在运行该脚本之前,可以将输出 SPOOL 到本地某个文件中。同时要注意,如果应用系统中的外键比较多且复杂的话,这个脚本的运行时间会比较长。

 

[1] [2] 

Oracle中用表外键来保证系统参照完整性

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

Video Face Swap

Video Face Swap

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

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

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.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

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.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

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

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

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's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

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.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

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

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

See all articles