I am having trouble running this query.
SELECT * FROM mysql.routines;
The error code 3554 says
Access to data dictionary table 'mysql.routines' is rejected.
I'm trying to look at all of my stored procedures. I've granted all privileges to the root user and it is still not working. Any ideas?
You seem to be looking for the INFORMATION_SCHEMA ROUTINES Table:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
From the documentation:
The ROUTINES table provides information about stored routines (stored procedures and stored functions). The ROUTINES table does not include built-in SQL functions or user-defined functions (UDFs).
In normal cases, it is recommended to use INFORMATION_SCHEMA instead of mysql schema : the former is the official interface to the system tables.
Related
I'm looking for a method or a query to retrieve all tables that are used in a procedure.
I tried information_schema.routinesbut it contains all the definition for a procedure.
Is there any system table that contains the dependency relationship for this ?
Or how can I get table names from the definitions using other language such as Python?
Thanks a lot!!
The current version of MySQL does not implement such a view in INFORMATION_SCHEMA.
MySQL 8.0.13 added I_S.VIEW_TABLE_USAGE, which allows you to look up the tables used by a view. This was done for WorkLog #11864. That WorkLog notes compatibility with PostgreSQL and Microsoft SQL Server.
However, there is no WorkLog I can find for an hypothetical I_S.ROUTINE_TABLE_USAGE table. I checked PostgreSQL, and it has this view: https://www.postgresql.org/docs/current/infoschema-routine-table-usage.html but MySQL does not.
So to get this information automatically, you would have to query the procedure body, and parse it for table references. Not an easy task.
For the sake of simplification, let's say I have 2 databases with data, db_data_1 and db_data_2 which have the same set of tables and I have a 3rd database where my stored procedures are defined, say db_sp. Let's say my stored procedure is called my_sp
When I try to call db_sp.my_sp from either db_data_1 or db_data_2, I get an error saying that the tables referenced in db_sp.my_sp don't exist.
How can I have db_sp.my_sp query the tables in the calling database vs the database where my_sp is defined (namely db_sp)
Thanks.
You must qualify the table names in your query with the database name in the stored procedure. SELECT col FROM db_data_1.tbl instead of SELECT col FROM tbl, for example.
The documentation says this:
USE statements within stored routines are not permitted. When a routine is invoked, an implicit USE db_name is performed (and undone when the routine terminates). The causes the routine to have the given default database while it executes. References to objects in databases other than the routine default database should be qualified with the appropriate database name.
Why is this so? It seems like a big pain in the xxx neck.
A big use of stored code is the hiding of data from unprivileged users. You can GRANT MySQL users access to stored procedures without granting access to the underlying tables. This restriction ties the tables to the procedures.
A user who has privileges only in the test database shouldn't be able to do this sort of thing.
USE production;
CALL test.get_all_user_private_data();
And, if you're USEing one database and you run stored code that's in a second database, it gets the data from that second database.
Your solution is to consider your stored code (procedures, functions) to be part of the schema definition for each database. They go along with your other data definition operations like CREATE TABLE. Don't try to put them in their own "code library" database, but put them in each database where they're needed.
Are all kinds of procedural code (stored procedure, functions, views
and triggers) stored on database or database management system
server, once created, regardless of how they are created (for
example, typing their definitions interactively in a command-line
client of the database management system)? I am asking this because
in the command-line client of a RDBMS (such as postgresql, mysql, server sql), when typing a query statement, the query statement
isn't stored any where.
in an interactive command-line REPL for a general purpose programming language, when typing the definition of a function or
procedure, the function or procedure will not exist after closing
the session of the REPL.
How are procedures, functions and triggers and views stored?
Are they stored per table, per database or per database management
system server?
If they are stored per database, are they stored in a separate table
from the tables that store the actual data and are in the same database? For example, in every database, besides those tables which store the actual data, is there a
table which stores the stored procedures of the database, a table
the functions of the database, a table the views of the database,
and a table the triggers of the database?
Similar questions if they are stored per table, or per database
management system server.
Thanks.
I am currently trying to make a dump of my production database and deploy it in my development database.
Before i do so, i am willing to add a script that will change the definer of the database's tables to a definer that is used in my development database after the deployment is complete.
Is there a way to tell what is the current definer for a table?
Iv'e tried SHOW CREATE TABLE but it doesn't give me the definer of that table.
Note that i already have a script that does so for another schema, but i am not sure if the definer for the other schema is the same.
Also, i couldn't find an answer to this over the internet, so i am asking this here.
Thanks.
Databases and tables do not have a 'definer' (or DBO) in MySQL.
"users" are "GRANTed" permissions to access databases and/or tables. Will having different GRANTs solve whatever you are trying to solve?
A related topic... Stored Routines execute as the "invoker" or a specified "user" (your choice when creating the routine), but I don't think that is what you are asking about.
You, or others lead to this question, may be thinking of procedures/functions/triggers/events/views that may be correlated with the tables you're attempting to find definers for.
Stored Object Access Control
Stored programs (procedures, functions, triggers, and events) and views are defined prior to use and, when referenced, execute within a security context that determines their privileges. The privileges applicable to execution of a stored object are controlled by its DEFINER attribute and SQL SECURITY characteristic.
In which case, you may find the following queries helpful:
SHOW CREATE PROCEDURE proc_name
SHOW CREATE FUNCTION func_name
SHOW TRIGGERS FROM db_name
SHOW CREATE EVENT event_name
SHOW CREATE VIEW view_name
Can I import Mysql function and use it for multiple database with same pattern name without import that function any more ?
I know Mysql save function in two table :
information_schema.ROUTINES and mysql.proc
Does anyone have an idea to do this?
Every stored procedure is associated with its schema or database.
So long as the SPs are just routines but not depended on table data, one can happily call them from anywhere.
And if the SPs are intended to calculate based on table data, then you definitely need that database qualifier while defining the SP body.
I.e. instead of calling
select count(*) from routines;,
you require to call
select count(*) from information_schema.routines;.
If you look into the table structure of both routines and proc you can find a column routine_schema and db respectively which point to target database of the SPs.
Unless you define SP bodies, which are table data related, in this way
you definitely have to redefine them in your database environment.
More important thing is that, you again require privileges to access and execute such cross database SPs.