SQL Data Types
Data types specify the kind of data that can be stored in a column of a table. Different database
systems may have slight variations in their implementation, but the common data types include:
1. Numeric Data Types:
o INT: Integer values, typically 4 bytes.
o SMALLINT: Small integer values, typically 2 bytes.
o TINYINT: Very small integer values, typically 1 byte.
o BIGINT: Large integer values, typically 8 bytes.
o FLOAT: Floating-point numbers, with approximate values.
o DOUBLE: Double-precision floating-point numbers.
o DECIMAL(p,s): Fixed-point numbers where p represents the precision and s the
scale.
2. Character and String Data Types:
o CHAR(n): Fixed-length string, padded with spaces if necessary.
o VARCHAR(n): Variable-length string up to n characters.
o TEXT: Stores large text strings, with varying maximum lengths depending on the
DBMS.
3. Date and Time Data Types:
o DATE: Stores date values (year, month, day).
o TIME: Stores time values (hours, minutes, seconds).
o DATETIME: Combines date and time values.
o TIMESTAMP: Similar to DATETIME, often used to track changes (may include
time zone).
4. Boolean Data Type:
o BOOLEAN or BOOL: Represents true or false values.
5. Binary Data Types:
o BLOB: Binary Large Object, used to store large binary data like images or audio
files.
o VARBINARY(n): Variable-length binary data up to n bytes.
6. Special Data Types:
o ENUM: A string object with a value chosen from a list of permitted values.
o SET: A string object that can have zero or more values, each of which must be
chosen from a list of permitted values.
SQL Constraints
Constraints are rules applied to columns in a table that ensure the integrity and validity of data.
Common constraints include:
1. NOT NULL: Ensures that a column cannot have a NULL value.
2. CREATE TABLE Employees (
3. EmployeeID INT NOT NULL,
4. FirstName VARCHAR(50) NOT NULL
5. );
6. UNIQUE: Ensures that all values in a column are distinct from one another.
7. CREATE TABLE Users (
8. UserID INT PRIMARY KEY,
9. Email VARCHAR(255) UNIQUE
10. );
11. PRIMARY KEY: A combination of NOT NULL and UNIQUE; uniquely identifies each
row in a table.
12. CREATE TABLE Orders (
13. OrderID INT PRIMARY KEY,
14. ProductID INT
15. );
16. FOREIGN KEY: Establishes a relationship between two tables by referencing a primary
key in another table. It ensures referential integrity.
17. CREATE TABLE Employees (
18. EmployeeID INT PRIMARY KEY,
19. DepartmentID INT,
20. FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
21. );
22. CHECK: Ensures that all values in a column satisfy a specific condition.
23. CREATE TABLE Products (
24. ProductID INT PRIMARY KEY,
25. Price DECIMAL(10, 2) CHECK (Price > 0)
26. );
27. DEFAULT: Provides a default value for a column when none is specified during the
insertion of a new row.
28. CREATE TABLE Items (
29. ItemID INT PRIMARY KEY,
30. Quantity INT DEFAULT 0
31. );
Summary
Understanding SQL data types and constraints is essential for designing a robust database
schema:
Data Types determine the kind of data stored and how it is processed.
Constraints enforce rules on the data, ensuring accuracy and consistency.