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
249
votes
13 answers

Dictionary: Get list of values for list of keys

Is there a built-in/quick way to use a list of keys to a dictionary to get a list of corresponding items? For instance I have: >>> mydict = {'one': 1, 'two': 2, 'three': 3} >>> mykeys = ['three', 'one'] How can I use mykeys to get the corresponding…
FazJaxton
  • 7,544
  • 6
  • 26
  • 32
248
votes
9 answers

How to get key names from JSON using jq

curl http://testhost.test.com:8080/application/app/version | jq '.version' | jq '.[]' The above command outputs only the values as below: "madireddy@test.com" "2323" "test" "02-03-2014-13:41" "application" How can I get the key names instead…
Ezhilan Mahalingam
  • 2,838
  • 3
  • 16
  • 16
245
votes
5 answers

What's the difference between using INDEX vs KEY in MySQL?

I know how to use INDEX as in the following code. And I know how to use foreign key and primary key. CREATE TABLE tasks ( task_id int unsigned NOT NULL AUTO_INCREMENT, parent_id int unsigned NOT NULL DEFAULT 0, task …
shin
  • 31,901
  • 69
  • 184
  • 271
240
votes
13 answers

Looking for ALT+LeftArrowKey solution in zsh

I just recently switched from bash to zsh, however I miss my Alt+LeftArrowKey and Alt+RightArrowKey to go back and forth a word at a time. Right now, if I press Alt+LeftArrowKey I go back a couple of letters and then I'm stuck. I won't go any…
Mads Ohm Larsen
  • 3,315
  • 3
  • 20
  • 22
239
votes
15 answers

How can I get dictionary key as variable directly in Python (not by searching from value)?

Sorry for this basic question but my searches on this are not turning up anything other than how to get a dictionary's key based on its value which I would prefer not to use as I simply want the text/name of the key and am worried that searching by…
Rick
  • 16,612
  • 34
  • 110
  • 163
223
votes
5 answers

Which characters are valid/invalid in a JSON key name?

Are there any forbidden characters in key names, for JavaScript objects or JSON strings? Or characters that need to be escaped? To be more specific, I'd like to use "$", "-" and space in key names.
Christophe
  • 27,383
  • 28
  • 97
  • 140
222
votes
18 answers

How can I get a random key-value pair from a dictionary?

In Python, given a dictionary like { 'VENEZUELA': 'CARACAS', 'CANADA': 'OTTAWA' } How can I choose a random item (key-value pair)? What if I only need the key, or only the value - can it be optimized?
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
207
votes
7 answers

Accessing dict_keys element by index in Python3

I'm trying to access a dict_key's element by its index: test = {'foo': 'bar', 'hello': 'world'} keys = test.keys() # dict_keys object keys.index(0) AttributeError: 'dict_keys' object has no attribute 'index' I want to get foo. same…
fj123x
  • 6,904
  • 12
  • 46
  • 58
203
votes
10 answers

How can I find the keys of an object?

I know in JavaScript, objects double as hashes, but I have been unable to find a built-in function to get the keys: var h = {a:'b', c:'d'}; I want something like var k = h.keys() ; // k = ['a', 'c']; It is simple to write a function myself to…
Pat
  • 36,282
  • 18
  • 72
  • 87
199
votes
15 answers

Get HTML5 localStorage keys

I'm just wondering how to get all key values in localStorage. I have tried to retrieve the values with a simple JavaScript loop for (var i=1; i <= localStorage.length; i++) { alert(localStorage.getItem(i)) } But it works only if the keys are…
Simone
  • 20,302
  • 14
  • 79
  • 103
194
votes
19 answers

Surrogate vs. natural/business keys

Here we go again, the old argument still arises... Would we better have a business key as a primary key, or would we rather have a surrogate id (i.e. an SQL Server identity) with a unique constraint on the business key field? Please, provide…
Manrico Corazzi
  • 11,299
  • 10
  • 48
  • 62
194
votes
8 answers

Check if value already exists within list of dictionaries in Python?

I've got a Python list of dictionaries as follows: a = [ {'main_color': 'red', 'second_color':'blue'}, {'main_color': 'yellow', 'second_color':'green'}, {'main_color': 'yellow', 'second_color':'blue'}, ] I'd like to check whether a…
AP257
  • 89,519
  • 86
  • 202
  • 261
184
votes
16 answers

Custom key-sort a flat associative based on another array

Is it possible in PHP to do something like this? How would you go about writing a function? Here is an example. The order is the most important thing. $customer['address'] = '123 fake st'; $customer['name'] = 'Tim'; $customer['dob'] =…
alex
  • 479,566
  • 201
  • 878
  • 984
182
votes
14 answers

How do you test a public/private DSA keypair?

Is there an easy way to verify that a given private key matches a given public key? I have a few *.puband a few *.key files, and I need to check which go with which. Again, these are pub/key files, DSA. I would really prefer a one-liner of some…
Loki
  • 6,205
  • 4
  • 24
  • 36
178
votes
7 answers

How do I make a composite key with SQL Server Management Studio?

How do I make a composite key with SQL Server Management Studio? I want two INT columns to form the identity (unique) for a table
mrblah
  • 99,669
  • 140
  • 310
  • 420