본문 바로가기
번역/MariaDB

[MariaDB Tutorial 번역] 19. Update

by 촌쥐 2021. 7. 14.

[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 : https://rural-mouse.tistory.com/62
  18. Insert Into Select : https://rural-mouse.tistory.com/63
  19. Update ←←

원문 : https://www.mariadbtutorial.com/mariadb-basics/mariadb-update/

 

MariaDB Update Statement By Practical Examples

In this tutorial, you will learn how to use the MariaDB update statement to modify data in 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의 update 명령문에 대한 소개

update 명령문은 Table의 하나 이상의 Column 데이터들을 수정할 수 있도록 해줍니다. 다음은 update 명령문의 문법을 보여줍니다.

update table_name
set column1 = value1,
    column2 = value2,
    ...
[where search_condition];
  • 첫 번째로 수정하고 싶은 데이터가 있는 table의 이름을 update 키워드 뒤에 입력합니다.
  • 다음으로 하나 이상의 column과 새로운 값을 set 절에 지정합니다.
  • 마지막으로 선택적으로 사용할 수 있는 wherw 절을 사용하여 데이터를 수정할 Row를 지정합니다. 만약 where 절을 생략한다면 update 명령문은 Table의 모든 Row의 데이터를 수정합니다.

 

MariaDB의 update 명령문의 예제

이전 튜토리얼에서 만들었던 contacts 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)
);

다음을 통해 contacts의 데이터를 조회합니다.

select * from contacts;

지금까지 생성한 데이터들이 나옵니다.

A) MariaDB에서 update를 사용하여 하나의 Row를 수정하는 예제

다음은 update 명령문을 사용하여 id 1의 Row에 있는 'Doe'를 'Smith'라는 이름으로 변경하는 예제입니다.

update contacts
set last_name = 'Smith'
where id = 1;

정상적으로 작동했다면 MariaDB는 다음과 같은 메시지를 보여줍니다.

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

영향을 받은 Row는 하나입니다. 이는 하나의 Row가 정상적으로 수정되었다는 것을 뜻합니다.

업데이트를 확인하기 위하여 다음의 쿼리문을 사용합니다.

select * from contacts
where id = 1;

정상적으로 수정되었습니다.

B) MariaDB에서 update를 사용하여 여러 Row를 수정하는 예제

다음은 update 명령문을 사용하여 contact의 Customer 그룹의 지역 전화번호를 408에서 510으로 수정하는 예제입니다.

update 
    contacts
set 
    phone = replace(phone,'(408)','(510)')
where 
    contact_group = 'Customers';

이 예제에서 where 절은 그룹이 Customers인 모든 연락처를 반환해줍니다. set 절은 replace() 함수를 사용하여 phone column의 (408)에서 (510)으로 수정합니다.

업데이트를 확인하기 위하여 다음의 쿼리문을 사용합니다.

select * from contacts
where contact_group = 'Customers';

정상적으로 변경된 전화번호

C) MariaDB에서 update를 사용하여 모든 Row를 수정하는 예제

다음은 update 명령문을 사용하여 contact의 phone Column의 모든 전화번호에서 -를 공백으로 수정하는 예제입니다.

update 
    contacts
set 
    phone = replace(phone,'-',' ')

이번 update 명령문에서는 where 절을 사용하지 않았으므로 contacts Table의 모든 row를 수정합니다.

이번 쿼리문은 contacts의 모든 Row를 반환해줍니다.

select * from contacts;

정상적으로 사라진 -

 

이번 튜토리얼에서는 데이터를 수정하는 update 명령문을 배워봤습니다.

 


페이지에 있는 DML 튜토리얼은 이렇게 끝이 났습니다.

처음부터 여기까지 쫓아오셨다면 기본적인 쿼리문은 모두들 쉽게 사용하실 수 있을 겁니다. 

DDL도 몇개 안되니까 조만간 번역해보도록 하겠습니다. 부족한 번역글이지만 봐주셔서 감사합니다!

댓글