Sign in to save your place.
Define tables with CREATE TABLE: column names, data types, PRIMARY KEY, and NOT NULL.
Use CREATE TABLE to define a new table: give it a name and list each column with a name and data type.
Column names must be unique within the table. Use descriptive names (e.g. CustomerId, OrderDate). Avoid spaces; use PascalCase or snake_case.
| Type | Use for |
|---|---|
| INT | Whole numbers (e.g. IDs, counts) |
| VARCHAR(n) | Variable-length text, max n characters |
| DATE | Date only (no time) |
| DECIMAL(p,s) | Exact numbers (p = total digits, s = decimal places) |
PRIMARY KEY identifies each row uniquely. Only one primary key per table; it implies NOT NULL and unique values. NOT NULL means the column cannot store NULL—every row must have a value.
Always define a primary key for your main tables. Use NOT NULL on columns that must always have a value.
A table is like a spreadsheet: columns (name + type) and rows. PRIMARY KEY marks the column(s) that uniquely identify each row.
-- Table with 3 columns: Id (PK), Name, BirthDate CREATE TABLE Customers ( Id INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, BirthDate DATE NULL );
Create a table Products with columns: ProductId (INT, PRIMARY KEY), ProductName (VARCHAR(200), NOT NULL), and Price (DECIMAL(10,2)).
Correct! PRIMARY KEY uniquely identifies each row.
PRIMARY KEY is the column(s) that uniquely identify each row in the table.
Correct! VARCHAR stores variable-length text.
VARCHAR is a data type for variable-length character strings.
A system-versioned temporal table keeps the current row and older versions of that row. Temporal tables are available on supported SQL Server editions, including 2022 and 2025. They answer "what did this row look like last Tuesday?" They do not replace a backup. A dropped database does not come back from a history table that lived in that database.
Choose types on purpose: int or bigint for counts, decimal for money, datetime2 for an instant, nvarchar for text. On SQL Server 2022, store a JSON document in nvarchar(max). Do not start a 2022 script with a native json column.
CREATE TABLE payments (
id int IDENTITY(1, 1) PRIMARY KEY,
amount decimal(10, 2) NOT NULL,
paid_at datetime2 NOT NULL
);Try this. Create a practice table with an identity key, a decimal(10, 2) amount, and a datetime2 column. Insert one row and read it back.
What does a temporal table replace?