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.

8372 questions
1702
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? It 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 provide…
mjs
  • 57,072
  • 26
  • 82
  • 114
679
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
  • 7,119
  • 3
  • 17
  • 17
625
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
572
votes
12 answers

Return None if 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
  • 41,000
  • 23
  • 80
  • 121
526
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
  • 8,344
  • 10
  • 40
  • 52
392
votes
14 answers

PHP: How to use array_filter() to filter array keys?

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
  • 69,649
  • 33
  • 159
  • 193
366
votes
27 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
  • 10,375
  • 5
  • 30
  • 24
361
votes
19 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: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ …
土豆丫
  • 3,715
  • 2
  • 11
  • 13
301
votes
12 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
  • 8,009
  • 11
  • 48
  • 88
285
votes
6 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
  • 11,712
  • 19
  • 51
  • 72
256
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
  • 65,227
  • 26
  • 112
  • 160
239
votes
20 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
  • 5,701
  • 9
  • 41
  • 77
226
votes
11 answers

get dictionary value by key

How can I get the dictionary value by key on 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 value); } My…
Matei Zoc
  • 2,671
  • 2
  • 12
  • 15
213
votes
11 answers

Python 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
  • 6,437
  • 6
  • 22
  • 31
209
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 VARCHAR(100) NOT NULL, …
shin
  • 28,830
  • 62
  • 170
  • 248
1
2 3
99 100