-- Display the name of the current database.

SELECT CURRENT_DATABASE();

-- Create tables starting with entities that do not depend on foreign keys.

CREATE TABLE enclosures (
    id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    area INTEGER,
    biome VARCHAR(20)
);

-- Delete a table.
DROP TABLE enclosures;

CREATE TABLE keepers (
    id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name VARCHAR NOT NULL,
    salary NUMERIC(10, 3)
);

CREATE TABLE animals (
    id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    enclosure_id INT,
    name VARCHAR NOT NULL,
    is_healthy BOOLEAN DEFAULT TRUE,
    adoption_date DATE,
    note TEXT,
    FOREIGN KEY (enclosure_id) REFERENCES enclosures (id)
);

-- Junction table for the many-to-many relationship.
CREATE TABLE animals_keepers (
    animal_id INT NOT NULL,
    keeper_id INT NOT NULL,
    PRIMARY KEY (animal_id, keeper_id),
    FOREIGN KEY (animal_id) REFERENCES animals (id),
    FOREIGN KEY (keeper_id) REFERENCES keepers (id)
);

-- Change an existing table structure with ALTER TABLE.
ALTER TABLE keepers ADD COLUMN role VARCHAR(255) DEFAULT 'caretaker';

ALTER TABLE keepers RENAME COLUMN role TO position;

ALTER TABLE keepers ALTER COLUMN position TYPE TEXT;

-- This statement intentionally fails because the text default cannot be converted to INT.
ALTER TABLE keepers ALTER COLUMN position TYPE INT;

ALTER TABLE keepers DROP COLUMN position;
