How to create SQL table using DBI library in R
Last Updated :
28 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
# importing the library
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
# creating a database connection
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
# creating a table
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
# inserting two rows into the table
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 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
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
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
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
CREATE TABLE in SQL Server SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
How to Create a Three Way Table in R A three-way table, also known as a three-dimensional contingency table, is a tabular representation of the joint frequencies of three categorical variables. It provides a way to analyze the relationships between three categorical variables simultaneously. In this article, we will study different app
6 min read
How to Create Multiple Sheets in Excel workbook Using R In this article, we will discuss how to create multiple sheets in an excel file using the xlsx package. As we all know in general an excel file might contain one or more than one sheet present in it. Manually we can create and insert data into multiple sheets in Excel GUI Application but when it co
2 min read