使用SQL*Plus連接數據庫


About SQL*Plus


SQL*Plus is the primary command-line interface to your Oracle database. You use SQL*Plus to start up and shut down the database, set database initialization parameters, create and manage users, create and alter database objects (such as tables and indexes), insert and update data, run SQL queries, and more.

Before you can submit SQL statements and commands, you must connect to the database. With SQL*Plus, you can connect locally or remotely. Connecting locally means connecting to an Oracle database running on the same computer on which you are running SQL*Plus. Connecting remotely means connecting over a network to an Oracle database that is running on a remote computer. Such a database is referred to as a remote database. The SQL*Plus executable on the local computer is provided by a full Oracle Database installation, an Oracle Client installation, or an Instant Client installation.

See Also:

SQL*Plus User's Guide and Reference

 

Connecting to the Database with SQL*Plus


Oracle Database includes the following components:

  • The Oracle Database instance, which is a collection of processes and memory

  • A set of disk files that contain user data and system data

When you connect with SQL*Plus, you are connecting to the Oracle instance. Each instance has an instance ID, also known as a system ID (SID). Because there can be multiple Oracle instances on a host computer, each with its own set of data files, you must identify the instance to which you want to connect. For a local connection, you identify the instance by setting operating system environment variables. For a remote connection, you identify the instance by specifying a network address and a database service name. For both local and remote connections, you must set environment variables to help the operating system find the SQL*Plus executable and to provide the executable with a path to its support files and scripts. To connect to an Oracle instance with SQL*Plus, therefore, you must complete the following steps:

Step 1: Open a Command Window

 

Step 2: Set Operating System Environment Variables

根據你的平台,您可能必須在啟動SQL * Plus之前設置環境變量,或至少驗證設置是否正確

例如,在大多數平台上,必須設置ORACLE_SID和ORACLE_HOME。另外,建議將PATH環境變量設置為包含ORACLE_HOME / bin目錄。有些平台可能需要額外的環境變量:

  • 在UNIX和Linux平台上,您必須通過輸入操作系統命令來設置環境變量。
  • 在Windows平台上,Oracle Universal Installer(OUI)會自動將值分配給Windows注冊表中的ORACLE_HOME和ORACLE_SID。

對於所有平台,當在具有不同Oracle主目錄的實例之間切換時,必須更改ORACLE_HOME環境變量。如果多個實例共享同一個Oracle主目錄,則在切換實例時必須僅更改ORACLE_SID。

1 setenv ORACLE_SID orcl
2 setenv ORACLE_HOME /u01/app/oracle/product/11.2.0/db_1
3 setenv LD_LIBRARY_PATH $ORACLE_HOME/lib:/usr/lib:/usr/dt/lib:/usr/openwin/lib:/usr/ccs/lib
Example 1-1 Setting Environment Variables in UNIX (C Shell)
1 SET ORACLE_SID=orawin2
Example 1-2 Setting Environment Variables in Windows

Example 1-2假定在注冊表中設置了ORACLE_HOME和ORACLE_SID,但你希望覆蓋ORACLE_SID的注冊表值以連接到其他實例。 在Windows上,您在命令提示符窗口中設置的環境變量值會覆蓋注冊表中的值。

 

Step 3: Start SQL*Plus

 1 sqlplus /nolog 


Step 4: Submit the SQL*Plus CONNECT Statement

the CONNECT statement is as follows:

CONN[ECT] [logon] [AS {SYSOPER | SYSDBA}]

 

The syntax of logon is as follows:

{username | /}[@connect_identifier] [edition={edition_name | DATABASE_DEFAULT}]

下表介紹了CONNECT語句的語法:

/

調用連接請求的外部認證。這種類型的認證中不使用數據庫密碼。

最常見的外部身份驗證形式是操作系統身份驗證,其中數據庫用戶通過使用

特定主機用戶帳戶登錄到主機操作系統進行身份驗證。外部認證也可以通過

Oracle wallet或network service來進行。

參閱:Oracle Database Security Guide

AS {SYSOPER | SYSDBA} 

表示數據庫用戶正在使用SYSOPER或SYSDBA系統特權進行連接。只有

某些預定義的管理用戶或已添加到密碼文件的用戶才可以使用這些權限進行連接。

參閱:Administrative Privileges    Using Operating System Authentication

username  

一個有效的數據庫用戶名。數據庫通過將用戶名與數據字典匹配並提示輸入用戶密碼

來驗證連接請求

connect_identifier (1)  一個Oracle Net連接標識符,用於遠程連接。確切的語法取決於Oracle Net配置。

如果省略,SQL * Plus將嘗試連接到本地實例。 常用連接標識符是網絡服務名稱。
這是Oracle Net連接描述符(網絡地址和數據庫服務名稱)的別名。別名通常在本
地計算機上的tnsnames.ora文件中解析,但可以用其他方式解析

connect_identifier (2)

 作為替代,連接標識符可以使用簡單連接語法。輕松連接為遠程數據庫提供開箱即用

的TCP / IP連接,而無需在客戶端(本地)計算機上配置Oracle Net Services。 連

接標識符的簡單連接語法如下(必須包含括起來的雙引號):

host[:port][/service_name][:server][/instance_name]

 

  • server是服務處理程序的類型。可接受的值是專用,共享和合並的。如果省略,

          則默認類型的服務器由偵聽器選擇:共享服務器(如果已配置),否則為專用

          服務器

參閱:Oracle Database Net Services Administrator's Guide

edition={edition_name|DATABASE_DEFAULT}

 指定新數據庫會話啟動的版本。如果指定一個版本,它必須存在,並且您必須具有USE

特權。如果未指定此子句,則會話將使用數據庫默認版本

參閱:Oracle Database Advanced Application Developer's Guide

 

參考資料

https://docs.oracle.com/cd/E11882_01/server.112/e25494/dba.htm#ADMIN12472


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM