Subject | ALTER TABLE, add default value to an existing field |
---|---|
Author | Hugo |
Post date | 2003-03-14T12:10:21Z |
I want to update the metadata of an existing table, but only adding a
DEFAULT value.
For example:
CREATE TABLE MYTABLE (
ID INTEGER NOT NULL PRIMARY KEY,
DESCRIPTION VARCHAR(40),
MYFLAG CHAR(1)
);
this table is in production, and now I want to convert it to:
CREATE TABLE MYTABLE (
ID INTEGER NOT NULL PRIMARY KEY,
DESCRIPTION VARCHAR(40),
MYFLAG CHAR(1) DEFAULT 'N'
);
After reading on SQL documentation about ALTER TABLE syntax, I cannot make:
ALTER TABLE MYTABLE ALTER COLUMN MYFLAG DEFAULT 'N';
nor
ALTER TABLE MYTABLE ALTER COLUMN MYFLAG TYPE CHAR(1) DEFAULT 'N';
1) The only possibility I see is:
ALTER TABLE MYTABLE
DROP MYFLAG,
ADD MYFLAG CHAR(1) DEFAULT 'N';
but this way I lost the data.
2) Another trick I have found, to avoid DROPing, is to define a DOMAIN:
CREATE DOMAIN MYDOMAIN CHAR(1) DEFAULT 'N';
and
ALTER TABLE MYTABLE ALTER COLUMN MYFLAG TYPE MYDOMAIN;
But on both 1) and 2) solutions, I must DROP and recreate all PROCEDUREs and
TRIGGERs with dependencies on MYFLAG.
And, finally, the question:
Is there a way to add this default, directly via system tables
(RDB$RELATION_FIELDS, I suppose)?
Many thanks in advance,
Hugo.
DEFAULT value.
For example:
CREATE TABLE MYTABLE (
ID INTEGER NOT NULL PRIMARY KEY,
DESCRIPTION VARCHAR(40),
MYFLAG CHAR(1)
);
this table is in production, and now I want to convert it to:
CREATE TABLE MYTABLE (
ID INTEGER NOT NULL PRIMARY KEY,
DESCRIPTION VARCHAR(40),
MYFLAG CHAR(1) DEFAULT 'N'
);
After reading on SQL documentation about ALTER TABLE syntax, I cannot make:
ALTER TABLE MYTABLE ALTER COLUMN MYFLAG DEFAULT 'N';
nor
ALTER TABLE MYTABLE ALTER COLUMN MYFLAG TYPE CHAR(1) DEFAULT 'N';
1) The only possibility I see is:
ALTER TABLE MYTABLE
DROP MYFLAG,
ADD MYFLAG CHAR(1) DEFAULT 'N';
but this way I lost the data.
2) Another trick I have found, to avoid DROPing, is to define a DOMAIN:
CREATE DOMAIN MYDOMAIN CHAR(1) DEFAULT 'N';
and
ALTER TABLE MYTABLE ALTER COLUMN MYFLAG TYPE MYDOMAIN;
But on both 1) and 2) solutions, I must DROP and recreate all PROCEDUREs and
TRIGGERs with dependencies on MYFLAG.
And, finally, the question:
Is there a way to add this default, directly via system tables
(RDB$RELATION_FIELDS, I suppose)?
Many thanks in advance,
Hugo.