Questions tagged [key]

A unique identifier used to retrieve a paired value. Used in hash tables and databases.

Keys In General

A "key" is unique identifier used to retrieve a paired value. In an associative data structure such as hashtable/hashmap or balanced tree, as in a database, each value is paired with a key; an arbitrary value can be retrieved given its key.

In opposition to an array index, a key doesn't necessarily determine the physical position of the value in the data structure.


Keys in Relational Databases

Definition

  • A "superkey" is any set of attributes that, when taken together, uniquely identify rows in the table.
  • A minimal1 superkey is called "candidate key", or just "key".

1 That is, a superkey that would stop being unique (and therefore, being a superkey) if any of the attributes were removed from it.

Kinds of Keys

All keys are logically equivalent, but one of them is chosen as "primary", for historical reasons and convenience. The remaining keys are called "alternate".

In addition to that, "natural" keys can be distinguished from "surrogate" keys, based on their "meaning".

Keys and Indexes

A Key is a different concept from an index. A Key is a logical concept that changes the meaning of data, which the index doesn't. An index merely changes the time needed to manipulate the data, shortening it significantly if used properly.

An index can exist on non-key columns. Conversely, a key can exist on non-indexed columns, although this is usually forbidden in practice, for performance reasons.

Keys and Foreign Keys

A foreign key references a key. The foreign key itself doesn't have to be a key.

Keys and Tables

In relational databases, a "table" is a physical representation of the mathematical concept of a "relation", which is a set. A set either contains an element or it doesn't. It cannot contain the same element multiple times. If there isn't at least one key in the table, then the same row can exist multiple times in the table, so the table no longer represents a set and therefore no longer represents a relation.

In other words, a database without keys is not relational database.

9456 questions
69
votes
9 answers

PHP: Get key from array?

I am sure that this is super easy and built-in function in PHP, but I have yet not seen it. Here's what I am doing for the moment: foreach($array as $key => $value) { echo $key; // Would output "subkey" in the example array …
Industrial
  • 41,400
  • 69
  • 194
  • 289
68
votes
8 answers

Class is not key value coding-compliant

I know the meaning of this error, but I'm really struggling with it, and I need someone's help : 2010-09-21 15:03:11.562 Stocks[5605:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[
Rob
  • 2,766
  • 5
  • 32
  • 39
65
votes
8 answers

How to sort a list with two keys but one in reverse order?

I was wondering what would be a Pythonic way of sorting a list of tuples by two keys whereby sorting with one (and only one) key would be in a reverse order and sorting with the the other would be case insensitive. More specifically, I have a list…
63
votes
10 answers

Google map signed api key errors in Android

When I switched from my debug map key to my signed map key my maps stop working. I get the following errors in logcat: 09-03 18:18:04.112: WARN/System.err(4073): IOException processing: 26 09-03 18:18:04.112: WARN/System.err(4073):…
Jen
  • 653
  • 1
  • 5
  • 5
63
votes
5 answers

How can I see if a Perl hash already has a certain key?

I have a Perl script that is counting the number of occurrences of various strings in a text file. I want to be able to check if a certain string is not yet a key in the hash. Is there a better way of doing this altogether? Here is what I am…
user105574
62
votes
8 answers

How to remove duplicates based on a key in Mongodb?

I have a collection in MongoDB where there are around (~3 million records). My sample record would look like, { "_id" = ObjectId("50731xxxxxxxxxxxxxxxxxxxx"), "source_references" : [ "_id" :…
user1518659
  • 2,198
  • 9
  • 29
  • 40
61
votes
1 answer

Relations on composite keys using sqlalchemy

I have this simple model of Author - Books and can't find a way to make firstName and lastName a composite key and use it in relation. Any ideas? from sqlalchemy import create_engine, ForeignKey, Column, String, Integer from sqlalchemy.orm import…
mdob
  • 2,224
  • 3
  • 22
  • 25
61
votes
5 answers

There are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key

In SQL Server, I got this error: There are no primary or candidate keys in the referenced table 'BookTitle' that match the referencing column list in the foreign key 'FK__BookCopy__Title__2F10007B'. I first created a relation called the BookTitle…
user2622438
  • 665
  • 1
  • 6
  • 9
61
votes
7 answers

How to check if key exists in list of dicts in python?

Say I have a list of dicts that looks like this: [{1: "a"}, {2: "b"}] What is the pythonic way to indicate if a certain key is in one of the dicts in the list?
user2057574
  • 779
  • 2
  • 8
  • 12
61
votes
5 answers

Find dictionary items whose key matches a substring

I have a large dictionary constructed like so: programs['New York'] = 'some values...' programs['Port Authority of New York'] = 'some values...' programs['New York City'] = 'some values...' ... How can I return all elements of programs whose key…
Abid A
  • 7,588
  • 4
  • 32
  • 32
60
votes
10 answers

ssh-keygen' is not recognized as an internal or external command

I am trying to add ssh keys for usage on github but on my xp on command prompt ssh-keygen does not work. It gives me the following error ssh-keygen' is not recognized as an internal or external command. Is there an alternative for generating keys…
pal4life
  • 3,210
  • 5
  • 36
  • 57
59
votes
10 answers

What is the best way to use two keys with a std::map?

I have a std::map that I'm using to store values for x and y coordinates. My data is very sparse, so I don't want to use arrays or vectors, which would result in a massive waste of memory. My data ranges from -250000 to 250000, but I'll only have a…
Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
59
votes
3 answers

Remove blacklist keys from array in PHP

I have an associative array of data and I have an array of keys I would like to remove from that array (while keeping the remaining keys in original order -- not that this is likely to be a constraint). I am looking for a one liner of php to do…
hackartist
  • 5,172
  • 4
  • 33
  • 48
58
votes
4 answers

How to modify key in a dictionary in C#

How can I change the value of a number of keys in a dictionary. I have the following dictionary : SortedDictionary>> I want to loop through this sorted dictionary and change the key to key+1 if the key value…
Bernard Larouche
  • 1,211
  • 2
  • 13
  • 22
58
votes
5 answers

How to get the index with the key in a dictionary?

I have the key of a python dictionary and I want to get the corresponding index in the dictionary. Suppose I have the following dictionary, d = { 'a': 10, 'b': 20, 'c': 30} Is there a combination of python functions so that I can get the index…
chapter3
  • 914
  • 2
  • 12
  • 21