ALTER TABLE talks
ALTER COLUMN session_timeslot SET NOT NULL;
ALTER TABLE talks
ALTER COLUMN session_timeslot DROP NOT NULL
ALTER TABLE talks
ADD UNIQUE (title);
ALTER TABLE talks
ADD UNIQUE (speaker_id, session_timeslot)Si queremos añadir una restricción en una columna (title) que ya no la cumple (ya hay valores nulos), podemos:
UPDATE talks
SET title = 'TBD'
WHERE title IS NULL;
ALTER TABLE talks
ALTER COLUMN title SET NOT NULL;
Condiciones más elaboradas:
ALTER TABLE talks
ADD CHECK (estimated_length > 0);
ALTER TABLE talks
ADD CHECK (estimated_length > 0 AND estimated_length < 120);
ALTER TABLE talks
ADD CHECK (estimated_length < 120 AND date_part('year', session_timeslot) = 2020);Al crear una tabla también:
CREATE TABLE registrations (
id integer NOT NULL,
attendee_id integer NOT NULL,
session_timeslot timestamp NOT NULL,
talk_id integer NOT NULL,
UNIQUE (session_timeslot, attendee_id)
);Otras:
ALTER TABLE attendees
ADD PRIMARY KEY (id);
ALTER TABLE talks
ADD FOREIGN KEY (speaker_id)
REFERENCES speakers (id);Esta última query añade a la tabla talks una columna speaker_id que debe coincidir con la columna id de la tabla speakers