Ted Henry
Ted Henry

Reputation: 1492

Grant MySQL permissions on current database?

I can grant permissions with a static database name in the GRANT statement.

GRANT SELECT,INSERT ON database_name.table_name TO 'username'@'localhost';

Is there a way to dynamically set the database name in a GRANT statement so that the grant is for whichever happens to be the current database?

USE database_name;
GRANT SELECT,INSERT ON <what goes here?>.table_name TO 'username'@'localhost';

Upvotes: 1

Views: 268

Answers (1)

Nick
Nick

Reputation: 147166

You don't need to specify anything. If no database is specified, MySQL will assume the default database, which is set when you USE database_name. From the manual:

The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements.

And from the section on GRANT:

If you specify tbl_name rather than db_name.tbl_name, the statement applies to tbl_name in the default database.

So you can just use

GRANT SELECT,INSERT ON table_name TO 'username'@'localhost';

Upvotes: 3

Related Questions