Home Database Mysql Tutorial 关于oracle外键引用与goldengate

关于oracle外键引用与goldengate

Jun 07, 2016 pm 04:21 PM
oracle about foreign key Quote

一、准备知识 约束放置在表中,有以下五种约束: NOT NULL 非空约束C 指定的列不允许为空值 UNIQUE 唯一约束U 指定的列中没有重复值,或该表中每一个值或者每一组值都将是唯一的 PRIMARY KEY 主键约束P 唯一的标识出表的每一行,且不允许空值值,一个表只能有一

   一、准备知识

  约束放置在表中,有以下五种约束:

  NOT NULL 非空约束C 指定的列不允许为空值

  UNIQUE 唯一约束U 指定的列中没有重复值,或该表中每一个值或者每一组值都将是唯一的

  PRIMARY KEY 主键约束P 唯一的标识出表的每一行,且不允许空值值,一个表只能有一个主键约束

  FOREIGN KEY 外键约束R 一个表中的列引用了其它表中的列,使得存在依赖关系,可以指向引用自身的列

  CHECK 条件约束C 指定该列是否满足某个条件

  约束命名规则

  如果不指定约束名Oracle server 自动按照SYS_Cn 的格式指定约束名,也可手动指定,

  推荐的约束命名是:约束类型_表名_列名。

  NN:NOT NULL 非空约束,比如nn_emp_sal

  UK:UNIQUE KEY 唯一约束

  PK:PRIMARY KEY 主键约束

  FK:FOREIGN KEY 外键约束

  CK:CHECK 条件约束

  外键约束是用来维护从表和主表的引用完整性的,所以外键约束要涉及两个表。

  FOREIGN KEY: 在表级指定子表中的列

  REFERENCES: 标示在父表中的列

  ON DELETE CASCADE: 当父表中的列被删除时,子表中相对应的列也被删除

  ON DELETE SET NULL: 子表中相应的列置空

  二、外键创建测试

  foreign_main为主表

  foreign_sub为从表

  object_id做为foreign_sub的外键,参考主表foreign_main的object_id值

  SQL> create table foreign_main as select object_id from all_objects;

  Table created.

  SQL> select count(*) from foreign_main;

  COUNT(*)

  ----------

  49571

  SQL> create table foreign_sub as select object_id,object_name from all_objects;

  Table created.

  建议使用主表的主键做外键,,即使不是主表的主键也应该是唯一约束的字段做为外键

  SQL> alter table foreign_main add constraint pk_fsid primary key(object_id);

  Table altered.

  SQL> delete from foreign_sub where object_name = 'FOREIGN_MAIN';

  1 row deleted.

  SQL> commit;

  Commit complete.

  SQL> alter table foreign_sub add constraint fr_fssid foreign key(object_id) references foreign_main(object_id);

  Table altered.

  从表插入一条主表object_id中不存在的记录测试

  SQL> insert into foreign_sub values(1,'ts');

  insert into foreign_sub values(1,'ts')

  *

  ERROR at line 1:

  ORA-02291: integrity constraint (TEST.FR_FSSID) violated - parent key not found

  提示主表数据不存在,从表不能创建主表不存在的object_id以保证完整性

  三、级联删除测试

  SQL> alter table foreign_sub drop constraint fk_fs_oid;

  Table altered.

  SQL> alter table foreign_sub add constraint fk_fs_oid foreign key(object_id) references foreign_main(object_id) on delete cascade;

  Table altered.

  cascade下仍然不能单独更新主表外键字段

  SQL> update foreign_main set object_id=52012 where object_id=52010;

  update foreign_main set object_id=52012 where object_id=52010

  *

  ERROR at line 1:

  ORA-02292: integrity constraint (TEST.FK_FS_OID) violated - child record found

  cascade模式下可以通过主表删除外键字段数据关联删除从表数据

  SQL> select * from foreign_sub where object_id=52010;

  OBJECT_ID OBJECT_NAME

  ---------- ------------------------------

  52010 IDX_BJNAME

  SQL> delete from foreign_main where object_id=52010;

  1 row deleted.

  SQL> commit;

  Commit complete.

  SQL> select * from foreign_sub where object_id=52010;

  no rows selected

  外键相关常用操作及参考文档

  建立外键

  alter table 表名 add constraint 外键名 foreign key(从表外键字段) references foreign_main(主表外键字段);

  drop表外键

  alter table 表名 drop constraint 外键名;

  通过外键找表

  select * from user_constraints where constraint_type='R' and constraint_name=upper('外键名');

  通过表找外键

  select * from user_constraints where constraint_type='R' and table_name=upper('表名');

  查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询):

  select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查询的表

  查询引用表的键的列名:

  select * from user_cons_columns cl where cl.constraint_name = 外键引用表的键名

  外键约束临时disabled

  alter table 表名 disable constraint 外键名;

  在SQL92标准中定义了几种外键改变后,如何处理子表记录的动作,其中包括:

  限制Restrict:这种方式不允许对被参考的记录的键值执行更新或删除的操作;置为空Set to null:当参考的数据被更新或者删除,那么所有参考它的外键值被置为空;

  置为默认值Set to default:当参考的数据被更新或者删除,那么所有参考它的外键值被置为一个默认值;

  级联Cascade:当参考的数据被更新,则参考它的值同样被更新,当参考的数据被删除,则参考它的子表记录也被删除;

  不做操作No action:这种方式不允许更新或删除被参考的数据。和限制方式的区别在于,这种方式的检查发生在语句执行之后。Oracle默认才会的方式就是这种方式。

  Col OWNER FOR A6

  COL R_OWNER FOR A6

  COL TABLE_NAME FOR A15

  select OWNER, TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE, R_OWNER, R_CONSTRAINT_NAME, DELETE_RULE from user_constraints where table_name in ('FOREIGN_MAIN', 'FOREIGN_SUB');

  Select CONSTRAINT_NAME from user_constraints e where e.table_name='IMS_COLUMN' and owner='WSJD_ELMS6';

  Select b.table_name,b.column_name, A.CONSTRAINT_TYPE, C.TABLE_NAME from user_constraints a, user_cons_columns b, user_constraints C

  WHERE a.constraint_name = b.constraint_name AND

  A.R_CONSTRAINT_NAME = C.CONSTRAINT_NAME

  AND a.r_constraint_name IN (Select CONSTRAINT_NAME from user_constraints e where e.table_name='FOREIGN_MAIN' and owner='SCOTT');

  create table foreign_sub as select object_id, object_name from user_objects;

  create table foreign_main as select object_id from foreign_sub;

  alter table foreign_main add constraint pk_foreign_main_object_id primary key(object_id);

  alter table foreign_sub add constraint fr_foreign_sub_object_id foreign key(object_id) references foreign_main(object_id) on delete cascade;

  alter table foreign_sub drop constraint fr_foreign_sub_object_id;

  alter table foreign_sub disable constraint fr_foreign_sub_object_id;

  如在goldengate 没有禁用外键约束会出现以现错误

  =============================================

  2013-12-26 04:51:25 INFO OGG-00996 Oracle GoldenGate Delivery for Oracle, rep_app.prm: REPLICAT REP_APP started.

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)

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

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 check invalid numbers of oracle How to check invalid numbers of oracle Apr 11, 2025 pm 08:27 PM

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

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.

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.

See all articles