Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: constraint name: what for?
A copy of this was sent to jerwynn_at_my-dejanews.com
(if that email address didn't require changing)
On Sun, 07 Mar 1999 00:30:29 GMT, you wrote:
>Hi,
>
> Can anyone explain the user of a constraint name such as pk_employee in:
>
>//specifies the primary key as a table constraint
>
>create table employee (
>id number(3),
>name varchar2(30),
>constraint pk_employee PRIMARY KEY (id)
>);
>
>or
>// specifies the primary key constraint in the field (column constraint)
>
>id number(3) constraint pk_employee PRIMARY KEY
>
>Isn't it that Oracle autogenerates an identifier/constraint_name
>if you do not specify it?
>What is the use of specifying the constraint_name in such a case?
>
yes, it will generate a unique name for you. *some* (there are lots) reasons you might want to name it yourself:
SQL> create table t ( x int check (x >0) );
SQL> !exp userid=tkyte/tkyte tables=t
Export: Release 8.0.3.0.0 - Production on Sat Mar 6 20:20:30 1999
(c) Copyright 1997 Oracle Corporation. All rights reserved.
Connected to: Oracle8 Enterprise Edition Release 8.0.3.0.0 - Production
With the Partitioning and Objects options
PL/SQL Release 8.0.3.0.0 - Production
Export done in US7ASCII character set and US7ASCII NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table T 0 rows exportedExport terminated successfully without warnings.
SQL> drop table t;
SQL> !imp userid=tkyte/tkyte full=y
Import: Release 8.0.3.0.0 - Production on Sat Mar 6 20:20:49 1999 (c) Copyright 1997 Oracle Corporation. All rights reserved. Connected to: Oracle8 Enterprise Edition Release 8.0.3.0.0 - Production With the Partitioning and Objects options PL/SQL Release 8.0.3.0.0 - Production
Export file created by EXPORT:V08.00.03 via conventional path . importing TKYTE's objects into TKYTE
. . importing table "T" 0 rows importedImport terminated successfully without warnings.
SQL> select constraint_name from user_constraints where table_name = 'T';
CONSTRAINT_NAME
Export file created by EXPORT:V08.00.03 via conventional path . importing TKYTE's objects into TKYTE
. . importing table "T" 0 rows importedImport terminated successfully without warnings.
SQL> select constraint_name from user_constraints where table_name = 'T';
CONSTRAINT_NAME
Export file created by EXPORT:V08.00.03 via conventional path . importing TKYTE's objects into TKYTE
. . importing table "T" 0 rows importedImport terminated successfully without warnings.
SQL> select constraint_name from user_constraints where table_name = 'T';
CONSTRAINT_NAME
SYS_C0039121 SYS_C0039122 SYS_C0039123
See, everytime we run IMP, it recreates the check constraint. Since the constraint doesn't have a name, it can create it over and over again. If we had named the constraint, this would not occur.
>BTW, I learned that it is necessary to supply a constraint_name when
>specifying a PRIMARY KEY table constraint. How come that it is allowed in
>column constraints to skip the constraint_name, while in table constraints
>there will be an error saying that the datatype is undefined.
>
no its not necessary:
SQL> create table t ( x int, y int, z int, primary key(x,y) )
2 /
Table created.
SQL> select constraint_name from user_constraints where table_name = 'T';
CONSTRAINT_NAME
Thomas Kyte
tkyte_at_us.oracle.com
Oracle Service Industries
Reston, VA USA
--
http://govt.us.oracle.com/ -- downloadable utilities
![]() |
![]() |