SQLite Forum

ATTACH DATABASE and DETACH DATABASE
Login

ATTACH DATABASE and DETACH DATABASE

(1) By anonymous on 2020-10-08 15:33:59 [link] [source]

Hello SQLite guys.

what ATTACH DATABASE and DETACH DATABASE do? I have not seen any SQL database systems have these two SQL statements.

(2) By luuk on 2020-10-08 16:10:37 in reply to 1 [source]

Microsoft SQL also has this, see: https://docs.microsoft.com/en-us/sql/relational-databases/databases/attach-a-database?view=sql-server-ver15

For SQLite see: https://www.sqlite.org/lang_attach.html

(3) By Gunter Hick (gunter_hick) on 2020-10-08 16:31:57 in reply to 1 [link] [source]

ATTACH allows you to access (open) another SQLite database file in your current session.

DETACH tells SQLite that you are finished with the database file so that SQLite can close it.

(4) By anonymous on 2020-10-08 17:00:47 in reply to 3 [link] [source]

But I see some SQLite tutorials
attach the same database file multiple times,
what does it mean then?
For example,
ATTACH DATABASE my.db AS name1
ATTACH DATABASE my.db AS name2
...

What it mean for ATTACH DATABASE and DETACH DATABASE in these cases?

(5) By David Raymond (dvdraymond) on 2020-10-08 18:16:37 in reply to 4 [link] [source]

But I see some SQLite tutorials attach the same database file multiple times, what does it mean then?

I can't think of a single reason why someone would want to do that. Do you have an example tutorial page where they're showing that?

In practical purposes it basically just means you have more than 1 name for the same thing.

(6) By anonymous on 2020-10-08 18:30:15 in reply to 5 [link] [source]

The page is here:

https://www.tutorialspoint.com/sqlite/sqlite_detach_database.htm

in the Example section.

sqlite>.databases
seq  name             file
---  ---------------  ----------------------
0    main             /home/sqlite/testDB.db
2    test             /home/sqlite/testDB.db
3    currentDB        /home/sqlite/testDB.db

Are "main" "test" and "currentDB"
just C pointers to the same database file(testDB.db)?

(7) By Keith Medcalf (kmedcalf) on 2020-10-08 18:52:48 in reply to 6 [link] [source]

This is an example of the DETACH command. It does not speak to the utility of ATTACHing the same database file multiple times.

It is sort of like saying that you could (if you really wanted to do so) cut off your sex organs with pinking shears. It does not mean that there is necessarily any utility in doing so.

(8) By Gunter Hick (gunter_hick) on 2020-10-09 07:46:12 in reply to 4 [link] [source]

It means that the creator of the tutorial was too lazy to create two different SQLite databases