|
Creating constraints in databases: specify the name |
|
Facts -
Databases
|
|
Monday, 07 September 2009 20:14 |
|
When creating constraints such as foreign or primary keys in Oracle database take care that you give each constraint a name, e.g.
alter table foo add CONSTRAINT foo_bar12_pk PRIMARY KEY (bar1, bar2);
This command creates a primary key named foo_bar12_pk on table foo using columns bar1 and bar2. Note that the constraint name must be unique, at least on on Oracle 9 databases, so use a combination of the table name and the field names for the constraint names.
If you forget to set the name explicitly then the system generates a name for you. This name
will vary among different environments. It will be different on the development environment, on the
test environment and on the production environment.
The problem occurs when you want to modify or
drop the constraint later. Because the constraint name is different on each environment it will be
difficult to make a single script changing the constraint on each environment. In that case you
need to either find out the system name on each environment and make a script for each environment
or create the table or the columns again from a copy and then apply the constraints again.
|