From 7c99a83c17e735561ffdaa584a4c8baf4db3b98e Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 12 Dec 2025 17:43:26 +0100
Subject: [PATCH 01/15] Create initial database schema with tables and views
This SQL script creates several tables including audit, department, dept_emp, dept_manager, employee, salary, title, and their respective constraints. It also includes views for current department employees and the latest department dates, along with a trigger function for logging DML operations.
---
schema/schema.sql | 120 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 120 insertions(+)
create mode 100644 schema/schema.sql
diff --git a/schema/schema.sql b/schema/schema.sql
new file mode 100644
index 0000000..33bb2e3
--- /dev/null
+++ b/schema/schema.sql
@@ -0,0 +1,120 @@
+COMMENT ON SCHEMA "public" IS 'standard public schema';
+
+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,
+ 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 TRIGGER salary_log_trigger AFTER DELETE OR UPDATE ON public.salary FOR EACH ROW EXECUTE FUNCTION public.log_dml_operations();
+
+CREATE TABLE "public"."t0" (
+ "id" serial,
+ "username" text NOT NULL,
+ CONSTRAINT "t0_pkey" PRIMARY KEY (id)
+);
+
+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 5eb3804a7bc84e537e9cbf298a53751642c83803 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 12 Dec 2025 17:45:47 +0100
Subject: [PATCH 02/15] Remove salary_log_trigger from schema.sql
Removed salary_log_trigger from salary table.
---
schema/schema.sql | 2 --
1 file changed, 2 deletions(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index 33bb2e3..044430e 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -66,8 +66,6 @@ CREATE TABLE "public"."salary" (
CREATE INDEX "idx_salary_amount" ON ONLY "public"."salary" (amount);
-CREATE TRIGGER salary_log_trigger AFTER DELETE OR UPDATE ON public.salary FOR EACH ROW EXECUTE FUNCTION public.log_dml_operations();
-
CREATE TABLE "public"."t0" (
"id" serial,
"username" text NOT NULL,
From 3db82d3ab6d9347fc86b12e28b13cab94fd4d93f Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 12 Dec 2025 17:49:59 +0100
Subject: [PATCH 03/15] 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 044430e..69af5f3 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -48,7 +48,7 @@ CREATE TABLE "public"."employee" (
"first_name" text NOT NULL,
"last_name" text NOT NULL,
"gender" text NOT NULL,
- "hire_date" date,
+ "hire_date" date NOT NUL,
CONSTRAINT "employee_pkey" PRIMARY KEY (emp_no),
CONSTRAINT "employee_gender_check" CHECK (gender = ANY (ARRAY['M'::text, 'F'::text]))
);
From fb8c88d7f3dd2353cdec4bc58f92029334989659 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 12 Dec 2025 17:55:04 +0100
Subject: [PATCH 04/15] Fix typo in hire_date column definition
---
schema/schema.sql | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index 69af5f3..b5d1ef3 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -48,7 +48,7 @@ CREATE TABLE "public"."employee" (
"first_name" text NOT NULL,
"last_name" text NOT NULL,
"gender" text NOT NULL,
- "hire_date" date NOT NUL,
+ "hire_date" date NOT NULL,
CONSTRAINT "employee_pkey" PRIMARY KEY (emp_no),
CONSTRAINT "employee_gender_check" CHECK (gender = ANY (ARRAY['M'::text, 'F'::text]))
);
From 2837337891b4d5687b81ae7c561b6059273e8bf2 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 12 Dec 2025 18:25:38 +0100
Subject: [PATCH 05/15] Rename employee table to ajkdfjdlkjfa
---
schema/schema.sql | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index b5d1ef3..1c2d400 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -42,14 +42,13 @@ CREATE TABLE "public"."dept_manager" (
CONSTRAINT "dept_manager_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE
);
-CREATE TABLE "public"."employee" (
+CREATE TABLE "public"."ajkdfjdlkjfa" (
"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]))
);
From 1779f711b16f0f1465833b6fcd0c098b83731cb3 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 12 Dec 2025 18:28:09 +0100
Subject: [PATCH 06/15] Add new table 'ajtkdfjklsa' and modify 'employee' table
---
schema/schema.sql | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index 1c2d400..25ee194 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,5 +1,9 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
+CREATE TABLE "public"."ajtkdfjklsa" (
+ "id" serial
+);
+
CREATE TABLE "public"."audit" (
"id" serial,
"operation" text NOT NULL,
@@ -42,13 +46,14 @@ CREATE TABLE "public"."dept_manager" (
CONSTRAINT "dept_manager_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE
);
-CREATE TABLE "public"."ajkdfjdlkjfa" (
+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]))
);
From 377ba42e0d0ef69518a9505206f404239d8e3358 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 12 Dec 2025 18:35:09 +0100
Subject: [PATCH 07/15] Create fakeTable with id and name columns
---
schema/schema.sql | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index 25ee194..f4f07d9 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,7 +1,8 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
-CREATE TABLE "public"."ajtkdfjklsa" (
- "id" serial
+CREATE TABLE "fakeTable" (
+ "idd" serial,
+ "name" VARCHAR(16)
);
CREATE TABLE "public"."audit" (
From ecba8b3a9278b98a718389ce89574594f15c106f Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 12 Dec 2025 18:43:29 +0100
Subject: [PATCH 08/15] 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 f4f07d9..8eb2a38 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,6 +1,6 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
-CREATE TABLE "fakeTable" (
+CREATE TABLE "public"."fakeTable" (
"idd" serial,
"name" VARCHAR(16)
);
From a7a80f208da54ffa5cd3b7578610f79b2a47b767 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 19 Dec 2025 13:01:19 +0100
Subject: [PATCH 09/15] 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 8eb2a38..d5524fc 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,7 +1,7 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
CREATE TABLE "public"."fakeTable" (
- "idd" serial,
+ "idddd" serial,
"name" VARCHAR(16)
);
From b879c9cd6ec135e9b0d54118e9d4467eeea7ca23 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 19 Dec 2025 13:54:53 +0100
Subject: [PATCH 10/15] 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 d5524fc..8eb2a38 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,7 +1,7 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
CREATE TABLE "public"."fakeTable" (
- "idddd" serial,
+ "idd" serial,
"name" VARCHAR(16)
);
From 4c6dd7afcbe2cd1ffedd7341845d4f66d637022d Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 19 Dec 2025 14:17:20 +0100
Subject: [PATCH 11/15] Rename 'idd' column to 'iddd' in fakeTable
---
schema/schema.sql | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index 8eb2a38..e40c219 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,7 +1,7 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
CREATE TABLE "public"."fakeTable" (
- "idd" serial,
+ "iddd" serial,
"name" VARCHAR(16)
);
From 9493725fce9a77343f10be11a69e5010b9a5aeec Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 19 Dec 2025 16:01:25 +0100
Subject: [PATCH 12/15] Rename column 'iddd' to 'idd' in fakeTable
---
schema/schema.sql | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index e40c219..8eb2a38 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,7 +1,7 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
CREATE TABLE "public"."fakeTable" (
- "iddd" serial,
+ "idd" serial,
"name" VARCHAR(16)
);
From 8d799eb6afb6326a4bbcb00a0d0fbfe77ce9a77c Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 19 Dec 2025 16:13:07 +0100
Subject: [PATCH 13/15] Rename column 'idd' to 'iddd' in fakeTable
---
schema/schema.sql | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index 8eb2a38..e40c219 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,7 +1,7 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
CREATE TABLE "public"."fakeTable" (
- "idd" serial,
+ "iddd" serial,
"name" VARCHAR(16)
);
From 9cf4841b7a9b229ee22e6e607fee2780f34789d3 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 19 Dec 2025 16:13:39 +0100
Subject: [PATCH 14/15] Delete unused table 't0' from schema.sql
Removed unused table 't0' from the schema.
---
schema/schema.sql | 6 ------
1 file changed, 6 deletions(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index e40c219..d40e1b0 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -71,12 +71,6 @@ CREATE TABLE "public"."salary" (
CREATE INDEX "idx_salary_amount" ON ONLY "public"."salary" (amount);
-CREATE TABLE "public"."t0" (
- "id" serial,
- "username" text NOT NULL,
- CONSTRAINT "t0_pkey" PRIMARY KEY (id)
-);
-
CREATE TABLE "public"."title" (
"emp_no" integer NOT NULL,
"title" text NOT NULL,
From 08245af8ac5e11e764d3460cf561eaf82c6921e5 Mon Sep 17 00:00:00 2001
From: Adela
Date: Fri, 19 Dec 2025 16:16:00 +0100
Subject: [PATCH 15/15] Rename column 'iddd' to 'idd' in fakeTable
---
schema/schema.sql | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/schema/schema.sql b/schema/schema.sql
index d40e1b0..3112865 100644
--- a/schema/schema.sql
+++ b/schema/schema.sql
@@ -1,7 +1,7 @@
COMMENT ON SCHEMA "public" IS 'standard public schema';
CREATE TABLE "public"."fakeTable" (
- "iddd" serial,
+ "idd" serial,
"name" VARCHAR(16)
);