How to Insert Data Into A Table In MySQL?

6 minutes read

To insert data into a table in MySQL, you can use the INSERT INTO statement. Here's an example of how to do it:

1
2
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);


  • table_name refers to the name of the table where you want to insert the data.
  • column1, column2, column3, ... are the names of the columns in the table that you want to insert the data into.
  • value1, value2, value3, ... are the actual values you want to insert into the respective columns.


Here's a practical example:


Let's say you have a table called "employees" with columns "id" (auto-increment), "name", and "age". To insert a new employee, you can use the following SQL statement:

1
2
INSERT INTO employees (name, age)
VALUES ('John Doe', 25);


This will insert a new record with the name "John Doe" and age 25 into the "employees" table.

Best MySQL Managed Hosting Providers in 2024

1
Vultr

Rating is 5 out of 5

Vultr

  • Ultra-fast Intel Core
  • High Performance and Cheap Cloud Dedicated Servers
  • 1 click install Wordpress
  • Low Price and High Quality
2
Digital Ocean

Rating is 4.9 out of 5

Digital Ocean

  • Active Digital Community
  • Simple Control Panel
  • Starting from 5$ per month


How to insert data into a table by reading from a CSV file in MySQL?

To insert data into a MySQL table by reading from a CSV file, you can use the LOAD DATA INFILE statement. Here's how you can do it:

  1. Create a table in MySQL with the appropriate columns and data types matching the CSV file. For example: CREATE TABLE your_table_name ( col1 datatype, col2 datatype, col3 datatype, ... );
  2. Ensure that the CSV file is accessible by the MySQL server. You can place the file in a location where MySQL has permission to read from.
  3. Use the LOAD DATA INFILE statement to read the CSV file and insert the data into the table. The syntax is as follows: LOAD DATA INFILE 'path/to/your/csv_file.csv' INTO TABLE your_table_name FIELDS TERMINATED BY ',' -- Specify the field delimiter in your CSV file LINES TERMINATED BY '\n' -- Specify the line terminator in your CSV file (e.g., '\n' for new lines) IGNORE 1 ROWS; -- Optional: if your CSV file contains a header row, this will skip it Make sure to replace 'path/to/your/csv_file.csv' with the actual path to your CSV file, and 'your_table_name' with the name of the table you created.
  4. Execute the LOAD DATA INFILE statement either through the MySQL command-line interface or via programming language MySQL connectors.


Upon execution, the data from the CSV file will be inserted into the specified table in MySQL.


How to insert BLOB data into a MySQL table?

To insert BLOB data into a MySQL table, you can follow these steps:

  1. Connect to the MySQL database using your preferred programming language (e.g., Python, PHP, Java). Make sure you have the necessary permissions to access the table you want to insert the BLOB data into.
  2. Prepare the BLOB data that you want to insert. BLOB data can be in the form of images, files, or any binary data.
  3. Create an INSERT statement to insert the BLOB data into the table. The syntax for the INSERT statement is as follows: INSERT INTO table_name (blob_column) VALUES (?); Replace table_name with the name of your table, and blob_column with the name of the column where you want to insert the BLOB data.
  4. Bind the BLOB data to the prepared statement. The method for binding parameters may vary depending on the programming language and database connector you are using. Refer to the documentation of your specific database connector for more information.
  5. Execute the prepared statement to insert the BLOB data into the table.


Here is an example in Python using the mysql-connector-python library:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import mysql.connector

# 1. Connect to the MySQL database
db = mysql.connector.connect(
  host="localhost",
  user="your_username",
  password="your_password",
  database="your_database"
)

# 2. Prepare the BLOB data
with open("image.jpg", "rb") as file:
    blob_data = file.read()

# 3. Create the INSERT statement
query = "INSERT INTO table_name (blob_column) VALUES (%s)"
insert_data = (blob_data,)

# 4. Execute the prepared statement
cursor = db.cursor()
cursor.execute(query, insert_data)

# 5. Commit the changes and close the database connection
db.commit()
db.close()


Make sure to replace localhost, your_username, your_password, your_database, table_name, blob_column, and "image.jpg" with your actual database connection details, table name, column name, and the path to the BLOB data file you want to insert.


How to insert current date and time into a MySQL table?

To insert the current date and time into a MySQL table, you can use the NOW() function, which returns the current date and time.


Here's an example of how you can insert the current date and time into a table:

1
INSERT INTO your_table_name (date_column) VALUES (NOW());


Replace "your_table_name" with the actual name of your table, and "date_column" with the name of the column where you want to store the date and time.


If you want to store the date and time in a specific format, you can use the DATE_FORMAT() function. Here's an example:

1
INSERT INTO your_table_name (date_column) VALUES (DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s'));


In this example, '%Y-%m-%d %H:%i:%s' represents the desired format for the date and time. You can adjust the format to suit your needs.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To insert data into a MySQL table, you can use the INSERT INTO statement. The syntax for the statement is as follows:INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);Here's an explanation of each component of the...
To insert values in a MySQL table, you can use the INSERT INTO statement. Here is the syntax: INSERT INTO tableName (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); Here, tableName is the name of the table in which you want to insert valu...
To insert values in a MySQL table, you can use the INSERT INTO statement followed by the name of the table and the columns in which you want to insert the values. Here is an example: INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, va...