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
80
votes
6 answers

Is it reasonable to use None as a dictionary key in Python?

None seems to work as a dictionary key, but I am wondering if that will just lead to trouble later. For example, this works: >>> x={'a':1, 'b':2, None:3} >>> x {'a': 1, None: 3, 'b': 2} >>> x[None] 3 The actual data I am working with is…
japhyr
  • 1,710
  • 2
  • 18
  • 24
79
votes
14 answers

How to iterate array keys in Javascript?

I have an array created with this code: var widthRange = new Array(); widthRange[46] = { min:0, max:52 }; widthRange[66] = { min:52, max:70 }; widthRange[90] = { min:70, max:94 }; I want to get each of the values 46, 66, 90 in a loop. I tried for…
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
77
votes
10 answers

switching keys and values in a dictionary in python

Say I have a dictionary like so: my_dict = {2:3, 5:6, 8:9} Is there a way that I can switch the keys and values to get: {3:2, 6:5, 9:8}
me45
  • 1,059
  • 3
  • 14
  • 16
77
votes
3 answers

Python: create dictionary using dict() with integer keys?

In Python, I see people creating dictionaries like this: d = dict( one = 1, two = 2, three = 3 ) What if my keys are integers? When I try this: d = dict (1 = 1, 2 = 2, 3 = 3 ) I get an error. Of course I could do this: d = { 1:1, 2:2, 3:3 } which…
Sindyr
  • 1,277
  • 1
  • 11
  • 16
75
votes
10 answers

Are mutable hashmap keys a dangerous practice?

Is it bad practice to use mutable objects as Hashmap keys? What happens when you try to retrieve a value from a Hashmap using a key that has been modified enough to change its hashcode? For example, given class Key { int a; //mutable field …
donnyton
  • 5,874
  • 9
  • 42
  • 60
74
votes
3 answers

What is the max key size for an array in PHP?

I am generating associative arrays and the key value is a string concat of 1..n columns. Is there a max length for keys that will come back to bite me? If so, I'll probably stop and do it differently.
Ross
  • 743
  • 1
  • 5
  • 4
74
votes
1 answer

Is there a class like Dictionary<> in C#, but for just keys, no values?

I guess another way to phrase this would be "Is there a class like List<> in C#, but optimized for checking whether a particular value is present?" I'm sure for a small set of values List<>.Contains would probably be fine, but what if I have a set…
Chuck Wilbur
  • 2,510
  • 3
  • 26
  • 35
73
votes
15 answers

How to print all key and values from HashMap in Android?

I am trying to use HashMap in Android sample project. Now, am doing sample project for learn android. I just store keys and values in HashMap, i want to show the keys and their values in EditView. I followed below code in my sample project. But,…
Gopinath
  • 5,392
  • 21
  • 64
  • 97
73
votes
1 answer

What's the difference between Ctrl+C and Ctrl+[?

What's the difference between Ctrl+C and Ctrl+[? The documents contain the following that I can find: or CTRL-[ End insert or Replace mode, go back to Normal mode. Finish abbreviation. Note: If your…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
72
votes
7 answers

iterating through Enumeration of hastable keys throws NoSuchElementException error

I am trying to iterate through a list of keys from a hash table using enumeration however I keep getting a NoSuchElementException at the last key in list? Hashtable vars = new Hashtable(); vars.put("POSTCODE","TU1…
David Cunningham
  • 957
  • 2
  • 12
  • 22
71
votes
8 answers

Is there a better PHP way for getting default value by key from array (dictionary)?

In Python one can do: foo = {} assert foo.get('bar', 'baz') == 'baz' In PHP one can go for a trinary operator as in: $foo = array(); assert( (isset($foo['bar'])) ? $foo['bar'] : 'baz' == 'baz' ); I am looking for a golf version. Can I do it…
Yauhen Yakimovich
  • 13,635
  • 8
  • 60
  • 67
71
votes
7 answers

Return copy of dictionary excluding specified keys

I want to make a function that returns a copy of a dictionary excluding keys specified in a list. Considering this dictionary: my_dict = { "keyA": 1, "keyB": 2, "keyC": 3 } A call to without_keys(my_dict, ['keyB', 'keyC']) should…
Juicy
  • 11,840
  • 35
  • 123
  • 212
71
votes
3 answers

How to Generate Unique Public and Private Key via RSA

I am building a custom shopping cart where CC numbers and Exp date will be stored in a database until processing (then deleted). I need to encrypt this data (obviously). I want to use the RSACryptoServiceProvider class. Here is my code to create my…
David Murdoch
  • 87,823
  • 39
  • 148
  • 191
70
votes
2 answers

C++ STL map::erase a non-existing key

Regarding the C++ STL map, erasing by key:- size_type map::erase ( const key_type& x ); Is it legal to erase a non-existing key? i.e. is the snippet below ok? map
fuad
  • 4,265
  • 9
  • 34
  • 32
70
votes
8 answers

How to get Map keys by values in Dart?

In Dart language how to get MAP keys by values? I have a Map like; { "01": "USD", "17": "GBP", "33": "EUR" } And I need to use values to get keys. How do I do that?
Nick
  • 4,163
  • 13
  • 38
  • 63