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
105
votes
21 answers

How to check if multiple array keys exists

I have a variety of arrays that will either contain story & message or just story How would I check to see if an array contains both story and message? array_key_exists() only looks for that single key in the array. Is there a way to do this?
Ryan
  • 2,144
  • 8
  • 28
  • 39
104
votes
3 answers

How to add multiple values to a dictionary key?

I want to add multiple values to a specific key in a python dictionary. How can I do that? a = {} a["abc"] = 1 a["abc"] = 2 This will replace the value of a["abc"] from 1 to 2. What I want instead is for a["abc"] to have multiple values (both 1 and…
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
102
votes
11 answers

How to elegantly rename all keys in a hash in Ruby?

I have a Ruby hash: ages = { "Bruce" => 32, "Clark" => 28 } Assuming I have another hash of replacement names, is there an elegant way to rename all the keys so that I end up with: ages = { "Bruce Wayne" => 32, "Clark Kent"…
Chanpory
  • 3,015
  • 6
  • 37
  • 49
101
votes
8 answers

Initializing a dictionary in python with a key value and no corresponding values

I was wondering if there was a way to initialize a dictionary in python with keys but no corresponding values until I set them. Such as: Definition = {'apple': , 'ball': } and then later i can set them: Definition[key] = something I only want to…
user2989027
  • 1,355
  • 3
  • 9
  • 13
95
votes
6 answers

Jump to Closing tag in VS Code?

I can't seem to find a way to select the beginning of a bracket and jump to the end of it through some key combination or something in VS Code. For example, in atom, this is done with Ctrl + m. I know there is a way to jump to the beginning and end…
Ciprian Turcu
  • 1,113
  • 2
  • 8
  • 16
94
votes
10 answers

Remove all elements from array that do not start with a certain string

I have an array that looks like this: array( 'abc' => 0, 'foo-bcd' => 1, 'foo-def' => 1, 'foo-xyz' => 0, // ... ) How can I retain only the elements that start with foo-?
Alex
  • 66,732
  • 177
  • 439
  • 641
94
votes
6 answers

Java AES and using my own Key

I want to encrypt a string using AES with my own key. But I'm having trouble with the bit length of the key. Can you review my code and see what I need to fix/change. public static void main(String[] args) throws Exception { String username =…
Bernie Perez
  • 12,513
  • 13
  • 46
  • 55
93
votes
12 answers

Characters allowed in php array keys?

I have some php array keys that are populated with a lot of weird characters. Is this allowed? Are there any constraints to what I cannot use?
cgwebprojects
  • 3,382
  • 6
  • 27
  • 40
91
votes
7 answers

What is the use of adding a null key or value to a HashMap in Java?

HashMap allows one null key and any number of null values. What is the use of it?
subhashis
  • 4,629
  • 8
  • 37
  • 52
89
votes
4 answers

What requirements must std::map key classes meet to be valid keys?

I want to map objects of a given class to objects of another. The class I want to use as key, however, was not written by me and is a simple struct with a few values. std::map orders it's contents, and I was wondering how it does it, and if any…
Kian
  • 1,654
  • 1
  • 14
  • 22
86
votes
3 answers

MongoDB - Multiple $or operations

How would I have multiple $or operations? So far I've tried the following but it silently ignores the 2nd $or. { $or: [{a: 2}, {a: 3}], $or: [{b: 5}, {b: 4}] } I assume this is because I'm using two identical keys. Is there any way around…
Mike Neumegen
  • 2,436
  • 1
  • 24
  • 39
85
votes
7 answers

What is difference between const and non const key?

What is the difference between the following two lines? map map_data; map map_data;
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
84
votes
5 answers

How to iterate through table in Lua?

So, I have a table something along these lines: arr = { apples = { 'a', "red", 5 }, oranges = { 'o', "orange", 12 }, pears = { 'p', "green", 7 } } It doesn't seem like it's possible to access them based on their index, and the values…
Lemony Lime
  • 1,113
  • 3
  • 11
  • 12
82
votes
5 answers

android: Softkeyboard perform action when Done key is pressed

I have an EditText. I want that after typing some text, when user presses the Done key of the softkeyboard, it should directly perform some search operation which I have also implemented in a button click event.
Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
81
votes
11 answers

php: how to get associative array key from numeric index?

If I have: $array = array( 'one' =>'value', 'two' => 'value2' ); how do I get the string one back from $array[1] ?
Mazatec
  • 11,481
  • 23
  • 72
  • 108