When is ordering not optional in index creation?

In summary, the conversation discusses the concept of creating indexes in a table and the use of unique constraints. It is mentioned that if there are duplicate entries in the table, it is not possible to create a unique index using that key. An example of this is provided with people having the same names and other details. It is suggested to choose something unique, such as SSN or EmployeeID, for creating a unique index. The conversation also touches upon the use of non-unique indexes and the optional use of ordering in creating indexes.
  • #1
momentum
111
0
red marker part in my book is very confusing.
It says you will not succeed to create index by that.

Is it trying to say that if we have two tables and there is a FK relation in them , then I won't be able to create index in either table?

Can you please help with an example exactly when I won't be able to create index as per that red marker.

dCIE4w3.jpg
 

Attachments

  • dCIE4w3.jpg
    dCIE4w3.jpg
    47.8 KB · Views: 376
Last edited:
Technology news on Phys.org
  • #2
If you have data in your table (relations) that has duplicate entries and you try to create a unique index using that key, you should not be able to.

For example you can have people with same names, dates of birth, address, department id, title, etc.

Example where it will fail: https://stackoverflow.com/questions...xisting-table-in-mysql-which-contains-records

You should choose something unique like SSN# or EmployeeID for unique index creation, i.e

likely to be a unique table:
EmployeeID FirstName LastName Title Department Salary

vs

unlikely to be unique:
FirstName Gender Department
 
  • #3
.some confusions ...

cronxeh said:
If you have data in your table (relations) that has duplicate entries ..

1. Are we talking about single table here ? Yes/No?

2 .You wrote table(relations) ? are they same here ?

You should choose something unique like SSN# or EmployeeID for unique index creation
3. You said "unique index creation"...ok ..fine ... can we create non unique index also ?

4. An example,suppose, vehicle_state_code and registration_no combination will make unique tuples in my table.
Shall I be able to create unique index with these two fields? Is it okay?
 
Last edited:
  • #4
The indexes are per specific table.

The “[unique]” in textbook means it’s optional and just adds more constraints. Whenever you see brackets that means it’s an optional specifier.

You can create index without unique constraints and have duplicate entries like second example below in that book where same name and same salary can exist in the table, for example interns with names like Dave/John/Andrew getting paid zero or minimum wage.

For vehicle state code and registration number question, it depends on the data, you can have same registration number but different vehicle state code, and same registration number if it’s been reused by that state. So given how dysfunctional governments are, you can bet there will be duplicates.
 
  • #5
cronxeh said:
.

You can create index without unique constraints and have duplicate entries like second example below in that book where same name and same salary can exist in the table, for example interns with names like Dave/John/Andrew getting paid zero or minimum wage.

okay.

Please look at this script.

create index empindex
on EMPLOYEE(Name Asc,Pay_Rate Desc)

Its very confusing that there is Asc,Desc. What index does with this ordering? does ordering is optional ?
 
  • #6
momentum said:
okay.

Please look at this script.

create index empindex
on EMPLOYEE(Name Asc,Pay_Rate Desc)

Its very confusing that there is Asc,Desc. What index does with this ordering? does ordering is optional ?

Your example is an index (name asc, pay_rate desc). This index will be used when it can be, but it's quite restrictive. For example, these will be quicker:

1. ORDER BY name
2. ORDER BY name, pay_rate desc
3. ORDER BY name desc, pay_rate

And these will be just as slow as before:

1. ORDER BY pay_rate desc
2. ORDER BY pay_rate desc, name
3. ORDER BY name, pay_rate

Ordering is not optional, it just defaults to ascending.
 

1. What is an index in a database?

An index in a database is a data structure that helps to optimize the performance of a database by speeding up data retrieval. It is created on one or more columns of a table and stores the values of those columns in a sorted order, making it easier and faster to search for specific data.

2. Why is index creation important in a database?

Index creation is important in a database because it helps to improve the speed and efficiency of data retrieval. Without indexes, the database would have to scan through every row in a table to find the desired data, which can be time-consuming and resource-intensive. Indexes make the search process more efficient by narrowing down the search to specific columns or values.

3. How is an index created in a database?

An index can be created in a database using a command or statement in the database management system (DBMS). The syntax for creating an index may vary depending on the type of database and DBMS being used. Generally, it involves specifying the table name, the column(s) on which the index will be created, and the type of index (e.g. B-tree, hash, etc.).

4. What are the different types of indexes available in a database?

There are several types of indexes available in a database, including B-tree, hash, bitmap, and functional indexes. B-tree indexes are the most commonly used and are suitable for a wide range of data types. Hash indexes are faster for exact matches but not suitable for range searches. Bitmap indexes are useful for columns with a small number of distinct values. Functional indexes are created on expressions or functions rather than on specific columns.

5. Can indexes be added to a database after the table has been created?

Yes, indexes can be added to a database after the table has been created. This process is known as index creation or index building. However, it is generally more efficient to create indexes when the table is first created, as adding indexes to an existing table can be time-consuming and may require reorganizing the data.

Similar threads

Replies
10
Views
961
  • Programming and Computer Science
Replies
5
Views
2K
Replies
3
Views
810
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Special and General Relativity
Replies
1
Views
676
  • Programming and Computer Science
Replies
5
Views
1K
Replies
2
Views
886
  • Biology and Medical
Replies
14
Views
2K
  • Advanced Physics Homework Help
Replies
1
Views
2K
Back
Top