SQL Data Definition: Building Tables

Before an application can store records, you need to specify what each record can contain. SQL Data Definition gives you the commands to establish that structure. SQL stands for Structured Query Language. Here, you will define one table, choose suitable column types, and insert initial rows using syntax compatible with PostgreSQL.

Separate structure from stored values

Data definition specifies database structures, including table names, column names, types, and rules. It is commonly abbreviated DDL. The basic command here is CREATE TABLE.


A column describes one field, such as an item number or an amount. A row contains one record's values across those fields. Executing a basic CREATE TABLE statement establishes the columns but leaves the table empty. Executing INSERT adds rows to that existing structure; insertion is data manipulation rather than data definition.


Two table states show defined columns before and after one row is inserted.



Choose types from meaning and limits

A data type specifies the kind of value a column stores and the operations appropriate for it. Choose a type from the field's meaning, rather than just the appearance of its current values.


Requirement

Suitable type

Reason

Whole-number count

INTEGER

Stores whole numbers within its supported range

Code or name of at most 24 characters

VARCHAR(24)

Stores variable-length text, including leading zeroes

Exact amount with two decimal places

DECIMAL(6,2)

Provides a declared decimal capacity

Calendar day without a time

DATE

Represents a date rather than arbitrary text


In DECIMAL(p,s), precision pp counts total digits, while scale ss counts fractional digits. For the declarations considered here, 0≤s≤p0 \leq s \leq p:


Integer-digit capacity=p−s,Largest nonnegative value=10p−s−10−s.\begin{aligned} \text{Integer-digit capacity} &= p-s,\\ \text{Largest nonnegative value} &= 10^{p-s}-10^{-s}. \end{aligned}


Thus, DECIMAL(6,2) allows four digits before the decimal point, with a maximum of 9999.99. The decimal point and any minus sign do not count toward pp. This declaration also permits negative values; it does not impose a nonnegative business rule.


Example: A code such as 0042 belongs in a character column when its leading zeroes matter. Treating it as an integer represents the number 42 instead.

Write a table definition

List each column name followed by its type and any rules. Separate declarations with commas, enclose the list in parentheses, and end the statement with a semicolon.


SQL
CREATE TABLE PriceTag (
    tag_id INTEGER PRIMARY KEY,
    note VARCHAR(24) NOT NULL,
    amount DECIMAL(6,2) DEFAULT 0.00
);


Here, PRIMARY KEY requires distinct, non-null identifiers. It does not automatically generate identifier values. NOT NULL rejects a missing value represented by NULL; it does not reject an empty text string. DEFAULT 0.00 supplies an amount when an insertion omits that column or explicitly requests DEFAULT.


A type and a default serve different purposes. The type controls representation; the default supplies a value when requested by the insertion rules.

Insert values into the definition

An explicit column list makes the connection between columns and values visible:


SQL
INSERT INTO PriceTag (tag_id, note, amount)
VALUES (701, 'Notebook', 18.75);


Values match the listed columns from left to right. You may change the column-list order if you change the value order to match. Without a column list, supply values in the table's declared column order.


Text literals use single quotes. Numeric literals need no quotes. For a date, a typed literal such as DATE '2026-10-12' makes its interpretation explicit. Every inserted row must satisfy the table's types and rules.



What to remember

  • CREATE TABLE defines an empty table; INSERT adds records.
  • Choose types from meaning, required range, and precision.
  • Match values to column order and distinguish defaults from constraints.

Example: Defining a Makerspace Deposit Table

A makerspace needs a table definition: a specification of its columns, their types, and their rules. Its new ToolDeposit table will store an integer asset_id, a tool_name of at most 40 characters, and an exact deposit amount in CAD.


Each identifier must be unique and non-null. Tool names and deposits must also be non-null. Deposits need two decimal places and enough capacity for amounts up to CAD 12000.00. When an insertion omits the deposit, it should receive CAD 0.00. Use PostgreSQL-compatible SQL, and assume the table does not yet exist. No additional range constraint is required.


a) Choose the smallest DECIMAL(p,2) declaration that can represent the required deposit amounts.


First, count the integer digits in 12000. The amount needs ________ integer-digit positions. It also needs ________ fractional-digit positions for cents.


The total capacity must include both groups:


p=5+2=7.p=5+2=7.


With this declaration, the largest nonnegative stored amount is:


107−2−10−2=99999.99 CAD.10^{7-2}-10^{-2}=99999.99\ \text{CAD}.


Using one fewer total digit would give DECIMAL(6,2), whose maximum is CAD 9999.99, below the required CAD 12000.00. Therefore, use DECIMAL(7,2). Its capacity covers the requirement; it does not itself enforce the business limit of CAD 12000.00.


b) Write the complete table-creation statement. How many rows does the table contain immediately afterward?


The definition needs ________ columns. The name limit is ________, so VARCHAR(40) represents the required text capacity.


Give the identifier a primary-key rule, require non-null tool names and deposits, and attach the zero default to the deposit:


SQL
CREATE TABLE ToolDeposit (
    asset_id INTEGER PRIMARY KEY,
    tool_name VARCHAR(40) NOT NULL,
    deposit_cad DECIMAL(7,2) NOT NULL DEFAULT 0.00
);


The primary key makes identifiers unique and non-null. The separate NOT NULL rules require values for the other columns. The deposit default permits omission during insertion while still supplying a non-null amount.


The statement defines columns without inserting records. Immediately after creation, the table contains 0 rows.


c) Insert asset 248, named Tile cutter, with a CAD 149.50 deposit. Then insert asset 249, named Work light, by requesting the deposit default through omission. State the resulting row count and deposit amounts.


First, list all three columns and supply the matching values. The tool name is text, so place it in single quotes; the identifier and deposit are numeric literals.


SQL
INSERT INTO ToolDeposit (asset_id, tool_name, deposit_cad)
VALUES (248, 'Tile cutter', 149.50);


For the second insertion, omit only the deposit column. Its declared default supplies CAD 0.00:


SQL
INSERT INTO ToolDeposit (asset_id, tool_name)
VALUES (249, 'Work light');


The identifiers differ, both names fit the text limit, and both deposits fit the decimal declaration. Starting from an empty table, the two successful insertions give:


Final row count=0+1+1=2.\text{Final row count}=0+1+1=2.


The table contains 2 rows: asset 248 has a CAD 149.50 deposit, and asset 249 has a CAD 0.00 deposit. Neither insertion changes the table's column definitions.

A laboratory successfully executes the following statement in a database where Calibration does not yet exist. Which description of the result is correct?


SQL
CREATE TABLE Calibration (
    code VARCHAR(8),
    offset_value DECIMAL(4,2)
);
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A food-storage facility records whole-number temperatures in degrees Celsius. Each recorded temperature must be between 2 °C and 8 °C, inclusive. Replace the two placeholders in this SQL table definition with the appropriate SQL keywords.


SQL
CREATE TABLE StorageReading (
    reading_id INTEGER PRIMARY KEY,
    temperature_c INTEGER NOT NULL,
    RULE (temperature_c >= 2 JOIN_TEST temperature_c <= 8)
);


Replace RULE with and JOIN_TEST with .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A measurement system uses the decimal column declarations below. Complete each row with the integer-digit capacity, fractional-digit capacity, and largest nonnegative stored value. Enter exact integers for digit counts and exact decimals for maximum values, without commas or units.

DeclarationInteger digitsFractional digitsMaximum value
DECIMAL(4,1)
DECIMAL(5,3)
DECIMAL(8,2)

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice