SQL as Data Definition Language
Last updated: 2026-07-21
Create Tables
- CREATE TABLE <name> (IF NOT EXISTS) <column specs>
- CREATE TEMPORARY TABLE ..
- table is gone when session ends
- more settings for indexing, storage enginee, etc
- constraints
- UNIQUE INDEX = NORMAL INDEX + UNIQUE
- NOT NULL
- DEFAULT
- CHECKa
- PRIMARY KEY
- must be NOT NULL
- FOREIGN KEY <col> references <table>(<col>)
- in Oracle, just reference...
- INSERT INTO <table> (<columns>) (<values>)
- DROP TABLE [IFEXISTS <table>]
- ALTER
- TRUNCATE TABLE <table_name>
- table will continue to exist, all data will be cleaned
- CREATE INDEX <index_name> ON <table_name> (<col_names>)
SQL
DROP TABLE IF EXISTS `player`;
CREATE TABLE `player` (
`player_id` int(11) NOT NULL AUTO_INCREMENT,
`team_id` int(11) NOT NULL,
`player_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`height` float(3, 2) NULL DEFAULT 0.00,
PRIMARY KEY (`player_id`) USING BTREE,
UNIQUE INDEX `player_name`(`player_name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;in most cases, use GUIs to create data tables first (eg. Navicat)
Common Constraints
- Primary Key
- ALTER TABLE <table> ADD PRIMARY KEY <key>
- is a kind of UNIQUE index
- Foreign Key
- ALTER TABLE <table> ADD FOREIGN KEY<key> REFERENCES <foreign table>
- Index
- CREATE INDEX,
- ALTER TABLE <table> ADD INDEX <index name>
- allow random access to rows to be faster
Database Normalization
- First Normal Form
- domain of each attribute contains only atomic(indivisible) value
- 2nd Normal Form
- not have any non-prime attribute that is functionally dependent(constraint between two sets of attributes) or any proper subset of any candidate key of a relation.
- 3rd Normal Form
- no non-prime (non-key) attribute is transitively dependent of any key. (no non-prime attribute dependes on the non prime attributes, all the non prime attributes must depend on the primary key only)
Examine common bugs/errors when designing data tables
- Redundancy
- Insert Error
- Delete Error
- Modification Error
The Tool for data table design; E-R diagram (Entity Relationship Diagram)