Back to Browse

Chapter_01_Practical SQL: Creating Your First Database and Table (Execution of the code)

5 views
Apr 10, 2026
8:24

Chapter 1: Creating Your First Database and Table — Deep DiveThe Big PictureChapter 1 is the launchpad for the entire book. Its mission is simple but essential: get you from zero to having a real, working database with a real table and real data in it. Every concept introduced here — databases, tables, columns, data types, rows, SQL statements — underpins everything that follows. DeBarros uses a deliberately simple example (a table of teachers) so that the mechanics of SQL, not the complexity of the data, are front and center.What Is a Database, and Why Does It Matter?Before writing a single line of SQL, it's worth understanding what a database actually is. In the context of PostgreSQL (which the book uses throughout), a database is a named container that holds all the objects you create — tables, views, functions, indexes, and more. Think of it as a filing cabinet: the cabinet itself is the database, and the individual folders inside are the tables.SQL is more than just a means for extracting knowledge from data — it is also a language for defining the structures that hold data so we can organize relationships in the data. Chief among those structures is the table: a grid of rows and columns that stores data, where each column contains data of a specified type, most commonly numbers, characters, and dates. NeocitiesThis is a conceptually important point DeBarros makes early: SQL isn't just about asking questions of data (querying), it's also about defining and structuring data. Chapter 1 is where that definitional work begins.Creating a DatabaseThe CommandThe very first SQL statement you execute in the book is:sqlCREATE DATABASE analysis;This is intentionally minimalist. There are no configuration options, no owner specified, no encoding declared — DeBarros strips it to its essence so you can focus on the concept. What this command does: Instructs PostgreSQL to create a new, empty database object. Names it analysis (chosen because throughout the book you'll be analyzing data within it). The semicolon at the end terminates the statement — sometimes you can omit the semicolon, but not always, and particularly not when running multiple statements in the admin tool. Agorism DeBarros establishes the habit of always including it from the start. Executing SQL in pgAdminpgAdmin is used for executing SQL statements through a graphical interface. The process includes connecting to the PostgreSQL server and executing the command to create the new database. Bookey The step-by-step workflow in pgAdmin is: Launch PostgreSQL — on macOS, you must double-click Postgres.app in your Applications folder. Agorism On Windows, it may launch automatically on boot. Open pgAdmin — in the left vertical pane (the object browser), expand the Servers node to find your default server, which may be named localhost or PostgreSQL x depending on your installation. Double-click the server name and enter your password if prompted. Open the Query Tool — click once on any database to highlight it, then open the Query Tool from the menu. This is where you type and execute SQL. Type CREATE DATABASE analysis; and click the lightning bolt icon to execute. After running it, your new analysis database will appear in the object browser under Databases.Connecting to the Analysis DatabaseOnce created, you need to explicitly connect to analysis before you can create objects inside it. This is because pgAdmin (and PostgreSQL generally) requires you to be connected to a specific database to work within it. You do this by clicking on the analysis database in the object browser, which also opens a fresh Query Tool session scoped to that database.Understanding Tables: The Core Unit of DataThe chapter stresses the significance of knowing how tables relate to each other, illustrated through a hypothetical school enrollment database demonstrating relationships between tables for students, classes, and student enrollment. BookeyBut before you can relate tables to each other, you need to understand what a single table is. DeBarros explains that a table is fundamentally a two-dimensional structure: Rows (also called records or tuples) — each row represents one entity or observation. Columns (also called fields or attributes) — each column represents one piece of information about that entity. Cells — the intersection of a row and a column holds a single value. For example, in a teachers table, one row might represent a specific teacher named Janet Smith, and the columns might be her first name, last name, school, hire date, and salary. Every row in that table follows the same column structure. The CREATE TABLE StatementSyntax BreakdownThe code begins with the two SQL keywords CREATE and TABLE that, together with the name teachers, signal PostgreSQL that the next bit of code describes a table to add to the database. Following an opening parenthesis the statement includes a comma-separated list of column names along with their data types. Chapter 1 looks like this.

Download

0 formats

No download links available.

Chapter_01_Practical SQL: Creating Your First Database and Table (Execution of the code) | NatokHD