Mysql use of views across databases - mysql

I am using myISam tables ONLY for this, and have two accounts/databases named cpm155 and cpm179.
I have a group of tables in cpm155 which I have deleted (made a backup first), and then run the following queries for each one:
CREATE OR REPLACE VIEW cpm155.financial_items AS SELECT * FROM cpm179.financial_items;
CREATE OR REPLACE VIEW cpm155.financial_headers AS SELECT * FROM cpm179.financial_headers;
/* etc */
then I ran this query:
GRANT ALL PRIVILEGES ON cpm179.* TO cpm155#localhost;
GRANT ALL PRIVILEGES ON cpm155.* TO cpm179#localhost;
so you would think that there would be no permissions problems.
I have two questions:
1. should update, insert and delete operations work normally in both databases since the view->table is a one-to-one correspondence?
2. are there any other gotchas that I am missing here?
Thank you! Sam

Related

MySQL multiple tables of the same name in the same database

I have a database, and a set of tables. The scenario is that, two different departments use the exact same table structure and the server code/frontend, but the data for the departments is completely different.
The best thing would be to have two different databases, with the exact structure and different data.
However, if I would try to implement this in just 1 database, would it be possible? I cannot change the table names, as it would require changing the entire code which queries the tables. Is there any way to create sub-folders/directories in a single database?
I searched, and there probably isn't. But maybe there is a workaround someone can suggest?
Read about Identifier Qualifiers. You can have tables of the same name only if they are in separate databases, and you use the database name as a qualifier.
SELECT * FROM db1.sametablename;
SELECT * FROM db2.sametablename;
You can join tables across databases in the same query:
SELECT * FROM db1.sametablename JOIN db2.sametablename;
You can even declare foreign key constraints across databases:
ALTER TABLE db1.sametablename ADD FOREIGN KEY (col1)
REFERENCES db2.sametablename (col1);
Databases are basically a namespace for tables and other objects.
Stop the Mysql Server and restart it again, This must solve the issue.
Windows
mysqladmin -u root -p shutdown
Linux
/etc/init.d/mysqld stop or service mysqld stop or service mysql stop

Grant users to create database with username

In phpmyadmin I want to grant users to create and delete databases but this access should be limited to a specific prefix.
My users have 3 different accounts on PhpMyAdmin: username_ro (for only reading), username_rw (for reading and writing) and username_admin (for creating other databases and tables into their account)
I want them to be able to create a database username_website but I don't want them to be able to create database theother_website. They should also be able to drop username_website but unable to drop theother_website
How can I do this with sql or PhpMyAdmin.
Thanks in advance.
With some trial and error I have found a solution. By doing this query I was able to create and drop database username_website but I wasn't able to create or drop database theother_clients
GRANT ALL PRIVILIGES
ON `username\_%`.*
TO 'username_admin'#'localhost';
PS. the query is a little edited. I changed the rights I actually gave with ALL PRIVILIGESand I changed the actual username with username.

MySQL - Trigger or Replication is better?

I want to replicate certain table from one database into another database in the same server. This tables contain exactly the same fields.
I was considering to use MySQL Replication to replicate that table but some people said that it will increase IO so i find another way to create 3 Trigger (Insert, update and Delete) that will perform exactly the same thing like what i expect.
My Question is, which way is better? Is it using MySQL replication is better even though it's in the same server or using Trigger to replicate the data is better.
Thanks.
I don't know what is your goal, but I got mine getting use of the VIEW functionality.
I had two different applications with separate databases but in the same Mysql server. Application2 needed to get a few data from Application1. In general, this is a trivial situation that you can handle with USE DB1; or USE DB2; as your needing, but my programming framework does not work very well with multiple DBs.
So, lets see my solution...
Here is my select query to retrieve this data:
SELECT id, name FROM DB1.customers;
So, using DB2 as default schema, I've created a VIEW:
USE DB2;
CREATE VIEW app1_customers AS SELECT id, name FROM DB1.customers;
Now I can retrieve this data in DB2 as a regular table with a regular SELECT statement.
SELECT * FROM DB2.app1_customers;
Hope ts useful. BR
Assuming you have two databases on the same server i.e DB1 and DB2 and the table is called tbl1 and it is sitting in DB1 you can query the table like this:
USE DB1;
SELECT * FROM tbl1;
USE DB2;
SELECT * FROM DB1.tbl1;
This way you wont need to copy the data and worry about extra space and extra code. You can query a table in another database on the same server. Replication and triggers are not your answer here. You could also create a view to encapsulate the SQL statement.
Definitely triggers is the way to go. Having another server (slave) will need to spare several MB for installation, logs, cpu and memory usage.
I'd use triggers to keep both tables equal. If you want to create a table with the same columns definition and data use:
USE db2;
CREATE TABLE t1 AS SELECT * FROM db1.t1;
After that, go ahead and create the triggers for Update, Insert and Delete statemetns.
Also you could ALTER the new table to a different engine like MEMORY or add indexes to see if you can improve something.

Permission to drop views but not drop tables

I want to create a user with permission to create and drop views but not drop tables or databases. This is so I can play around when testing my views without the risk of accidentally deleting a table.
There is a GRANT CREATE VIEW privilege but there doesn't appear to be a GRANT DROP VIEW counterpart. GRANT DROP apparently applies to databases, tables and views.
Is this possible in MySQL?
I've been researching this, too, and the answer appears to be No. You can restrict the DROP to only tables/views within one database (or a group of LIKE pattern-matched databases). This will make sure they cannot drop the entire database. Unfortunately, you cannot do pattern-matching on the table/view names. It's either all the tables/views (*) in those databases, or only explicity mentioned tables/views.

What database privileges does a Wordpress Blog really need?

I am setting up a few Wordpress blog sites. I have created a user in mysql that wordpress will use to access its database. The docs say to give this user all privileges on the database.
Does it really need full privileges? I expect not, so does anyone know the min set of privileges that it really needs?
I'm no Wordpress expert, but I would recommend it does actually have all privileges apart from GRANT. It will need to be able to create tables and insert/update etc. Several plugins use their own tables, which they create on the fly if they do not exist.
I grant:
ALTER
CREATE
CREATE TEMPORARY TABLES
DELETE
DROP
INDEX
INSERT
LOCK TABLES
SELECT
UPDATE
Hope that helps anyone else that looks into this.
grant select, insert, delete, update, create, drop, alter on myblog