Default registry if you have no other - mysql

2

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

So if you have other records, it ignores the default, if you do not have any, use the default.

Is it possible?

I am implementing access permissions on a system that already has many users and a lot of users do not have registered permissions, so it causes problems when checking permissions and etc ... so I would like to set up a registry that always existed by default configuration .

    
asked by anonymous 26.02.2016 / 15:00

2 answers

-1

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

    
26.02.2016 / 15:09
-1

You can create a dummy id and do the following:

SELECT (SELECT id FROM tbl WHERE id = 9823474) AS id;

Works with PostgreSQL , SQL Server and MySQL . Also with SQLite .

Alternative with UNION ALL :

SELECT id FROM tbl WHERE id = 9823474
UNION  ALL
SELECT NULL -- FROM DUAL  -- apenas para Oracle
LIMIT 1;

Source: link

    
26.02.2016 / 15:11