How to create SQL table using DBI library in R
Last Updated :
24 Apr, 2025
DBI library in R programming is used for interacting with different types of database systems such as MySQL for different types of professional work like data analysis using R language. We can easily connect to the database, run queries and retrieve results from the database in the R environment with the DBI library. In this article, we looked at how to write SQL tables in R using the DBI library.
Before we start, make sure you have R and R Studio installed on your computer, which contains the following R packages: DBI and RMySQL. In this article, we are using MySQL Database. If you don’t have these packages installed, you can install them by running the following code in the console of R Studio:
install.packages("DBI")
install.packages("RMySQL")
Creating SQL table using DBI library in R
Here is a step-by-step guide to create an SQL table using DBI Library in R:
Step 1: Importing Required Library
R
library (DBI)
library (RMySQL)
|
Step 2: Connecting to the Database
After successfully importing the library, we now need to connect to the database. DBI library has a function dbConnect() which we can use to establish a connection with the database using the appropriate driver.
Syntax:
dbConnect(RMySQL::MySQL(), dbname = "database_name", host = "localhost", port = 3306,
user = "username", password = "password")
R
connection <- dbConnect (RMySQL:: MySQL (),
dbname = "Rlanguage" ,
host = "localhost" ,
port = 3306,
user = "username" ,
password = "password" )
|
We used a database named RLanguage on the localhost machine. As you see, we specify the port number, username, and password, which is required to access the database.
Step 3: Creating a Table
After successfully Connecting with Database, now we can start writing our query for performing specific operations on our database. We can use the dbSendQuery() function to send a SQL query to the database.
Here’s how we can create a table using R language:
R
dbSendQuery (connection, "CREATE TABLE geeksforgeeks (
S_NO INT (11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR (50),
Feedback VARCHAR (1000)
)")
|
Output:
After executing the dbSendQuery(), we can see a table named “geeksforgeeks” is created in the MySQL server with the following column names – S_NO, Name, and Feedback.

Table created in MySQL server
Step 4: Inserting Data into Table
In this step, we will insert data into the table with dbSendQuery() function by using SQL INSERT INTO statement followed by the table name and values which we want to insert in the database like this:
Syntax:
dbSendQuery(connection,"INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);")
R
dbSendQuery (connection, "INSERT INTO geeksforgeeks (Name, Feedback)
VALUES ( 'Raj' , 'I love GeeksForGeeks 3000' ),
( 'Yash' , 'I love Reading Article on GeeksForGeeks' );")
|
Output:
Now we will execute the following query in our MySQL shell to see if our data is inserted into the table.
select * from geeksforgeeks;
We can see that the rows are inserted into the table geeksforgeeks.

Data inserted in the table
Similar Reads
How to Create Tables in R?
In this article, we will discuss how to create tables in R Programming Language. Method 1: Create a table from scratch We can create a table by using as.table() function, first we create a table using matrix and then assign it to this method to get the table format. Syntax: as.table(data) Example: I
2 min read
How to Create Summary Tables in R?
In this article, we will discuss how to create summary tables in R Programming Language. The summary table contains the following information: vars: represents the column numbern: represents the number of valid casesmean: represents the mean valuemedian: represents the median valuetrimmed: represent
4 min read
How to Create Pivot Tables in R?
In this article, we will discuss how to create the pivot table in the R Programming Language. The Pivot table is one of Microsoft Excel's most powerful features that let us extract the significance from a large and detailed data set. A Pivot Table often shows some statistical value about the dataset
2 min read
How to create table in Ruby on Rails?
In Ruby on Rails, creating tables involves using migrations, which are a powerful mechanism for managing database schema changes. Here's a detailed breakdown of the process: 1. Database Setup (Optional): While APIs can function without databases, many Rails applications use them for data persistence
3 min read
How to Connect Teradata Using SAS in SQL?
SAS is a popular statistical software that provides a powerful suite of tools for data management, analytics, and reporting. In this blog post, we will show you how to connect to Teradata using SAS in SQL. Teradata is a high-performance, relational database management system that is widely used for
3 min read
How to Create Frequency Table by Group using Dplyr in R
In this article, we will be looking at the approach to creating a frequency table group with its working examples in the R programming language. Create Frequency Table by Group using dplyr package: In this approach to create the frequency table by group, the user first needs to import and install th
2 min read
How to merge data in R using R merge, dplyr, or data.table
Merging data is a common task in data analysis and data manipulation. It enables to combine information from different sources based on shared keys, creating richer datasets for exploration and modeling. Choosing the right merge method lets one balance speed, flexibility and ease of use. Different M
7 min read
How to Create a Two Way Table in R?
In this article, we will create a two-way table in R programming language. A two-way table is used to display frequency for two categorical variables. The rows represent the categorical features and the column represents frequency. We can create two-way table using as.table() method. as.table() func
2 min read
Left join using data.table in R
The data. table package in R is one of the best data manipulation tools that enable users to manage big data with so much ease and flexibility. One of its essential operations is the join, particularly the left join. This article will explore how to perform a left join using data.table, its advantag
6 min read
Concatenate List of Two data.tables Using rbindlist() Function in R
In this article, we will be looking at the approach to concatenating a list of two data.tables using rbindlist() function in the R programming language. Concatenate List of Two data.tables Using rbindlist() Function In this method of concatenating a list of two data.tables using the rbindlist() func
3 min read