Skip to content

Commit c4ba2de

Browse files
Merge pull request #44 from adela-bytebase/adela-bytebase-patch-40
Create public schema and define multiple tables
2 parents ca8ad6f + c345cd8 commit c4ba2de

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

schema/schema.sql

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
COMMENT ON SCHEMA "public" IS 'standard public schema';
2+
3+
CREATE TABLE "public"."fake_table" (
4+
"id" serial,
5+
"name" text
6+
);
7+
8+
CREATE TABLE "public"."audit" (
9+
"id" serial,
10+
"operation" text NOT NULL,
11+
"query" text,
12+
"user_name" text NOT NULL,
13+
"changed_at" timestamp(6) with time zone DEFAULT CURRENT_TIMESTAMP,
14+
CONSTRAINT "audit_pkey" PRIMARY KEY (id)
15+
);
16+
17+
CREATE INDEX "idx_audit_changed_at" ON ONLY "public"."audit" (changed_at);
18+
19+
CREATE INDEX "idx_audit_operation" ON ONLY "public"."audit" (operation);
20+
21+
CREATE INDEX "idx_audit_username" ON ONLY "public"."audit" (user_name);
22+
23+
CREATE TABLE "public"."department" (
24+
"dept_no" text NOT NULL,
25+
"dept_name" text NOT NULL,
26+
CONSTRAINT "department_pkey" PRIMARY KEY (dept_no),
27+
CONSTRAINT "department_dept_name_key" UNIQUE (dept_name)
28+
);
29+
30+
CREATE TABLE "public"."dept_emp" (
31+
"emp_no" integer NOT NULL,
32+
"dept_no" text NOT NULL,
33+
"from_date" date NOT NULL,
34+
"to_date" date NOT NULL,
35+
CONSTRAINT "dept_emp_pkey" PRIMARY KEY (emp_no, dept_no),
36+
CONSTRAINT "dept_emp_dept_no_fkey" FOREIGN KEY ("dept_no") REFERENCES "public"."department" ("dept_no") ON DELETE CASCADE,
37+
CONSTRAINT "dept_emp_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE
38+
);
39+
40+
CREATE TABLE "public"."dept_manager" (
41+
"emp_no" integer NOT NULL,
42+
"dept_no" text NOT NULL,
43+
"from_date" date NOT NULL,
44+
"to_date" date NOT NULL,
45+
CONSTRAINT "dept_manager_pkey" PRIMARY KEY (emp_no, dept_no),
46+
CONSTRAINT "dept_manager_dept_no_fkey" FOREIGN KEY ("dept_no") REFERENCES "public"."department" ("dept_no") ON DELETE CASCADE,
47+
CONSTRAINT "dept_manager_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE
48+
);
49+
50+
CREATE TABLE "public"."employee" (
51+
"emp_no" serial,
52+
"birth_date" date NOT NULL,
53+
"first_name" text NOT NULL,
54+
"last_name" text NOT NULL,
55+
"gender" text NOT NULL,
56+
"hire_date" date NOT NULL,
57+
CONSTRAINT "employee_pkey" PRIMARY KEY (emp_no),
58+
CONSTRAINT "employee_gender_check" CHECK (gender = ANY (ARRAY['M'::text, 'F'::text]))
59+
);
60+
61+
CREATE INDEX "idx_employee_hire_date" ON ONLY "public"."employee" (hire_date);
62+
63+
CREATE TABLE "public"."salary" (
64+
"emp_no" integer NOT NULL,
65+
"amount" integer NOT NULL,
66+
"from_date" date NOT NULL,
67+
"to_date" date NOT NULL,
68+
CONSTRAINT "salary_pkey" PRIMARY KEY (emp_no, from_date),
69+
CONSTRAINT "salary_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE
70+
);
71+
72+
CREATE INDEX "idx_salary_amount" ON ONLY "public"."salary" (amount);
73+
74+
CREATE TABLE "public"."title" (
75+
"emp_no" integer NOT NULL,
76+
"title" text NOT NULL,
77+
"from_date" date NOT NULL,
78+
"to_date" date,
79+
CONSTRAINT "title_pkey" PRIMARY KEY (emp_no, title, from_date),
80+
CONSTRAINT "title_emp_no_fkey" FOREIGN KEY ("emp_no") REFERENCES "public"."employee" ("emp_no") ON DELETE CASCADE
81+
);
82+
83+
CREATE VIEW "public"."current_dept_emp" AS SELECT l.emp_no,
84+
d.dept_no,
85+
l.from_date,
86+
l.to_date
87+
FROM (public.dept_emp d
88+
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))));
89+
90+
CREATE VIEW "public"."dept_emp_latest_date" AS SELECT emp_no,
91+
max(from_date) AS from_date,
92+
max(to_date) AS to_date
93+
FROM public.dept_emp
94+
GROUP BY emp_no;
95+
96+
CREATE OR REPLACE FUNCTION public.log_dml_operations()
97+
RETURNS trigger
98+
LANGUAGE plpgsql
99+
AS $function$
100+
BEGIN
101+
IF (TG_OP = 'INSERT') THEN
102+
INSERT INTO audit (operation, query, user_name)
103+
VALUES ('INSERT', current_query(), current_user);
104+
RETURN NEW;
105+
ELSIF (TG_OP = 'UPDATE') THEN
106+
INSERT INTO audit (operation, query, user_name)
107+
VALUES ('UPDATE', current_query(), current_user);
108+
RETURN NEW;
109+
ELSIF (TG_OP = 'DELETE') THEN
110+
INSERT INTO audit (operation, query, user_name)
111+
VALUES ('DELETE', current_query(), current_user);
112+
RETURN OLD;
113+
END IF;
114+
RETURN NULL;
115+
END;
116+
$function$;
117+

0 commit comments

Comments
 (0)