Default record if no other - mysql

Asked

Viewed 100 times

2

Is it possible to program the database to have a default record if there is no other record in the table? making the table impossible to be empty.

then if there are other records it ignores the default if it has none it uses the default.

it is possible?

I am implementing access permissions on a system that already has many users and most users do not have registered permission so it causes problems when checking permissions and so on.. then I would like to mount a record that always existed by default configuration.

  • It would probably be the case to do this in the application. IF has record in db, $permissions = db field, Else $permissions = default user. The advantage of this is that if there are several different permissions, you only write different permissions to each user.

  • Yes. the biggest problem is that these permissions are tied to different plans so I must have basic plans and permissions registered @Bacco

  • @Silvioandorinha I think that the best option is to try to correct the method that validates the permissions, I think a lot of ways to create a default record in the bank.

2 answers

-1

You can create a Rigger that when adding a user, you check if that user has any record in the table of PERMISAO, if it does not have it inserts a default perm, if it has it ignores.

  • Yes.. the problem is not with new users, but with existing users.. for new users I have already done this type of treatment

  • 1

    For existing users you will need to make a treatment script. You can make a query by doing JOIN select USUARIO.USU_CODIGO, USU_NOME, GRU_CODIGO from USUARIO
left join USUGRUPO
ON USUARIO.USU_CODIGO = USUGRUPO.USU_CODIGO
WHERE GRU_CODIGO IS NULL The search result above will bring me all users who are not in the table USUGRUPO, being thus caught these users and I do an Insert with the default value

  • I adapted this interim solution from left join.

-1

Browser other questions tagged

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