dm_add_pk() marks the specified columns as the primary 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.
If force == TRUE, the function will replace an already
set key, without altering foreign keys previously pointing to that primary key.
dm_add_pk(dm, table, columns, ..., check = FALSE, force = FALSE)A dm object.
A table in the dm.
Table columns, unquoted.
To define a compound key, use c(col1, col2).
These dots are for future extensions and must be empty.
Boolean, if TRUE, a check is made if the combination of columns is a unique key of the table.
Boolean, if FALSE (default), an error will be thrown if there is already a primary key
set for this table.
If TRUE, a potential old pk is deleted before setting a new one.
An updated dm with an additional primary key.
Other primary key functions:
dm_get_all_pks(),
dm_has_pk(),
dm_rm_pk(),
enum_pk_candidates()
# NOT RUN {
nycflights_dm <- dm(
planes = nycflights13::planes,
airports = nycflights13::airports,
weather = nycflights13::weather
)
nycflights_dm %>%
dm_draw()
# Create primary keys:
nycflights_dm %>%
dm_add_pk(planes, tailnum) %>%
dm_add_pk(airports, faa, check = TRUE) %>%
dm_add_pk(weather, c(origin, time_hour)) %>%
dm_draw()
# Keys can be checked during creation:
try(
nycflights_dm %>%
dm_add_pk(planes, manufacturer, check = TRUE)
)
# }
Run the code above in your browser using DataLab