目录
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教程
1587
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 04, 2025 am 11:19 AM

要优化SQL中ORDERBY的性能,首先要理解其执行机制并合理利用索引和查询结构。当排序字段无索引时,数据库会触发“filesort”,消耗大量资源;因此应避免对大表直接排序,并通过WHERE条件减少排序数据量。其次,为排序字段建立匹配顺序的索引,可大幅加速查询,如在MySQL8.0 创建倒序索引提升效率。此外,深分页(如LIMIT1000,10)应改用基于索引的游标分页(如WHEREid>12345),以跳过无效扫描。最后,结合缓存、异步聚合等手段也可进一步优化大数据集场景下的排序性能。

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

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

See all articles