What is the update of the SQLite database?

Source: Internet
Author: User

AlthoughSQLiteHas more than a year of experience, but in fact there is noSQLiteAfter all, most of the time you chooseSQLiteThis kind of micro-database, table structure design is very simple, some complexSQLOperations are rarely used. On the other handSQLiteTheoretically supported2 TBWe believe thatSQLiteIn the future.

I want to talk about it todaySQLiteInUpdate.

1.TypicalUpdate(Supported)

Update

T1

Set

Column1 = V1,

Column2 = v2

Where

Key = V3;

2. update... From (Unfortunately,SQLiteIs not supported)

Update

T1

Set

Column1 = T2. column1

From

T2, T1

Where

T2.key = t1.key;

Update between tablesUpdate... FromIs required.SQLiteNo. Is there any other way? There are indeed two alternative methods:

First, SQLiteThere is a new thing in it"Insert or replace",MySQLSimilarly, this structure can be replaced if it exists and is updated if it does not exist. This mechanism can be easily implemented.Update... From.

Insert or replace

T1 (Key, column1, column2)

Select

T2.key, T2. column1, T2. column2

From

T2, T1

Where

T2.key = t1.key;

Note: To avoid insert operations, make sure that the update is performed according to the primary key. IfWhereIf the condition is not a primary key, it may be a bit difficult.

What should I do if I want to update the primary key? Is there any other way? In this case, we can onlyUpdate... WhereFor help, the following is an example:

Update

T1

Set

Column1 = (select columnx from T2 where t2.key = t1.key ),

Column2 = (select columny from T2 where t2.key = t1.key ),

Where t1.key = (select key from T2 where t2.key = t1.key );

The following is an example of a master-slave table, a department table, a member table, and the department name andCodeIs redundant information. Update the redundant information in the member table with the department name and code in the department table:

Update

Userlogins

Set

Deptname = (select deptname from attributes where attributes. [deptid] = userlogins. [deptid]),

Deptcode = (select deptcode from attributes where attributes. [deptid] = userlogins. [deptid])

Where

userlogins. [deptid] = (select [deptid] From orders where orders. [deptid] = userlogins. [deptid]);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.