PostgreSQL constraints மூலம் தரவு integrity ஐ கட்டுப்படுத்துகிறது — இவை table தரவுக்கான rules. நிலையான ones (, , , , , ) தவிர, Postgres constraints மற்றும் நমনीய expressions போன்ற சக்திவாய்ந்த விருப்பங்களைச் சேர்க்கிறது.
PostgreSQL constraints மூலம் தரவு integrity ஐ கட்டுப்படுத்துகிறது — இவை table தரவுக்கான rules. நிலையான ones (, , , , , ) தவிர, Postgres constraints மற்றும் நমনीய expressions போன்ற சக்திவாய்ந்த விருப்பங்களைச் சேர்க்கிறது.
PRIMARY KEYFOREIGN KEYUNIQUENOT NULLCHECKDEFAULTCHECKCREATE TABLE bookings (
id SERIAL PRIMARY KEY, -- unique identifier
room_id INT NOT NULL REFERENCES rooms(id), -- foreign key (referential integrity)
email VARCHAR(255) UNIQUE NOT NULL, -- unique + required
guests INT CHECK (guests > 0 AND guests <= 10), -- value constraint
status TEXT DEFAULT 'pending' -- default value
);
PRIMARY KEY → unique row identifier
FOREIGN KEY → references another table (referential integrity, with ON DELETE options)
UNIQUE → no duplicate values
NOT NULL → must have a value
CHECK → value must satisfy a condition
DEFAULT → default value when not provided
CHECK (price > 0)
CHECK (end_date > start_date) -- logical consistency between columns
CHECK (status IN ('active', 'inactive', 'pending'))
CHECK (email ~ '^[^@]+@[^@]+\.[^@]+$') -- regex validation
Postgres CHECK constraints செல்வாக்கான business rules களை கட்டுப்படுத்தலாம், cross-column conditions மற்றும் regex patterns உட்பட.
-- prevent OVERLAPPING bookings for the same room (impossible with UNIQUE alone!)
CREATE TABLE bookings (
room_id INT,
during TSRANGE, -- a time range
EXCLUDE USING GIST (room_id WITH =, during WITH &&) -- no two rows where room_id is
); -- equal AND time ranges OVERLAP
EXCLUDE constraints (ஒரு Postgres-specific feature) தனிப்பட்ட operator மூலம் conflict செய்யும் rows களைத் தடுக்கிறது — பிரசித்தமாக, overlapping time ranges தடுக்கிறது (double-bookings), இது UNIQUE constraint முடியாது.
-- check constraints at COMMIT instead of immediately (useful for circular references)
FOREIGN KEY (...) REFERENCES ... DEFERRABLE INITIALLY DEFERRED
Constraints தரவு integrity க்கு அপরिহार்யம் — அவை database level இல் தரவு rules களை கட்டுப்படுத்துகின்றன (கடைசி பாதுகாப்பு வரி, எப்போதும் application code ஐ பொருட்படுத்தாமல் கட்டுப்படுத்தப்படுகிறது), எனவே PostgreSQL இன் constraint support ஐ புரிந்துகொள்வது robust databases களை designing க்கு முக்கியமானது.
நிலையான constraints (PRIMARY KEY, FOREIGN KEY referential integrity உடன், UNIQUE, NOT NULL, CHECK, DEFAULT) மற்றும் ஒவ்வொன்றும் என்ன கட்டுப்படுத்துகிறது என்பதை தெரிந்துகொள்வது schema design க்கு அடிப்படையாகும், database-level guarantees (uniqueness, valid references, required values, valid data) வழங்குகிறது, application code மட்டும் முடியாத வகையில் bugs, race conditions, மற்றும் inconsistent data களிலிருந்து பாதுகாக்கிறது.
PostgreSQL இன் CHECK constraints குறிப்பாக திறமையுடையவை, cross-column conditions (logical consistency like end_date > start_date) உட்பட rich business rules களைக் கட்டுப்படுத்துகின்றன மற்றும் regex patterns கூட — schema இல் directly domain rules களை encode செய்ய உங்களை அனுமதிக்கிறது.
விசேษமாக மதிப்புள்ளது Postgres-specific EXCLUDE constraint, இது தனிப்பட்ட operators மூலம் conflicting rows களைத் தடுக்கிறது — மிகவும் குறிப்பாக overlapping time ranges தடுக்கிறது (room/resource க்கு double-bookings), ஒரு சக்திவாய்ந்த திறமை இது UNIQUE constraint முடியாது வழங்க மற்றும் ஒரு பொதுவான real-world problem (scheduling, reservations) ஐ database level இல் elegantly தீர்க்கிறது.
Constraints களை புரிந்துகொள்வது — integrity க்கான நிலையான ones, Postgres இன் நมனீய CHECK expressions, மற்றும் conflict prevention க்கான சக்திவாய்ந்த EXCLUDE — databases களை designing க்கு முக்கியமானது, இது தானாகவே correctness ஐ கட்டுப்படுத்துகிறது.
ডেটা integrity গুருத்வமானது மற்றும் constraints application code முடியாத guarantees களை வழங்குகிறது, மற்றும் Postgres சக்திவாய்ந்த விருப்பங்களை (rich CHECK, overlaps க்கான EXCLUDE) basics களுக்கு அப்பால் வழங்குவதால், PostgreSQL இன் constraint support ஐ புரிந்துகொள்வது மதிப்புள்ளது, நடைமுறையாக-relevant knowledge robust databases களை building க்கு, EXCLUDE constraint குறிப்பாக ஒரு distinctive, useful Postgres capability worth knowing scheduling/reservation systems க்கு.