atoti.Table.keys#

property Table.keys: Sequence[str]#

Names of the key columns of the table.

When a table does not have keys, adding the same row twice will result in two identical rows in the table:

>>> table = session.create_table(
...     "No keys", data_types={"Product": "String", "Quantity": "int"}
... )
>>> table.keys
()
>>> table += ("Book", 42)
>>> table += ("Book", 42)
>>> table.row_count
2
>>> table.head().sort_index()
  Product  Quantity
0    Book        42
1    Book        42

The identical rows can be deleted:

>>> table.drop((table["Product"] == "Book") & (table["Quantity"] == 42))
>>> table.row_count
0

When a table has some keys, inserting a new row with key values equal to the ones of an existing row will overwrite the old row:

>>> table = session.create_table(
...     "Some keys",
...     data_types={
...         "Country": "String",
...         "City": "String",
...         "Year": "int",
...         "Population": "int",
...     },
...     keys={"Country", "City"},
... )
>>> table.keys
('Country', 'City')
>>> table += ("France", "Paris", 2000, 9_737_000)
>>> table += ("United States", "San Diego", 2000, 2_681_000)
>>> table.head().sort_index()
                         Year  Population
Country       City
France        Paris      2000     9737000
United States San Diego  2000     2681000
>>> table += ("France", "Paris", 2024, 11_277_000)
>>> table.head().sort_index()
                         Year  Population
Country       City
France        Paris      2024    11277000
United States San Diego  2000     2681000

Key columns cannot have None as their default_value:

>>> session.create_table(  
...     "Keys are non-nullable",
...     data_types={"ID": "int"},
...     default_values={"ID": None},
...     keys={"ID"},
... )
Traceback (most recent call last):
    ...
py4j.protocol.Py4JJavaError: An error occurred ...