Is it possible to create a decision structure in a table in SQL?

Asked

Viewed 284 times

4

I have a massive database, has more than 2,000 lines, and the second column has an ENUM that allows only 'Option 1' and 'Option 2'. If I insert 200 lines into the table and write the wrong ENUM name as an example insert into tabela values ('blabla', 'Ocao 2') he had returned an empty area in the ENUM field and this would lead to a huge headache. Is there any way in SQL I prevent this from happening, such as a check structure, preventing me from running the code?

1 answer

4


You can use a trigger that in the events UPDATE and INSERT verify whether the data in the ENUM column is valid or not. If it is not, generate an exception to be dealt with in the application.

Another option is to create another table with possible ENUM values, and then refer it to the first one via a foreign key, ensuring through integrity restrictions that an unsupported value is inserted.

I believe it is possible to do this using DOMAIN, but I don’t know if Mysql supports this, nor what the extension of this support.

  • 1

    I’ll take a look Vinicius, thanks for the reply, I’ll see how this Rigger works and later I’ll give you the feedback

  • I decided not to use the ENUM anymore, there are many people presenting several negative points... But you answered my question, vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.