The database: from creating it to DROP
In short: CREATE DATABASE creates a new database on a server, while DELETE, TRUNCATE, DROP TABLE and DROP DATABASE remove rows, all rows, a table or the whole database.
Problem
The warehouse wants its own database
The shop opens a warehouse. The warehouse has its own tables: shelves, deliveries, stock counts. They are not part of the website, and nobody wants a mistake in the warehouse to stop the orders. So the warehouse gets its own separate database, on the same server.
So far you have worked only in the shop's database. Now you will see the whole life of a database: how it is made, how you connect to it, how it is filled and how it is removed.
The idea
One server, many databases
A database server (PostgreSQL, MySQL, SQL Server) holds many databases. Each has its own tables, its own users and permissions. A new database is made with CREATE DATABASE:
-- PostgreSQLCREATE DATABASE warehouse;\c warehouse -- psql connects to the new database
-- MySQLCREATE DATABASE warehouse;USE warehouse; -- the next queries run in itThese commands do not work in the fields here, because the site uses SQLite. SQLite has no server: a database is one file. A new database is a new file: sqlite3 warehouse.db in the terminal makes it if it is not there. That is why SQLite has no CREATE DATABASE.
Which database does the field use now? main is the shop's database:
The idea
A new database in SQLite: ATTACH
Inside a connection that is already open, SQLite attaches another database with ATTACH. On a computer you write a file name (ATTACH 'warehouse.db' AS warehouse) and the file is created if it is not there. In the browser we use ':memory:': a new empty database in memory that disappears with the field. The name after AS is the name of the database, and its tables are written with it in front:
The table shelves is in the database warehouse, not in the shop. How exactly the columns are written in CREATE TABLE is in the next lesson. What matters here is where they go.
See it
A query across two databases
In SQLite and MySQL one query can read from two databases at once. The shelves are in the warehouse, and the names and stock are in the shop:
main.products is the shop's table. main. can be left out: for a table without a database name SQLite looks in the temporary database, then in main and only then in the attached ones. In PostgreSQL this does not work: a query sees only the database it is connected to. There separate parts of one system are split into schemas in one database (warehouse.shelves is the schema warehouse), and separate databases are truly separate.
Try it
The warehouse database
Make the database warehouse (in memory), in it a table deliveries with the columns id (primary key), product_id and quantity (whole numbers, required) and record a delivery: 40 pieces of product 12.
Solution
ATTACH ':memory:' AS warehouse;
CREATE TABLE warehouse.deliveries (
id INTEGER PRIMARY KEY,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL
);
INSERT INTO warehouse.deliveries (product_id, quantity) VALUES (12, 40);ATTACH ':memory:' AS warehouse, then CREATE TABLE warehouse.deliveries (…) and INSERT INTO warehouse.deliveries …. Without warehouse. in front of the name the table goes into main, that is the shop's database, and the warehouse stays empty. This is the most common mistake with several databases.
The idea
Four ways to remove
From the smallest to the largest:
DELETE FROM t WHERE …removes the chosen rows. WithoutWHEREit removes all of them, but the table stays.TRUNCATE TABLE t(PostgreSQL, MySQL, SQL Server) removes all rows at once, much faster thanDELETEon a large table. In MySQL and SQL Server it also resets the numbering ofid, and in PostgreSQL only withTRUNCATE TABLE t RESTART IDENTITY. SQLite has noTRUNCATE: thereDELETE FROM twithoutWHEREdoes the same in a fast way.DROP TABLE tremoves the table itself: the rows, the columns, its indexes. After thattis gone.DROP DATABASE warehouseremoves the whole database with all its tables. In SQLite this is deleting the file.
Here DELETE empties the promotions table, and DROP TABLE removes it. The last query has nothing left to read:
More precisely, if you are curious
DROP does not ask "are you sure" and does not come back with Ctrl+Z. In PostgreSQL DROP DATABASE does not work while someone is connected to the database, and in PostgreSQL and SQLite a DROP TABLE inside a transaction can be undone with ROLLBACK. In MySQL DROP and TRUNCATE do a COMMIT on their own. That is why before a DROP on a real database you first make a copy (that is what the lesson about the real site in the module "The database in a program" is for).
Try it
A table from an old import
The table promo_import is left over from an import of promotions and is no longer needed. Remove it together with the data, so that the script does not stop if someone has already removed it.
Solution
DROP TABLE IF EXISTS promo_import;DROP TABLE IF EXISTS promo_import. DELETE FROM promo_import leaves the empty table standing, and without IF EXISTS the second attempt stops with "no such table".
Exercise
Empty it, but keep it
The table cart_log records what customers put in the cart. At the start of every month it is emptied, but it stays for the new records. Empty it.
Solution
DELETE FROM cart_log;DELETE FROM cart_log without WHERE (in PostgreSQL and MySQL it would be TRUNCATE TABLE cart_log). DROP TABLE would remove the table too, and the next record from the site would stop with "no such table".
The idea
Detaching
When you no longer need it, the database is detached with DETACH. The file stays on disk, and its tables are no longer visible from this connection:
The whole path of a database is: creating (CREATE DATABASE, in SQLite a new file or ATTACH), connecting (\c, USE, ATTACH), tables (CREATE TABLE, the next lesson), data (INSERT, UPDATE, DELETE), queries (SELECT) and finally removing (DROP TABLE, DROP DATABASE). The project at the end of the course goes the whole path with the shop.
Test
Check yourself
Related lessons
Section titled "Related lessons"- Builds on: Primary key and foreign key · JOIN: two tables in one query · INSERT: new rows · DELETE: deleting rows
- Continues in: The bakery, stage 1: the database and the tables · The bakery, stage 6: the report, the copy and DROP
- Tasks on this topic: Tables and rules