1 a guide to sql chapter 2. 2 introduction mid-1970s: sql developed under the name sequel at ibm by...

23
1 A Guide to SQL Chapter 2

Upload: geraldine-lester

Post on 03-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

1

A Guide to SQLChapter 2

Page 2: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

2

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 3: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

3

SQL Text Case Sensitivity

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 4: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

4

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_NUMBER

SALES_REP.SLSREP_NUMBER

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

Page 5: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

5

Database Creation

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 parenthesesData type indicates the type of data that the column can contain (for example, characters, numbers, or dates)

Page 6: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

6

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 7: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

7

Create Table Command for SALES_REP Table

Page 8: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

8

Common Data Types

Page 9: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

9

SQL Commands

Commands can be entered in a free format no rule says that a particular word must begin in a

particular position on a line the 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 lineIndicate the end of a command line by typing a semicolon

Page 10: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

10

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 11: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

11

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 12: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

12

Loading a Table with Data

Add 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 13: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

13

INSERT Command

Page 14: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

14

Editing

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

The easiest way to edit a command is to use the Notepad (type “Edit” at the SQL prompt).

Page 15: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

15

Using an Editor to Modify the INSERT Command

Page 16: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

16

The 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 17: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

17

Inserting a Row Containing Null Values

Page 18: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

18

Correcting Errors in the Database

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

Use the UPDATE command shown in Figure 2.13 to correct errors

Page 19: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

19

Modifying the Contents of a Column

Page 20: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

20

DELETE Command

To delete a record, use the DELETE command

The command in Figure 2.14 deletes any row on which the sales rep number is 18

Page 21: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

21

Deleting a Row

Page 22: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

23

Describing a Table

Use the DESCRIBE command to describe the layout of a table

The DESCRIBE command, as shown in Figure 2.23 lists all the columns in the SALES_REP table and their corresponding data types

Page 23: 1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation

24

DESCRIBE Command for SALES_REP Table