Learn R Programming

PivotalR (version 0.1.18.5)

key: Get or set the primary key for a table

Description

This function gets or sets the primary key for a db.obj table.

Usage

key(x)

key(x) <- value

Arguments

x

is a db.obj object.

value

must be a string.

Value

The return value is the primary key of the table.

Details

key() will return the primary key of a table. If the primary key is not set, key() will return the character 0. If key() is being used to set the primary key, then value must be a string, and it must match one of the column names in the table.

If this function is used to change the primary key to a new column name, this function does NOT check if all the values in that column are unique.

See Also

lk or lookat Displays the contents of a db.obj object.

Examples

Run this code
# NOT RUN {
<!-- %% @test .port Database port number -->
<!-- %% @test .dbname Database name -->
## set up the database connection
## Assume that .port is port number and .dbname is the database name
cid <- db.connect(port = .port, dbname = .dbname, verbose = FALSE)

## create a table from the example data.frame "abalone"
tmp <- as.db.data.frame(abalone, conn.id = cid, verbose = FALSE)
x <- db.data.frame(content(tmp), key = 'id', conn.id = cid, verbose = FALSE)

## getting the primary key
key(x) # Display the primary key for x

## Changing the primary key
key(x) <- 'length'

db.disconnect(cid, verbose = FALSE)
# }

Run the code above in your browser using DataLab