Inserting multiple rows in MySQL Table in Single Statement - MySQL Developer Tutorial

Inserting multiple rows in MySQL Table in Single Statement 

If you need to insert multiple rows of data into MySQL Table, you can use below syntax.

insert into TableName
(Column1,Column2,....)
Values (ValueForColumn1,ValueforColumn2,....),
(ValueForColumn1,ValueforColumn2,....),
(ValueForColumn1,ValueforColumn2,....);



Example : 

In below example, we are creating customer table and then insert multiple rows into customer table by using single insert statement.

create customer table syntax:


CREATE TABLE `customer` (
  `idcustomer` int(11) NOT NULL auto_increment,
  `firstname` varchar(50)  NULL,
  `lastname` varchar(30)  NULL,
  `age` int(11) DEFAULT NULL,
  `phonenumber` char(11) DEFAULT NULL,
  `dob` date DEFAULT NULL,
  `gender` char(1) NOT NULL,
  Constraint pk_idcustomer Primary key (idcustomer)

) ;


Insert multiple rows in MySQL Table.


insert into customer
(firstname,lastname,age,phonenumber,dob,gender)
values
('Aamir1','Shahzad1',39,'505-414000','1980-01-01','M'),
('Aamir2','Shahzad2',40,'505-4141000','1980-02-01','M');


Insert Multiple records in MySQL table in single Insert Statement - MySQL Tutorial

No comments:

Post a Comment