Indexes and Constraints Flashcards

1
Q

What are the 6 types of indexes?

A

1- Clustered Index
2- Non-Clustered Index
3- Unique Index
4- Composite Index
5- Covered Index
6- Full-Text Index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Definition: “Clustered Index”

A

This type of index determines the physical order of data in a table. Each table can have only one clustered index. When a clustered index is created, the data in the table is physically reordered to match the order of the index. It is commonly used on columns that are frequently used for sorting or range queries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Definition: “Non-Clustered Index”

A

Unlike clustered indexes, non-clustered indexes do not determine the physical order of data in a table. Instead, they create a separate structure that contains the indexed column(s) along with a reference to the corresponding row in the table. Non-clustered indexes are useful for improving the performance of search queries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Definition: “Unique Index”

A

A unique index ensures that the values in the indexed column(s) are unique, meaning no duplicate values are allowed. It is similar to a non-clustered index, but with an additional constraint of uniqueness.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Definition: “Composite Index”

A

A composite index is created on multiple columns of a table. It allows for efficient searching and sorting based on combinations of the indexed columns. Composite indexes can be useful when queries involve multiple conditions that span multiple columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Definition: “Covered Index”

A

A covered index includes all the columns required to satisfy a query, thereby eliminating the need to access the actual table. It improves query performance by reducing disk I/O.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Definition: “Full-Text Index”

A

Full-text indexing is used for efficient text-based searching. It enables searching for words or phrases within large amounts of text stored in a column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How many clustered indexes we can have on a table?

A

It’s important to note that a table can have only one clustered index because the clustered index determines the physical order of the data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can we set a clustered index?

A

CREATE CLUSTERED INDEX IDX_DimEmployee_EmpCode ON DimEmployee (EmpCode);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can we update/change a clustered index?

A

Changing a clustered index involves dropping the existing clustered index and creating a new one:

DROP INDEX IDX_DimEmployee_EmpCode ON DimEmployee;

CREATE CLUSTERED INDEX IDX_DimEmployee_EmpCode ON DimEmployee (EmpCode);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we delete a clustered index?

A

DROP INDEX IDX_DimEmployee_EmpCode ON DimEmployee;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we have a list of indexes?

A

EXEC sp_helpindex DimEmployee

An example which gives indexes on DimEmployee Table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the relationship between clustered index and the primary key?

A

A clustered index is commonly used as the primary key constraint, providing uniqueness and efficient retrieval of data by the primary key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the pros of the clustered index? (8)

A

Physical Data Order
Faster Data Retrieval
Eliminates Index Lookup
Covering Queries
Supports Primary Key Constraint
Improved Sorting
Data Integrity
Optimized Joins

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the cons of the clustered index? (7)

A

Disk Space
Data Modification Performance
Impact on Insert Performance
Fragmentation
Limited Number of Clustered Indexes
Non-Covering Queries
Inflexible Order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many non-clustered indexes we can have on a table?

A

In Microsoft SQL Server, the limit on the number of non-clustered indexes per table is 999. This limit includes both unique and non-unique non-clustered indexes. It’s important to note that this limit is for non-clustered indexes specifically and does not include the clustered index, which is limited to one per table.

17
Q

How can we set a non-clustered index?

A

CREATE INDEX IDX_DimEmployee_EmpName
ON DimEmployee (EmpName);

18
Q

How can we change a non-clustered index?

A

DROP INDEX IDX_DimEmployee_EmpName ON DimEmployee;
CREATE INDEX IDX_DimEmployee_EmpName
ON DimEmployee (EmpName);

19
Q

What are pros of non-clustered index? (10)

A

Improved Query Performance
Flexible Index Design
Covering Queries
Efficient Data Modification
Index Joining
Index Scans and Seeks
Query Optimization
Conserve Disk Space
Data Integrity
Multiple Indexes

20
Q

What are cons of non-clustered index? (10)

A

Increased Disk Space
Impact on Data Modification
Index Maintenance Overhead
Query Plan Complexity
Fragmentation
Data Duplication
Non-Covering Queries
Index Selection Overhead
Index Overhead for Low-Selectivity Columns
Index Management Complexity

21
Q

What does “Data Manipulation” mean?

A

Insert, Delete, Update

22
Q

What does “Low Selectivity” mean?

A

Columns with low selectivity are those with a relatively small number of distinct values.

23
Q

What does “Covering Query” mean in the context of indexes?

A

An index can include all the columns required for a query. In this case, the query does not need to refer to the actual table to run.

24
Q

How can we set a unique index?

A

CREATE UNIQUE INDEX IDX_DimEmployee ON DimDate (Year, Month, Day)

25
Q

How can we change a unique index?

A

DROP INDEX IDX_DimEmployee ON DimDate
CREATE UNIQUE INDEX IDX_DimEmployee ON DimDate (Year, Month, Day)

26
Q

What are the pros of unique index? (10)

A

Enforces Uniqueness
Data Validation
Efficient Data Retrieval
Improved Query Performance
Primary Key Constraint
Foreign Key Constraints
Index Optimization
Implicit Index Coverage
Index-Nested Loop Join
Clustered Index on Unique Column

27
Q

What are the cons of unique index? (10)

A

Increased Disk Space
Impact on Data Modification
Index Maintenance Overhead
Complexity of Key Selection
Query Plan Complexity
Index Overhead for Low-Selectivity Columns
Index Management Complexity
Limited Uniqueness Scope
Concurrency Issues
Index Naming Conflicts

28
Q

What is the primary key?

A

The primary key (PK) is a concept in relational databases that identifies a unique identifier for each row in a table. It is a column or a combination of columns that uniquely identifies each record in the table. The primary key serves as a means to ensure data integrity, enforce uniqueness, and provide a way to reference individual rows within the table and in related tables.

29
Q

Key characteristics of a primary key

A

Uniquenes
Non-Nullability
Persistence
Stability
Single Value or Combination

30
Q

How can we set a primary key while table creation?

A

CREATE TABLE table_name (
column1 data_type PRIMARY KEY,
column2 data_type,

);

31
Q

How can we set a primary key after the table is created?

A

ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1);

32
Q

How can we change a primary key?

A

ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1);

33
Q

How can we get the list of constraints of database?

A

SELECT * FROM sys.key_constraints

34
Q

How can we set a composite index?

A

CREATE INDEX IDX_DimEmployee_EmpCodeandName
ON DimEmployee (EmpCode, EmpName);

35
Q

How can we change a composite index?

A

DROP INDEX IDX_DimEmployee_EmpCodeandName ON DimEmployee;

36
Q

How can we set a covered index?

A

CREATE INDEX index_name
ON table_name (covered_column1, covered_column2, …);

37
Q

How can we change a covered index?

A

DROP INDEX index_name ON table_name;
CREATE INDEX index_name
ON table_name (covered_column1, covered_column2, …);

38
Q

How can we set a Full-Text index?

A

Prerequisites:
CREATE FULLTEXT CATALOG catalog_name AS DEFAULT;
ALTER FULLTEXT INDEX ON table_name SET (KEY INDEX index_name, STOPLIST OFF);

Creation:
CREATE FULLTEXT INDEX ON table_name (column_name)
KEY INDEX index_name
ON catalog_name
WITH (CHANGE_TRACKING = AUTO);

39
Q

How can we change a Full-Text index?

A

DROP FULLTEXT INDEX ON table_name;
CREATE FULLTEXT INDEX ON table_name (column_name)
KEY INDEX index_name
ON catalog_name
WITH (CHANGE_TRACKING = AUTO);