Oracle之外键(Foreign Key)用法详解(一)
来源:互联网 发布:淘宝二手哑铃 编辑:程序博客网 时间:2024/05/20 11:20
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));
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));
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
版权@:转载请标明出处!--------------------------------------------------------------------------------------------------------------------
1 0
- Oracle之外键(Foreign Key)用法详解(一)
- Oracle之外键(Foreign Key)用法详解(二)- 级联删除(DELETE CASCADE)
- oracle中五中约束详解(CHECK,UNIQUE,PRIMARY KEY,FOREIGN KEY,NOT NULL)
- Oracle Foreign key
- ORACLE foreign key
- MySQL 外键约束(FOREIGN KEY)
- 外键(Foreign Key)
- 聊聊Oracle外键约束(Foreign Key)的几个操作选项
- django 外键(Foreign Key)值(多对一)
- mysql 外键(foreign key)的详解和实例
- mysql 外键(foreign key)的详解和实例
- mysql外键(foreign key)的用法
- mysql外键(foreign key)的用法
- 外键(Foreign Key)约束。
- SQL FOREIGN KEY(约束)
- oracle约束总结(not null/unique/primary key/foreign key/check)
- FOREIGN KEY 在ORACLE中的使用
- MySQL command Line 外键约束(FOREIGN KEY)
- 大数的各种操作模板
- 怎样查看端口号和端口号被哪个程序占用
- 思科路由器基本配置
- android 禁止viewpager预加载
- c# 操作ACCESS数据库
- Oracle之外键(Foreign Key)用法详解(一)
- [Erlang危机](2.1)项目结构
- Opencv获取并改变图像的像素值
- C++虚函数与虚函数表
- UIViewExt宽,高,顶部,底部
- VS2010添加CPP文件后打开崩溃的问题
- 第十一周项目5--当年第几天
- Android文件下载导致进度条为负数
- latex IEEE论文里的公式换行