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
174
votes
19 answers

SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch

I'm not able to setup SSL. I've Googled and I found a few solutions but none of them worked for me. I need some help please... Here's the error I get when I attempt to restart nginx: root@s17925268:~# service nginx restart Restarting nginx: nginx:…
Galou
  • 1,739
  • 2
  • 12
  • 7
172
votes
4 answers

How to access first level keys of a 2d array with a foreach loop?

How do I access the first level key of a two-dimensional array using a foreach loop? I have a $places array like this: [Philadelphia] => Array ( [0] => Array ( [place_name] => XYX …
matthewb
  • 3,462
  • 8
  • 37
  • 53
170
votes
8 answers

get all keys set in memcached

How can I get all the keys set in my memcached instance(s)? I tried googling, but didn't find much except that PHP supports a getAllKeys method, which means it is actually possible to do this somehow. How can I get the same within a telnet…
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
156
votes
11 answers

Is there any way to use a numeric type as an object key?

It seems that when I use a numeric type as a key name in an object, it always gets converted to a string. Is there anyway to actually get it to store as a numeric? The normal typecasting does not seem to work. Example: var userId = 1; console.log(…
Spot
  • 7,962
  • 9
  • 46
  • 55
152
votes
16 answers

Android EditText delete(backspace) key event

How can I detect delete (backspace) key event for a editText? I've tried using TextWatcher, but when the editText is empty, when I press delete key, nothing happens. I want to detect delete key press foe an editText even if it has no text.
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
146
votes
3 answers

Sort rows in data.table in decreasing order on string key `order(-x,v)` gives error on data.table 1.9.4 or earlier

Let's say I have the following data.table in R: library(data.table) DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9) I want to order it by two columns (say columns x and v). I used this: DT[order(x,v)] # sorts first by x then…
nhern121
  • 3,831
  • 6
  • 27
  • 40
143
votes
11 answers

How can I tell if an object has a key value observer attached

If you tell an Objective-C object to removeObservers: for a key path and that key path has not been registered, it cracks a sad, like: Cannot remove an observer for the key path "theKeyPath" from because it is…
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
138
votes
10 answers

PHP Multidimensional Array Searching (Find key by specific value)

I have this multidimensional array. I need to search it and return only the key that matches the value of the "slug". I know there are other threads about searching multidimensional arrays, but I'm not really understanding enough to apply to my…
Ben Kouba
  • 1,383
  • 2
  • 9
  • 4
126
votes
2 answers

How to remove a lua table entry by its key?

I have a lua table that I use as a hashmap, ie with string keys : local map = { foo = 1, bar = 2 } I would like to "pop" an element of this table identified by its key. There is a table.remove() method, but it only takes the index of the element to…
Wookai
  • 20,883
  • 16
  • 73
  • 86
122
votes
11 answers

Easiest way to check for an index or a key in an array?

Using: set -o nounset Having an indexed array like: myArray=( "red" "black" "blue" ) What is the shortest way to check if element 1 is set? I sometimes use the following: test "${#myArray[@]}" -gt "1" && echo "1 exists" || echo "1 doesn't…
Luca Borrione
  • 16,324
  • 8
  • 52
  • 66
116
votes
5 answers

What is the difference between DSA and RSA?

It appears they are both encryption algorithms that require public and private keys. Why would I pick one versus the other to provide encryption in my client server application?
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
115
votes
8 answers

Python: Tuples/dictionaries as keys, select, sort

Suppose I have quantities of fruits of different colors, e.g., 24 blue bananas, 12 green apples, 0 blue strawberries and so on. I'd like to organize them in a data structure in Python that allows for easy selection and sorting. My idea was to put…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
108
votes
7 answers

NLTK python error: "TypeError: 'dict_keys' object is not subscriptable"

I'm following instructions for a class homework assignment and I'm supposed to look up the top 200 most frequently used words in a text file. Here's the last part of the code: fdist1 = FreqDist(NSmyText) vocab=fdist1.keys() vocab[:200] But when I…
user3760644
  • 1,137
  • 2
  • 9
  • 6
107
votes
4 answers

python JSON only get keys in first level

I have a very long and complicated json object but I only want to get the items/keys in the first level! Example: { "1": "a", "3": "b", "8": { "12": "c", "25": "d" } } I want to get 1,3,8 as result! I found this…
TeNNoX
  • 1,899
  • 3
  • 16
  • 27
105
votes
7 answers

Search for highest key/index in an array

How can I get the highest key/index in an array with php? I know how to do it for the values. E.g.: from this array I would like to get 10 as an integer value: $arr = array(1 => "A", 10 => "B", 5 => "C"); I know how I could code it but I was asking…
Raffael Luthiger
  • 2,191
  • 3
  • 19
  • 31