![]() |
In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database. Understanding how to use the INSERT INTO statement is key for managing and ensuring the integrity and functionality of the database. INSERT INTO StatementThe Let’s look at Various ways of using the INSERT INTO statement, exploring how it can be used to add data efficiently to our table.
Syntax: There are two ways to write an INSERT INTO statement in MySQL. 1. The general syntax for inserting a single record into a MySQL table using the SQL INSERT INTO command is as follows: INSERT INTO table_name (column1, column2, ..., columnN) 2. Use the following statement to insert multiple records with a single command: INSERT INTO table_name (column1, column2, ..., columnN) Demo MySQL DatabaseWrite the following command to create an empty table on which we will perform the INSERT INTO. CREATE TABLE employees ( INSERT Statement ExamplesLet’s look at the examples of the INSERT INTO statement, to understand the workings of INSERT INTO statement, and cover its different use cases. Example 1: MySQL INSERT INTO Table Single RowTo insert a single record into a table, specify the table name and provide values for each column in the order they appear in the table. Syntax: INSERT INTO table_name (column1, column2, column3, ...) Query: -- Insert single record into the employees table Output:
Explanation: This syntax inserts a single row of data into the ‘employees‘ table. You specify the table name and the columns into which you want to insert data, followed by the corresponding values. In this example, we insert the name ‘John Doe‘, age ‘30‘, and department ‘IT’ into the respective columns. Example 2: MySQL INSERT INTO table Multiple RowsMySQL allows inserting multiple rows of data in a single query. It is reducing the overhead of executing multiple queries. The syntax is similar to inserting a single row but with multiple sets of values enclosed within parentheses and separated by commas Syntax: INSERT INTO table_name (column1, column2, column3, ...) Query: -- Insert multiple records into the employees table Output:
Explanation: This syntax allows you to insert multiple rows of data into the ‘employees‘ table in a single query. Each set of values represents a separate row to be inserted. In this example, we insert three new employees into the table with their respective details. Example 3: MySQL INSERT INTO SELECTSometimes, We may need to insert data into a table based on the results of a SELECT query. MySQL facilitates this through the INSERT INTO SELECT statement. This statement selects data from one table and inserts it into another Syntax: INSERT INTO table_name (column1, column2, column3, ...) Query: -- Create the employees table Output:
Explanation: This syntax inserts data into the ‘employees_backup‘ table based on the results of a SELECT query. In this example, we select employees from the ’employees’ table where the age is greater than 30 and insert their details into the ’employees_backup’ table. Example 4: MySQL INSERT INTO DATEInserting date values into a MySQL database is a common requirement. MySQL provides several date and time functions to handle date-related operations efficiently. To insert the current date into a date column, we can use the NOW() function with MySQL Insert Date. Syntax: INSERT INTO table_name (date_column) Query: INSERT INTO orders (order_date) Output: 2024-02-20 12:00:00
Explanation: To insert the current date into a date column, you can use the NOW() function within the INSERT INTO statement. This example inserts the current date into the ‘order_date’ column of the ‘orders’ table. ConclusionIn conclusion, INNER JOIN in MySQL is essential for combining rows from multiple tables based on a common column, ensuring only matching rows are included in the results. This article has explained the basic syntax and usage, showing examples with GROUP BY, HAVING, WHERE, and USING clauses. Mastering INNER JOIN allows you to perform complex queries and manage database information efficiently, making it a crucial skill for effective database management. FAQs on MySQL INNER JOINWhat is MySQL INNER JOIN?
How does INNER JOIN differ from other types of joins?
When should I use INNER JOIN?
|
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |