SQLite Tutorial

  • Home
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • Interfaces
    • SQLite Java
    • SQLite Node.js
    • SQLite PHP
    • SQLite Python
  • Try It
Home / SQLite Tutorial / SQLite Rename Column

SQLite Rename Column

Summary: in this tutorial, you will learn step by step how to rename a column of a table in SQLite.

Different from other database systems, SQLite does not directly support the ALTER TABLE RENAME COLUMN statement that allows you to rename an existing column of a table.

To work around this, you follow these steps:

  • First, start a transaction.
  • Second, create a new table whose structure is the same as the original one except for the column that you want to rename.
  • Third, copy data from the original table to the new table.
  • Fourth, drop the original table.
  • Fifth, rename the new table to the original table.
  • Finally, commit the transaction.

SQLite rename column example

The following statement creates a new table called Locations in the database:

1
2
3
4
5
6
7
CREATE TABLE Locations(
    LocationId INTEGER PRIMARY KEY,
    Address TEXT NOT NULL,
    District TEXT NOT NULL,
    City TEXT NOT NULL,
    Country TEXT NOT NULL
);

And this INSERT statement inserts a new row into the Locations table:

1
2
INSERT INTO Locations(Address,City,State,Country)
VALUES('3960 North 1st Street','San Jose','CA','USA');

Suppose, you want to the change the column Address to Street.

First, start a new transaction:

1
BEGIN TRANSACTION;

Second, create a new table called LocationsTemp with the same structure as the Locations table except for the Address column:

1
2
3
4
5
6
7
CREATE TABLE LocationsTemp(
    LocationId INTEGER PRIMARY KEY,
    Street TEXT NOT NULL,
    City TEXT NOT NULL,
    State TEXT NOT NULL,
    Country TEXT NOT NULL
);

Third, copy data from the table Locations to LocationsTemp:

1
2
3
INSERT INTO LocationsTemp(Street,City,State,Country)
SELECT Address,City,State,Country
FROM Locations;

Fourth, drop the Locations table:

1
DROP TABLE Locations;

Fifth, rename the table LocationsTemp to Locations:

1
2
ALTER TABLE LocationsTemp
RENAME TO Locations;

Finally, commit the transaction:

1
COMMIT;

If you query the Locations table, you will see that the Address column has been renamed to Street:

1
SELECT * FROM Locations;

Here is the output:

sqlite rename column example

In this tutorial, you have learned step by step how to rename a column in a table in the SQLite database.

  • Was this tutorial helpful ?
  • YesNo
Previous Tutorial: SQLite ALTER TABLE
Next Tutorial: SQLite Drop Table

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK Constraint
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL
  • SQLite Rename Column
  • SQLite DROP VIEW
  • SQLite Window Frame
  • SQLite CUME_DIST

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2020 SQLite Tutorial. All rights Reserved.

⤒