Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Sq lite module5
1. MODULE 5: SQL AND SQLITE
SQL and Scripting Training
(C) 2020-2021 Highervista, LLC 1
2. 2
TOPICS
Types of RDBMS systems
Getting started with a database
project (e.g., Capstone)
SQL Concepts (DDL and DML
starter)
DDL Starter
DML Starter
Introducing SQLite
Setting up the SQLite
Environment
DB Browser for SQLite
Simple Commands in the SQLite
CLI
Preview Day 5 Afternoon
Activities
5. 5
GUI, SLI, OTHER
Some RDBMSs are primarily graphic
by design (GUI) – Think Microsoft
Access.
Most have a command-line interface
too (CLI).
SQLite is an RDBMS that has
command-line interface (CLI). It also
has a browser option (SQLite Browser
and other browser-like tools available).
6. 6
DATABASE MANAGEMENT SYSTEM/SQL
RDBMS SQL Engine
SQLite SQL – 1 file .db
MySQL SQL – complex file system
PostgreSQL SQL – complex file system
Oracle SQL – complex file system
9. 9
DDL, DML, AND DCL
Category Description Examples
Data definition language (DDL) A series of commands used to change
the way data is stored in the database
and/or alter its structure.
ALTER
COMMENT
CREATE
DROP
RENAME
TRUNCATE
Data manipulation language
(DML)
A series of commands used to add
and/or manipulate data within the
existing database schema.
INSERT
UPDATE
DELETE
Data control language (DCL) Two commands used to allow or
remove access and/or privileges to the
database
GRANT
REVOKE
10. 10
A NOTE ON DATABASE VIEWS
A VIEW is a virtual table displaying the
results of an SQL statement.
It can contain data from more than one table
and runs when it′s accessed, so the data
displayed is current.
It’s useful if you want a user to be able to see
specific data without having access to the
entire database.
11. 11
CLI VS. GUI
CLI: All commands (DDL and DML) can be
entered into the command line. Tedious.
GUI: We’ll use DB Browser for SQLite. As you
make selections in the user interface, you can
see the DDL/DML be created.
12. 12
COURSE FOCUS WITH SQLITE
DDL (like CREATE database, etc.)
DML (like SELECT commands, etc.)
Views (restrict user’s view of the database)
SQLite ‘.dot’ commands (‘support’ commands in
SQLite, similar to other database packages)
14. 14
DATA DEFINITION LANGUAGE (DDL)
• You can either create DDL ″by hand″
(text) or, for some ER design tools,
the DDL can be automatically
created from the design.
• Keep in mind that a complete Data
Dictionary can serve as a basis for
creating tables in the database.
• There are other DDL commands that
will assist you in creating the tables
and altering the table structure.
15. 15
DDL STATEMENTS
• CREATE: Used to create the database or its objects (like table,
index, function, views, store procedure and triggers).
• DROP: Used to delete objects from the database.
• ALTER: Used to alter the structure of the database.
• TRUNCATE: Used to remove all records from a table, including
all spaces allocated for the records are removed.
• COMMENT: Used to add comments to the data dictionary.
• RENAME: Used to rename an object existing in the database.
(Note: Command varies based on RDBMS.)
20. 20
DATA MANIPULATION LANGUAGE (DML)
• SELECT – Look for (query)
• INSERT – Insert into a table
UPDATE – Update existing info in a table
• DELETE – Delete existing info in a table
21. 21
QUERY, STATEMENT, AND CLAUSE
A query is a request (a question) that returns information from the
database.
An SQL statement is any valid piece of SQL code that is executed by
the SQL engine.
A clause is a subsection of a query containing at least one keyword and
relevant information needed by the query (clause is a portion of a
query).
22. 22
SQL DML SELECT
Words in ALL CAPS are SQL
keywords.
A query can contain multiple
clauses, each starting with a
keyword.
32. 32
REVIEW OF SQL CONCEPTS
A table is a two-dimensional grid of rows and columns that contain data.
Logical and Physical database terms include
table (physical) = entity (logical)
field (physical) = attribute (logical)
A database record is a row in a table or multiple rows in a table (with a
unique primary key).
Data can exist as a variety of different data types, such as strings of text,
numbers, or special characters.
Metadata describes the nature and format of the data, including any
minimum/maximum character length or required numbers, letters, or special
characters.
33. 33
REVIEW OF SQL CONCEPTS
Relational databases can contain many tables.
Each table in a relational database should have a primary
key that serves as a unique identifier for a row in a given
table.
A foreign key is any column in a table that exists as a primary
key in another table.
The relationship between tables and their primary and foreign
keys is called a database schema.
A database schema can be shown visually by an ERD (Entity
Relationship Diagram), which serves as a blueprint for a
database.
34. 34
REVIEW OF SQL CONCEPTS
There are a variety of relational database (RDBMS)
products, including the Oracle database, Microsoft
SQL Server, MySQL, IBM DB2, and SQLite. While
they may differ somewhat in their interface, the
fundamental SQL engine is essentially the same (not
including extensions).
The SELECT keyword is the most common
SQL command used in SQL queries.
SQL statements can contain multiple clauses
that use different SQL keywords.
36. 36
SQLITE INTRODUCTION
SQLite is the most popular SQL engine in
the world.
This section of the course includes hands-
on, real-world problem solving using
SQLite (the CLI portion)
SQLite (the GUI portion)
37. 37
USE CASE OF SQLITE DATABASE
SQLite is likely used more than all other database
engines combined. It is found in
•Every Android device
•Every iPhone and iOS device
•Every Mac
•Every Windows10 machine
•Every Firefox, Chrome, and Safari web browser
•Every instance of Skype
38. 38
FEATURES OF SQLITE
•SQLite does not require a separate server
process or system (serverless).
•SQLite comes with zero-configuration, so
no setup or administration are needed.
•A complete SQLite database is stored in a
single cross-platform disk file.
•SQLite is self-contained, which means no
external dependencies.
39. SETTING UP THE SQLITE
ENVIRONMENT
SQL and Scripting
Training
41. 41
GAINING ACCESS TO SQLITE
Using SQLite, users can create file-based databases that can be
transported across machines, platforms, etc.
The only thing needed to view or edit these databases is the
SQLite command line program (downloads from SQLite website), or
another tool capable of communicating with SQLite (SQLite
browser)
45. 45
CREATE A DATABASE USING DB BROWSER
With the DB Browser,
create a new database.
Create a database for
the Capstone, with your
initials.
For example, I’ve
created a database
named
RM_AWS_CAPSTONE.
46. 46
CREATE A DATABASE USING DB BROWSER
Since SQLite is one database
file, it requires at least 1 table.
Add TestTable (Table).
Add a field Test_id, INTEGER,
PK (Primary Key).
Add a field Test_Desc, TEXT.
Notice the SQL commands
populated in the bottom pane.
51. 51
CREATE A DATABASE
Open terminal window and enter
sqlite3 college.db
This starts SQLite and opens college.db
SQLite creates college.db, if not found
52. 52
CREATE A TABLE: STUDENT
Note: Commands are not case sensitive. I will use UPPER CASE for
commands/reserved words and lower case for fields.
Note: commands are separated by commas and a line ends with ;
53. 53
CREATE A TABLE: STUDENT
When there isn’t an error, sqlite prompts for another line.
For this table, four field names are identified (in lower case), as well as
the data type for each field. Commas separate the commands for each
field.
The entire SQL command ends with a ;
55. 55
SQLITE DOT OR SHELL COMMANDS:
.TABLES
SQLite comes with a handful of ′dot′ commands (aka shell commands)
Dot or shell commands allow you to control the SQLite dbms environment (more on these later)
Our first dot or shell command: .tables
The .tables command identifies the tables within the database. The example includes two tables:
course
student
56. 56
INSERTING VALUES INTO TABLES: STUDENT
AND INSTRUCTOR
The student and course tables do not have any data.
To insert data into both student and course tables:
57. 57
NOTES ON INSERTING
Remember that text can be anything. You need to identify the text by enclosing it in
single or double quotes.
Note: End a SQL command with a semicolon.
Any dot commands are NOT SQL commands, so they do not need a semicolon.
58. 58
SEE WHAT’S IN EACH TABLE: SELECT
• Select is a query (question) you ask SQL.
• Provide a table name after FROM.
• * means ″ALL″
• Two selects (with select *) reveal data in the student and course table
60. 60
SECOND SHELL COMMAND: .HEADERS ON OR
.HEADERS OFF
The shell command .headers allows you to turn headers on or off.
The headers are the column (or field) names in the table.
61. 61
INSERTING DATA INTO KNOWN FIELDS
In the previous insertion, there were values for each column in the
table.
What if you don’t have values for each field? (Example: no address
for student_id 77)
Solution:
62. 62
OTHER SHELL COMMANDS
At the end of the session .quit or .q
Help list .help
Restarting
sqlite3 test.db. << start sqlite and include the college.db name
Use .tables to confirm that you have the tables in your database
63. 63
BACKING UP A DATABASE: .BACKUP AND .RESTORE
When in sqlite and your active
database, use .backup to
create a backup of your
database
Create a backup, drop the
tables from the active
database, and restore the
database from the backup
using .restore
64. 64
INSTEAD OF A PIPE, USE CSV
By default, the pipe is used to
separate fields
You can use comma-separated
values (csv) to separate fields
HINT: The csv format is a
common Excel format, so data
can be input from Excel (as a
csv file) into your existing
tables.
66. 66
WAITING FOR MORE INPUT …
If you get …> after entering a Sequel command …
You did not enter ;
The SQLite software is waiting for more input.
67. 67
WAITING FOR MORE INPUT …
Make sure you type a semicolon at the end of each SQL command!
The sqlite3 program looks for a semicolon to know when your SQL
command is complete.
If you omit the semicolon, sqlite3 will give you a continuation
prompt and wait for you to enter more text.
This feature allows you to enter SQL commands that span multiple
lines.
68. 68
TASK
Use .schema to examine how each table was constructed.
1. What are the problems with each table?
2. How are these tables associated (linked)?
69. 69
RE-EXAMINING THE ERD
• What needs to be done with
student, course?
o Normalize the relationship.
o Ensure no repeating groups.
o Create an intersection entity
(table).
o Document items in your data
dictionary.
o Craft queries to include all
elements.
71. 71
PRIMARY KEY
A primary key is a column or group of columns that identifies the
uniqueness of rows in a table.
Each table has one, and only one, primary key.
SQLite allows you to define primary key in two ways…
72. 72
PRIMARY KEY: OPTION ONE
If the primary key has only one column, use the PRIMARY KEY
column constraint to define the primary key as follows:
CREATE TABLE table_name(
column_1 INTEGER NOT NULL PRIMARY KEY,
...
);
73. 73
PRIMARY KEY: OPTION TWO
Second, in case primary key consists of two or more columns, you use the PRIMARY KEY
table constraint to define the primary as shown in the following statement.
CREATE TABLE table_name(
column_1 INTEGER NOT NULL,
column_2 INTEGER NOT NULL,
...
PRIMARY KEY(column_1,column_2,...)
);
74. 74
PRIMARY KEY–NOT NULL
In SQL standard, the primary key column must not contain NULL
values. It means that the primary key column has an implicit NOT
NULL constraint.
However, to make the current version of SQLite compatible with the
earlier version, SQLite allows the primary key column to contain
NULL values.
75. 75
PRIMARY KEY EXAMPLE
CREATE TABLE course (
course_id INTEGER PRIMARY KEY,
course_name TEXT NOT NULL,
course_number INTEGER NOT NULL);
76. 76
PRIMARY KEY EXAMPLE
For tables in which the primary keys consist of more than
one column, you must use the PRIMARY KEY table
constraint to define primary keys.
The following slides show how to create the
student_course intersection table whose primary key
consists of two columns.
78. 78
RESOLVING M:N RELATIONSHIPS
Many-to-many M:N relationships add complexity and confusion to your model and the
application development process.
To resolve M:N relationships, separate the two entities and create two one-to-many 1:M
relationships between them with a third intersection entity.
The intersection entity usually contains attributes from both connecting entities.
79. 79
THE OVERALL MODEL AND SPECIFIC
FOCUS
Note: The model shows the derivation of
composite key on COURSE and STUDENT.
For this example, the composite key is
collapsed into one field.
80. 80
START WITH A LOGICAL M:N
Student ID Student Name Student Address Course ID Course Description
1111 Priya 123 Hilltop Ave CIS111 Introduction to Computers
ENG201 Research Writing
MAT211 Calculus I
2222 Tom 234 Carpentaria Way CIS111 Introduction to Computers
ENG102 English Composition II
MAT101 College Algebra
81. 81
M:N RESOLUTION AND IMPLEMENTATION
INTO SQLITE
• Read relationship both ways to ensure that it meets the business rules.
• For example: One to Many STUDENTs have One to Many COURSEs, and
inversely One to Many COURSEs have One to Many STUDENTs.
• The problem is that a M:N cannot be implemented physically. (It makes sense
logically!)
• Add FK to each entity, which points to back to its associated primary entity.
• ″Normalize″ to remove repeating groups.
• Add any fields to ensure data is ″atomic.″
82. 82
M:N RESOLUTION AND IMPLEMENTATION
INTO SQLITE
• In this version, fields are added to the address to provide more discrete parts of the address
(atomic values), which make it easier to maintain.
• FK is added in each entity to ″tie together″ each entity pair.
• There is still a repeating group of values if a student registers for more than one course.
• Note: Document everything you do in the data dictionary.
83. 83
REPEATING GROUPS OF INFORMATION
REMAIN Student
ID Student Name
Student
Address Course ID Course Description
1111 Priya 123 Hilltop Ave CIS111
Introduction to
Computers
ENG201 Research Writing
MAT211 Calculus I
2222 Tom
234
Carpenteria
Way CIS111
Introduction to
Computers
ENG102 English Composition II
MAT101 College Algebra
84. 84
M:N RESOLUTION AND IMPLEMENTATION
INTO SQLITE
To resolve the M:N relationship, add an intersection entity between STUDENT and COURSE.
• The intersect entity between
STUDENT and COURSE entities,
titled STUDENTCOURSE, contains
the PK attributes from STUDENT and
COURSE.
• The relationship between STUDENT
and STUDENTCOURSE is 1:M.
• The relationship between COURSE
and STUDENTCOURSE is 1:M.
85. 85
M:N RESOLUTION AND IMPLEMENTATION
IN SQLITE
• A STUDENT will have many
STUDENTCOURSEs.
• A COURSE will have many
STUDENTCOURSEs.
86. 86
FOREIGN KEYS AND FK CONSTRAINT
(ENFORCEMENT) • Keep in mind that FKs are an
association from an entity to
another PRIMARY entity.
• The decomposition of M:N
uses FKs to ensure that the
data between entities are
correctly implemented.
• As such, foreign keys must
be implemented with
constraints.
• Constraints enforce that a
foreign key in a given entity
refers to a PRIMARY entity
in an associated entity.
89. 89
SQLITE FOREIGN KEY CONSTRAINT
SUPPORT
SQLite has supported foreign key
constraint since version 3.6.19.
To check whether your current
version of SQLite supports foreign
key constraints, use:
PRAGMA foreign_keys;
The commands returns 1: enable, 0:
disable
90. 90
SQLITE FOREIGN KEY CONSTRAINT SUPPORT
To enable/disable foreign key
constraint:
PRAGMA foreign_keys = OFF;
PRAGMA foreign_keys – ON:
Note: Ensure that you have foreign key
constraint support turned on.
91. 91
START WITH TWO TABLES: STUDENT AND
STUDENTCOURSE
DROP TABLE student;
CREATE TABLE student (
student_id text PRIMARY KEY,
student_name text NOT NULL,
student_address text NOT NULL,
student_address_city text NOT NULL,
student_address_state_code text NOT NULL,
student_address_zip_code text NOT NULL,
studentcourse_id text NOT NULL,
FOREIGN KEY (studentcourse_id)
REFERENCES studentcourse (studentcourse_id)
);
92. 92
CREATE STUDENT
DROP TABLE student;
CREATE TABLE student (
student_id integer PRIMARY KEY,
student_name text NOT NULL,
student_address text,
course_id text,
FOREIGN KEY (course_id)
REFERENCES studentcourse
(course_id)
);
93. 93
SUMMARY ON PK AND FK
Keep in mind that a ″logical″ model that
contains M:N relationships will need to be
decomposed.
Taking a relationship pair that is indicated by
M:N will result in 3 entities. The two original
entities plus one association (intersection) entity.
Note that the association entity now has the
many side of the relationship (see diagram).
You will work on an exercise that will
solidify many SQL concepts including PK/FK
and the intersection entity.
94. 94
REFERENCES
Draw.io. (2020). Diagrams.net - free flowchart maker and
diagrams online. Retrieved November 23, 2020, from
https://app.diagrams.net/
SQLite Browser. (2020, November 09). DB Browser for SQLite.
Retrieved November 23, 2020, from https://sqlitebrowser.org/
SQLite. (2020). SQLite Main Website. Retrieved November 23,
2020, from https://sqlite.org/index.html
Tutorialspoint. (2020). SQLite Tutorial. Retrieved November 23,
2020, from https://www.tutorialspoint.com/sqlite/index.htm
96. 96
ABOUT THIS COURSE
This course is distributed free. I use several
sources. But importantly, I use the book noted
on the next slide.
If you are using these PowerPoints, please
attribute Highervista, LLC and me (Ron
McFarland). IN ADDITION, please attribute
the author noted on the next slide, as the
author’s textbook provides essential
information for this course.
Source: Microsoft Images
97. 97
INTRODUCTION
This course is offered to you free. HOWEVER, please
purchase the following book, as it is a primary resource for
this course. I do not make any $ from this course or this
book. So, since a handful of good content is derived from the
following text, please support this author!
Title: SQL Quickstart Guide
Author: Walter Shields
Available: Amazon, B&N, and through ClydeBank media
website at:
https://www.clydebankmedia.com/books/programming-
tech/sql-quickstart-guide