rg_backend_academy/lecture_2/01-definicni-prikazy.sql

43 lines
1.1 KiB
SQL

-- 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.