| Lesson 5 | Using Literal Values |
| Objective | Use literal values in INSERT, UPDATE, and DELETE statements in SQL Server. |
Using Literal Values in SQL Server
In SQL Server, a literal value is a fixed value directly specified in a Transact-SQL (T-SQL) query, such as a string, number, or date, as opposed to a variable or column reference. Literal values are commonly used in INSERT, UPDATE, and DELETE statements to add, modify, or filter data. String literals (e.g., 'Anthony') and date literals (e.g., '2025-08-03') must be enclosed in single quotes, while numeric literals (e.g., 101) do not require quotes. Literal values must match the target column’s data type to avoid errors.
INSERT Statement
To add a new employee, Anthony Mann, hired on August 3, 2025, to the
Employees table, use the
INSERT statement with literal values:
INSERT INTO Employees (FirstName, LastName, HireDate)
VALUES ('Anthony', 'Mann', '2025-08-03');
Here,
'Anthony' and
'Mann' are string literals for the
FirstName and
LastName columns (typically
VARCHAR), and
'2025-08-03' is a date literal for the
HireDate column (e.g.,
DATE). Ensure the literal values match the column data types to prevent errors.
UPDATE Statement
To correct a misspelled record for an employee with
EmployeeID = 101 (e.g., from “Tony Man” to “Anthony Mann”), use the
UPDATE statement with literal values:
UPDATE Employees
SET FirstName = 'Anthony',
LastName = 'Mann'
WHERE EmployeeID = 101;
The
WHERE clause uses the numeric literal
101 to target the specific row. If
EmployeeID is a primary key or has a unique constraint, only one row is updated, ensuring precision and avoiding unintended changes to other records.
DELETE Statement
To remove the record for an employee with
EmployeeID = 101 who no longer works for the company, use the
DELETE statement:
DELETE FROM Employees
WHERE EmployeeID = 101;
The literal value
101 in the
WHERE clause ensures only the specified row is deleted.
Caution: Omitting the
WHERE clause deletes all rows in the table, so always verify the condition before executing a
DELETE statement.
Best Practices for Literal Values
In the next lesson, you will explore advanced techniques for using literal values, such as combining them with expressions and variables.
