sql / intermediate
Snippet
Enforcing Data Validity with Multi-Column CHECK Constraints
CHECK constraints guarantee data integrity directly at the schema level. Multi-column CHECK constraints evaluate logical relationships across multiple attributes within the same row before allowing any INSERT or UPDATE transaction to complete.
snippet.sql
sql
1
2
3
4
5
6
7
8
9
CREATE TABLE promotional_events (event_id INT PRIMARY KEY,event_name VARCHAR(100) NOT NULL,start_date DATE NOT NULL,end_date DATE NOT NULL,discount_rate DECIMAL(5, 2) NOT NULL,CONSTRAINT chk_event_dates CHECK (end_date >= start_date),CONSTRAINT chk_discount_range CHECK (discount_rate BETWEEN 0.00 AND 100.00));
Breakdown
1
CREATE TABLE promotional_events (
Begins creating a new relational table schema definition.
2
discount_rate DECIMAL(5, 2) NOT NULL,
Defines an exact fixed-point numeric datatype specifying 5 total digits with 2 decimal places.
3
CONSTRAINT chk_event_dates CHECK (end_date >= start_date),
Enforces cross-column validation preventing chronologically invalid event windows.
4
CONSTRAINT chk_discount_range CHECK (discount_rate BETWEEN 0.00 AND 100.00)
Ensures percentage values stay strictly within valid bounds at the database layer.