Data Integrity
Data Integrity
A property that ensures the accuracy and consistency of data. Divided into entity integrity, referential integrity, and domain integrity.
Data Integrity
A property that ensures the accuracy and consistency of data. Divided into entity integrity, referential integrity, and domain integrity.
Data Integrity is a core property of a database that ensures the accuracy, consistency, and validity of data. It protects against incorrect data being stored and inconsistencies arising between data.
Main types of integrity: entity integrity (a Primary Key cannot be NULL or duplicated), referential integrity (a Foreign Key must exist in the referenced table), domain integrity (column values must be within the defined type/range), user-defined integrity (applying business rules).
A DBMS enforces integrity through constraints, Triggers, and Transactions (ACID). Validation is also needed at the application level.
-- 개체 무결성: Primary Key 제약
CREATE TABLE users (
id SERIAL PRIMARY KEY, -- NOT NULL + UNIQUE 자동 적용
email VARCHAR(255) NOT NULL UNIQUE
);
-- 참조 무결성: Foreign Key 제약
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE RESTRICT -- 참조된 사용자 삭제 방지
ON UPDATE CASCADE -- 사용자 ID 변경 시 자동 반영
);
-- 도메인 무결성: CHECK 제약
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10,2) CHECK (price >= 0),
stock INT DEFAULT 0 CHECK (stock >= 0),
status VARCHAR(20) CHECK (status IN ('active', 'inactive', 'discontinued'))
);
-- 사용자 정의 무결성: 트리거
CREATE OR REPLACE FUNCTION check_order_total()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.total < 0 THEN
RAISE EXCEPTION 'Order total cannot be negative';
END IF;
IF NEW.total > 1000000 THEN
RAISE EXCEPTION 'Order total exceeds maximum limit';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER validate_order
BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE FUNCTION check_order_total();
QA: "There's a user_id in the order data that doesn't exist. What happened?"
DBA: "There's no Foreign Key constraint. Someone must have removed it for performance issues."
Backend developer: "I asked for it to be removed. INSERT was slow."
DBA: "The cost of fixing wrong data is much greater than the cost of FK checks. Let's add it back and solve performance with indexes."
Interviewer: "Please explain the types of data integrity and how to ensure them."
Candidate: "Entity integrity is ensured with a PK, referential integrity with an FK, domain integrity with CHECK/NOT NULL, and user-defined integrity with triggers. Consistency during operations is also maintained with ACID transactions."
Interviewer: "How is it handled in NoSQL?"
Candidate: "Since there are no DB-level constraints, validation is done in the application. We use schema validation libraries or the ODM's validator, and for critical logic we choose a DB that supports transactions."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Reviewer: "The status value is just a VARCHAR, so incorrect values could get in."
Developer: "We're validating it with an enum in the application."
Reviewer: "Still, add a CHECK constraint to the DB. Data could come in through other paths, and the DB is the last line of defense."
Developer: "You're right. I'll add CHECK (status IN ('pending', 'confirmed', 'shipped'))."