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
1968
votes
20 answers

How to efficiently count the number of keys/properties of an object in JavaScript

What's the fastest way to count the number of keys/properties of an object? Is it possible to do this without iterating over the object? I.e., without doing: var count = 0; for (k in myobj) if (myobj.hasOwnProperty(k)) ++count; (Firefox did…
mjs
  • 63,493
  • 27
  • 91
  • 122
783
votes
17 answers

How to update a value, given a key in a hashmap?

Suppose we have a HashMap in Java. How do I update (increment) the integer-value of the string-key for each existence of the string I find? One could remove and reenter the pair, but overhead would be a concern. Another way would be…
laertis
  • 8,229
  • 4
  • 21
  • 17
750
votes
15 answers

Return a default value if a dictionary key is not available

I need a way to get a dictionary value if its key exists, or simply return None, if it does not. However, Python raises a KeyError exception if you search for a key that does not exist. I know that I can check for the key, but I am looking for…
Spyros
  • 46,820
  • 25
  • 86
  • 129
661
votes
3 answers

Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?

What are the differences between PRIMARY, UNIQUE, INDEX and FULLTEXT when creating MySQL tables? How would I use them?
Sam
626
votes
13 answers

How to permanently add a private key with ssh-add on Ubuntu?

I have a private key protected with a password to access a server via SSH. I have 2 linux (ubuntu 10.04) machines and the behavior of ssh-add command is different in both of them. In one machine, once I use "ssh-add .ssh/identity" and entered my…
duduklein
  • 10,014
  • 11
  • 44
  • 55
607
votes
25 answers

SSH Key: “Permissions 0644 for 'id_rsa.pub' are too open.” on mac

I generate a ssh key pair on my mac and add the public key to my ubuntu server(in fact, it is a virtual machine on my mac),but when I try to login the ubuntu server,it says: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ …
土豆丫
  • 6,175
  • 3
  • 14
  • 16
478
votes
36 answers

JavaScript: Object Rename Key

Is there a clever (i.e. optimized) way to rename a key in a javascript object? A non-optimized way would be: o[ new_key ] = o[ old_key ]; delete o[ old_key ];
Jean Vincent
  • 11,995
  • 7
  • 32
  • 24
453
votes
12 answers

How to filter an associative array comparing keys with values in an indexed array?

The callback function in array_filter() only passes in the array's values, not the keys. If I have: $my_array = array("foo" => 1, "hello" => "world"); $allowed = array("foo", "bar"); What's the best way to delete all keys in $my_array that are not…
maček
  • 76,434
  • 37
  • 167
  • 198
419
votes
25 answers

Replace keys in an array based on another lookup/mapping array

I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an ID number and the value is a count. This is fine for most instances, however I want a…
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
404
votes
13 answers

Get the new record primary key ID from MySQL insert query?

Let's say I am doing a MySQL INSERT into one of my tables and the table has the column item_id which is set to autoincrement and primary key. How do I get the query to output the value of the newly generated primary key item_id in the same…
Amy Neville
  • 10,067
  • 13
  • 58
  • 94
340
votes
7 answers

SQL keys, MUL vs PRI vs UNI

What is the difference between MUL, PRI and UNI in MySQL? I'm working on a MySQL query, using the command: desc mytable; One of the fields is shown as being a MUL key, others show up as UNI or PRI. I know that if a key is PRI, only one record per…
themaestro
  • 13,750
  • 20
  • 56
  • 75
326
votes
12 answers

Get dictionary value by key

How can I get the dictionary value by a key on a function? My function code (and the command I try doesn't work): static void XML_Array(Dictionary Data_Array) { String xmlfile = Data_Array.TryGetValue("XML_File", out…
Matei Zoc
  • 3,739
  • 3
  • 16
  • 18
293
votes
19 answers

How to print a dictionary's key?

I would like to print a specific Python dictionary key: mydic = {} mydic['key_name'] = 'value_name' Now I can check if mydic.has_key('key_name'), but what I would like to do is print the name of the key 'key_name'. Of course I could use…
neydroydrec
  • 6,973
  • 9
  • 57
  • 89
275
votes
9 answers

KeyboardEvent.keyCode deprecated. What does this mean in practice?

According to MDN, we should most definitely not be using the .keyCode property. It is deprecated: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode On W3 school, this fact is played down and there is only a side note saying that…
Jason210
  • 2,990
  • 2
  • 17
  • 18
272
votes
9 answers

Is the primary key automatically indexed in MySQL?

Do you need to explicitly create an index, or is it implicit when defining the primary key? Is the answer the same for MyISAM and InnoDB?
Alex Miller
  • 69,183
  • 25
  • 122
  • 167
1
2 3
99 100