The SQL code snippets covered in this chapter are all contained in this folder. The SQL code snippets covered in this chapter work perfectly with MySQL, MariaDB, and SQLite.
If you are using PostgreSQL, you should refer to the SQL script contained in this folder and pay attention to the following differences that were made to make the script compatible with PostgreSQL:
TIMESTAMP
was used instead ofDATETIME
: PostgreSQL doesn't supportDATETIME
data type.
If you are using SQL Server, you should refer to the SQL script contained in this folder and pay attention to the following differences that were made to make the script compatible with SQL Server:
NVARCHAR(MAX)
was used instead ofTEXT
: SQL Server doesn't supportTEXT
data type.- Data insertion of string values was changed from
'...'
toN'...'
: SQL Server requires theN
prefix for string values, e.g.,N'Apple'
. SQL Server uses the UCS-2 encoding for string values, which is a subset of UTF-16. TheN
prefix tells SQL Server to use UTF-16 encoding for the string value. GETDATE()
was used instead ofCURRENT_TIMESTAMP
: SQL Server doesn't supportCURRENT_TIMESTAMP
function.
If you are using SQLite database, you should refer to the SQL script contained in this folder and pay attention to the following differences that were made to make the script compatible with SQLite database:
TEXT
was used instead ofVARCHAR
:VARCHAR
is the same asTEXT
and has no length limit in SQLite.TEXT
was used instead ofDECIMAL
: SQLite doesn't supportDECIMAL
data type.REAL
is typically used instead ofDECIMAL
in SQLite when precision doesn't matter. When precision matters,TEXT
is typically used in practice instead ofDECIMAL
in SQLite.TEXT
was used instead ofDATETIME
: SQLite doesn't supportDATETIME
data type.TEXT
is typically used instead ofDATETIME
in SQLite.
How to load the prepared scripts using different RDBMS and tools are covered in the following subsections.
- SQLite online
- SQLite database
- MySQL database
- PostgreSQL database
- SQL Server database
- Oracle database
SQLite online is a web-based tool that you can use to execute SQL queries. You can access it at SQLite online.
The script prepared for SQLite online is sqlite_online.sql
. You can load the prepared script by:
- Navigate to SQLite Online (https://sqliteonline.com)
- Click Import and load
sqlite_online.sql
- Click Okay
Now you should see the script executed in the right sidebar. You are ready to type into the console, follow the book, and execute SQL queries.
In case you jump to this chapter directly, you need to refer to the README file in the chapter_01
folder to install SQLite first.
The script prepared for SQLite database is sqlite_db.sql
. You can load the prepared script by:
- Open the terminal, navigate into the
chapter_02
folder of this repository - **** Run the following command, and you will be in the SQLite console environment:
sqlite3 onlinestore_chapter2.db
- In the same SQLite console environment (e.g., the line prompt starts with
sqlite>
), run the following command to load the script:
.read sqlite_db.sql
- In the same SQLite console environment, check if the table is created by running the following command:
.tables
If you see products
and review
, that means the script is loaded successfully. In the same SQLite console environment, you can run any SQL queries you want, including the examples covered by Chapter 2. You can always quit the SQLite console by typing .quit
and pressing Enter
.
In case you jump to this chapter directly, you need to refer to the README file in the chapter_01
folder to install MySQL first.
The script prepared for MySQL database is mysql_db.sql
. You can load the prepared script by:
- Open the terminal, navigate into the
chapter_02
folder of this repository - Run the following command:
mysql -u root < mysql_db.sql
If you have set up a password for your MySQL server, you can run the following command instead:
mysql -u root -p < mysql_db.sql
After that, you will be prompted to enter your password.
Now you have loaded the script. To query the database and table created by the script, you will need to navigate to the MySQL console environment. You can do this by running the following command:
mysql -u root
In the MySQL console environment, you can run any SQL queries you want, including the examples covered by Chapter 2. You can always quit the MySQL console by typing quit
and pressing Enter
.
In case you jump to this chapter directly, you need to refer to the README file in the chapter_01
folder to install PostgreSQL first.
Before loading the script, you will need to create a database named onlinestore_chapter2
and use this database via the PostgreSQL console environment first. Different from SQLite and MySQL, there are no easy approach to merge these two steps into the script.
You can follow the following steps to load the prepared script:
-
Open the terminal, and navigate into the
chapter_02
folder of this repository -
Log in to the PostgreSQL console environment by running the following command:
psql -U postgres
- In the console environment, create a database named
onlinestore_chapter2
by running the following command:
CREATE DATABASE onlinestore_chapter2;
- In the console environment, use the database
onlinestore_chapter2
by running the following command:
\c onlinestore_chapter2
- Now you are ready to load the prepared script. The script prepared for PostgreSQL database is
postgresql_db.sql
. In the console environment, run the following command:
\i postgresql_db.sql
In the same console environment, you can run any SQL queries you want, including the examples covered by Chapter 2. You can always quit the PostgreSQL console by typing \q
and pressing Enter
.
In case you jump to this chapter directly, you need to refer to the README file in the chapter_01
folder to install SQL Server first.
The script prepared for SQL Server database is sql_server_db.sql
. You can refer to the same instructions for Chapter 1 to load the script. Please remember that you should load the script from the folder of chapter_02
instead of chapter_01
.
In case you jump to this chapter directly, you need to refer to the README file in the chapter_01
folder to install Oracle first.
The script prepared for Oracle is sql_server_db.sql
. You can refer to the same instructions for Chapter 1 to load the script. Please remember that you should load the script from the folder of chapter_02
instead of chapter_01
.