SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Use trigger to update ENUM field in another table

    Is it possible to define a trigger on table A, which updates the enum values of table B based on the values in table A?

    If I've got an enum field in my products table for 'productType'
    Code:
    alter table products add productType enum('BSK', 'CK','PDD')
    , and I want to store a more textual representation of these codes in a productTypes table
    Code:
    CREATE TABLE productTypes
    (
    	typeCode VARCHAR(10),
    	typeText VARCHAR(200)
    )
    INSERT INTO productTypes VALUES("BSK","Biscuits"), ("CK","Cakes"),("PDD","Puddings")
    If I want another productType value, I'd have to insert it into productTypes, and change the productType enum in products. Can I use a trigger to do this whenever the prodyctTypes table is changed?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by hessodreamy View Post
    Can I use a trigger to do this whenever the prodyctTypes table is changed?
    i suppose you could, if you really wanted to, but why?

    since you already have a producttypes table in mind, which will contain all the valid codes, you really shouldn't be using an evil ENUM

    use a foreign key instead -- same data integrity, but also portable to other database systems (ENUM is non-standard)...

    ... and no trigger necessary

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •