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
# 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 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 Relational Tables in Excel?
Excel directly doesn't provide us ready to use a database, but we can create one using relationships between various tables. This type of relationship helps us identify the interconnections between the table and helps us whenever a large number of datasets are connected in multiple worksheets. We ca
4 min read
How to Display all Tables in PL/SQL?
In Oracle PL/SQL, we need to work with database objects like tables. It provides various approaches to display all the tables. Such as using the USER_TABLES, ALL_TABLES, and DBA_TABLES views. Each approach is explained with syntax and examples. In this article, We will learn about How to Display all
5 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 Me
7 min read
How to Retrieve Data From Multiple Tables Using PL/SQL Cursors
In database programming, the ability to retrieve data from multiple tables is essential for building robust and efficient applications. PL/SQL Cursors is a powerful feature that enables developers to navigate through result sets and make them the best option for querying data from multiple tables. I
4 min read