oracle pl sql error oracle pl sql error oracle pl sql error
 

Oracle/PLSQL: Unique Constraints


What is a unique Oracle constraint?

A unique constraint is a single field or combination of fields that uniquely defines a record. In pl sql oacle is possible that fields can contain null values.

Note: In Oracle sql server, a unique constraint can not contain more than 32 columns.

A unique constraint can be defined in either a CREATE TABLE statement or an ALTER TABLE statement.

What is the difference between a unique constraint and a primary key?

Primary Key Unique Constraint
None of the fields that are part of the primary key can contain a null value. Some of the fields that are part of the unique constraint can contain null values as long as the combination of values is unique.

Oracle does not permit you to create both a primary key and unique constraint with the same columns. When you use insert into Oracle statment the Oracle server check for unique constrints.

Using a CREATE TABLE statement

The syntax for creating a unique constraint using a Oracle CREATE TABLE PLSQL statement is:

CREATE TABLE table_name (column1 datatype null/not null, column2 datatype null/not null, ... CONSTRAINT constraint_name UNIQUE (column1, column2, . column_n) );

For example:

CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_unique UNIQUE (supplier_id)
);

In this example, we've created a unique constraint on the supplier table called supplier_unique. It consists of only one field - the supplier_id field.

We could also create a unique constraint with more than one field as in the example below:

CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_unique UNIQUE (supplier_id, supplier_name)
);

Using an ALTER TABLE statement

The syntax for creating a unique constraint in an ALTER TABLE statement is:

ALTER TABLE table_name add CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n);

For example:

ALTER TABLE supplier add CONSTRAINT supplier_unique UNIQUE (supplier_id);

In this example, we've created a unique constraint on the existing supplier table called supplier_unique. It consists of the field called supplier_id.

We could also create a unique constraint with more than one field as in the example below:

ALTER TABLE supplier add CONSTRAINT supplier_unique UNIQUE (supplier_id, supplier_name);

Drop a Unique Constraint

The syntax for dropping a unique constraint is:

ALTER TABLE table_name drop CONSTRAINT constraint_name;

For example:

ALTER TABLE supplier drop CONSTRAINT supplier_unique;

In this example, we're dropping a unique constraint on the supplier table called supplier_unique.

Disable a Unique Constraint

The syntax for disabling a unique constraint is:

ALTER TABLE table_name disable CONSTRAINT constraint_name;

For example:

ALTER TABLE supplier disable CONSTRAINT supplier_unique;

In this example, we're disabling a unique constraint on the supplier table called supplier_unique.

Enable a Unique Constraint

The syntax for enabling a unique constraint is:

ALTER TABLE table_name enable CONSTRAINT constraint_name;

For example:

ALTER TABLE supplier enable CONSTRAINT supplier_unique;

In this example, we're enabling a unique constraint on the supplier table called supplier_unique.

 

 
 

 

UNIQUE ORALCE FAQ


One common question about sequences is:

Question:  While creating a sequence, what does cache and nocache options mean? For example, you could create a sequence with a cache of 20 as follows:

CREATE SEQUENCE supplier_seq     MINVALUE 1     START WITH 1     INCREMENT BY 1     CACHE 20;

Or you could create the same sequence with the nocache option:

CREATE SEQUENCE supplier_seq     MINVALUE 1     START WITH 1     INCREMENT BY 1     NOCACHE;

Answer:  With respect to a sequence, the cache option specifies how many sequence values will be stored in memory for faster access.

The downside of creating a sequence with a cache is that if a system failure occurs, all cached sequence values that have not be used, will be "lost". This results in a "gap" in the assigned sequence values. When the system comes back up, Oracle will cache new numbers from where it left off in the sequence, ignoring the so called "lost" sequence values.

Note: To recover the lost sequence values, you can always execute an ALTER SEQUENCE command to reset the counter to the correct value.

Nocache means that none of the sequence values are stored in memory. This option may sacrifice some performance in stored procedure Oracle, however, you should not encounter a gap in the assigned sequence values.


Question:  How do we set the LASTVALUE value in an Oracle Sequence?

Answer:  You can change the LASTVALUE for an Oracle sequence, by executing an ALTER SEQUENCE command.

For example, if the last value used by the Oracle sequence was 100 and you would like to reset the sequence to serve 225 as the next value. You would execute the following commands.

alter sequence seq_name increment by 124;

select seq_name.nextval from dual;

alter sequence seq_name increment by 1;

Now, the next value to be served by the sequence will be 225.

 
 
 
Oracle news Oracle guide
 
     
  29-11-2006 Guida Oracle 10g
     
  30-02-2007 Oracle Server
     
  29-02-2007 Oracle Errors
Functions Oracle Functions
Delete Delete
Between Between
Check Check
Count Count
Datatypes Datatypes
Delete Oracle Delete Statement
Distinct Distinct
Exists Exists
Functions Functions
Grant revoke Grant Revoke
Group by Oracle Group by
Having Having
Oracle Function in Oracle Function in
Indexes Indexes
Insert Insert
Intersect Intersect
Isnull Is null
Joins Joins
Substr Substr
 
Oracle Functions PLSQL  l Consulenza Software Verona  l  Siti Web Verona  l   Exceptions  l  Oracle Errors  l  Oracle Collaborations Suite  l  Products  l  Contact
Oracle errors ora