Data Integrity means ensuring that the data in a database is accurate, consistent, and reliable over its entire lifecycle. It prevents errors, duplicates, and corruption.
Types of Data Integrity:
-
Entity Integrity
-
Ensures each row in a table is unique, often using a primary key.
-
-
Referential Integrity
-
Ensures relationships between tables stay consistent, using foreign keys to link tables properly.
-
-
Domain Integrity
-
Ensures that data entered is valid and follows defined rules, such as data type, format, or range (e.g., age must be a positive number).
-
-
User-Defined Integrity
-
Custom rules set by the user or application, such as business logic (e.g., an order date cannot be in the future).
-
🔍 Indexing
Indexing is a technique to speed up searching and querying data in a database.
-
An index works like the index in a book: it helps the database quickly locate rows without scanning the entire table.
-
Indexes are created on columns that are often used in search conditions (e.g.,
WHEREclauses).
Benefits of Indexing:
-
Much faster data retrieval
-
Improved performance for SELECT queries
Downsides:
-
Indexes take extra storage space.
-
Slows down INSERT, UPDATE, and DELETE operations slightly because indexes must be updated too.