As well as installing the InstantUniversity database, you'll need to connect to it in order to execute SQL statements against it. If you're using the SQL editing tools that you looked at previously, this may be done for you. However, if you're using a command-line interpreter or a less sophisticated graphical interface, you'll need to be explicit about what database you're using and provide the correct security credentials to do so, based on the security setting in your Relational Database Management System (RDBMS). You may also need to supply these details should you want to reconnect to a different database.
Three databases—Oracle, DB2, and SQL Server—support the SQL CONNECT statement in some form, which allows you to connect remotely to a specific database. The basic syntax for this is as follows:
CONNECT TO [database] USER [user_name];
The syntax for connecting to a database using SQL*Plus is as follows:
CONNECT username@servicename/password [AS SYSOPER | AS SYSDBA];
If Oracle can't resolve the servicename, you'll receive an ORA-12154 error:
Again, if this happens, you can supply a complete TNS descriptor instead. For example:
CONNECT InstSql@(DESCRIPTION=(ADDRESS_LIST=( ADDRESS=(PROTOCOL=TCP)(HOST=servername)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=servicename)))/password;
If you need to connect with SYSDBA or SYSOPER privileges rather than as a normal user, you can specify AS SYSDBA or AS SYSOPER.
You can close the connection in SQL*Plus by executing the DISCONNECT statement.
The DB2 version of CONNECT follows the standard basic syntax:
CONNECT TO database [USER user [USING password]];
You can connect using either the actual name of the database or its alias (if this is different). If no password is supplied with the USING clause, you'll be prompted for one when you execute the statement.
Finally, you can close the connection using the command CONNECT RESET.
SQL Server supports CONNECT TO only in Embedded SQL (ESQL) statements. ESQL statements are Transact-SQL statements embedded into C programs. However, Microsoft won't support this Application Programming Interface (API) beyond SQL Server 2000, so it's probably advisable to use a data-access technology such as ODBC or OLE DB in preference to ESQL.
The ESQL syntax for CONNECT TO is as follows:
EXEC SQL CONNECT TO database USER user
Here database is the name of the database to connect to, which will be InstantUniversity or will be MyServer.InstantUniversity if you're accessing the database remotely. The user is the name of the user account to connect with, which may include a password in the format username.password.
You can issue the DISCONNECT command to close the connection; again, you can use this only from ESQL.