spring-petclinic/src/main/resources/db/h2/V1__add_appointment.sql
Bastriver c009d77305 changes:
1.add appointment feature;
2.add new api for creation,query and cancel.
3.add new table for appointment.
2026-01-28 11:39:31 +08:00

23 lines
No EOL
1 KiB
SQL

-- Create appointments table
CREATE TABLE appointments (
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
pet_id INTEGER NOT NULL,
owner_id INTEGER NOT NULL,
start_time TIMESTAMP NOT NULL,
end_time TIMESTAMP NOT NULL,
status VARCHAR(20) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
version INTEGER DEFAULT 0
);
-- Add foreign key constraints
ALTER TABLE appointments ADD CONSTRAINT fk_appointments_pets FOREIGN KEY (pet_id) REFERENCES pets (id);
ALTER TABLE appointments ADD CONSTRAINT fk_appointments_owners FOREIGN KEY (owner_id) REFERENCES owners (id);
-- Create indexes for performance
CREATE INDEX appointments_pet_id ON appointments (pet_id);
CREATE INDEX appointments_start_time ON appointments (start_time);
CREATE INDEX appointments_pet_time_range ON appointments (pet_id, start_time, end_time);
-- Add check constraint to ensure end_time is after start_time
ALTER TABLE appointments ADD CONSTRAINT chk_appointment_time_order CHECK (end_time > start_time);