dm_add_uk | R Documentation |
dm_add_uk()
marks the specified columns as a unique key of the specified table.
If check == TRUE
, then it will first check if
the given combination of columns is a unique key of the table.
dm_add_uk(dm, table, columns, ..., check = FALSE)
dm |
A |
table |
A table in the |
columns |
Table columns, unquoted.
To define a compound key, use |
... |
These dots are for future extensions and must be empty. |
check |
Boolean, if |
The difference between a primary key (PK) and a unique key (UK) consists in the following:
When a local dm
is copied to a database (DB) with copy_dm_to()
, a PK will be set on the DB by default, whereas a UK is being ignored.
A PK can be set as an autoincrement
key (also implemented on certain DBMS when the dm
is transferred to the DB)
There can be only one PK for each table, whereas there can be unlimited UKs
A UK will be used, if the same table has an autoincrement PK in addition, to ensure that during delta load processes
on the DB (cf. dm_rows_append()
) the foreign keys are updated accordingly.
If no UK is available, the insertion is done row-wise, which also ensures a correct matching, but can be much slower.
A UK can generally enhance the data model by adding additional information
There can also be implicit UKs, when the columns addressed by a foreign key are neither a PK nor a UK.
These implicit UKs are also listed by dm_get_all_uks()
An updated dm
with an additional unqiue key.
Other primary key functions:
dm_add_pk()
,
dm_get_all_pks()
,
dm_get_all_uks()
,
dm_has_pk()
,
dm_rm_pk()
,
dm_rm_uk()
,
enum_pk_candidates()
nycflights_dm <- dm(
planes = nycflights13::planes,
airports = nycflights13::airports,
weather = nycflights13::weather
)
# Create unique keys:
nycflights_dm %>%
dm_add_uk(planes, tailnum) %>%
dm_add_uk(airports, faa, check = TRUE) %>%
dm_add_uk(weather, c(origin, time_hour)) %>%
dm_get_all_uks()
# Keys can be checked during creation:
try(
nycflights_dm %>%
dm_add_uk(planes, manufacturer, check = TRUE)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.