MySQL MySQL (select) UPDATE

2018.06.20 16:41

졸리운_곰 조회 수:54

 

MySQL (select) UPDATE

 

Summary: updating data is one of the most important tasks when you work with the database. In this tutorial, you will learn how to use the MySQL UPDATE statement to update data in a table.

Introduction to MySQL UPDATE statement

We use the UPDATE statement to update existing data in a table. We can use the UPDATE statement to change column values of a single row, a group of rows, or all rows in a table.

The following illustrates the syntax of the MySQL UPDATE statement:

In the UPDATE statement:

  • First, specify the table name that you want to update data after the UPDATE keyword.
  • Second, the SET clause specifies which column that you want to modify and the new values. To update multiple columns, you use a list comma-separated assignments. You supply the value in each column’s assignment in the form of a literal value, an expression, or a subquery.
  • Third, specify which rows to be updated using a condition in the WHERE clause. The WHERE clause is optional. If you omit the WHERE clause, the UPDATE statement will update all rows in the table.

Notice that the WHERE clause is so important that you should not forget. Sometimes, you may want to change just one row; However, you may forget the WHERE clause and accidentally updates all the rows in the table.

MySQL supports two modifiers in the UPDATE statement.

  1. The LOW_PRIORITY modifier instructs the UPDATE statement to delay the update until there is no connection reading data from the table. The LOW_PRIORITY takes effect for the storage engines that use table-level locking only, for example, MyISAM, MERGE, MEMORY.
  2. The IGNORE modifier enables the UPDATE statement to continue updating rows even if errors occurred. The rows that cause errors such as duplicate-key conflicts are not updated.

MySQL UPDATE examples

Let’s practice the UPDATE statement with some tables in the MySQL sample database.

MySQL UPDATE a single column example

In this example, we are going to update the email of Mary Patterson to the new email mary.patterso@classicmodelcars.com.

First, to make sure that we update the email successfully, we query Mary’s email from the employees table using the following SELECT statement:

MySQL Update example

Second, we can update Mary’s email to the new email mary.patterson@classicmodelcars.com using the UPDATE statement as the following query:

Because we just want to update one row, we use the WHERE clause to specify the row using the employee number 1056. The SET clause sets the value of the email column to the new email.

Third, we execute the SELECT statement again to verify the change.

MySQL UPDATE table example

MySQL UPDATE multiple columns

To update values in the multiple columns, you need to specify the assignments in the SET clause. For example, the following statement updates both last name and email columns of employee number 1056:

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Let’s check the changes.

MySQL UPDATE multiple columns

MySQL UPDATE from SELECT statement example

You can supply the values for the SET clause from a SELECT statement that queries data from other tables.

For example, in the customers table, some customers do not have any sale representative. The value of the column saleRepEmployeeNumber is NULL as follows:

MySQL UPDATE From SELECT example

We can take a sale representative and update for those customers.

To do this, we can select a random employee whose job title is Sales Rep from the employees table and update it for the employees table.

This query selects a random employee from the employees table whose job title is the Sales Rep.

To update the sales representative employee number  column in the customers table, we put the query above in the SET clause of the UPDATE statement as follows:

If you query data from the  employees table, you will see that every customer has a sales representative. In other words, the following query returns no row.

In this tutorial, you have learned how to use MySQL UPDATE statement to update data in a database table.

 

[출처] http://www.mysqltutorial.org/mysql-update-data.aspx

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 25085
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 24564
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 25943
465 Easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code. file 졸리운_곰 2018.07.02 86
464 Recurrent Neural Network for Text Calssification file 졸리운_곰 2018.07.02 317
463 char-rnn-tensorflow file 졸리운_곰 2018.07.02 43
462 TensorFlow-Char-RNN file 졸리운_곰 2018.07.02 42
461 Docker가 있는 SQL Server 2017 컨테이너 이미지를 실행 하는 빠른 시작 file 졸리운_곰 2018.06.26 131
460 MongoDB CRUD 동작의 이해 졸리운_곰 2018.06.26 54
459 Install TensorFlow with GPU Support the Easy Way on Ubuntu 18.04 (without installing CUDA) file 졸리운_곰 2018.06.25 76
458 A step by Step Guide to Install Tensorflow GPU on Ubuntu 18.04 LTS file 졸리운_곰 2018.06.25 55
457 MariaDB 10의 NoSQL 기능과 MySQL의 Json 관련 UDF 졸리운_곰 2018.06.22 112
456 [MySQL] Select 결과 Update하는 SQL 작성 file 졸리운_곰 2018.06.20 65
455 조건에 맞게 select 한 후 update 시키기 졸리운_곰 2018.06.20 24
» MySQL (select) UPDATE file 졸리운_곰 2018.06.20 54
453 Lessons from installing TensorFlow 1.7 for NVIDIA GPU on a Samsung Odyssey running Ubuntu 17.10 file 졸리운_곰 2018.06.20 157
452 [MSSQL] Management Studio 이용해 데이터베이스 생성하기 file 졸리운_곰 2018.06.17 87
451 MySQL에서 중복 값 찾기 졸리운_곰 2018.06.15 111
450 [MSSQL - GROUP BY HAVING 을 이용한 중복 데이타 체크] file 졸리운_곰 2018.06.15 72
449 mysql case문 사용하기 졸리운_곰 2018.06.14 69
448 CNTK 설치 및 테스트 file 졸리운_곰 2018.06.11 45
447 TensorFlow Lite 101 - MoblieNet 맛보기 file 졸리운_곰 2018.05.30 41
446 딥러닝(Deep learning)을 R로 구현하기 – Prediction Model file 졸리운_곰 2018.05.30 641
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED