This article brings you relevant knowledge about Oracle, which mainly introduces the related issues of adding unique constraints. A unique constraint means that one field or multiple fields in the table can be unique when combined. Let’s take a look at the constraints that mark a record. I hope it will be helpful to everyone.
Recommended tutorial: "Oracle Video Tutorial"
Use Demonstration example demonstrates how to create, delete, disable and use unique constraints
Uniqueness constraints refer to constraints where one field or multiple fields combined in the table can uniquely identify a record.
Union fields can include null values.
Note: In Oracle, unique constraints can have up to 32 columns.
Unique constraints can be created when creating a table or using the ALTER TABLE statement.
CREATE TABLE table_name ( column1 datatype null/not null, column2 datatype null/not null, ... CONSTRAINT constraint_name UNIQUE (column1, column2,...,column_n) );
create table tb_supplier ( supplier_id number not null ,supplier_name varchar2(50) ,contact_name varchar2(50) ,CONSTRAINT tb_supplier_u1 UNIQUE (supplier_id)--创建表时创建唯一性约束 );
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) --定义复合唯一性约束 );
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... , column_n);
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 );
alter table tb_supplier add constraint tb_supplier_u1 unique (supplier_id);
alter table tb_products add constraint tb_products_u1 unique (product_id,product_name);
ALTER TABLE table_name DISABLE CONSTRAINT constraint_name;
ALTER TABLE tb_supplier DISABLE CONSTRAINT tb_supplier_u1;
ALTER TABLE tb_supplier ENABLE CONSTRAINT tb_supplier_u1;
ALTER TABLE tb_supplier ENABLE CONSTRAINT tb_supplier_u1;
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
ALTER TABLE tb_supplier DROP CONSTRAINT tb_supplier_u1; ALTER TABLE tb_products DROP CONSTRAINT tb_products_u1;
Recommended tutorial: "Oracle Tutorial"
The above is the detailed content of Detailed example of Oracle adding unique constraints. For more information, please follow other related articles on the PHP Chinese website!