1
A Guide to SQLChapter 2
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
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
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
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)
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
7
Create Table Command for SALES_REP Table
8
Common Data Types
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
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
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) );
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’)
13
INSERT Command
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).
15
Using an Editor to Modify the INSERT Command
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
17
Inserting a Row Containing Null Values
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
19
Modifying the Contents of a Column
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
21
Deleting a Row
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
24
DESCRIBE Command for SALES_REP Table