Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
How to create SQL table using DBI library in R
Next article icon

How to create SQL table using DBI library in R

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Next Article
How to create SQL table using DBI library in R

I

ishukatiyar16
Improve
Article Tags :
  • R Language

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences