본문 바로가기
번역/MariaDB

[MariaDB Tutorial 번역] 17. Insert Multiple Rows (bluk insert)

by 촌쥐 2021. 7. 12.

[MariaDB Tutorial 번역 글]

  1. Select : https://rural-mouse.tistory.com/43
  2. Order By : https://rural-mouse.tistory.com/44
  3. Where : https://rural-mouse.tistory.com/45
  4. Distinct : https://rural-mouse.tistory.com/46
  5. Between : https://rural-mouse.tistory.com/48
  6. Like : https://rural-mouse.tistory.com/49
  7. In : https://rural-mouse.tistory.com/50
  8. Limit : https://rural-mouse.tistory.com/51
  9. Is Null : https://rural-mouse.tistory.com/52
  10. Joins : https://rural-mouse.tistory.com/53
  11. Inner Join : https://rural-mouse.tistory.com/54
  12. Left Join : https://rural-mouse.tistory.com/55
  13. Group By : https://rural-mouse.tistory.com/56
  14. Having : https://rural-mouse.tistory.com/59
  15. Subqueries : https://rural-mouse.tistory.com/60
  16. Insert : https://rural-mouse.tistory.com/61
  17. Insert Multiple Rows ←←
  18. Insert Into Select : https://rural-mouse.tistory.com/63
  19. Update : https://rural-mouse.tistory.com/64

원문 : https://www.mariadbtutorial.com/mariadb-basics/mariadb-insert-multiple-rows/

 

MariaDB Insert Multiple Rows Into a Table

In this tutorial, you will learn how to use the MariaDB insert statement to insert multiple rows into a table.

www.mariadbtutorial.com


여기에 있는 모든 예제는 MariaDB sample database인 nation을 가지고 진행됩니다.

만약 읽고 따라 해 보시는 게 목적이라면 다운로드를 받고 읽어보시길 추천합니다.

원본 링크 : https://www.mariadbtutorial.com/getting-started/mariadb-sample-database/

 

MariaDB Sample Database

In this tutorial, you will learn about a MariaDB sample database nation and how to load the sample database into the MariaDB server.

www.mariadbtutorial.com


MariaDB의 insert를 사용한 여러 개의 Row 삽입 명령문

하나의 insert 명령문을 사용하여 Table에 여러 개의 Row를 삽입하려면 다음의 문법을 사용하면 됩니다.

insert into
    table_name(column_list)
values
    (value_list_1),
    (value_list_2),
    (value_list_3),
    ...;
  • 첫 번째, Table의 이름과 괄호 안에 Column 리스트를 지정합니다.
  • 다음으로 쉼표( comma / , )로 구분된 Column에 해당하는 값들의 리스트를 작성합니다. 각 리스트는 Table의 Row에 해당됩니다.

MariaDB의 여러개의 row 삽입 예제

지난번에  만들었던 contact Table을 사용하도록 하겠습니다. 

create table if not exists contacts(
    id int auto_increment,
    first_name varchar(50) not null,
    last_name varchar(50) not null,
    full_name varchar(101) 
        as (concat(first_name, ' ', last_name)) virtual,
    phone varchar(100),
    contact_group varchar(50) default 'General',
    primary key(id)
);

 

A) Table에 여러 개의 Row를 삽입하는 예제

다음은 contacts Table에 3개의 row를 삽입하는 예제입니다.

insert into contacts(first_name, last_name, phone, contact_group)
values
    ('James','Smith','(408)-232-2352','Customers'),
    ('Michael','Smith','(408)-232-6343','Customers'),
    ('Maria','Garcia','(408)-232-3434','Customers');

MariaDB는 다음과 같은 메시지를 보여줄 것입니다.

Affected rows: 3  Found rows: 0  Warnings: 0  Duration for 1 query: 0.016 sec.

3개의 Row가 정상적으로 삽입되었다는 것을 뜻합니다.

다음은 contacts Table의 요소들을 보여주는 쿼리입니다.

select * from contacts;

지난 번 데이터에 이어 새로 추가된 3개의 Row

 

B) MariaDB에서 여러개의 Row를 삽입하고 삽입된 id를 반환하는 예제

여러 개의 Row를 Table에 삽입할 때 Column이 auto_increment Column이라면 last_insert_id() 함수는 처음에 생성된 id만 반환합니다. 게다가 모든 삽입된 id들을 반환해주지 않습니다.

다음 예제를 봅시다.

처음으로 두개의 Row를 contacts Table에 삽입합니다.

insert into contacts(first_name, last_name, phone, contact_group)
values
    ('James','Johnson','(408)-232-4523','Customers'),
    ('Mary','Rodriguez','(408)-232-4532','Customers');

다음으로 table의 콘텐츠를 쿼리문을 사용하여 확인합니다.

select * from contacts;

추가된 2개의 Row

마지막으로 last_insert_id() 함수를 사용하여 삽입된 id를 가져와봅니다.

select last_insert_id();

8이 아닌 7이 반환되었습니다.

 

 

이번 튜토리얼에서는 하나의 insert 명령문으로 여러 개의 Row를 삽입하는 방법에 대해 배워봤습니다.

다음은 Select문의 Result set을 insert를 사용하여 Table에 삽입하는 방법을 배워보겠습니다.

댓글