0% found this document useful (0 votes)
53 views19 pages

Creating Database Tables: Jon Flanders

CREATE TABLE is used to create database tables and define columns and constraints. ALTER TABLE allows modifying existing tables by adding, removing, or changing columns and constraints. DROP TABLE removes the table and all its data from the database. Understanding SQL data definition language (DDL) commands like CREATE TABLE, ALTER TABLE, and DROP TABLE provides a good foundation for working with relational databases even if directly writing DDL is used infrequently.

Uploaded by

Enrik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
53 views19 pages

Creating Database Tables: Jon Flanders

CREATE TABLE is used to create database tables and define columns and constraints. ALTER TABLE allows modifying existing tables by adding, removing, or changing columns and constraints. DROP TABLE removes the table and all its data from the database. Understanding SQL data definition language (DDL) commands like CREATE TABLE, ALTER TABLE, and DROP TABLE provides a good foundation for working with relational databases even if directly writing DDL is used infrequently.

Uploaded by

Enrik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 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