SSISO Community 
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분
회원가입 I 비밀번호 찾기

회원: 1명 카페짱: ssiso10 클럽생성일: 2012-01-26 11:58:10
안드로이드(android)

공지사항 및 게시판
사진게시판
SSISO Community카페
안드로이드 설치 및 개발
이름
제목
패스워드
HTML
1
2
INSERT OR REPLACE INTO table(column_list)
VALUES(value_list);

Or in a short form:

Let’s take a look at some examples of using the SQLite REPLACE statement to understand how it works.

The SQLite REPLACE statement examples

First, create a new table named positions with the following structure.

Try It

Second, insert some rows into the positions table.

Try It

Third, verify the insert using the following SELECT statement.

Try It

SQLite REPLACE positions table

The following statement creates a unique index on the title column of the positions table to ensure that it doesn’t have any duplicate position title:

Try It

Suppose, you want to add a position into the positions table if it does not exist, in case the position exists, update the current one.

The following REPLACE statement inserts a new row into the positions table because the position title Full Stack Developer is not in the positions table.

Try It

SQLite REPLACE insert new row

You can verify the REPLACE operation using the SELECT statement.

Try It

See the following statement.

Try It

SQLite REPLACE - replace the existing row

First, SQLite checked the UNIQUE constraint.

Second, because this statement violated the UNIQUE constraint by trying to add the DBA title that already exists, SQLite deleted the existing row.

Third, SQLite inserted a new row with the data provided by the REPLACE statement.

Notice that the REPLACE statement means INSERT or REPLACE, not INSERT or UPDATE.

See the following statement.

Try It

What the statement tried to do is to update the min_salary for the position with id 2, which is the developer.

First, the position with id 2 already exists, the REPLACE statement removes it.

Then, SQLite tried to insert a new row with two columns: ( id, min_salary). However, it violates the NOT NULL constraint of the title column. Therefore, SQLite rolls back the transaction.

If the title column does not have the NOT NULL constraint, the REPLACE statement will insert a new row whose the title column is NULL.

In this tutorial, we have shown you how to use the SQLite REPLACE statement to insert or replace a row in a table.


출처 : https://www.sqlitetutorial.net/sqlite-replace-statement/

사진첨부
 
 

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.