Presentation is loading. Please wait.

Presentation is loading. Please wait.

ORACLE SQL Developer & SQLPLUS Statements

Similar presentations


Presentation on theme: "ORACLE SQL Developer & SQLPLUS Statements"— Presentation transcript:

1 ORACLE SQL Developer & SQLPLUS Statements
Schedule: Timing Topic 40 minutes Lecture 25 minutes Practice 65 minutes Total

2 SQL Dev and SQL*Plus Interaction
SQL Statements Buffer SQL Statements Server SQL*Plus or SQL Dev SQL*Plus Commands Query Results SQL and SQL*Plus SQL is a command language for communication with the Oracle Server from any tool or application. Oracle SQL contains many extensions. When you enter a SQL statement, it is stored in a part of memory called the SQL buffer and remains there until you enter a new statement. SQL*Plus is an Oracle tool that recognizes and submits SQL statements to the Oracle Server for execution and contains its own command language. Features of SQL Can be used by a range of users, including those with little or no programming experience Is a nonprocedural language Reduces the amount of time required for creating and maintaining systems Is an English-like language Features of SQL*Plus Accepts ad hoc entry of statements Accepts SQL input from files Provides a line editor for modifying SQL statements Controls environmental settings Formats query results into a basic report Accesses local and remote databases Formatted Report

3 SQL Statements Versus SQL*Plus Commands
A language ANSI standard Keyword cannot be abbreviated Statements manipulate data and table definitions in the database SQL*Plus An environment Oracle proprietary Keywords can be abbreviated Commands do not allow manipulation of values in the database SQL and SQL*Plus (continued) The following table compares SQL and SQL*Plus: SQL statements SQL buffer SQL*Plus commands SQL*Plus buffer

4 SQL Developer Optional software with GUI Interface
Most commands not needed – Point and Click REQUIRES connection to be made to user account/schema/db container where database contents are built and data is stored.

5 ORACLE Data Types Character: char(n) - fixed length character string with user specified length n (n <=255) Oracle = 1 – 2,000 characters varchar(n) - variable length character strings, with user-specified maximum length n. Oracle = VARCHAR2 – up to 4,000 characters Numeric: NUMBER(l,d) - fixed point number, with user-specified precision of digits, with d digits to the right of decimal point. If no digits specified, Oracle will assume NO decimal points in number DECIMAL(l,d) INTEGER – Up to 11 digits. SMALLINT – integer values up to six digits. Date: Oracle date: DD-MON-YY example: 09-Oct-03

6 SQL Commands Creating a table
Create Table Dept ( DEPT_NO NUMBER(2) , DNAME VARCHAR2(14), LOC VARCHAR2(13), PRIMARY KEY (DEPT_NO)); OR (to create an alias constraint PK-DEP PRIMARY KEY (DEPT_NO)); *** an Alias is a shortcut name for the same field

7 SQL Commands Deleting a Table
DROP TABLE tablename; This will delete the table (and it’s contents) as long as it is not being referenced (a foreign key relationship) from another table.

8 Displaying Table Structure
Use the SQL*Plus DESCRIBE command to display the structure of a table. DESC[RIBE] tablename SQL> DESCRIBE dept Name Null? Type DEPTNO NOT NULL NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13) Displaying Table Structure In SQL*Plus, you can display the structure of a table using the DESCRIBE command. The result of the command is to see the column names and datatypes as well as whether a column must contain data. In the syntax: tablename is the name of any existing table, view, or synonym accessible to the user

9 SQL Developer Displaying Table Structure
Default – Viewing Metadata Displaying Table Structure (continued) The example on the slide displays the information about the structure of the DEPT table. In the result: Null? indicates whether a column must contain data; NOT NULL indicates that a column must contain data Type displays the datatype for a column The datatypes are described in the following table: Instructor Note Inform students that the column sequence in DESCRIBE tablename is the same as that in SELECT * FROM tablename . The order in which the columns are displayed is determined when the table is created. To view data in table select – Data Tab

10 SQL Commands Adding a Foreign Key
Create Table Employee ( EMP_NO INTEGER, FNAME VARCHAR2(25), LNAME VARCHAR2(25), DEP_NO NUMBER(2), PRIMARY KEY (Emp_NO), FOREIGN KEY (Dep_NO) references DEPT(DEP_NO); OR (to create an alias for FK) constraint FK_DEPNO FOREIGN KEY (DEP_NO) references DEPT(DEPT_NO);

11 Adding PK and FK Constraints after Table is Built
USE ALTER Command Primary Key ALTER TABLE employee ADD CONSTRAINT pk_employee PRIMARY KEY (emp_id); Foreign Key ALTER TABLE employee ADD CONSTRAINT fk_dep FOREIGN KEY (dept_no) REFERENCES dept(dept_no);

12 Inserting Data into Table
BASIC SYNTAX: INSERT INTO <table name> VALUES(attribute1 value, attribute 2 value, …); Example using data to insert entire records: INSERT INTO VENDOR VALUES(21225, ‘Bryson, Inc’, ‘Smithson’, ‘615’, ‘ ’, ‘TN’, ‘Y’); INSERT INTO PRODUCT VALUES( ‘11QER/31’, ‘Power painter, 15 psi., 3-nozzle’, ’02-Jul-99’, 8, 5, , 00.00, 21225); Character and Date data: MUST be in quotes Numeric Data: NO quotes All Fields must have comma after it except for last one

13 Data Constraints (Rules)
When adding data: ALL PK data must be unique for each row and NO nulls. ALL FK data matches related parent tables PK

14 Viewing Data in a Table SELECT * FROM TABLENAME; This command will allow you to validate that all inserted data can be viewed. With SQL Developer simply hitting the Data tab with do the same thing without the SELECT statement.

15 SQLPlus Running a Script File (contiguous SQL statements)
symbol before the file will “run” the file within SQLPlus environment A:\Ora\createtbl.sql (or .txt) Spooling Results to a File Before running your script file, you can spool the results to a “audit” file This file contains all output until it is turned off This file will be saved and can be reviewed SQL> Spool a:\spool\Lab1.txt a:\ora\createtbl.sql SQL> Spool off SQL> exit

16 SQL Developer Running a Script File (contiguous SQL statements)
Store your statements in a .sql text file then copy/paste into the user window

17 Summary Use SQL*Plus or SQLDeveloper as an environment to:
Connect to your database Execute SQL statements Edit SQL statements View contents of database Administer your Oracle DBMS SELECT Statement In this lesson, you have learned about retrieving data from a database table with the SELECT statement. SQL*Plus SQL*Plus is an execution environment that you can use to send SQL statements to the database server and to edit and save SQL statements. Statements can be executed from the SQL prompt or from a script file. SELECT [DISTINCT] {*,column [alias],...} FROM table;


Download ppt "ORACLE SQL Developer & SQLPLUS Statements"

Similar presentations


Ads by Google