liveBook · Manning

Database table

As explained in Rust Servers, Services, and Apps

A database table is a structured relational-data object consisting of rows organized under defined columns and constraints.

A database table stores application records as rows under named columns. In a course service, a PostgreSQL table is created with a SQL script, seeded with test rows, and queried from Rust through a sqlx connection pool. section 4.2.2

/* Drop table if it already exists*/
drop table if exists ezy_course_c4;
/* Create a table. */
/* Note: Don't put a comma after last field */
create table ezy_course_c4
(
   course_id serial primary key,
   tutor_id INT not null,
   course_name varchar(140) not null,
   posted_time TIMESTAMP default now()
);
 
/* Load seed data for testing */
insert into ezy_course_c4
   (course_id,tutor_id, course_name,posted_time)
values(1, 1, 'First course', '2020-12-17 05:40:00');
insert into ezy_course_c4
   (course_id, tutor_id, course_name,posted_time)
values(2, 1, 'Second course', '2020-12-18 05:45:00');

The initial course table defines an ID, required tutor and course fields, and a timestamp with a default; the script also inserts two test rows. section 4.2.2

Run the script against the target database, then use `select * from ezy_course_c4;` in psql to check the rows before connecting application code. A Rust `Course` struct holds the selected column values. section 4.2.2

/* Drop tables if they already exist*/
 
drop table if exists ezy_course_c6;
 
/* Create tables. */
/* Note: Don't put a comma after last field */
 
create table ezy_course_c6
(
    course_id serial primary key,
    tutor_id INT not null,
    course_name varchar(140) not null,
    course_description varchar(2000),
    course_format varchar(30),
    course_structure varchar(200),
    course_duration varchar(30),
    course_price INT,
    course_language varchar(30),
    course_level varchar(30),
    posted_time TIMESTAMP default now()
 
);

The later course table adds columns for details such as description, format, price, and language. `NOT NULL` marks required fields; columns without it correspond to `Option<T>` fields in the Rust `Course` struct. section 6.2.1

Create tables before compiling SQL-backed APIs: sqlx checks referenced table and column names at compile time. The tutor APIs introduce a tutor table alongside the course table. section 6.3.4

In a PostgreSQL Docker container, psql calls tables “relations.” If `\d` finds none, run the initialization script, check that the tables and seed rows exist, then restart the container and query again to test persistence. section 12.3.2

How Rust Servers, Services, and Apps covers it

How deep it goes
Walks through SQL creation, constraints, seed data, psql checks, model alignment, and container verification without detailing database storage internals.
Examples
Uses seeded course rows, an expanded course schema, a tutor table, and psql checks inside a PostgreSQL container.
What it assumes
Assumes a Rust project while walking through PostgreSQL setup, table creation, and psql verification.
What you can do after
Build it from scratch with SQL scripts, then verify tables and rows with psql and check persistence after a container restart.
How it connects
Connects SQL constraints and primary keys to Rust data models, sqlx queries, PostgreSQL relations, and Docker persistence.

Also explained in

Compare how 12 books explain database table →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.