Creating Database Tables: Jon Flanders

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Creating Database Tables

Jon Flanders

@jonflanders www.jonflanders.com
DATA SQL subset for creating databases and tables
DEFINITION
Most tools have a visual method
LANGUAGE
Good to have an idea of what they are doing
(DDL)
Oddly not part of the SQL Standard

CREATE Is supported by most implementations

DATABASE USE DATABASE to scope future queries


Can also fully qualify table name to database
CREATE DATABASE Contact; CREATE DATABASE COMMAND

USE DATABASE Contact; USE DATABASE COMMAND


SELECT * FROM person p; ALL QUERIES WILL BE IN THIS
DATABASE

SELECT * FROM FULLY QUALIFIED TABLE


Contact.person p; NAME
Is part of SQL Standard

CREATE Followed by table name

TABLE Then list of column definitions


At minimum column name and type
CREATE TABLE CREATE TABLE
email_address TABLE NAME
(
email_address_id
INTEGER,
email_address_person_id
COLUMNS
INTEGER,
email_address
VARCHAR(55)
);
Standard SQL Data Types

Data Type Value Space


CHARACTER Can hold N character values – set to N statically
Can hold N character values – set to N dynamically –
CHARACTER VARYING
storage can be less than N
BINARY Hexadecimal data
SMALLINT -2^15 (-32,768) to 2^15-1 (32,767)
INTEGER -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647)
-2^63 (-9,223,372,036,854,775,808) to
BIGINT
2^63-1 (9,223,372,036,854,775,807)
BOOLEAN TRUE or FALSE
DATE YEAR, MONTH, and DAY in the format YYYY-MM-DD
HOUR, MINUTE, and SECOND in the format
TIME
HH:MM:SS[.sF] where F is the fractional part of the
TIMESTAMP SECOND
Both DATE andvalue
TIME
NULL is a special value in SQL
Indicates a lack of a value

NULL VALUES Columns can be required or not required


Required is NOT NULL
Not required is NULL
NULL or NOT NULL

NULL NOT NULL


Default for a column definition Must be specified on column definition

Inserting NULL value ok Inserting NULL value is an error


CREATE TABLE CREATE TABLE COMMAND
email_address
(
email_address_id
INTEGER NOT NULL, NOT NULL
email_address_person_id NULL
INTEGER,
email_address
VARCHAR(55) NOT NULL); NOT NULL
Must be a unique value per row

PRIMARY KEY Must be NOT NULL


Can be a multiple columns (compound key)
CREATE TABLE CREATE TABLE
email_address TABLE NAME
(
email_address_id PRIMARY KEY
INTEGER PRIMARY KEY,
email_address_person_id COLUMNS
INTEGER,
email_address
VARCHAR(55) NOT NULL
);
Way to add keys in one grouping
CONSTRAINT
Primary or foreign keys
CREATE TABLE phone_number CREATE TABLE COMMAND
(
phone_number_id
INTEGER NOT NULL,
phone_number_person_id
INTEGER NOT NULL,
phone_number
VARCHAR(55) NOT NULL,
CONSTRAINT CONSTRAINT
PK_phone_number
PRIMARY KEY
(phone_number_id)
);
Used to change an existing table
Add/remove column

ALTER TABLE Change column data type


Change column constraints
Must comport with current data
ALTER TABLE ALTER TABLE
email_address TABLE NAME
ADD CONSTRAINT ADD KEYWORD
FK_email_address_person
FOREIGN KEY FOREIGN KEY
(email_address_person_id)
REFERENCES
person
(person_id);
Removes table and all data from database

DROP TABLE BE CAREFUL!


Error if table is a foreign key to another table
DROP TABLE person; DROP TABLE COMMAND
Understanding DDL is a good
foundation for working with SQL, even
Summary if you use it rarely
CREATE TABLE is the command to
configure your columns and relations
ALTER TABLE lets you change existing
definitions
DROP TABLE removes the table and all
its rows from the database

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy