• Oracle外键(Foreign Key)使用详细的说明(一)


    Oracle外键(Foreign Key)使用详细的说明(一)

    1.目标

    演示如何Oracle使用外键数据库


    2.什么是外键?

    1)在Oracle数据库中,外键是用来实现參照完整性的方法之中的一个。打个形象的比喻。外键是指定义外键的表的列的值必须在还有一个表中出现。

    2)被參照的表称之为父表(parent table),创建外键的表称之为子表(child table)。子表中的外键关联了父表中的主键。

    3)外键能够在创建表时定义或者通过ALTER TABLE语句创建。


    3.创建表时定义外键

    语法:

    CREATE TABLE table_name
    (
       column1 datatype null/not null,
       column2 datatype null/not null,
       ...
       
       CONSTRAINT fk_column FOREIGN KEY  (column1,column2,... column_n) REFERENCES parent_table (column1,column2,...column_n)
    );


    演示样例1:基于单列的外键

    create table tb_supplier
    (
      supplier_id number not null,
      supplier_name varchar2(50) not null,
      contact_name varchar2(50),
      CONSTRAINT pk_supplier PRIMARY KEY (supplier_id)
    );
    
    create table tb_products
    (
      product_id number not null,
      product_name varchar2(100),
      supplier_id number not null,
      constraint fk_products_supplier foreign key (supplier_id) references tb_supplier(supplier_id)
    );


    演示样例2:基于多列的外键

    drop table TB_PRODUCTS;
    drop table TB_SUPPLIER;
    
    create table tb_supplier
    (
      supplier_id number not null,
      supplier_name varchar2(50) not null,
      contact_name varchar2(50),
      CONSTRAINT pk_supplier PRIMARY KEY (supplier_id,supplier_name)
    );
    
    create table tb_products
    (
      product_id number not null,
      product_name varchar2(100),
      supplier_name varchar2(50),
      supplier_id number not null,
      constraint fk_products_supplier foreign key (supplier_id,supplier_name) references tb_supplier(supplier_id,supplier_name)
    );


    4.使用ALTER TABLE命令创建外键

    语法:

    ALTER TABLE table_name 
    ADD CONSTRAINT constraint_name 
    FOREIGN KEY (column1, column2,...column_n) 
    REFERENCES parent_table (column1,column2,...column_n);

    演示样例:

    drop table TB_PRODUCTS;
    drop table TB_SUPPLIER;
    
    create table tb_supplier
    (
      supplier_id number not null,
      supplier_name varchar2(50) not null,
      contact_name varchar2(50),
      CONSTRAINT pk_supplier PRIMARY KEY (supplier_id,supplier_name)
    );
    
    create table tb_products
    (
      product_id number not null,
      product_name varchar2(100),
      supplier_name varchar2(50),
      supplier_id number not null
    );
    
    --使用alter table创建外键
     alter table tb_products
     add constraint fk_products_supplier 
     foreign key (supplier_id,supplier_name) 
     references tb_supplier(supplier_id,supplier_name);


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

    假设您们在尝试的过程中遇到什么问题或者我的代码有错误的地方。请给予指正,很感谢!

    联系方式:david.louis.tian@outlook.com

    版权@:转载请标明出处!


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

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Java学习之Java历史版本
    小数的二进制转换
    cpp反汇编调试一
    理解 typedef 定义的函数指针
    mov 与 lea 区别
    asmlinkage的用法
    秒的换算:ms(毫秒),μs(微秒),ns(纳秒),ps(皮秒)
    Java字节码例子解析
    Go程序的一生是怎样的?
    Linux内核 hlist_head/hlist_node结构解析
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4856189.html
Copyright © 2020-2023  润新知