PDA

View Full Version : alter column enum


BubikolRamios
11-07-2009, 08:55 AM
1.have tabA with field int, values 1,2,3
2.have tabB with field ENUM('1','2','3')

So I suppose, to dynamicaly update tabB field ENUM whenewer TabA is modified (say I add value 4 into int col in tabA --> ENUM('1','2','3','4')
), I have to create a trigger ?

Any tip appreciated.

BubikolRamios
11-07-2009, 12:01 PM
to clarify:

If I have tabA containing list of possible values that can be inserted into
tabB,
is there any other way to achive that except having ENUM type in tabB ?

was thinking somethink like, does not work of course:


insert into tabB B left join tabA A on A.id = B.id (B.id) values(3)


And if 3 does not exists in tabA then nothing wold be inserted into tabB

Old Pedant
11-07-2009, 08:43 PM
Why not simply:

INSERT INTO tabB ( fieldname )
SELECT fieldname FROM tabA WHERE fieldname = 3;

???

So, for example, if your code attempted to do

INSERT INTO tabB ( fieldname )
SELECT fieldname FROM tabA WHERE fieldname = 781;

you would find that there is no id of 781 in tabA and so the SELECT would return no records and nothing would be inserted into tabB.

If you have several other values to insert:

INSERT INTO tabB ( field1, field2, field3, enumField )
SELECT 'value1', 'value2', 'valud3', enumField FROM tabA WHERE enumField = 3;

Guaranteed to not insert anything into tabB if that enum value does not exist in tabA.