September 2013
Intermediate to advanced
350 pages
9h 38m
English
Our query in the previous section relied on the fact that our regions and countries were uniquely identified by their names. A column in a table that is used this way is called a key. Ideally, a key’s values should be unique, just like the keys in a dictionary. We can tell the database to enforce this constraint by adding a PRIMARY KEY clause when we create the table. For example, when we created the PopByRegion table, we should have specified the primary key:
| | >>> cur.execute('''CREATE TABLE PopByRegion ( |
| | Region TEXT NOT NULL, |
| | Population INTEGER NOT NULL, |
| | PRIMARY KEY (Region))''') |
Just as a key in a dictionary can be made up of multiple values, the primary key for a database table can consist ...
Read now
Unlock full access