select privilege on more than one table - mysql

How do I grant select privilege on more than one table in a single statement?
mysql> grant select on dbName.crw_changes to sink;
Query OK, 0 rows affected (0.02 sec)
mysql> grant select on dbName.crw_changes, dbName.bs_services to sink;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' dbName.bs_services at line 1

You can't do it in standard SQL. More than one user is permitted, but only one table.
That's generally true for most SQL dialects, too. There are reasons for the limitation, most notably related to checking permissions on the separate tables, though they were perhaps more relevant in the 1980s than they are now.

You can't. GRANT SELECT only works on one table at a time.
There's a workaround, however. You can do something like:
SELECT CONCAT('GRANT SELECT ON test.', TABLE_NAME, ' to ''foouser'';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test'
AND TABLE_NAME LIKE 'foo_%'
The above extracts tables in the 'test' schema that start with 'foo_' and prints them out. You can then take those names and copy and paste them into a stored procedure or query builder window and execute them.
By the way, the code is untested (by me). I took it from http://lists.mysql.com/mysql/202610.

Related

How append MySQL table name with today's date in format "-YYYY-MM-DD" with BASH

From the Linux CMD line, how would I append a MySQL table name with today's date in YYYY-MM-DD format. For example, if today's date is 4/13/18, it would change tablename to tablename-2018-04-13.
I believe I'm close with what I've tried after entering mysql from command line:
ALTER TABLE database.tablename RENAME database.tablename-$(date +%Y-%m-%d);
This generates an error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near '-$(date +%Y-%m-%d)' at line 1
You seem to be using a bash command substitution inside the MySQL client. That won't work, because SQL doesn't know anything about bash syntax like $().
Another problem is that you must use an identifier, not a string. The name of the table can't be an expression, it has to be fixed in your ALTER statement before it is parsed.
You could do this with PREPARE and EXECUTE:
mysql> SET #sql = CONCAT('ALTER TABLE database.tablename RENAME database.tablename_', DATE_FORMAT(CURDATE(), '%Y%m%d'));
mysql> PREPARE stmt FROM #sql;
mysql> EXECUTE stmt;
Note I changed your table name, because - is a special character in SQL identifiers. You can't normally use a table name that has - characters. You can if you put your table name in delimiters. MySQL's delimiters are back-ticks.
mysql> SET #sql = CONCAT('ALTER TABLE database.tablename RENAME database.`tablename-', DATE_FORMAT(CURDATE(), '%Y-%m-%d'), '`');
Then prepare and execute like before.
But this is needlessly complex, and you would need to remember to use back-ticks every time you want to use this table in a query.
It's much simpler to avoid using special characters in your table names.
set #sql = CONCAT('RENAME TABLE `tablename` TO tablename', DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y_%m_%d'));
prepare s from #sql;
execute s;
Updated based on #Anthony's comments. The sql command is tested;
All the answers and comments brought me to the answer that worked best for me. I ditched the -YYYY-MM-DD appending convention as it does nothing for me other than create problems, as highlighted by #Bill_Karwin above. This is the simple BASH command, very similar to the comment by #Anthony above, that works for me:
mysql database -e "ALTER TABLE table RENAME table_$(date +%Y%m%d)";
This renames table to (assuming for example if ran on 4/15/18) table_20180415.

Making a backup copy of SQL table throws me this error: "#1064 - You have an error in your SQL syntax; "

I'm trying to make a backup of my table in MySql but I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table `zbackup_oc_t_city` from `oc_t_city` LIMIT 0, 30' at line 1
This is the code that I'm using to backup
SELECT * INTO TABLE `zbackup_oc_t_city` FROM `oc_t_city`
Here is my oc_t_city table:
Here is zbackup_oc_t_city
I have tried it on numerous tables and it keeps throwing me the same error... any ideas?
Thanks
If you want to create your backup table and do the backup in just one statement use
CREATE TABLE `zbackup_oc_t_city` SELECT * FROM `oc_t_city`;
CREATE TABLE ... SELECT Syntax
You can create one table from another by adding a SELECT statement at
the end of the CREATE TABLE statement:
CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;
With MySQL you can't use SELECT ... INTO to select into a new table:
SELECT ... INTO Syntax
The SELECT ... INTO form of SELECT enables a
query result to be stored in variables or written to a file:
SELECT ... INTO var_list selects column values and stores them into
variables.
SELECT ... INTO OUTFILE writes the selected rows to a file. Column and
line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILE writes a single row to a file without any
formatting.
I do remember having similar troubles while working with SQL myself. One cause of error I found was the use of citation marks... try removing the citation marks like this:
SELECT * INTO zbackup_oc_t_city FROM oc_t_city;
I'm not sure this fixes your problem (but I can't see anything else wrong with your query). I hope it does though. :)

SHOW TABLES statement with multiple LIKE values

mysql> SHOW TABLES like 'cms';
+-------------------------+
| Tables_in_tianyan (cms) |
+-------------------------+
| cms |
+-------------------------+
1 row in set (0.00 sec)
Result
mysql> SHOW TABLES like 'cms' or like 'role';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual...
How can I filter by multiple conditions ?
You need to use the WHERE clause. As shown in the docs, you can only have a single pattern if you use "SHOW TABLES LIKE ...", but you can use an expression in the WHERE clause if you use "SHOW TABLES WHERE ...". Since you want an expression, you need to use the WHERE clause.
SHOW TABLES
FROM `<yourdbname>`
WHERE
`Tables_in_<yourdbname>` LIKE '%cms%'
OR `Tables_in_<yourdbname>` LIKE '%role%';
You can just use a normal SQL WHERE statement to do it.
SHOW TABLES WHERE Tables_in_tianyan LIKE '%cms%'
show tables from mydb
where
Tables_in_mydb like '%statistics%'
or Tables_in_mydb like '%device%';
You take table list using the below code
select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA = 'database_name'
Hope it will help you.
this will help
SELECT
TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
LIKE 'cms%';

How can i check if the table mysql.proc exists

I am working on a WPF application which installs if MySQL is installed,
so before installation I want to check whether mysql.proc table exists or not.
I googled about it and ended up with a query
select * from information_schema.Tables
where Table_schema = Schema() and Table_Name = 'mysql.proc'
This query returns an empty row.
I also tried simple select statement
select * from mysql.proc,
and this returned a table with the names of all the stored procedures, but if this table didn't exists then it throws an exception in the c# code.
So is there any way that I can fire a query from c# and get a boolean value depending on whether mysql.proc table exists or not?
Try SHOW TABLES FROM mysql LIKE 'proc'. If there are no result rows, the table doesn't exist. If there is one row, the table exists. Note that this approach isn't portable across RDBMSs, though that doesn't seem to be a concern of yours.
As for your first query, SCHEMA() returns the default database, so if it's not "mysql", the query will fail. Likewise, data in the Table_Name column doesn't include the database name, so comparing to 'mysql.proc' will always fail.

Existence confirmation method of the table of MySQL

I want to confirm whether there is a certain table.
When create a table, there is an SQL sentence such as DROP TABLE IF EXISTS xxx_tb.
Will there be the method that can identify the existence of the table by SQL likewise?
Use INFORMATION_SCHEMA:
select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'MyTable';
Should be portable across most databases.
You want the SHOW TABLES command of MySQL:
SHOW TABLES LIKE 'xxx_tb';
Or indeed, you can just do a query like
SELECT COUNT(*) FROM tbl WHERE 1=0
Which will give an error (see documentation for exact error code, or try it) if the table doesn't exist, but succeed with no results if it does.