Mysql 8.X
Database name: db1.
In this database, I create a view:
CREATE VIEW vw1 AS
SELECT * FROM sometable;
When I check the source code of the view, instead of the code above, I can see:
SELECT * FROM db1.sometable;
I.e. MySQL engine automatically adds a database name prefix to every table I refer to in the view.
Now, I need to rename my database from db1 to db2. There is no built-in database rename functionality in MySQL. I have to take a backup, then drop the original database, then restore the backup under a new name.
Result: the vw1 view in my new db2 database tries to select rows from (now non-existing) db1 database, causing errors.
Now imagine 100s of databases, with 100s or 1000s of tables each. This issue makes things absolutely non-manageable.
Is there any way of stopping MySQL from adding database prefixes in view definitions?
Related
I have two separate databases installed on the same server. I name the representation as db1 and db2. I have a table named sample_table in my db1 database. I am trying to copy this table with its records to my db2 database with the following queries:
CREATE TABLE db2.sample_table LIKE db1.sample_table
insert into db2.sample_table select * from db1.sample_table
As a result of my research, I found that this method works in most places. However, when I type and run my query from the db2 console, I am having a user permission problem for db1. The query might be running, but should there be a configuration link between these two databases? If so, how can I get this link? What are the alternative solution methods I can overcome this problem?
We are using MYSQL in that we have 10 databases as single project.
my problem is to auto-merge 10 database tables into single database using replication.
for example :
MasterDatabases
database1
....table1
....table2
database2
....table21
....table22
database3
....table31
....table33
Replication Database
slavedatabase
....table1
....table2
....table21
....table22
....table31
....table33
You can use --replicate-rewrite-db for that.
Tells the slave to create a replication filter that translates the
default database (that is, the one selected by USE) to to_name if it
was from_name on the master. Only statements involving tables are
affected (not statements such as CREATE DATABASE, DROP DATABASE, and
ALTER DATABASE), and only if from_name is the default database on the
master. To specify multiple rewrites, use this option multiple times.
The server uses the first one with a from_name value that matches. The
database name translation is done before the --replicate-* rules are
tested. You can also create such a filter by issuing a CHANGE
REPLICATION FILTER REPLICATE_REWRITE_DB statement.
Read more about it here.
I have a view in my production replica (it's a read-only database) and I need to copy one of the views to my data warehouse.
What would be the best way to go about it?
I can not create a table from the view on the replica. Meaning I can not do CREATE TABLE Table_From_View AS SELECT * FROM My_View;
I can not copy all the tables that create the view definition. Some of them contain sensitive data.
I am sure I missing something basic here...
Ideas?
In MySQL, a view doesn't store anything. It's more like an alias or a macro. It just passes the query logic through to underlying base tables.
You need the underlying base tables to exist on the same MySQL instance where you create the view, and the base tables must remain existing on that same MySQL instance while you query the view.
If you only want to copy the contents of a view, so you don't copy the other sensitive data that is not selected by the view, you would have to create a base table to copy the data:
mysql> CREATE TABLE myview_base AS SELECT * FROM myview;
Then you can do a logical dump of that copy table:
shell > mysqldump --single-transaction mydatabase myview_base > myview_base.sql
Then restore that dump file to your data warehouse as you would any other SQL dump file.
Another possible strategy:
SELECT * FROM myview INTO OUTFILE 'filename.csv';
This dumps the result of an SQL query to a file. See https://dev.mysql.com/doc/refman/8.0/en/select-into.html
The file will be created on the database server, so if you don't have shell access to the server, you won't be able to retrieve the file.
We will migrate the database from mysql to postgresql in our product(through java). So we need to change the mysql query to postgresql query in java application. How to create the table i.e., databasename.tablename in postgresql.
For mysql, we can directly create the table e.g create table information.employee.
Here database name is "information" and table name is "employee" . Is it possible to achieve same query in postgresql.
I searched google it says cross database reference is not possible. Please help me.
I saw pg_class table it contains the table names in the specific database, like wise databse and tables relationships are stored in any other table.
This is normally done using schemas rather than databases, which is more or less like how MySQL organizes it anyway.
Instead of
create database xyz
use
create schema xyz
When you create tables, create them:
create table xyz.myTable
you will need to update your search path to see them on the psql command line tool, or if you want to query them without using the schema explicitly. The default schema is public, so when you create a table without a schema name, it ends up in public. If you modify your search_path as below, the default schema becomes the first in the list: xyz.
set search_path=xyz,public,pg_catalog;
and you must not have spaces in that statement. You can do it globally for a user/role too:
alter role webuser set search_path=xyz,public,pg_catalog;
Also, don't forget that postgresql string matches are case sensitive by default (this one catches people out a lot).
If you want to have different physical locations for the files for each schema, you can do that with tablespaces. If you have a look at the postgresql documentation page, they have info on how to do it, it's pretty easy.
database in MySQL == schema in PostgreSQL. So you will most probably want to migrate all your mysql dbs into one postgres db. Then you will be able to do "cross-database" queries.
See my answer to this question: Relationship between catalog, schema, user, and database instance
I would like to select data from a second MySQL database in order to migrate data from one server to another.
I'm looking for syntax like
SELECT * FROM username:password#serverip.databaseName.tableName
Is this possible? I would be able to do this in Microsoft SQL Server using linked servers, so I'm assuming it's possible in MySQL as well.
You can create a table using FEDERATED storage engine:
CREATE TABLE tableName (id INT NOT NULL, …)
ENGINE=FEDERATED
CONNECTION='mysql://username:password#serverip/databaseName/tableName'
SELECT *
FROM tableName
Basically, it will serve as a view over the remote tableName.
There are generally two approaches you can take, although neither of them sound like what you're after:
Use replication and set up a master/slave relationship between the two databases.
Simply dump the data (using the command line mysqldump tool) from the 1st database and import it into the 2nd.
However, both of these will ultimately migrate all of the data (i.e.: not a subset), although you can specify specific table(s) via mysqldump. Additionally, if you use the mysqldump approach and you're not using InnoDB you'll need to ensure that the source database isn't in use (i.e.: has integrity) when the dump is created.
You can't do this directly, but as someone else alluded to in a comment, you can use mysqldump to export the contents of a table as a SQL script.
At that point you could run the script on the new server to create the table, or if more manipulation of the data is required, import that data into a table with a different name on the new server, then write a query to copy the data from there.