SQL Introduction Chapter 7, sections 1 & 4 Objectives To understand Oracle s SQL client interface Understand the difference between commands to the interface and SQL language. To understand the Oracle server environment, and how to access that environment. Outline SQL what is it? Basic functions DDL coomands DML commands Basic Queries System Catalog Querying User_Tables Querying All_Tables SQLPlus commands SQLPlus installation SQL meets ideal database language requirements: SQL is relatively easy to learn Basic command set has vocabulary of less than 100 words Nonprocedural a 4 th Generation Language Specifies what you want, not how to retrieve it. American National Standards Institute (ANSI) prescribes a standard SQL Several SQL dialects exist 1 2 SQL coverage fits into two categories: Data definition (DDL) Create database objects and define rights to those objects Data manipulation (DML) To retrieve, insert, update, and delete data. SQL DML Commands SQL DDL Commands 3 4
Listing Table Rows Using the Select Command SELECT Used to list contents of table Syntax: SELECT column, column, FROM user.tablename; SELECT * FROM user.tablename; The column list represents one or more attributes, separated by commas Asterisk can be used as wildcardcharacterto list all attributes Useful to view the data in a table before and again after changes/additions have been made. Examples: SELECT EMPNO, ENAME FROM CS275.EMP; SELECT * FROM CS275.EMP; SELECT DNAME, LOC FROM CS275.DEPT; 5 6 Selecting Rows with Conditional Restrictions Basic Select partial table contents by placing restrictions on rows to be included in output Add conditional restrictions to SELECT statement, using WHERE clause Syntax: SELECT columnlist FROM tablelist [ WHERE conditionlist] ; Partial Listing of Table Contents SELECT <column(s)> FROM <table name> WHERE <conditions>; Example: SELECT ENAME, JOB WHERE DEPTNO = 20; ENAME JOB ---------- --------- SMITH CLERK JONES MANAGER SCOTT ANALYST ADAMS CLERK FORD ANALYST 8 7
The where clause selects rows based a logical comparison operator given two operands. SELECT EMPNO, ENAME WHERE SAL >= 2500; EMPNO ENAME ------- ------- 7566 JONES 7698 BLAKE 7788 SCOTT 7839 KING 7902 FORD 9 10 SELECT DEPTNO, DNAME FROM DEPT WHERE LOC!= 'NEW YORK ; SELECT EMPNO, ENAME, DEPTNO WHERE HIREDATE >= '01-JAN-85 ; DEPTNO DNAME ------- ---------- 20 RESEARCH 30 SALES 40 OPERATIONS EMPNO ENAME DEPTNO -------- ---------- ---------- 7788 SCOTT 20 7876 ADAMS 20 11 12
The user s data dictionary The user s data dictionary Looking at your environment Displays tables for current logon >select table_name from user_tables; >select * from tab; Displays other objects for current logon >select * from user_objects; Looking at a table s data dictionary with a SQLPlus Command sqlplus> describe user.<tablename> Looking at objects and tables in the database where you are not the owner. Displays tables or objects >select table_name from all_tables; >select * from all_objects; To display tables or objects from a specific account where you have read access rights >select table_name from all_tables where owner = CS275 ; 13 14 Oracle Interface - Basic SQLPLUS Review Basic SQLPlus Interface Getting on the Data Base SQLPLUS Programs->Oracle->Appl.Dev.->SQL plus Vista: Run as Administrator Logon: Name/password/database Name: FirstName * Password:L# Database CITORACLE & CITORACLE_W Logging off EXIT * duplicates need to add the first character of the last name MENU System File: Open, Save, Spool Open will set your local directory path Editing Invoking the editor Temp file afied.buf Saving from the editor Options -> Environment Pagesize Linesize These also have command line alternatives 15 16
Basic SQLPLUS Commands Basic SQLPLUS >RUN [/] Executes the sql statement in the buffer >LIST [l] Displays current statement in the buffer >DESCRIBE Displays attribute information for a table >EDIT invokes host operating system text editor >EXIT terminates the client session Scripting SQL from TEXT FILES >GET <filename> Retrieves SQL statement from a file and puts it into the active buffer. >SAVE <filename> Saves the active SQL statement in named file >START <filename> Allows execution of multiple SQL statements from a file >SPOOL <filename> Saves session history in named file 17 18 Working off-site - SQLPlus Summary Installing the SQL Client on your own machine. Must have WinXP, Vista, or Win7, but higher versions than the limited home version. Download the directions & file from our website resource page. Connecting to the Database. You will first need to put the tnsnamesfile into your Oracle home. This file will be given out in class or you may get it in the Lab. Open the SQL Client and logon using the Wireless database connection. For Vista, use Run as an administrator SQL commands can be divided into two overall categories: Data definition language commands Data manipulation language commands The basic SELECT statement Where clause Using the SQLPlus environment Basic SQLplus commands Setting the environment. Creating and using script files 19 20