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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Huawei's Qiankun ADS3.0 intelligent driving system will be launched in August and will be launched on Xiangjie S9 for the first time Jul 30, 2024 pm 02:17 PM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

How long will Oracle database logs be kept? How long will Oracle database logs be kept? May 10, 2024 am 03:27 AM

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

Function to calculate the number of days between two dates in oracle Function to calculate the number of days between two dates in oracle May 08, 2024 pm 07:45 PM

The function in Oracle to calculate the number of days between two dates is DATEDIFF(). The specific usage is as follows: Specify the time interval unit: interval (such as day, month, year) Specify two date values: date1 and date2DATEDIFF(interval, date1, date2) Return the difference in days

The order of the oracle database startup steps is The order of the oracle database startup steps is May 10, 2024 am 01:48 AM

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Always new! Huawei Mate60 series upgrades to HarmonyOS 4.2: AI cloud enhancement, Xiaoyi Dialect is so easy to use Jun 02, 2024 pm 02:58 PM

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

How much memory does oracle require? How much memory does oracle require? May 10, 2024 am 04:12 AM

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

How to use interval in oracle How to use interval in oracle May 08, 2024 pm 07:54 PM

The INTERVAL data type in Oracle is used to represent time intervals. The syntax is INTERVAL <precision> <unit>. You can use addition, subtraction, multiplication and division operations to operate INTERVAL, which is suitable for scenarios such as storing time data and calculating date differences.

How to see the number of occurrences of a certain character in Oracle How to see the number of occurrences of a certain character in Oracle May 09, 2024 pm 09:33 PM

To find the number of occurrences of a character in Oracle, perform the following steps: Get the total length of a string; Get the length of the substring in which a character occurs; Count the number of occurrences of a character by subtracting the substring length from the total length.

See all articles