0% found this document useful (0 votes)
48 views52 pages

A Guide To SQL, Ninth Edition: Chapter Three Creating Tables

The document discusses creating and managing database tables using SQL. It describes how to use the CREATE TABLE command to define a table structure with columns and data types, add rows to tables, and view, correct and describe table data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views52 pages

A Guide To SQL, Ninth Edition: Chapter Three Creating Tables

The document discusses creating and managing database tables using SQL. It describes how to use the CREATE TABLE command to define a table structure with columns and data types, add rows to tables, and view, correct and describe table data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 52

A Guide to SQL, Ninth Edition

Chapter Three
Creating Tables
Objectives
• Create and run SQL commands

• Create tables

• Identify and use data types to define columns in


tables

• Understand and use nulls

• Add rows to tables


©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
2
Objectives (continued)
• View table data

• Correct errors in a table

• Save SQL commands to a file

• Describe a table’s layout using SQL

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
3
Introduction
• Structured Query Language (SQL)

– Most popular and widely used language for


retrieving and manipulating database data

– Developed in mid 1970s under the name


SEQUEL

– Renamed SQL in 1980

– Used by most DBMSs


©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
4
Creating and Running SQL
Commands
• Oracle Database 11g Express
– Software used in text to illustrate SQL
– Commands will work the same in other versions
of Oracle
• Differences between Oracle and Microsoft
Access and SQL Server are noted in special
boxes

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
5
Using the Oracle Database 11g
Express Edition
• App loads in your web browser

• Must have a DBA database role username and


password

• Create a workspace

– A work area that allows multiple users to work with the


same installation of Oracle

• Login to the workspace

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
6
Using the Oracle Database 11g
Express Edition (continued)

Figure 3-1: Oracle Database 11g Express Edition home page

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
7
Using the Oracle Database 11g
Express Edition (continued)

Figure 3-2: Entering the username and password for a DBA user

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
8
Using the Oracle Database 11g
Express Edition (continued)

Figure 3-3: Creating a new workspace


©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
9
Using the Oracle Database 11g
Express Edition (continued)

Figure 3-4: Logging in to an Application Express workspace


©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
10
Using the Oracle Database 11g
Express Edition (continued)
• Application Express home page provides
access to various tools
• Use SQL Workshop tool in this text
• SQL Scripts page displays scripts

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
11
Using the Oracle Database 11g
Express Edition (continued)

Figure 3-5: Oracle Application Express home page


©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
12
Using the Oracle Database 11g
Express Edition (continued)

Figure 3-6: SQL Workshop page


©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
13
Using the Oracle Database 11g
Express Edition (continued)

Figure 3-7: SQL Scripts page


©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
14
Using the Oracle Database 11g
Express Edition (continued)

Figure 3-8: Two scripts displayed on the SQL Scripts page

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
15
Entering Commands

Figure 3-9: SQL Workshop menu

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
16
Entering Commands (continued)

Figure 3-10: SQL Commands page

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
17
Creating a Table
• Describe the layout of each table in the database

• Use CREATE TABLE command

• TABLE is followed by the table name

• Follow this with the names and data types of the


columns in the table

• Data types define type and size of data

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
18
Creating a Table (continued)
• Table and column name restrictions

– Names cannot exceed 30 characters

– Must start with a letter

– Can contain letters, numbers, and underscores


(_)

– Cannot contain spaces

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
19
Creating a Table (continued)

Figure 3-11: CREATE TABLE command for the REP table

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
20
Creating a Table (continued)
• Commands are free-format; no rules stating
specific words in specific positions

• Indicate the end of a command by typing a


semicolon

• Commands are not case sensitive

• In Oracle, enter the command in the SQL editor


pane
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
21
Creating a Table (continued)

Figure 3-12: Running the CREATE TABLE command for the REP table

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
22
Creating a Table (continued)

Figure 3-13: Using Access SQL view to create a table

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
23
Creating a Table (continued)

Figure 3-14: Using Microsoft SQL Server to create a table

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
24
Correcting Errors in SQL
Commands
• Use the same techniques that you might
use in a word processor
• Make changes and click Run button to
execute command again
• Check Results pane to determine if
command executed successfully

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
25
Dropping a Table
• Can correct errors by dropping (deleting) a table
and starting over

• Useful when table is created before errors are


discovered

• Command is followed by the table to be dropped


and a semicolon

• Any data in table also deleted


©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
26
Using Data Types
• For each column, the type of data must be defined
• Common data types
– CHAR(n)
– VARCHAR(n)
– DATE
– DECIMAL(p,q)
– INT
– SMALLINT
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
27
Using Nulls
• A special value to represent a situation when the
actual value is not known for a column

• Can specify whether to allow nulls in the


individual columns

• Should not allow nulls for primary key columns

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
28
Using Nulls (continued)
• Use NOT NULL clause in CREATE TABLE
command to exclude the use of nulls in a column

• Default is to allow null values

• If a column is defined as NOT NULL, system will


reject any attempt to store a null value there

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
29
Using Nulls (continued)
CREATE TABLE REP
(REP_NUM CHAR(2) PRIMARY KEY,
LAST_NAME CHAR(15) NOT NULL,
FIRST_NAME CHAR(15) NOT NULL,
STREET CHAR(15),
CITY CHAR(15),
STATE CHAR(2),
POSTAL_CODE CHAR(5),
COMMISSION DECIMAL(7,2),
RATE DECIMAL(3,2) );

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
30
Adding Rows to a Table
• INSERT Command
– INSERT INTO followed by table name
– VALUES command followed by specific values in
parentheses
– Values for character columns in single quotation
marks

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
31
The Insert Command

Figure 3-17: INSERT command for the first record in the REP table

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
32
The INSERT Command (continued)
• To add new rows, modify previous insert
command

• Use same editing techniques as those used to


correct errors

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
33
Inserting a Row that Contains
Nulls
• Use a special format of INSERT command to
enter a null value in a table

• Identify the names of the columns that accept


non-null values and then list only the non-null
values after the VALUES command

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
34
Inserting a Row that Contains Nulls
(continued)

Figure 3-20: Inserting a row that contains null values in the REP table

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
35
Viewing Table Data
• Use SELECT command

– Can display all the rows and columns in a table

• SELECT * FROM followed by the name of the


table

• Ends with a semicolon

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
36
Viewing Table Data (continued)

Figure 3-21: Using a SELECT command to view table data

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
37
Viewing Table Data (continued)
• In Access
– Enter SELECT statement in SQL view
• In SQL Server
– Enter SELECT statement in Query Editor window

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
38
Correcting Errors in a Table
• UPDATE command is used to update a value in
a table

• DELETE command allows you to delete a record

• INSERT command allows you to add a record

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
39
Correcting Errors in a Table
(continued)

Figure 3-26: Using an UPDATE command to change a value

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
40
Correcting Errors in a Table
(continued)

Figure 3-27: Last name changed for sales rep number 75

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
41
Correcting Errors in a Table
(continued)

Figure 3-28: Using a DELETE command to delete a row

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
42
Saving SQL Commands
• Allows you to use commands again without
retyping
• Save commands in a script file or script
– Text file with .sql extension

• Script repository
– Special location in Oracle
– Can download to local drive

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
43
Saving SQL Commands
(continued)
• To create a script file in Oracle:
– Use Script Editor page
– Enter a name for script
– Type the command or commands to save in
script
– Save the script

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
44
Saving SQL Commands
(continued)
• Once a script file is created:
– Can view, edit, or run
– Can delete
– Can download from script repository to local drive
– Can upload from local drive to script repository

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
45
Saving SQL Commands
(continued)
• Access
– Does not use script files
– Save SQL commands as query objects
• SQL Server
– Can create scripts
– Can view, edit, run scripts
– Can delete scripts

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
46
Creating the Remaining Database
Tables
• Execute appropriate CREATE TABLE and
INSERT commands
• Save these commands as scripts
• Separate multiple commands in a script file with
a semicolon
• Figures 3-30 through 3-37 give additional table
information for TAL Distributors

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
47
Describing a Table
• DESCRIBE command (Oracle)

• Documenter tool (Access)

• Exec sp_columns command (SQL Server)

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
48
Describing a Table (continued)

Figure 3-38: DESCRIBE command for the REP table

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
49
Summary
• Use the CREATE TABLE command to create
tables

• Use the DROP TABLE command to delete a


table

• CHAR, VARCHAR, DATE, DECIMAL, INT, and


SMALLINT data types

– Access does not support DECIMAL

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
50
Summary (continued)
• Null value used when actual value for a column
is unknown, unavailable, or not applicable
• Use NOT Null clause to identify columns that
cannot have a null value
• Use INSERT command to add rows
• Use SELECT command to view data in a table

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
51
Summary (continued)
• Use UPDATE command to change the value in a
column
• Use DELETE command to delete a row
• Save SQL commands in a script file
• Use DESCRIBE command to display a table’s
structure

©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
52

You might also like