1 an introduction to sql. 2 objectives understand the concepts and terminology associated with...

23
1 An Introduction to SQL

Upload: gervais-boone

Post on 26-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

1

An Introduction to SQL

Page 2: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

2

Objectives

Understand the concepts and terminology associated with relational databases

Create and run SQL commands Create tables using SQL Identify and use data types to define the columns in

SQL tables Understand and use nulls Add rows to tables

Page 3: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

3

Getting Started with Oracle

putty ubunix.buffalo.edu

>setenv EDITOR nano

>source /util/bin/coraenv

>sqlplus

Page 4: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

4

Introduction

Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose

research facilities to be the data manipulation language for IBM’s prototype relational model DBMS, System R

1980: language renamed SQL to avoid confusion with an unrelated

hardware product called SEQUEL Currently:

SQL used as the data manipulation language for IBM’s current relational DBMS, DB2

Most relational DBMSes use a version of SQL

Page 5: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

5

Common Shorthand Representation

Write the table name and then, within parentheses, list all the columns (fields) in the table

SQL is not case sensitive Type commands using uppercase or lowercase

letters Exception: when inserting character values into a table,

use the correct case

Page 6: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

6

Shorthand of Premier Products Database

SALES_REP (SLSREP_NUMBER, LAST, FIRST, STREET, CITY, STATE, ZIP_CODE, TOTAL COMMISSION, COMMISSION_RATE)

CUSTOMER (CUSTOMER_NUMBER, LAST, FIRST, STREET, CITY, STATE, ZIP_CODE, BALANCE, CREDIT_LIMIT, SLSREP_NUMBER)

ORDERS (ORDER_NUMBER, PART_NUMBER, NUMBER_ORDERED, QUOTED_PRICE)

ORDER_LINE (ORDER_NUMBER, PART_NUMBER, NUMBER_ORDERED, QUOTED_PRICE)

PART (PART_NUMBER, PART_DESCRIPTION, UNITS_ON_HAND, ITEM_CLASS, WAREHOUSE_NUMBER, UNIT_PRICE)

Page 7: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

7

Qualifying Names

To associate the correct table with the column name:

Write both the table name and the column name, separated by a period

CUSTOMER.SLSREP_NUMBERSALES_REP.SLSREP_NUMBER

This technique of including the table name with the column name is known as qualifying the names

Page 8: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

8

SQL Commands

Commands are free format no rule says that a particular word must begin in a particular position

on a line manner in which the command is written simply makes the

command more readable Press the Enter key at the end of each line and then continue

typing the command on the next line Indicate the end of a command line by typing a semicolon

(required in Oracle, but it is not universal)

Page 9: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

9

Database Creation

Describe the layout of each table to be contained in the database

The SQL command used to describe the layout of a table is CREATE TABLE followed by the name of the table to be created and the names and data types of the columns that comprise the table in parentheses

Data type indicates the type of data that the column can contain (for example, characters, numbers, or dates)

Page 10: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

10

Data Types

Char or Varchar2 Date Decimal Integer Smallint

Page 11: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

11

Typical Column Naming Conventions

The name cannot be longer than 18 characters (in Oracle, names can be up to 30 characters in length)

The name must start with a letter The name can contain letters, numbers, and

underscores ( _ ) The name cannot contain spaces

Page 12: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

12

Example 1

Describe the layout of the SALES_REP table to the DBMS CREATE TABLE SALES_REP (SLSREP_NUMBER

CHAR(2), LAST CHAR(10), FIRST CHAR (5), STREET CHAR(15), CITY CHAR(15), STATE CHAR(2), ZIP_CODE CHAR(5), TOTAL_COMMISSION DECIMAL(7,2), COMMISSION_RATE DECIMAL(3,2));

Page 13: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

13

Editing

In Oracle, the most recent command entered is stored in the command buffer

Edit the command in the buffer by using the special editing commands

Page 14: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

14

After Editing

Use the L command to list the codified SQL command, to verify that changes are correct

Type a semicolon after the command to run (execute) it immediately

Type RUN to display the command before it is executed

Type RUN followed by a slash (/) to execute the command without first displaying it

Page 15: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

15

Dropping a Table

Use the DROP TABLE command to delete a table The command DROP TABLE is followed by the

name of the table you want to delete and a semicolon.

DROP TABLE SALES_REP;

Note when a table is dropped, any data that you entered into the table is dropped

Page 16: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

16

Nulls

Occasionally, when you enter a new row into a table or modify an existing row, the values for one or more columns are unknown or unavailable A customer who does not have a sales rep

This special value is called a null data value, or simply null.

Page 17: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

17

Implementation of Nulls

CREATE TABLE SALES_REP (SLSREP_NUMBER CHAR(2) NOT NULL,

LAST CHAR(10) NOT NULL,FIRST CHAR (5) NOT NULL,STREET CHAR(15),CITY CHAR(15),STATE CHAR(2),ZIP_CODE CHAR(5),TOTAL_COMMISSION DECIMAL(7,2),COMMISSION_RATE DECIMAL(3,2) );

Page 18: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

18

Loading a Table with Data

Add the necessary rows to each table using the INSERT command

When adding rows to character (CHAR) columns, make sure to enclose the values in single quotation marks (for example, ‘Jones’)

Page 19: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

19

INSERT Command with Nulls

To enter a null value into a table, use a special format of the INSERT command

In this special format, identify the names of the columns that will accept non-null values, and then list only these non-null values after the VALUES command

Page 20: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

20

SELECT/UPDATE/DELETE Command

View data in the table to make sure that it is entered correctly by using the SQL SELECT command

After reviewing the data in the table changes may have to be made to the value in a column

To delete a record, use the DELETE command

Page 21: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

21

.SQL files

Use an editor to create a file containing the CREATE TABLE and INSERT commands to save from having to retype commands

To create a file in Oracle, type EDIT followed by the name of the file to create

Oracle assigns the file the extension .SQL automatically

Must have EDITOR environment variable set in UNIX for this to work

Page 22: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

22

cre_cust.SQL

To run the cre_cust.SQL file Save the file Close the editor Type @cre_cust (and full path if stored in another folder) After creating the table, another file could be created

containing all the necessary INSERT commands to add the necessary records to the table

Each command in the file must end with a semicolon

Page 23: 1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands

23

Describing a Table

Use the DESCRIBE command to describe the layout of a table

The DESCRIBE command lists all the columns in the table and their corresponding data types