0% found this document useful (0 votes)
11 views

2-oracle-developer-tables-indexes-essentials-m2-slides

The document provides an overview of table basics in SQL, including naming rules, syntax for creating tables, and defining columns with constraints such as primary keys and foreign keys. It discusses the use of NULL values and default values in columns, as well as the concept of virtual columns that compute values from other columns. Additionally, it emphasizes the importance of clear naming conventions and understanding data representation in tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

2-oracle-developer-tables-indexes-essentials-m2-slides

The document provides an overview of table basics in SQL, including naming rules, syntax for creating tables, and defining columns with constraints such as primary keys and foreign keys. It discusses the use of NULL values and default values in columns, as well as the concept of virtual columns that compute values from other columns. Additionally, it emphasizes the importance of clear naming conventions and understanding data representation in tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Table Basics

David Berry
http://buildingbettersoftware.blogspot.com/
Introduction to Tables

student_id first_name last_name phone email major_id

208718 Adam Brady 360-592-7886 [email protected] 3

208723 George Ward 216-952-0212 [email protected] 5

208728 Marcia Garcia 630-239-4325 [email protected] 8

208741 Walter Stumpf 419-500-5489 [email protected] 12

208749 Brenda Brown 979-859-7105 [email protected] 5

208755 Christopher Glenn 803-980-0813 [email protected] 5

209628 Lisa Shifflett 229-409-0015 [email protected] 4


Tables Roadmap

Table Basics Table Constraints Table Storage


(this module) (next module) (in two modules)

Create table syntax Primary keys Storage characteristics


Naming rules Foreign keys Storage options
Null values Check constraints
Default values
Naming Rules

Maximum 30 characters for all objects


Length

Tables, views must be unique within schema


Uniqueness
Columns must be unique within a table

SQL Reserved Possible to use but discouraged


Words
Tables Represent Business Data
Basic Create Table Syntax

CREATE TABLE courses


(
department_code VARCHAR2(2) NOT NULL,
course_number NUMBER(3,0) NOT NULL,
course_title VARCHAR2(64) NOT NULL,
course_description VARCHAR2(512) NOT NULL,
credits NUMBER(3,1) NOT NULL,
CONSTRAINT pk_courses PRIMARY KEY
(department_code, course_number),
CONSTRAINT fk_courses_department_code FOREIGN KEY
(department_code) REFERENCES departments (department_code)
CONSTRAINT ck_courses_course_number
CHECK (course_number BETWEEN 100 AND 999)
)
TABLESPACE users
PCTFREE 75;
Naming Good names communicate intent
Tables and
Poor names lead to confusion
Columns
Naming Rules

Maximum 30 characters for all objects


Length

Tables, views must be unique within schema


Uniqueness
Columns must be unique within a table

SQL Reserved Possible to use but discouraged


Words
Naming Rules

Maximum
• 30 characters for all object types
Length

• Tables, views, indexes must be unique within


a user schema
Uniqueness
• Columns must be unique within a table or
view

SQL Reserved
• Possible to use but discouraged
Words
Unquoted Names

 Most common way of naming objects


CREATE TABLE zip_codes
(
zip_code VARCHAR2(5) NOT NULL,
city VARCHAR2(30) NOT NULL,
state VARCHAR2(30) NOT NULL
);

 Allowable characters are


 Alphanumeric characters
 Underscore (_) character
 $ and # characters

 Names will be case insensitive


Unquoted Names Are Case Insensitive

CREATE TABLE zip_codes


(
zip_code VARCHAR2(5) NOT NULL,
city VARCHAR2(30) NOT NULL,
state VARCHAR2(30) NOT NULL
);

Allowed SQL Statements


-- This is OK
select ZIP_CODE, CITY, STATE
from ZIP_CODES;

-- So is this
SELECT Zip_Code, City, State
FROM Zip_Codes;
Quoted Identifiers

 Allows a wider range of options


CREATE TABLE “ZipCodes”
(
“zip code” VARCHAR2(5) NOT NULL,
“city.name” VARCHAR2(30) NOT NULL,
“state-abbr” VARCHAR2(30) NOT NULL
);

 You must also use quoted names in SQL statements


SELECT
“zip code”, “city.name”, “state-abbr”
FROM “ZipCodes”
WHERE “zip code” = ‘54911’
Defining Columns

CREATE TABLE students


(
student_id NUMBER(6) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
email_address VARCHAR2(128) NOT NULL,
phone VARCHAR2(20) NULL,
city VARCHAR2(30) NULL,
state VARCHAR2(2) NULL,
status_code VARCHAR2(1) DEFAULT ‘A’ NOT NULL,
CONSTRAINT pk_students PRIMARY KEY (student_id)
);
Column Definition Rules

Number of • Maximum of 1000 columns


Columns per
Table • Over 255 columns results in row chaining

• 30 characters maximum
Column
• Names must be unique within a table
Naming Rules
• Names can be reused in different tables

Minimum • Name and datatype must be specified


Information • NULL an default clauses are optional
NULL Column Values

CREATE TABLE students


(
student_id NUMBER(6,0) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
city VARCHAR2(30) NULL,
state VARCHAR2(2) NULL,
date_of_birth DATE
);

 In character based columns (CHAR, VARCHAR, NCHAR, NVARCHAR)


 Empty string is treated as a NULL value
Default Values

CREATE TABLE students


(
student_id NUMBER(6,0) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
city VARCHAR2(30) NULL,
state VARCHAR2(2) NULL,
date_of_birth DATE,
enrollment_status VARCHAR2(1) DEFAULT ‘E’ NOT NULL
);
NULL, NOT NULL or DEFAULT VALUE
That is the question
Data You Do Not Have

Using NULL values


user_id email_address first_name last_name phone_number city

8385930 [email protected] Adam Brady 303-555-1212 Denver

8385931 [email protected] George Ward <NULL> Phoenix

8385932 [email protected] Marcia Garcia <NULL> <NULL>


8385933 [email protected] Walter Stumpf 414-555-8965 <NULL>

Using default values


user_id email_address first_name last_name phone_number city

8385930 [email protected] Adam Brady 303-555-1212 Denver

8385931 [email protected] George Ward 000-000-0000 Phoenix

8385932 [email protected] Marcia Garcia 000-000-0000 UNKNOWN


8385933 [email protected] Walter Stumpf 414-555-8965 UNKNOWN
An Event That Has Not Occurred

Using NULL values


order_id customer_id order_status_code order_date ship_date

567893 832 S 2014-08-03 2014-08-05


56784 944 S 2014-08-03 2014-08-07
567895 968 P 2014-08-05 <NULL>
567896 1024 N 2014-08-06 <NULL>

Using default values


order_id customer_id order_status_code order_date ship_date

567893 832 S 2014-08-03 2014-08-05


56784 944 S 2014-08-03 2014-08-07
567895 968 P 2014-08-05 1900-01-01
567896 1024 N 2014-08-06 1900-01-01
Use Defaults To Indicate an Initial State
Using default values
member email_address first_name last_name phone_number members
_id hip_level
8385930 [email protected] Adam Brady 303-555-1212 P

8385931 [email protected] George Ward 602-555-3547 F

8385932 [email protected] Marcia Garcia 608-555-2575 F


8385933 [email protected] Walter Stumpf 414-555-8965 P

Using NULL values


member email_address first_name last_name phone_number members
_id hip_level
8385930 [email protected] Adam Brady 303-555-1212 P

8385931 [email protected] George Ward 602-555-3547

8385932 [email protected] Marcia Garcia 608-555-2575


8385933 [email protected] Walter Stumpf 414-555-8965 P
Know your data
 What does the data mean
NULL
 How is it used
versus
Default Values What communicates intent most clearly?
• Would another user draw the same conclusion?
Virtual Columns Introduction

Normal column Virtual column

Data is physically stored Value is computed from


on disk by Oracle other columns in table
Virtual Column Use Case

CREATE TABLE orders


(
order_id NUMBER(10) NOT NULL,
order_date DATE NOT NULL,
customer_id NUMBER(10) NOT NULL,
subtotal NUMBER(10,2),
tax NUMBER(10,2),
shipping NUMBER(10,2),
grand_total NUMBER(10,2)
);
Defining a Virtual Column

CREATE TABLE orders


(
order_id NUMBER(10) NOT NULL,
order_date DATE NOT NULL,
customer_id NUMBER(10) NOT NULL,
subtotal NUMBER(10,2),
tax NUMBER(10,2),
shipping NUMBER(10,2),
grand_total NUMBER(10,2)
AS (subtotal + tax + shipping) VIRTUAL
);
Defining a Virtual Column With a Function

CREATE TABLE students5


(
student_id NUMBER(10) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
middle_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
email_address VARCHAR2(60) NOT NULL,
email_domain VARCHAR2(60) AS (
SUBSTR(email_address, INSTR(email_address, '@', 1, 1)+1)
) VIRTUAL
);
Virtual Column Properties

Value is computed in result set of query

Cannot INSERT into or UPDATE virtual columns

Can only make use of columns in the same table

Indexes can be created over virtual column values


Virtual
Useful for situations where a
Columns derived value is needed
Summary
Summary

CREATE TABLE NULLS and


Naming Rules
Syntax Default Values

You might also like