createTable: create table in SQLite and postgresql databases

Description Usage Arguments Value Examples

Description

create table in SQLite and postgresql databases

Usage

1
2
3
createTable(src, table, types, temporary = FALSE, typeUnique = NULL,
  typePK = NULL, typeRequired = NULL, typeDefault = NULL, typeFK = NULL,
  ...)

Arguments

src

It is the connection as done by dplyr src_* group of methods.

table

Name if table. The function will raise error if table name already exists.

types

Named vector of column names and type. For character data type, we can add "character(30)" to denote length of column. Common types are "integer", "numeric", "character", "date". We can also add "TIMESTAMP" and "SERIAL" data type in postgresql for timestamp, with default CURRENT_TIMESTAMP and primary key with autonumbering.

temporary

(default FALSE). It denotes whether the table is a temporary one.

typeUnique

character vector denoting the columns with only unique values allowed.

typePK

character vector (usually, single string) denoting the Primary Key of the table.

typeRequired

character vector denoting the columns which cannot be left empty.

typeDefault

named list with list(column1 = "defaultVal1", column2 = "defaultval2", ...) format.

typeFK

named list of the format list(fk_column1 = "tableName1withPK", fk_column2 = "tableName2withPK", ...)

onUpdate

to be used only if foreign key is defined. One of the following options: "cascade", "noAction", "setNull".

onDelete

to be used only if foreign key is defined. One of the following options: "cascade", "noAction", "setNull".

Value

Resulting SQL statement.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## Not run: 
createTable(src, table = "tbl1", types = c(id = "numeric", name =
"character(50)", address = "character(50)", age = "numeric"), temporary = F,
typeUnique = c("name", "address"), typePK = "id", typeRequired = "name",
typeDefault = list(age = 20, name = "mee"))

createTable(src, table = "tbl2", types = c(id = "numeric", fk_id  =
"numeric", name = "character(50)", address = "character(50)", age =
"numeric"), temporary = F, typeUnique = "name", typePK = "id", typeRequired =
c("fk_id", "name"), typeDefault = list(age = 20), typeFK = list(fk_id =
"tbl1"), onUpdate = "setNull", onDelete = "cascade")

## End(Not run)

sumprain/dbMapR documentation built on May 30, 2019, 8:37 p.m.