MySQL Insert Query


MySQL Insert Query : To insert data into a MySQL table, you would need to use the SQL INSERT INTO command.


Syntax:

The below is generic syntax of SQL INSERT INTO command to insert a single record in MySQL table:

INSERT INTO table_name ( field1, field2,...fieldN )  

VALUES  

( value1, value2,...valueN );  

In the above syntax, we first have to specify the table name and list of comma-separated columns. Second, we provide the list of values corresponding to columns name after the VALUES clause.


If we want to insert multiple records within a single command, use the following statement:


INSERT INTO table_name VALUES

( value1, value2,...valueN )

( value1, value2,...valueN )

...........

( value1, value2,...valueN );  

In the above syntax, all rows should be separated by commas in the value fields.


MySQL INSERT Example

Let us understand how INSERT statements work in MySQL with the help of multiple examples. First, create a table “People” in the database using the following command:

CREATE TABLE People(

    id int NOT NULL AUTO_INCREMENT,

    name varchar(45) NOT NULL,

    occupation varchar(35) NOT NULL,

    age int,

    PRIMARY KEY (id)

);

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *