Skip to content

Commit 1bf9ccb

Browse files
authored
Merge pull request #254 from GeoSegun/new-branch
How to Check Indexes on a Table in SQL
2 parents e377b8a + f327b92 commit 1bf9ccb

8 files changed

+23
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SHOW INDEX FROM Student;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\di
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT INDEX_NAME, COLUMN_NAME, SEQ_IN_INDEX, NON_UNIQUE, INDEX_TYPE
2+
FROM information_schema.STATISTICS
3+
WHERE TABLE_SCHEMA = 'University' AND TABLE_NAME = 'Exam';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT i.indexrelid::regclass AS index_name,
2+
array_to_string(array_agg(a.attname), ', ') AS index_columns
3+
FROM pg_index i
4+
JOIN pg_attribute a ON a.attnum = ANY(i.indkey)
5+
WHERE i.indrelid = 'course'::regclass
6+
GROUP BY i.indexrelid;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT indexname, indexdef
2+
FROM pg_indexes
3+
WHERE tablename = 'student';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EXEC sp_helpindex 'Student';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SELECT i.name AS index_name, c.name AS column_name, ic.index_column_id
2+
FROM sys.indexes i
3+
JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
4+
JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
5+
WHERE i.object_id = OBJECT_ID('Student');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT name AS index_name, type_desc, is_unique, is_primary_key
2+
FROM sys.indexes
3+
WHERE object_id = OBJECT_ID('Course')

0 commit comments

Comments
 (0)