From 83ce6d3fcc588610dba8c79e21f2b4bc421138bc Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 16:18:34 +0100 Subject: [PATCH 01/11] Create public schema and define multiple tables This SQL script creates a public schema with multiple tables including 'fakeTable', 'audit', 'department', 'dept_emp', 'dept_manager', 'employee', 'salary', and 'title'. It also includes views for current department employees and the latest department dates, as well as a function for logging DML operations. --- schema/schema.sql | 117 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 schema/schema.sql diff --git a/schema/schema.sql b/schema/schema.sql new file mode 100644 index 0000000..3112865 --- /dev/null +++ b/schema/schema.sql @@ -0,0 +1,117 @@ +COMMENT ON SCHEMA "public" IS 'standard public schema'; + +CREATE TABLE "public"."fakeTable" ( + "idd" serial, + "name" VARCHAR(16) +); + +CREATE TABLE "public"."audit" ( + "id" serial, + "operation" text NOT NULL, + "query" text, + "user_name" text NOT NULL, + "changed_at" timestamp(6) with time zone DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "audit_pkey" PRIMARY KEY (id) +); + +CREATE INDEX "idx_audit_changed_at" ON ONLY "public"."audit" (changed_at); + +CREATE INDEX "idx_audit_operation" ON ONLY "public"."audit" (operation); + +CREATE INDEX "idx_audit_username" ON ONLY "public"."audit" (user_name); + +CREATE TABLE "public"."department" ( + "dept_no" text NOT NULL, + "dept_name" text NOT NULL, + CONSTRAINT "department_pkey" PRIMARY KEY (dept_no), + CONSTRAINT "department_dept_name_key" UNIQUE (dept_name) +); + +CREATE TABLE "public"."dept_emp" ( + "emp_no" integer NOT NULL, + "dept_no" text NOT NULL, + "from_date" date NOT NULL, + "to_date" date NOT NULL, + CONSTRAINT "dept_emp_pkey" PRIMARY KEY (emp_no, dept_no), + CONSTRAINT "dept_emp_dept_no_fkey" FOREIGN KEY ("dept_no") REFERENCES "public"."department" ("dept_no") ON DELETE CASCADE, + CONSTRAINT "dept_emp_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE +); + +CREATE TABLE "public"."dept_manager" ( + "emp_no" integer NOT NULL, + "dept_no" text NOT NULL, + "from_date" date NOT NULL, + "to_date" date NOT NULL, + CONSTRAINT "dept_manager_pkey" PRIMARY KEY (emp_no, dept_no), + CONSTRAINT "dept_manager_dept_no_fkey" FOREIGN KEY ("dept_no") REFERENCES "public"."department" ("dept_no") ON DELETE CASCADE, + CONSTRAINT "dept_manager_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE +); + +CREATE TABLE "public"."employee" ( + "emp_no" serial, + "birth_date" date NOT NULL, + "first_name" text NOT NULL, + "last_name" text NOT NULL, + "gender" text NOT NULL, + "hire_date" date NOT NULL, + CONSTRAINT "employee_pkey" PRIMARY KEY (emp_no), + CONSTRAINT "employee_gender_check" CHECK (gender = ANY (ARRAY['M'::text, 'F'::text])) +); + +CREATE INDEX "idx_employee_hire_date" ON ONLY "public"."employee" (hire_date); + +CREATE TABLE "public"."salary" ( + "emp_no" integer NOT NULL, + "amount" integer NOT NULL, + "from_date" date NOT NULL, + "to_date" date NOT NULL, + CONSTRAINT "salary_pkey" PRIMARY KEY (emp_no, from_date), + CONSTRAINT "salary_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE +); + +CREATE INDEX "idx_salary_amount" ON ONLY "public"."salary" (amount); + +CREATE TABLE "public"."title" ( + "emp_no" integer NOT NULL, + "title" text NOT NULL, + "from_date" date NOT NULL, + "to_date" date, + CONSTRAINT "title_pkey" PRIMARY KEY (emp_no, title, from_date), + CONSTRAINT "title_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE +); + +CREATE VIEW "public"."current_dept_emp" AS SELECT l.emp_no, + d.dept_no, + l.from_date, + l.to_date + FROM (public.dept_emp d + JOIN public.dept_emp_latest_date l ON (((d.emp_no = l.emp_no) AND (d.from_date = l.from_date) AND (l.to_date = d.to_date)))); + +CREATE VIEW "public"."dept_emp_latest_date" AS SELECT emp_no, + max(from_date) AS from_date, + max(to_date) AS to_date + FROM public.dept_emp + GROUP BY emp_no; + +CREATE OR REPLACE FUNCTION public.log_dml_operations() + RETURNS trigger + LANGUAGE plpgsql +AS $function$ +BEGIN + IF (TG_OP = 'INSERT') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('INSERT', current_query(), current_user); + RETURN NEW; + ELSIF (TG_OP = 'UPDATE') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('UPDATE', current_query(), current_user); + RETURN NEW; + ELSIF (TG_OP = 'DELETE') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('DELETE', current_query(), current_user); + RETURN OLD; + END IF; + RETURN NULL; +END; +$function$; + From 754935f52fd9fe6c1e0d51c0c1c32ca30379e79e Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 16:39:58 +0100 Subject: [PATCH 02/11] Rename fakeTable to fake_table and change name type --- schema/schema.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schema/schema.sql b/schema/schema.sql index 3112865..ea015e6 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,8 +1,8 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; -CREATE TABLE "public"."fakeTable" ( +CREATE TABLE "public"."fake_table" ( "idd" serial, - "name" VARCHAR(16) + "name" text ); CREATE TABLE "public"."audit" ( From 96d6976863242cf0f0b99fdbb529dc7e6335c1da Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 16:51:07 +0100 Subject: [PATCH 03/11] Rename column 'idd' to 'iddd' in fake_table --- schema/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/schema.sql b/schema/schema.sql index ea015e6..0f9f71e 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,7 +1,7 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; CREATE TABLE "public"."fake_table" ( - "idd" serial, + "iddd" serial, "name" text ); From 381c6fafca8f62bbf5fbb936920be3a1f33c37a2 Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 16:55:29 +0100 Subject: [PATCH 04/11] Rename column 'iddd' to 'id' in fake_table --- schema/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/schema.sql b/schema/schema.sql index 0f9f71e..a7ce541 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,7 +1,7 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; CREATE TABLE "public"."fake_table" ( - "iddd" serial, + "id" serial, "name" text ); From e7e59da528228457d6fbd69fe720ecf2597e1916 Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 17:02:59 +0100 Subject: [PATCH 05/11] Rename 'id' to 'idddd' in fake_table --- schema/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/schema.sql b/schema/schema.sql index a7ce541..0530e2e 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,7 +1,7 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; CREATE TABLE "public"."fake_table" ( - "id" serial, + "idddd" serial, "name" text ); From 220e40945987007d9d6e934fba5f5f139a9c5c16 Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 17:04:41 +0100 Subject: [PATCH 06/11] Rename column 'idddd' to 'idd' in fake_table --- schema/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/schema.sql b/schema/schema.sql index 0530e2e..ea015e6 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,7 +1,7 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; CREATE TABLE "public"."fake_table" ( - "idddd" serial, + "idd" serial, "name" text ); From 7a6582bcb73765a2ecabc17c8ce644922419c21b Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 17:07:31 +0100 Subject: [PATCH 07/11] Rename column 'idd' to 'idddd' in fake_table --- schema/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/schema.sql b/schema/schema.sql index ea015e6..0530e2e 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,7 +1,7 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; CREATE TABLE "public"."fake_table" ( - "idd" serial, + "idddd" serial, "name" text ); From 33c7424135b679391c109b21a8e6f686708a9aa0 Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 17:19:15 +0100 Subject: [PATCH 08/11] Fix table creation syntax in schema.sql --- schema/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/schema.sql b/schema/schema.sql index 0530e2e..2003010 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,6 +1,6 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; -CREATE TABLE "public"."fake_table" ( +CREATE TABLE fake_table ( "idddd" serial, "name" text ); From 3fbc3993570eac30469bc0cea1ca90dab5d417a3 Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 17:20:26 +0100 Subject: [PATCH 09/11] Update schema.sql --- schema/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/schema.sql b/schema/schema.sql index 2003010..d572e30 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,6 +1,6 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; -CREATE TABLE fake_table ( +CREATE TABLE "public".fake_table ( "idddd" serial, "name" text ); From e8ddb7b23f4d09fa25b575fac477b93ba11553b9 Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 17:22:36 +0100 Subject: [PATCH 10/11] Rename fake_table to fakeTable and update id column --- schema/schema.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schema/schema.sql b/schema/schema.sql index d572e30..46c59de 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,7 +1,7 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; -CREATE TABLE "public".fake_table ( - "idddd" serial, +CREATE TABLE "public"."fakeTable" ( + "id" serial, "name" text ); From c345cd84f5286c16005ac08ab969049049f4db1b Mon Sep 17 00:00:00 2001 From: Adela Date: Fri, 19 Dec 2025 17:26:03 +0100 Subject: [PATCH 11/11] Rename fakeTable to fake_table in schema.sql --- schema/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/schema.sql b/schema/schema.sql index 46c59de..a7ce541 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -1,6 +1,6 @@ COMMENT ON SCHEMA "public" IS 'standard public schema'; -CREATE TABLE "public"."fakeTable" ( +CREATE TABLE "public"."fake_table" ( "id" serial, "name" text );