目錄
What is a Database Link?
Creating a Database Link in Oracle
2. Example
Types of Database Links
Prerequisites
Using a Full Connection String (No TNS)
Testing the Link
Dropping a Database Link
Notes for Other Databases
首頁 資料庫 SQL 如何在SQL中創建數據庫鏈接?

如何在SQL中創建數據庫鏈接?

Aug 08, 2025 pm 05:13 PM

在Oracle中創建數據庫鏈接的步驟如下:1. 使用CREATE DATABASE LINK語句定義鏈接名稱、遠程用戶名密碼及連接字符串;2. 確保網絡可達並配置TNS或使用完整連接描述符;3. 具備CREATE DATABASE LINK權限;4. 可通過SELECT * FROM dual@link_name測試連接;5. 用DROP DATABASE LINK刪除鏈接。該操作支持跨數據庫訪問對象,需遵循安全規範。

How to create a database link in SQL?

Creating a database link in SQL allows you to access objects (like tables or views) from a remote database. The exact syntax and support depend on the database system you're using. Below is a guide focused on Oracle , which has robust support for database links. Other systems like PostgreSQL also support similar features, but Oracle is the most common context for this operation.

How to create a database link in SQL?

A database link (DB Link) is a schema object that enables you to access tables, views, or run queries on a remote database as if you were connected directly to it.


1. Basic Syntax

 CREATE DATABASE LINK link_name
CONNECT TO remote_username IDENTIFIED BY remote_password
USING 'connect_string';
  • link_name : The name you assign to the database link.
  • remote_username/password : Credentials for the remote database user.
  • connect_string : The TNS (Transparent Network Substrate) name or full connection descriptor for the remote database.

2. Example

Suppose you want to create a link to a remote database where:

How to create a database link in SQL?
  • Remote user: scott
  • Password: tiger
  • TNS alias: REMOTE_DB
 CREATE DATABASE LINK remote_db_link
CONNECT TO scott IDENTIFIED BY tiger
USING 'REMOTE_DB';

After creating the link, you can query remote tables like:

 SELECT * FROM employees@remote_db_link;

  • Private : Owned by a specific user and accessible only to that user.
  • Public : Accessible to all users in the local database.
  • Shared : Allows multiple sessions to reuse the same connection.

To create a public database link, add the PUBLIC keyword:

How to create a database link in SQL?
 CREATE PUBLIC DATABASE LINK remote_public_link
CONNECT TO scott IDENTIFIED BY tiger
USING 'REMOTE_DB';

Prerequisites

Before creating a database link, ensure:

  • The remote database is accessible over the network.
  • The TNS entry ( REMOTE_DB in the example) is configured in tnsnames.ora , or use a full connection string.
  • The user has CREATE DATABASE LINK privilege.

You can grant the privilege like this:

 GRANT CREATE DATABASE LINK TO your_user;

Using a Full Connection String (No TNS)

If you don't want to use a TNS name, you can embed the connection details:

 CREATE DATABASE LINK remote_db_link
CONNECT TO scott IDENTIFIED BY tiger
USING '(DESCRIPTION=
    (ADDRESS=(PROTOCOL=TCP)(HOST=remote_host)(PORT=1521))
    (CONNECT_DATA=(SERVICE_NAME=orcl)))';

After creation, test it with a simple query:

 SELECT * FROM dual@remote_db_link;

If it returns a row, the link is working.


To remove a link:

 DROP DATABASE LINK remote_db_link;

-- For public link
DROP PUBLIC DATABASE LINK remote_public_link;

Notes for Other Databases

  • PostgreSQL : Uses postgres_fdw (Foreign Data Wrapper) to link to other PostgreSQL databases.
  • SQL Server : Uses linked servers via sp_addlinkedserver .
  • MySQL : No native database link support; use FEDERATED engine (limited) or application-level joins.

In Oracle, database links are powerful but should be used carefully—especially with stored credentials. Always follow security best practices like restricting privileges and auditing usage.

Basically, it's about connecting across databases securely and efficiently.

以上是如何在SQL中創建數據庫鏈接?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP教程
1589
276
SQL無服務器計算選項 SQL無服務器計算選項 Jul 27, 2025 am 03:07 AM

SQLServer本身不支持無服務器架構,但云平台提供了類似方案。 1.Azure的ServerlessSQL池可直接查詢DataLake文件,按資源消耗計費;2.AzureFunctions結合CosmosDB或BlobStorage可實現輕量SQL處理;3.AWSAthena支持S3數據的標準SQL查詢,按掃描數據量計費;4.GoogleBigQuery通過FederatedQuery接近Serverless理念;5.若必須使用SQLServer功能,可選AzureSQLDatabase的無服

您如何計算SQL中兩個日期之間的差異? 您如何計算SQL中兩個日期之間的差異? Aug 02, 2025 pm 01:29 PM

要計算兩個日期之間的差值,需根據數據庫類型選擇相應函數:1.在MySQL中使用DATEDIFF()計算天數差,或TIMESTAMPDIFF()指定單位如HOUR、MINUTE;2.在SQLServer中使用DATEDIFF(date_part,start_date,end_date)並指定單位;3.在PostgreSQL中通過直接相減得到天數差,或使用EXTRACT(DAYFROMAGE(...))獲取更精確間隔;4.在SQLite中利用julianday()函數相減得出天數差;始終注意日期順序

掌握商業智能分析的SQL 掌握商業智能分析的SQL Jul 26, 2025 am 07:53 AM

tomastersqlforbianalytics,startByudeSandingBidAtatrasturesLikeFactandDimensionTables,thenusestrategicicaggregationswithgroupbybyandhaving,loveragedateFounctionsFormionsFortionsFortionsFortionsFortimeNalysis,and wertriteClean,andWealableAbleableSublequeries.firstable Quirst,graspDimensimentionalModeLingtojoJoii

SQL中的BLOB和CLOB數據類型是什麼? SQL中的BLOB和CLOB數據類型是什麼? Aug 07, 2025 pm 04:22 PM

BLOBstoresbinarydatalikeimages,audio,orPDFsasrawbyteswithoutcharacterencoding,whileCLOBstoreslargetextsuchasarticlesorJSONusingcharacterencodinglikeUTF-8andsupportsstringoperations;2.Bothcanhandleuptogigabytesofdatadependingonthedatabase,butperforman

用於多維聚合的SQL立方體和匯總 用於多維聚合的SQL立方體和匯總 Jul 29, 2025 am 12:28 AM

CUBE用於生成所有維度組合的聚合,適用於交叉分析;ROLLUP按層級逐步匯總,適合有層級關係的數據。 CUBE按Region、Product、Quarter生成8種組合的總計,而ROLLUP按Year、Month、Day逐層上卷生成年、月、日等層級匯總。 CUBE適合查看所有交叉維度結果,ROLLUP適合展示層級結構。使用時注意CUBE可能導致結果集爆炸,ROLLUP依賴字段順序。可通過GROUPING()函數識別匯總行,用COALESCE命名總計行提升可讀性。

SQL中的骨料功能是什麼? SQL中的骨料功能是什麼? Jul 26, 2025 am 05:43 AM

SQL的聚合函數用於從多行數據中計算出單個匯總值,常見函數包括SUM()求和、AVG()求平均值、COUNT()計數、MAX()找最大值、MIN()找最小值。這些函數常與GROUPBY配合使用,對分組後的數據進行統計。例如,用SUM(units_sold)可得總銷量,加GROUPBYproduct_id可按產品統計;COUNT()統計所有記錄,COUNT(sale_date)則忽略空值。使用時需注意:NULL值通常被忽略,除COUNT()外;多函數混用可能產生意外結果;過濾分組數據應使用HAVI

與SQL中的運算符相比,運算符的存在如何? 與SQL中的運算符相比,運算符的存在如何? Aug 05, 2025 pm 01:08 PM

useexists forexistenceChecks,尤其是WithlargeorCorrecoredsubqueries and whennullvaluesarepresent,AsitStopsatthefirstthefirstmatchandhandhandlesnullssafely; usiseInformembersHipshipsagainstsmall,已知

通過查詢性能優化SQL訂單 通過查詢性能優化SQL訂單 Aug 04, 2025 am 11:19 AM

要優化SQL中ORDERBY的性能,首先要理解其執行機制並合理利用索引和查詢結構。當排序字段無索引時,數據庫會觸發“filesort”,消耗大量資源;因此應避免對大表直接排序,並通過WHERE條件減少排序數據量。其次,為排序字段建立匹配順序的索引,可大幅加速查詢,如在MySQL8.0 創建倒序索引提升效率。此外,深分頁(如LIMIT1000,10)應改用基於索引的游標分頁(如WHEREid>12345),以跳過無效掃描。最後,結合緩存、異步聚合等手段也可進一步優化大數據集場景下的排序性能。

See all articles