Search code snippets, questions, articles...

Insert data into table mysql query

When working with databases, it is often necessary to insert data into a table. This can be done using the INSERT INTO SQL query. The INSERT INTO query is used to insert data into a table in a database. The data that is inserted into the table is specified in the VALUES clause of the query.
INSERT INTO table_name (Column1, Column2, Column3)
VALUES(1, 'Value2', 'Value3')

or

INSERT INTO table_name
SET
Column1 = 'Value1',
Column2 = 'Value2',
Column3 = 'Value3'

If you are adding data for selected columns in the table then you have to specify the columns.

If you are adding data for all the columns of the MySQL table, you don't need to specify columns.

INSERT INTO table_name
VALUES(1, 'Value2', 'Value3')

INSERT query in MYSQL is used to fill data into table columns as a row. You can define column names into the query or you can directly insert data using 'VALUES'. An easy way is also to use 'SET' and assign values to its column directly.

# Example 1

Assuming you have a MySQL table called "animals" with three columns called "id", "name", and "type", the following SQL statement would insert a new record into the "animals" table:

INSERT INTO animals (id, name, type)
VALUES (1, 'Cat', 'Pet');

This statement would insert a new record into the "animals" table with an "id" value of 1, a "name" value of 'Cat', and a "type" value of 'Pet'.

Was this helpful?
0 Comments