cs 340 lab manualpsu-cs340.yolasite.com/resources/cs 340 lab manual.docx · web viewcs 340 lab...
TRANSCRIPT
CS 340 Lab ManualOracle & SQL
Prince Sultan UniversityComputer and Information Sciences Department
TABLE OF CONTENTSIntroduction to Oracle........................................................................................................... 1
Oracle Utilities and Development Tools......................................................................1
SQL*Plus..................................................................................................................................... 1
Introduction..................................................................................................................1
Getting Started.............................................................................................................. 1
Getting Help...................................................................................................................1
Entering and Executing SQL Statements.....................................................................1
Saving Buffer Contents.................................................................................................1
Retrieving and Executing SQL Files.............................................................................1
Describing Tables..........................................................................................................1
Quitting..........................................................................................................................1
Structured Query Language (SQL).....................................................................................1
Types of SQL Statements..............................................................................................1
Syntax............................................................................................................................ 1
Oracle Express Download..................................................................................................... 1
INTRODUCTION TO ORACLE
ORACLE UTILITIES AND DEVELOPMENT TOOLS
SQL*Plus:
• For creating and testing command-line SQL queries and executing PL/SQL procedural programs.
Developer Suite:
• For developing database applications.
• Two developer tools:
• Forms Builder: for creating custom user applications.
• Report Builder: for creating reports for displaying, printing, and distributing summary data.
Enterprise Manager:
• For performing database administration tasks such as creating new user accounts and configuring how the DBMS stores and manages data.
1
SQL*PLUS
INTRODUCTION
SQL*Plus is essentially an interactive command editor which enables the user to interrogate the database, e.g.:
CREATE, ALTER, and DROP tables.
INSERT, UPDATE, and DELETE rows.
Specify primary keys, foreign keys, and indexes.
GRANT and REVOKE access privileges.
In order to perform these functions, the user enters a series of SQL statements.
2
GETTING STARTED
Open SQL*Plus, enter appropriate name, password, & host string
If the previous information have been entered correctly, SQL*Plus displays its own command line prompt; by default, SQL>.
3
scottpsustddata
Any SQL command can be entered here
GETTING HELP
Full on-line help is available while using SQL*Plus.
To get help:
• Select the help menu option, or
• Enter help at the SQL*Plus prompt
ENTERING AND EXECUTING SQL STATEMENTS
SQL statements entered at the SQL*Plus prompt are held in a multi-line command buffer. Non-SQL statements (such as help) may be entered without changing the contents of the SQL buffer.
The buffer retains the contents of the latest SQL statement until a new SQL statement is entered or the buffer is cleared using the clear buffer command. The contents of the SQL buffer can be edited using several commands. (see SQL part)
An SQL statement is executed immediately if the last line of the statement is terminated with a semi-colon. Additionally, the statement currently in the SQL buffer can be executed by entering:
A / (on its own), or The keyword run
at the SQL*Plus prompt.
SAVING BUFFER CONTENTS
The contents of the SQL buffer can be saved in an operating system file by entering the command:
save filename[.ext] [replace]
• If the .ext is omitted, the file will be given the extension .sql.
• The optional argument replace allows an existing file to be overwritten.
• Using the optional argument append instead of replace allows the contents of the SQL buffer to be appended to an existing file.
4
RETRIEVING AND EXECUTING SQL FILES
Either of the following commands can be issued at the SQL*Plus prompt:
start filename[.ext]
or
@filename[.ext]
In either case the specified file is executed and the final SQL statement in the file is retained in the buffer.
To load an SQL file into the buffer without executing it the following command may be issued:
get filename[.ext]
Using a Text Editor
To overcome the limitations of the SQL buffer, a host operating system text editor or notepad may be used instead. The main benefit of using a text editor (apart from its editing capabilities) is the ability to create files containing multiple SQL statements.
Each statement (except optionally the last) must be terminated by a semi-colon.
5
DESCRIBING TABLES
The structure of any table within the database may be displayed by entering the command describe at the SQL*Plus prompt.
Syntax:
describe tablename
The names and types of each column within the specified table will be displayed.
QUITTING
To quit SQL*Plus, either one of the following commands can be used:
• quit, or
• exit.
Both commands automatically commit any outstanding changes to the database.
6
STRUCTURED QUERY LANGUAGE (SQL)
TYPES OF SQL STATEMENTS
• Data Definition Language (DDL):
• Commands that define a database.
• E.g. CREATE, ALTER, DROP, ...etc.
• Data manipulation language (DML):
• Commands that maintain and query a database.
• E.g. SELECT, INSERT, UPDATE, DELETE.
• Data Control Language (DCL):
• Commands that control a database, including administering privileges and committing data.
• E.g. CONNECT, GRANT, REVOKE, ...etc.
7
SYNTAX
Data Types:
• CHAR(n)
• VARCHAR2(n)
• NUMBER
• NUMBER(n,d)
• NUMBER(n)
• DATE
Column Level Constraints:
• NOT NULL
• UNIQUE
• PRIMARY KEY
• REFERENCES table (key)
• CHECK (condition)
Table Level Constraints:
• UNIQUE (column-list)
• [CONSTRAINT name] PRIMARY KEY (column-list)
• [CONSTRAINT name] FOREIGN KEY (column-list) REFERENCES table (column-list) [ON DELETE CASCADE] [ON DELETE SET NULL]
• CHECK (condition)
8
CREATE TABLE Syntax:
CREATE TABLE name (
column-1 TYPE [DEFAULT value] [constraints],
column-2 TYPE [DEFAULT value] [constraints],
:
column-n TYPE [DEFAULT value] [constraints],
[table-constraints])
ALTER TABLE Syntax:
ALTER TABLE name ADD …
ALTER TABLE name MODIFY …
ALTER TABLE name DROP …
DROP TABLE Syntax:
DROP TABLE name [CASCADE CONSTRAINTS]
SELECT Statement Syntax:
SELECT [DISTINCT] attribute-list
FROM table-list
WHERE condition(s)
GROUP BY grouping-attributes
HAVING group-condition(s)
ORDER BY attribute-list
9
10
INSERT INTO Syntax:
INSERT INTO table [(attribute-list)]
VALUES (value-list)
INSERT INTO table [(attribute-list)]
SELECT statement
DELETE FROM Syntax:
DELETE FROM table
[WHERE conditions]
UPDATE Syntax:
UPDATE table
SET attribute-1 = value-1,
:
attribute-n = value-n
[WHERE conditions]
UPDATE table
SET (attribute-list)
=
(SELECT statement)
[WHERE conditions]
11
CREATE VIEW Syntax:
CREATE VIEW name AS
SELECT statement
ORACLE EXPRESS DOWNLOAD
Oracle Database 10g Express Edition (XE) is an entry-level, small-footprint (150MB) database based on the Oracle Database 10g.
• Free to develop, deploy, and distribute,• Fast to download;• Simple to administer.
Oracle Database XE is a great starter database for students who need a free database for their curriculum With Oracle Database XE.
To download, go to:
http://www.oracle.com/technetwork/database/express-edition/overview/index.html
12
13
Then you’ll need to register to be able to download Oracle Database XE.
14