Yes, that's exactly right. We did this with the CREDIT table in the WMDB, where the key was (NM,TT). That means both values are required to uniquely identify a row.
The primary key is how the data is organized (say in the B-tree) and is therefore always fast. Say the NM in the Person table.
Looking up someone by name would involve linear search, which is O(n).
But if we build an index, (say a hashtable) in addition to the B-tree, looking up by name is now O(1).
We don't use them directly. The database does. The query optimizer will use an index if it believes it's useful.
Speeds up queries that use the column(s) that the index is based on.
speed and, possibly, uniqueness. Giving a raise to employee ID 24601 is different from giving a raise to everyone named "Jean Val Jean" (there might be more than one).
You can build one on any column you think might be useful, in the sense of often using that column in a query. So an index on name
makes sense, but maybe not on birthdate
.
Why not build an index on every column, just in case? Well, there is a downside: they have to be kept up to date, which slows down inserts, updates and deletes. (Think about updating the hashtable of names.) So they aren't completely free.
Yes.
Referential integrity means that a foreign key is valid: it references something that exists. Take our Pet and Owner. Each Pet has an Owner, so each row in the Pet table has an OID.
The OID is a foreign key. It's key in a different table. It's not a key in the Pet table, since an owner might have more than one pet (Homer has 3 pets).
But OID is a key in the Owner table.
Referential integrity means that the owner exists.
How could it not? The owner was deleted, but not the pet.
So, before deleting an owner, we need to check the Pet table to see if there are any pets with that OID.
Similarly, albums and tracks
Does the example above help?
Before we delete Homer or any owner, we need to check the Pet table to see if Homer has any pets. An index helps make that fast.
It's a different data structure for tables, compared to MyISAM, which used to be the default default.
The InnoDB data structure has additional bookkeeping info to do referential integrity checking.
Referential integrity is mostly one-way: making sure that the one in a one:many
relationship doesn't become zero
Importing CSV is just like inserting a row. So
compared to importing a CSV file like:Primary keys would only affect the importing if your CSV file has duplicate values for the primary key column, in which case you will get an error. You can't insert (1,Homer) twice into the Pet table. It's okay to have duplicates of the name, but not the OID.
Yes, you'll get an error.