Vous êtes sur la page 1sur 10

Database and Information Retrieval

ICT118 Lecture 3 SQL command types Syntax for querying a table with the SELECT statement

Database Administration
Creating and maintaining tables according to the database design (schema) Creating and controlling users Applying constraints to tables to implement business rules Using SQL statements in the Database Management System ours is Oracle 11g Interfacing the database to application programs and middleware
22 July, 2010 ICT118 Database and Information Retrieval, Sem 2, 2010 3. 2

Architecture
multiclientTypically a multi-tier client-server architecture
Application Client Application Client Application Client Application Client

Database Server

Middleware Server

Legacy Legacy Applications

Legacy Files

Web Clients DBMS Web Server

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 3

Database Management System


The DBMS uses SQL language subsets to manage different functions DDL Define, create, alter the data structures DML Insert, find, modify, report data values DCL Create and manage user rights TCL Control transaction behaviour, timing

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 4

Database Administrator (DBA)


Writes DDL and DCL within the DBMS application to create the database according to the database schema Configures the DBMS for middleware and web server interfacing or for direct application interfacing environment dependent

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 5

Application programmers
Write DML and TCL statements to access and populate the data structures needed by application programs During maintenance phase use DDL to create and alter tables Embed the statements in application program code Work with DBA to optimise TCL for security and performance
22 July, 2010 ICT118 Database and Information Retrieval, Sem 2, 2010 3. 6

Oracle 11g: SQL


Casteel, Chapter 2 Basic SQL SELECT Statements The following slides contain material from the Cengage web site for Casteel, Oracle 11g SQL, 2010

Objectives
Create the initial database Identify keywords, mandatory clauses, and optional clauses in a SELECT statement Select and view all columns of a table Select and view one column of a table Display multiple columns of a table

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 8

Objectives (continued)
Use a column alias to clarify the contents of a particular column Perform basic arithmetic operations in the SELECT clause Remove duplicate lists using either the DISTINCT or UNIQUE keyword Use concatenation to combine fields, literals, and other data
22 July, 2010 ICT118 Database and Information Retrieval, Sem 2, 2010 3. 9

Using the JustLee Database


Sign Oracles Student User license form to permit use of the DBMS for teaching Obtain your Oracle username and password from the tutor Install SQL Developer (if not already installed) Connect to remote Oracle 11g server Verify table contents using the DESCRIBE command
22 July, 2010 ICT118 Database and Information Retrieval, Sem 2, 2010 3. 10

SELECT Statement Syntax


SELECT statements are used to retrieve data from the database A SELECT statement is referred to as a query Syntax gives the basic structure, or rules, for a command Optional clauses and keywords are shown in brackets

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 11

SELECT Statement Syntax (continued)

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 12

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

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 13

Selecting One Column


Enter the column name in SELECT clause

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 14

Selecting Multiple Columns from a Table


Separate column names with a comma

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 15

Selecting All Data in a Table


Substitute an asterisk for the column names in a SELECT clause to retrieve all columns

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 16

Operations within the SELECT Statement


Give an alias name to column headings Perform arithmetic operations Suppress duplicates Concatenate data

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 17

Using Column Aliases


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

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 18

Column Alias Example

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 19

Using Arithmetic Operations


Arithmetic operations
Executed left to right Multiplication and division are solved first Addition and subtraction are solved next Override the solving sequence with parentheses

Remember BODMAS from high school maths

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 20

Arithmetic Operation with Column Alias


Remember: the AS keyword is optional (but is recommended for clarity) Retail minus cost arithmetic operation

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 21

NULL Values
Null operand in any operation gives a null result

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 22

Using DISTINCT and UNIQUE


Enter DISTINCT or UNIQUE after the SELECT keyword to suppress duplicates

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 23

Using Concatenation
You can combine data in different columns with the concatenation operator || The data appear side by side For better formatting, you can include a space between the data with a string literal Any input enclosed in is interpreted literally An alias used with concatenation improves Heading readability

22See figures 2-18 to 2-20


22 July, 2010 ICT118 Database and Information Retrieval, Sem 2, 2010 3. 24

Concatenation Example
Concatenation symbol Alias

String literal, where the string is a space character

22 July, 2010

ICT118 Database and Information Retrieval, Sem 2, 2010

3. 25

Summary
A basic query in Oracle 11g SQL includes the SELECT and FROM clauses, the only mandatory clauses in a SELECT statement To view all columns in the table, specify an asterisk (*) or list all of the column names individually in the SELECT clause To display a specific column or set of columns, list the column names in the SELECT clause in the order in which you want them to appear and separated by commas
22 July, 2010 ICT118 Database and Information Retrieval, Sem 2, 2010 3. 26

Summary (continued)
A column alias is used to clarify the contents of a particular column If the alias contains spaces or special symbols, or If you want to display the column with any lowercase letters Thenyou must enclose the column alias in double quotation marks (" ") Indicate the table name following the FROM keyword
22 July, 2010 ICT118 Database and Information Retrieval, Sem 2, 2010 3. 27

Summary (continued)
Basic arithmetic operations can be performed in the SELECT clause NULL values indicate an absence of a value and nullify any operation results To remove duplicate listings, include either the DISTINCT or UNIQUE keyword To specify which table contains the desired columns, you must list the name of the table after the keyword FROM Use vertical bars ( || ) to combine, or concatenate, fields, literals, and other data
22 July, 2010 ICT118 Database and Information Retrieval, Sem 2, 2010 3. 28

10

Vous aimerez peut-être aussi