Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ALTER TABLE Registration
DROP CONSTRAINT registration_student_id_fkey;

ALTER TABLE Registration
ADD CONSTRAINT registration_student_id_fkey
FOREIGN KEY (student_id)
REFERENCES Student (id)
ON DELETE CASCADE;

DELETE FROM Student WHERE id = 1007;


ALTER TABLE Exam
DROP CONSTRAINT exam_student_id_fkey;

ALTER TABLE Exam
ADD CONSTRAINT exam_student_id_fkey
FOREIGN KEY (student_id)
REFERENCES Student (id)
ON DELETE CASCADE;

DELETE FROM Student WHERE id = 1007;

SELECT * FROM student limit 10;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DELETE FROM Exam WHERE student_id = 1007;
DELETE FROM Registration WHERE student_id = 1007;

DELETE FROM Student WHERE id = 1007;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DELETE FROM department WHERE id = 1;

SELECT * FROM department;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE Student ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMP;

UPDATE Student SET deleted_at = CURRENT_TIMESTAMP WHERE id = 1011;

SELECT * FROM Student WHERE id = 1011;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE Student ADD COLUMN is_deleted BOOLEAN DEFAULT FALSE;

UPDATE Student SET is_deleted = TRUE WHERE id = 1010;

SELECT * FROM Student WHERE is_deleted = FALSE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT
schemaname AS schema_name,
tablename AS table_name,
pg_size_pretty(pg_table_size(schemaname || '.' || tablename)) AS table_size
FROM
pg_tables
WHERE
schemaname NOT IN ('information_schema', 'pg_catalog')
ORDER BY
pg_table_size(schemaname || '.' || tablename) DESC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT
relname AS table_name,
pg_size_pretty(pg_total_relation_size(relid)) AS total_size,
pg_size_pretty(pg_relation_size(relid)) AS data_size,
pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) AS index_size
FROM
pg_stat_user_tables
ORDER BY
pg_total_relation_size(relid) DESC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT
table_schema AS 'Database',
table_name AS 'Table',
ROUND((data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)'
FROM
information_schema.tables
WHERE
table_schema = 'University'
ORDER BY
(data_length + index_length) DESC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT
table_schema,
table_name,
pg_size_pretty(pg_total_relation_size(table_schema || '.' || table_name)) AS table_size
FROM
information_schema.tables
WHERE
table_schema NOT IN ('information_schema', 'pg_catalog')
ORDER BY
pg_total_relation_size(table_schema || '.' || table_name) DESC;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SHOW TABLE STATUS FROM University;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
EXEC sp_MSforeachtable 'EXEC sp_spaceused ''?''';

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SELECT
t.name AS table_name,
SUM(a.total_pages) * 8 AS total_space_kb,
SUM(a.used_pages) * 8 AS used_space_kb,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS unused_space_kb
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.object_id = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
GROUP BY
t.name
ORDER BY
total_space_kb DESC;