Mysql
連接到同一 DBMS 上的“外部”數據庫
看到 JDBC 允許我在同一個 DBMS 上透明地從“外部”數據庫中的表中選擇表,我感到有些驚訝,我對此具有必要的權限,而無需顯式連接到外部數據庫。這是它應該如何與 MySQL 一起使用,還是只是 JDBC 的怪癖?
詳細資訊:
我在 DBMS 上創建了兩個數據庫:stkovrflo_1 和 stkovrflo_2。我從MySQL World 數據庫中填充了這些數據庫中的表。
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_2.City
的連接來選擇表中的條目。stkovrflo_1
我對這兩個數據庫都有 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
MySQL 所謂的數據庫實際上是一個模式。他們在文件中說了這麼多:
CREATE SCHEMA
是同義詞CREATE DATABASE
模式是數據庫對象(表等)的邏輯分組;可以在單個 MySQL 實例中定義多個模式,並且您使用相同的連接屬性(“數據庫”/模式名稱除外)來訪問它們,因此它們實際上彼此之間並不是很“陌生”。
在大多數其他 DBMS中,可以在數據庫中定義多個模式。在那些您連接到數據庫並訪問其模式中的對象的情況下。MySQL 將這兩個術語混為一談。
在數據庫術語中,外部(或聯合)數據庫或表是駐留在不同 DBMS 實例中的數據庫或表。