Learn R Programming

dm (version 0.0.6)

dm_add_pk: Mark a column of a table in a dm object as its primary key

Description

dm_add_pk() marks the specified column as the primary key of the specified table. If check == TRUE, then it will first check if the given column is a unique key of the table. If force == TRUE, the function will replace an already set key.

Usage

dm_add_pk(dm, table, column, check = FALSE, force = FALSE)

Arguments

dm

A dm object.

table

A table in the dm.

column

A column of that table.

check

Boolean, if TRUE, a check is made if the column is a unique key of the table.

force

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.

Value

An updated dm with an additional primary key.

See Also

Other primary key functions: dm_get_all_pks(), dm_get_pk(), dm_has_pk(), dm_rm_pk(), enum_pk_candidates()

Examples

Run this code
# NOT RUN {
library(dplyr)

nycflights_dm <- dm_from_src(src_df(pkg = "nycflights13"))

# the following works
dm_add_pk(nycflights_dm, planes, tailnum)
dm_add_pk(nycflights_dm, airports, faa, check = TRUE)
dm_add_pk(nycflights_dm, planes, manufacturer)

# the following does not work (throws an error)
try(dm_add_pk(nycflights_dm, planes, manufacturer, check = TRUE))
# }

Run the code above in your browser using DataLab