How SQLite modifies table structure field types

Source: Internet
Author: User

SQLite only supports part of the ALTER TABLE statement, we can change the name of a table with the ALTER TABLE statement, or we can add a field (column) to the table, but we cannot delete an existing field, or change the name of an existing field, the data type , qualifiers, and so on.

Change table name-ALTER TABLE old table name RENAME to new table name

Add a column-ALTER table name add column name data type

Modifying a column can not be done directly with the "ALTER table table name ADD column name data type" in the same way as other databases, so take a different approach, see below

Tools/Materials
    • Sqlite

Method/Step
    1. 1

      --1. Change table name to temp table

      ALTER table Student "RENAME to" _student_old_20140409 ";

    2. 2

      --2. Create a new table

      CREATE TABLE "Student" (

      "Id"   INTEGER PRIMARY KEY AutoIncrement,

      "Name"   Text);

    3. 3

      --3. Import data

      INSERT into "Student" ("id", "Name") Select "id", "Title" From "_student_old_20140409";

    4. 4

      --4. Update sqlite_sequence

      Update "sqlite_sequence" SET seq = 3 WHERE name = ' Student ';

      Because the self-growing field is used in SQLite, the engine automatically generates a Sqlite_sequence table that records the maximum value used for the self-growing field of each table, so it is updated together. If there is no self-growth set, skip this step.

    5. 5

      --5. Delete temporary table (optional)

      DROP table _student_old_20140409;

      end
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.