您好,欢迎访问一九零五行业门户网

实例详解oracle添加唯一约束

本篇文章给大家带来了关于oracle的相关知识,其中主要介绍了添加唯一约束的相关问题,唯一性约束指表中一个字段或者多个字段联合起来可以唯一标识一条记录的约束,下面一起来看一下,希望对大家有帮助。
推荐教程:《oracle视频教程》
1 目标用演示样例演示怎样创建、删除、禁用和使用唯一性约束
2 什么是唯一性约束?唯一性约束指表中一个字段或者多个字段联合起来可以唯一标识一条记录的约束。
联合字段中,可以包括空值。
注:在oracle中,唯一性约束最多能够有32列。
唯一性约束能够在创建表时或使用alter table语句创建。
3 唯一性约束和主键的差别主键(primary key):全部组成主键的列都不能包括空值。唯一性约束(unique constraint):假设唯一性约束由多列组成,当中的部分列能够包括空值。oracle中不容许在同样列上既创建主键又创建唯一性约束。4 创建表时定义唯一性约束1)语法:create table table_name(    column1 datatype null/not null,    column2 datatype null/not null,    ...    constraint constraint_name unique (column1, column2,...,column_n));
2)基于单列的唯一性约束演示样例:create table tb_supplier(  supplier_id          number not null ,supplier_name        varchar2(50) ,contact_name         varchar2(50) ,constraint tb_supplier_u1 unique (supplier_id)--创建表时创建唯一性约束);
3)基于多列的唯一性约束演示样例:create table tb_products(  product_id        number not null,  product_name      number not null,  product_type      varchar2(50),  supplier_id       number,  constraint tb_products_u1 unique (product_id, product_name) --定义复合唯一性约束);
5 使用alter table语法创建唯一性约束1)语法alter table table_nameadd constraint constraint_nameunique (column1, column2, ... , column_n);
2)演示样例准备,先创建表drop table tb_supplier;drop table tb_products;create table tb_supplier(  supplier_id          number not null ,supplier_name        varchar2(50) ,contact_name         varchar2(50));create table tb_products(  product_id        number not null,  product_name      number not null,  product_type      varchar2(50),  supplier_id       number);
3)基于单列的唯一性约束alter table tb_supplieradd constraint  tb_supplier_u1unique (supplier_id);
4)基于多列的唯一性约束alter table tb_productsadd constraint  tb_products_u1unique (product_id,product_name);
6 禁用唯一性约束1)语法:alter table table_namedisable constraint constraint_name;
2)演示样例:alter table tb_supplierdisable constraint  tb_supplier_u1;
7 使用唯一性约束1)语法:alter table tb_supplierenable constraint tb_supplier_u1;

2)演示样例:alter table tb_supplierenable constraint tb_supplier_u1;

7  删除唯一性约束1)语法:alter table table_namedrop constraint constraint_name;
2)演示样例:alter table tb_supplier drop constraint tb_supplier_u1;alter table tb_products drop constraint tb_products_u1;
推荐教程:《oracle教程》
以上就是实例详解oracle添加唯一约束的详细内容。
其它类似信息

推荐信息