Sunteți pe pagina 1din 52

Week 7

Housekeeping:
Attendance sheet Expectations next few weeks SDLC extended doc posted in course documents Log-in info

ORACLE
Database commands Class work/Lab Quiz
Oracle 10g: SQL 1

Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL

SELECT Statement Syntax


SELECT statements are used to retrieve data from the database Syntax gives the basic structure, or rules, for a command

Oracle 10g: SQL

SELECT Statement Syntax (continued)


Optional clauses and keywords are shown in brackets

Oracle 10g: SQL

SELECT Statement Syntax (continued)


SELECT and FROM clauses are required SELECT clause identifies column(s) FROM clause identifies table(s) Each clause begins with a keyword

Oracle 10g: SQL

SELECT Statement Syntax (continued)


? Write a select statement to extract all data from a table named Customers

Oracle 10g: SQL

Selecting All Data in a Table


Substitute an asterisk for the column names in a SELECT clause

Oracle 10g: SQL

Selecting One Column from a Table


? Write a select statement to extract data from one column named title from the table books

Oracle 10g: SQL

Selecting One Column from a Table


Enter column name in SELECT clause

Oracle 10g: SQL

Selecting Multiple Columns from a Table


? Write a select statement to pull data from the columns title and pubdate from a table named books

Oracle 10g: SQL

10

Selecting Multiple Columns from a Table


Separate column names with a comma

Oracle 10g: SQL

11

Operations Within the SELECT Statement


Column alias can be used for column headings Perform arithmetic operations Suppress duplicates Concatenate data

Oracle 10g: SQL

12

Using Column Aliases


List the alias after the column heading AS keyword is optional Enclose in double quotation marks:
If it contains blank space(s) If it contains special symbol(s) To retain case

Oracle 10g: SQL

13

Column Aliases
? Write a select statement to pull data from the title column using the alias title of books and from the category column.

Oracle 10g: SQL

14

Column Alias Example

Oracle 10g: SQL

15

Using Arithmetic Operations


Arithmetic operations:
Executed left to right Multiplication and division are solved first Addition and subtraction are solved last Override order with parentheses

Oracle 10g: SQL

16

Arithmetic Operation with Column Alias


? Write a Select statement to extract the title column and calculate the value of columns retail less cost and store in an column profit from the table books

Oracle 10g: SQL

17

Example Arithmetic Operation with Column Alias

Oracle 10g: SQL

18

Using DISTINCT and UNIQUE


Enter DISTINCT or UNIQUE after SELECT keyword to suppress duplicates

Oracle 10g: SQL

19

Using Concatenation
You can combine data with a string literal Use the concatenation operator, || It allows the use of column aliases

Oracle 10g: SQL

20

Concatenation Example

Note: incomplete results shown

Oracle 10g: SQL

21

Inserting a Line Break


A line break code of CHR(10) can be used to format output on multiple lines The output must be formatted as text output in SQL*Plus for the line break command to operate properly

Oracle 10g: SQL

22

Chapter 3 Table Creation and Management Oracle 10g: SQL

Database Table
A database object Stores data for the database Consists of columns and rows Created and modified through data definition language (DDL) commands

Oracle 10g: SQL

24

Table Design
Table and column names:
Can contain a maximum 30 characters - no blank spaces Must begin with a letter Can contain numbers, underscore (_), and number sign (#) Must be unique No reserved words are allowed
Oracle 10g: SQL 25

Table Design (continued)

Oracle 10g: SQL

26

Table Creation

Oracle 10g: SQL

27

Defining Columns
Column definition list must be enclosed in parentheses Datatype must be specified for each column Maximum of 1,000 columns

Oracle 10g: SQL

28

Create Table Command


? Table name acctmanager Columns
Amid char length 4 Amfirst variable length 12 Amlast variable length 12 Amedate type date default sysdate Region char length 2
29

Oracle 10g: SQL

CREATE TABLE Command Example

Oracle 10g: SQL

30

Viewing List of Tables: USER_TABLES


A data dictionary is a typical component of a DBMS that maintains information about database objects You can query the data dictionary to verify all the tables that exist in your schema The USER_TABLES data dictionary object maintains information regarding all your tables

Oracle 10g: SQL

31

Viewing Table Structures: DESCRIBE


DESCRIBE displays the structure of a specified table

Oracle 10g: SQL

32

Table Creation Through Subqueries


You can use subqueries to retrieve data from an existing table Requires use of AS keyword New column names can be assigned

Oracle 10g: SQL

33

CREATE TABLEAS

Oracle 10g: SQL

34

Create table AS
? Table name to be created cust_mkt Base this table on the table customers and the following columns from the customers table: customer #, city, state, zip and referred. Use a subquery.

Oracle 10g: SQL

35

CREATE TABLEAS Command Example

Oracle 10g: SQL

36

Modifying Existing Tables


Accomplished through the ALTER TABLE command Use an ADD clause to add a column Use a MODIFY clause to change a column Use a DROP COLUMN to drop a column

Oracle 10g: SQL

37

ALTER TABLE Command Syntax

Oracle 10g: SQL

38

Alter Table
? Alter the table named publisher Add the column ext of data type number length 4

Oracle 10g: SQL

39

ALTER TABLEADD Command

Oracle 10g: SQL

40

ALTER TABLEMODIFY Command

Oracle 10g: SQL

41

Modification Guidelines
Column must be as wide as the data it already contains If a NUMBER column already contains data, size cannot be decreased Adding or changing default data does not affect existing data

Oracle 10g: SQL

42

ALTER TABLEDROP COLUMN Command


Can only reference one column per execution Deletion is permanent Cannot delete last remaining column in a table

Oracle 10g: SQL

43

ALTER TABLESET UNUSED Command


Once marked for deletion, a column cannot be restored Storage space is freed at a later time

Oracle 10g: SQL

44

ALTER TABLEDROP UNUSED Command


Frees up storage space from columns previously marked as unused

Oracle 10g: SQL

45

Renaming a Table
RENAMETO is used to rename a table the old name is no longer valid

Oracle 10g: SQL

46

Truncating a Table
TRUNCATE TABLE command - rows are deleted Structure of table remains

Oracle 10g: SQL

47

Deleting a Table
DROP TABLE command - table structure and contents are deleted

Oracle 10g: SQL

48

DROP TABLE Without Purge Option


Oracle 10g introduced a recycle bin Dropped tables can be recovered from the recycle bin

Oracle 10g: SQL

49

FLASHBACK Command
The FLASHBACK command recovers a table from the recycle bin

Oracle 10g: SQL

50

Use PURGE to Remove a Table from the Recycle Bin

Oracle 10g: SQL

51

PURGE Option Available for DROP TABLE Command


Using the PURGE option will permanently remove a table from the database The table will not be copied into the recycle bin

Oracle 10g: SQL

52

S-ar putea să vă placă și