-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_db.sql
More file actions
119 lines (106 loc) · 4.75 KB
/
test_db.sql
File metadata and controls
119 lines (106 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-- Sets the search path to the schema where tables are located
SET search_path TO sample;
-- Checks if the 'Questions' table exists
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'sample' AND table_name = 'Questions') THEN
RAISE EXCEPTION 'Table "Questions" does not exist!';
ELSE
RAISE NOTICE 'Table "Questions" exists.';
END IF;
END $$;
-- Checks if the 'Questions' table has the correct columns
DO $$
BEGIN
-- Checks column 'id'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Questions' AND column_name = 'id') THEN
RAISE EXCEPTION 'Column "id" does not exist in "Questions" table!';
ELSE
RAISE NOTICE 'Column "id" exists in "Questions" table.';
END IF;
-- Checks column 'text'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Questions' AND column_name = 'text') THEN
RAISE EXCEPTION 'Column "text" does not exist in "Questions" table!';
ELSE
RAISE NOTICE 'Column "text" exists in "Questions" table.';
END IF;
-- Checks column 'chapterid'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Questions' AND column_name = 'chapterid') THEN
RAISE EXCEPTION 'Column "chapterid" does not exist in "Questions" table!';
ELSE
RAISE NOTICE 'Column "chapterid" exists in "Questions" table.';
END IF;
-- Checks column 'options'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Questions' AND column_name = 'options') THEN
RAISE EXCEPTION 'Column "options" does not exist in "Questions" table!';
ELSE
RAISE NOTICE 'Column "options" exists in "Questions" table.';
END IF;
-- Checks column 'multiplecorrectanswersallowed'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Questions' AND column_name = 'multiplecorrectanswersallowed') THEN
RAISE EXCEPTION 'Column "multiplecorrectanswersallowed" does not exist in "Questions" table!';
ELSE
RAISE NOTICE 'Column "multiplecorrectanswersallowed" exists in "Questions" table.';
END IF;
END $$;
-- Checks if the 'Options' table exists
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'sample' AND table_name = 'Options') THEN
RAISE EXCEPTION 'Table "Options" does not exist!';
ELSE
RAISE NOTICE 'Table "Options" exists.';
END IF;
END $$;
-- Checks if the 'Options' table has the correct columns
DO $$
BEGIN
-- Checks column 'id'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Options' AND column_name = 'id') THEN
RAISE EXCEPTION 'Column "id" does not exist in "Options" table!';
ELSE
RAISE NOTICE 'Column "id" exists in "Options" table.';
END IF;
-- Checks column 'text'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Options' AND column_name = 'text') THEN
RAISE EXCEPTION 'Column "text" does not exist in "Options" table!';
ELSE
RAISE NOTICE 'Column "text" exists in "Options" table.';
END IF;
-- Checks column 'iscorrect'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Options' AND column_name = 'iscorrect') THEN
RAISE EXCEPTION 'Column "iscorrect" does not exist in "Options" table!';
ELSE
RAISE NOTICE 'Column "iscorrect" exists in "Options" table.';
END IF;
-- Checks column 'questionid'
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'sample' AND table_name = 'Options' AND column_name = 'questionid') THEN
RAISE EXCEPTION 'Column "questionid" does not exist in "Options" table!';
ELSE
RAISE NOTICE 'Column "questionid" exists in "Options" table.';
END IF;
END $$;
-- Checks the number of records in 'Questions' table
DO $$
DECLARE
questions_count INT;
BEGIN
SELECT COUNT(*) INTO questions_count FROM sample."Questions";
IF questions_count != 40 THEN
RAISE EXCEPTION 'Questions table should have 40 records but found %', questions_count;
ELSE
RAISE NOTICE 'Questions table has 40 records.';
END IF;
END $$;
-- Checks the number of records in 'Options' table
DO $$
DECLARE
options_count INT;
BEGIN
SELECT COUNT(*) INTO options_count FROM sample."Options";
IF options_count != 160 THEN
RAISE EXCEPTION 'Options table should have 160 records but found %', options_count;
ELSE
RAISE NOTICE 'Options table has 160 records.';
END IF;
END $$;