Database Concepts  «Prev  Next»

Lesson 2

Table Structures in Relational Databases

Lesson 1 introduced the landscape: relational database systems, the technologies that connect to them, and the SQL language that speaks to all of them. This lesson gets specific about the one structure that landscape is built on: the table itself. Before writing a single SELECT statement against VirtualBookShelf's database, it helps to see exactly what a table is made of and how SQL Server enforces the rules that keep it trustworthy.

What a Table Actually Is

Relational databases organize data into structures that make information easy to store, retrieve, and analyze. In SQL Server, and in every other relational database system, the foundational structure is the table. A table represents a collection of related information: each row captures one instance of something, a single customer, a single book, and each column represents one attribute of that thing, a last name, a price, a quantity on hand.

A row is also called a record: the full set of information for one entity. A column is also called a field: one specific attribute stored for every record in the table, a customer's first name, say. Records and fields give relational databases a predictable shape, and that predictable shape is what makes consistent queries, reliable indexing, and constraints such as primary and foreign keys possible in the first place. Without it, a database would be closer to a pile of loosely related documents than a system you could query with confidence.

SQL Server 2025 continues to follow these same relational principles while adding capability on top of them: user-defined data types, computed columns, system-versioned temporal tables, and increasingly sophisticated indexing options, all covered briefly below. None of that changes the fundamental concept underneath. Whatever features a modern SQL Server release adds, a table still stores structured data using rows and columns, and that idea is worth holding onto as this course moves from lesson to lesson.

VirtualBookShelf's Customer Table, Defined

Module 1, Lesson 3 introduced VirtualBookShelf's Customer table as part of the course project's starting schema: CustomerID, FirstName, LastName, Email, Phone, and DateJoined. This lesson uses that same table to show what a table definition actually looks like once it's written out in Transact-SQL, rather than inventing a separate example from scratch.

In this view, each row represents one customer, and each column describes one attribute of that customer:
CustomerIDFirstNameLastNameEmailPhoneDateJoined
1ArnoldSmithasmith@example.com555-01012026-01-14
2JohnJonesjjones@example.com555-01322026-02-03
3AnneReynoldsareynolds@example.com555-01772026-02-20
4BettyFluvalbfluval@example.com555-01452026-03-02
5HankHenryhhenry@example.com555-01902026-03-15

Four things are worth noticing about this table:
  1. Rows have no guaranteed order. SQL Server doesn't promise to return these five customers in any particular sequence just because they were entered that way. If an order matters, an ORDER BY clause has to say so explicitly.
  2. Every column has a declared data type and a nullability rule. This table uses int for CustomerID and nvarchar for the text columns, and it declares whether each column accepts NULL. Email is required, so every customer must have one; Phone is optional, so some customers can leave it blank.
  3. CustomerID is the primary key. A PRIMARY KEY constraint on CustomerID guarantees that no two customers can ever share the same value, and that no customer row can have a NULL CustomerID. That guarantee is what lets a query, or a Razor Page, refer to exactly one customer without ambiguity.
  4. IDENTITY(1,1) generates the values automatically. SQL Server assigns each new CustomerID starting at 1 and incrementing by 1, so nothing in the application has to invent its own identifiers. IDENTITY guarantees the values are generated automatically; it doesn't, by itself, guarantee there will never be a gap if a row is later deleted. The actual uniqueness guarantee still comes from the PRIMARY KEY constraint, not from IDENTITY alone.

Define the Table with Transact-SQL

CREATE TABLE dbo.Customer
(
    CustomerID  int IDENTITY(1,1) NOT NULL,
    FirstName   nvarchar(100) NOT NULL,
    LastName    nvarchar(100) NOT NULL,
    Email       nvarchar(255) NOT NULL,
    Phone       nvarchar(20) NULL,
    DateJoined  date NOT NULL DEFAULT (GETDATE()),

    CONSTRAINT PK_Customer PRIMARY KEY (CustomerID)
);

This is the same Customer table ADO.NET, and later Entity Framework Core, will read from and write to throughout the rest of this course. A column's data type constrains what values it can physically store; a constraint like PRIMARY KEY enforces a business rule on top of that, uniqueness, in this case. Getting both right at this stage saves a lot of defensive code later, since SQL Server is now doing some of that validation work before a Razor Page ever sees the data.


NULL Is Not the Same as Blank

The Customer table above marks Phone as nullable and Email as not nullable, and it's worth being precise about what that nullability rule actually means. NULL isn't the same thing as an empty string, and it isn't the same thing as zero. An empty string, '', is a known value: a piece of text that happens to have zero characters. NULL means something different: the value for that column, for that row, is simply unknown or doesn't apply.

This distinction matters once queries start filtering on a column. WHERE Phone = '' only matches rows where Phone was explicitly set to an empty string; it will never match a row where Phone is NULL. Finding those rows instead requires WHERE Phone IS NULL, a different comparison entirely, since SQL's ordinary equality operator was never designed to compare against "unknown." A column declared NOT NULL, like Email, sidesteps the whole question for that column: SQL Server simply refuses to let a row exist without some value there, known or not.

SQL Server Keeps Evolving, But Tables Are Tables

SQL Server 2025 adds several capabilities on top of the basic row-and-column model described above. None of them change what a table fundamentally is, but a few are worth a brief look now, since later lessons touch on some of them:
  • User-defined data types let a team define a reusable type built on a built-in one, such as a PhoneNumber type built on nvarchar(20), so related columns across several tables stay consistent without repeating the same declaration everywhere.
  • Computed columns calculate their value from other columns in the same row rather than storing it directly. A TotalValue column on VirtualBookShelf's Inventory table, computed as Price * QuantityOnHand, is a natural example a later module could introduce; SQL Server recalculates it automatically whenever either input column changes.
  • System-versioned temporal tables have SQL Server automatically keep a full history of every change made to a row, a record of exactly what values existed at any point in the past, without any extra application code to build that history. This matters far more for an audit trail on a production system than it does for a teaching project at VirtualBookShelf's scale.
  • Advanced indexing, such as columnstore and filtered indexes, speeds up specific kinds of large-scale queries. These techniques matter far more at production data volumes, millions of rows, than they do at the handful of sample rows this course works with.
Knowing these exist is enough for now. The table definitions this course actually builds, Customer and Inventory included, stay deliberately simple, so the focus stays on the relational concepts rather than on SQL Server's more advanced tooling. A production database at a real bookstore chain might lean on several of these features at once, a computed loyalty-points column, a temporal history of every price change, columnstore indexes across millions of sales rows, but none of that changes the underlying table shape a student needs to understand first.

Querying and Changing This Table: CRUD in Practice

Lesson 1 introduced the four CRUD operations, Create, Read, Update, Delete, as the four things SQL ultimately lets an application do to a table. The Customer table defined above is a good place to see all four written out concretely, since it's the exact table this course's Razor Pages will eventually read from and write to through ADO.NET.

Adding a new customer is an INSERT:
INSERT INTO dbo.Customer (FirstName, LastName, Email, Phone)
VALUES ('Miguel', 'Alvarez', 'malvarez@example.com', '555-0188');
Notice that CustomerID and DateJoined aren't listed. CustomerID is handled by IDENTITY(1,1), and DateJoined falls back to its DEFAULT of today's date, so the statement only has to supply the values that actually vary from customer to customer.

Finding a customer, or a group of customers, is a SELECT:
SELECT CustomerID, FirstName, LastName, Email
FROM dbo.Customer
WHERE LastName = 'Reynolds';

Changing an existing customer's information is an UPDATE:
UPDATE dbo.Customer
SET Phone = '555-0199'
WHERE CustomerID = 3;
The WHERE CustomerID = 3 clause is doing important work here. Without it, SQL Server would update every row's Phone column to the same value, which is exactly the kind of mistake a reliable primary key helps an application avoid, since CustomerID gives every statement an unambiguous way to target one specific row.

Removing a customer is a DELETE:
DELETE FROM dbo.Customer
WHERE CustomerID = 5;
The same targeting principle applies: the WHERE clause narrows the statement down to the one row that should actually be affected. Later lessons in this course wrap statements like these inside ADO.NET SqlCommand objects and, further on, behind Entity Framework Core's object-based API, but the four SQL statements underneath stay exactly this simple.

When One Table Isn't Enough: A Quick Illustration

VirtualBookShelf's own Customer and Inventory tables don't currently reference each other directly, and Module 1, Lesson 3 explicitly kept a sales or order table out of scope for this course. That means there isn't yet a built-in VirtualBookShelf example of two tables linked together, but the general concept of a table relationship is still worth seeing clearly, through a small, standalone illustration that's deliberately separate from VirtualBookShelf's own schema.

Imagine two independent tables, CustTable and SaleTable, unrelated to anything VirtualBookShelf has introduced so far:

CustTable
CustNo       | LName    | FName | ADDR1
0000000003   | Reynolds | Anne  | 3345

SaleTable
CustNo       | ItemNo | Qty | Date
0000000003   | 1027   | 2   | 2026-10-14

A shared column, CustNo in this illustration, links a row in SaleTable back to the matching row in CustTable: the sale recorded above belongs to customer 0000000003, Anne Reynolds. In SQL Server, this relationship is implemented through a FOREIGN KEY constraint on SaleTable's CustNo column, which ensures that every sale references a customer who actually exists in CustTable, SQL Server simply refuses to insert a sale for a customer number that isn't there. A single customer can appear in SaleTable many times, once per purchase, which lets reporting tools or application code iterate through the related rows and summarize that customer's activity.

The naming used in this illustration, CustNo and CustTable rather than CustomerID and Customer, is intentional. This is a separate, generic illustration meant only to show how a shared key links two tables together; it isn't a preview of a table VirtualBookShelf itself will build. If a later version of this course ever did add an orders or sales table to the project, it would most likely follow VirtualBookShelf's existing naming directly, referencing CustomerID and BookID rather than the CustNo/ItemNo naming used here purely to keep this standalone example clearly distinct from the actual project.

Relationships like this one are what the word "relational" in "relational database" actually refers to: tables related to each other through shared key values, not simply data sitting in rows and columns. Enforcing that relationship at the database level, rather than trusting every piece of application code to check it correctly, is what catches a whole category of data-integrity mistakes before they ever reach a user.

A Few Common Questions

Why does every table need a primary key? Without one, there's no reliable way to refer to exactly one row. Two customers could easily share the same first and last name; nothing but a dedicated identifier guarantees uniqueness. Every meaningful operation later lessons cover, updating one book's quantity, looking up one customer's details, depends on being able to say "this exact row and no other," and a primary key is what makes that possible.

Is "record" just another word for "row," and "field" just another word for "column"? Functionally, yes, in the context of a relational table. "Row" and "column" describe the table's physical layout; "record" and "field" describe the same thing from the data's point of view, one complete record per entity, one field per attribute of that entity. Both pairs of terms show up throughout SQL Server documentation and this course interchangeably.

Why store a phone number as nvarchar instead of a number type? A phone number isn't really a number in the arithmetic sense, nothing about it is ever added, subtracted, or averaged, and it often includes characters a true numeric type can't hold: a leading zero, a dash, a parenthesis, an extension. Storing it as text avoids losing meaningful characters and avoids implying that arithmetic on it would ever make sense.

Does every relational database system enforce foreign keys the same way SQL Server does? The core idea, a column that must reference an existing row in another table, is part of the standard relational model and shows up in PostgreSQL, MySQL, and Oracle Database the same way it does in SQL Server. The exact syntax for declaring the constraint varies slightly between systems, but the underlying guarantee, no orphaned reference to a row that doesn't exist, is the same everywhere.

What happens if two INSERT statements try to create a customer at the exact same moment? SQL Server handles this correctly without any extra code in this course's examples. IDENTITY(1,1) hands out the next CustomerID value atomically, so two simultaneous inserts still get two different numbers, and the PRIMARY KEY constraint would reject a collision anyway if one somehow occurred. This kind of correctness under concurrent access is one of the quieter benefits of letting the database enforce these rules, rather than trying to coordinate them from application code.

Could the Customer table's columns change later in the course? Quite possibly. Module 1, Lesson 3 described later modules "enhancing" Customer and Inventory as the course introduces new SQL concepts, a new column, a new index, a new constraint. The table defined in this lesson is the starting point, not a final, frozen design; SQL Server's ALTER TABLE statement is how a column gets added to a table that already has data in it, and later lessons may well use it.

What's Ahead

This lesson covered how a single table is actually built: rows, columns, declared data types, nullability, and the primary key that keeps every row unique, using VirtualBookShelf's own Customer table as the working example throughout. It also looked briefly at how two tables can relate to each other through a shared key, even though VirtualBookShelf's own tables don't yet do that.

The next lesson in this module builds directly on this one, moving from a table's logical structure toward how that data actually gets stored and worked with once it's inside SQL Server. Nothing covered here needs to be memorized in exhaustive detail yet; the table, row, column, and key vocabulary introduced in this lesson is what the rest of this module, and the rest of this course, will keep building on.

[1]iterate: To execute program statements zero or more times in a loop structure.
[2]Relational database: A database organized using relationships among tables, rows, and columns.

SEMrush Software 2 SEMrush Banner 2