SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Tutorial / SQLite Attach Database

SQLite Attach Database

Summary: in this tutorial, you will learn how to attach additional databases to the current database connection using the SQLite ATTACH DATABASE statement.

Introduction to the SQLite ATTACH DATABASE statement

When you connect to a database, its name is main regardless of the database file name. In addition, you can access the temporary database that holds temporary tables and other database objects via the temp database.

Therefore, every SQLite database connection has the main database and also temp database in case you deal with temporary database objects.

To attach an additional database to the current database connection, you use the ATTACH DATABASE statement as follows:

ATTACH DATABASE file_name AS database_name;

The statement associates the database file file_name with the current database connection under the logical database name database_name.

If the database file file_name does not exist, the statement creates a new database file.

Once the additional database attached, you can refer to all objects in the database under the name database_name. For example, to refer to the people table in the contacts database, you use the contacts.people.

In case you want to create a new memory database and attach it to the current database connection, you use :memory: filename.

You can attach multiple in-memory databases at the same time with a condition that each memory database must be unique.

If you specify an empty file name '', the statement creates a temporary file-backed database.

Note that SQLite automatically deletes all temporary and memory databases when the database connection is closed.

SQLite ATTACH DATABASE example

First, connect to the chinook sample database using sqlite3 command as follows:

>sqlite3 c:\sqlite\db\chinook.db;

Next, use the .databases command to list all databases in the current database connection.

sqlite> .databases

SQLite returns the following output.

seq name file --- --------------- ---------------------------------------------------------- 0 main c:\sqlite\db\chinook.db

Then, use the ATTACH DATABASE statement to create a new database named contacts and associates it in the current database connection.

sqlite> attach database 'c:\sqlite\db\contacts.db' as contacts;

Fourth, use the .database command to display all databases in the current database connection.

sqlite> .databases

SQLite returns 2 databases as follows:

seq name file --- --------------- ---------------------------------------------------------- 0 main c:\sqlite\db\chinook.db 2 contacts c:\sqlite\db\contacts.db

After that, create a new table named people in the contacts database and populate data from the customers table in the main database.

sqlite> CREATE TABLE contacts.people(first_name text, last_name text); sqlite> INSERT INTO contacts.people SELECT firstName, lastName FROM customers;

Notice that we referred to the people table in the contacts database using the contacts.people naming convention.

Finally, query data from the people table in the contacts database.

SELECT * FROM contacts.people;

For more information on the ATTACH DATABASE statement, check out its documentation.

In this tutorial, you have learned how to use the SQLite ATTACH DATABASE statement to associate additional databases in the current database connection.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Date & Time
Next SQLite Create 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 IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2020 SQLite Tutorial. All Rights Reserved.