连接到同一 DBMS 上的 "foreign" 数据库

Connecting to "foreign" database on the same DBMS

我有点惊讶地看到 JDBC 允许我从“外部”数据库中的 table 透明地 SELECT,在同一个 DBMS 上,我有必要的特权,而不需要显式连接到外部数据库。这是 MySQL 应有的样子,还是只是 JDBC 的怪癖?

详情:
我在我的 DBMS 上创建了两个数据库:stkovrflo_1 和 stkovrflo_2。我从 MySQL World database.

在这些数据库中填充了 tables
CREATE TABLE stkovrflo_1.Country
SELECT name, region FROM world.Country;

CREATE TABLE stkovrflo_2.City
SELECT world.City.name, world.Country.name AS country
FROM world.City INNER JOIN world.Country ON world.City.CountryCode = world.Country.code;

在 JDBC 中,我能够通过 stkovrflo_1 数据库的连接 select stkovrflo_2.City table 的条目。我可以 SELECT 访问这两个数据库。

这是我的 JDBC 代码:

import java.sql.*;

public class JDBCExample {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/stkovrflo_1";

    public static void main(String[] args) throws Exception{
        
        String uid = args[0];
        String pswd = args[1];
        
        if(pswd.toUpperCase().equals("NULL"))
            pswd = null;

        Connection conn = null;
        conn = DriverManager.getConnection(DB_URL,uid,pswd);
        
        processTableSameDB(conn);
        System.out.println("\n\n");
        processTableDifferentDB(conn);
        
        if(conn != null)
            conn.close();
    }

    protected static void processTableSameDB(Connection conn) throws Exception {
        Statement stmt = null;
        String tableName = "Country";

        try{
            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Retrieving from table in same database...");

            stmt = conn.createStatement();
            String sql;
            sql = "SELECT * FROM " + tableName + " LIMIT 10";
            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()){
                String name = rs.getString("name");
                String region = rs.getString("region");

                System.out.print("Name: " + name);
                System.out.println(", Region: " + region);
            }
            rs.close();
        }
        catch(SQLException se){
            se.printStackTrace();
        }
        finally{
            if(stmt!=null)
                stmt.close();
        }
    }

    protected static void processTableDifferentDB(Connection conn) throws Exception {
        Statement stmt = null;
        String tableName = "stkovrflo_2.City";

        try{
            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Retrieving from table in different database...");

            stmt = conn.createStatement();
            String sql;
            sql = "SELECT * FROM " + tableName + " LIMIT 10";
            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()){
                String name = rs.getString("name");
                String country = rs.getString("Country");

                System.out.print("Name: " + name);
                System.out.println(", Country: " + country);
            }
            rs.close();
        }
        catch(SQLException se){
            se.printStackTrace();
        }
        finally{
            if(stmt!=null)
                stmt.close();
        }
    }
}  

JDBC输出如下:

Retrieving from table in same database...
Name: Aruba, Region: Caribbean
Name: Afghanistan, Region: Southern and Central Asia
Name: Angola, Region: Central Africa
Name: Anguilla, Region: Caribbean
Name: Albania, Region: Southern Europe
Name: Andorra, Region: Southern Europe
Name: Netherlands Antilles, Region: Caribbean
Name: United Arab Emirates, Region: Middle East
Name: Argentina, Region: South America
Name: Armenia, Region: Middle East



Retrieving from table in different database...
Name: Oranjestad, Country: Aruba
Name: Kabul, Country: Afghanistan
Name: Qandahar, Country: Afghanistan
Name: Herat, Country: Afghanistan
Name: Mazar-e-Sharif, Country: Afghanistan
Name: Luanda, Country: Angola
Name: Huambo, Country: Angola
Name: Lobito, Country: Angola
Name: Benguela, Country: Angola
Name: Namibe, Country: Angola

阅读https://dev.mysql.com/doc/refman/8.0/en/identifier-qualifiers.html

这是正常的。只要数据库在同一个 MySQL 实例上并且您对该数据库有权限,哪个是您的“默认”数据库并不重要。您可以引用任何 databasename.tablename.

默认数据库类似于shell中的当前工作目录(cwd)。您始终可以通过提供路径来引用另一个目录中的文件。