Tables and integrity
CREATE TABLE and choosing column types
Define a small disposable table with types chosen for its values.
Lesson 51 of 84
Reading: about 2 min
Lesson + practice: about 15 min
Difficulty: Intermediate
On this page4 sections
This lesson uses DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Define a small disposable table with types chosen for its values. Run changing examples only on a disposable training instance. The example uses a session-local object or rolls back its changes.
Run the example
CREATE TABLE #PracticeLabels (LabelID int NOT NULL PRIMARY KEY,
LabelName nvarchar(40) NOT NULL, CreatedOn date NOT NULL);
INSERT INTO #PracticeLabels VALUES (1, N'Example', CONVERT(date, '2023-01-02'));
SELECT LabelID, LabelName, CreatedOn FROM #PracticeLabels ORDER BY LabelID;
DROP TABLE #PracticeLabels;Read it step by step
- int represents the key; nvarchar holds Unicode text; date holds a calendar date without a time.
- NOT NULL requires a value and PRIMARY KEY enforces unique non-NULL IDs.
- The # prefix makes the table session-local; DROP removes it after the check.
Check the result
| LabelID | LabelName | CreatedOn |
|---|---|---|
| 1 | Example | 2023-01-02 |
The canonical training schema remains unchanged.
Practice
Create a temporary product note table with int ID and nvarchar(40) Note, insert one row, show it, and drop the table.
Check your answer and expected result
Reveal answer
CREATE TABLE #ProductNotes (ProductID int NOT NULL PRIMARY KEY, Note nvarchar(40) NOT NULL);
INSERT INTO #ProductNotes (ProductID, Note) VALUES (1, N'Practice');
SELECT ProductID, Note FROM #ProductNotes ORDER BY ProductID;
DROP TABLE #ProductNotes;| ProductID | Note |
|---|---|
| 1 | Practice |
The one temporary row demonstrates the chosen key and Unicode text types.
Your progress
Reveal the check answer or reach the end to complete this lesson.