Lecture 2 Examples
Lecture 2 Examples
Database
Lecture 2 SQL
Topics: Introduction to SQL, SQLite, Creating a
DB in SQLite, Table Schema, Data Insertion,
select statement, Filters, Aggregate Functions,
Action Queries
The primary way to work with a relational database is to use Structured Query
Language, SQL (pronounced “sequel,” or simply stated as S-Q-L)
SQL is a language to operate databases; it includes database creation,
deletion, fetching rows, modifying rows, etc. SQL is an ANSI (American
National Standards Institute) standard language, but there are many different
versions of the SQL language.
Almost all applications that work with databases (such as database
management systems) make use of SQL to analyze and manipulate relational
data
From a simple request for data to a complex update operation, SQL is a
mainstay of programmers and database administrators.
2
A Brief History of SQL
Records/Rows/Tuples
select * from students; Prints all the rows from the table
MacOS users can simply open the Terminal and type “sqlite3” to open the program
SQLite Via Command Line
- You may first have to navigate to C:\ by typing ”cd C:\”
- You can then type “cd SQLite” to navigate to the SQLite folder shown in the
previous slide
“sqlite3 retailapp.db”
.database command
prints the DB’s that
are created
Tables
Tables are the basic structures where data is stored in the database.
Given that in most cases, there is no way for the database vendor/software to
know ahead of time what your data storage needs are, you will need to
create tables in the database yourself.
Many database tools allow you to create tables without writing SQL but given
that tables are the container of all the data, it is important to briefly
introduce the create table syntax in this lecture.
11
create table Syntax
create table [if not exists] [schema_name].table_name (
table_column_1 data_type primary key,
table_column_2 data_type not null,
table_column_3 data_type default 0,
table_constraints
);
First, specify the name of the table that you want to create after the create table keywords.
The name of the table cannot start with sqlite_ because it is reserved for the internal use of
SQLite.
Second, use if not exists option to create a new table if it does not exist. Attempting to
create a table that already exists without using the if not exists option will result in an error.
Third, optionally specify the schema_name to which the new table belongs. The schema can
be the main database, temp database or any attached database.
Fourth, specify the column list of the table. Each column has a name, data type, and the
column constraint. SQLite supports primary key, unique, not null, and check column
constraints.
Fifth, specify the table constraints such as primary key, foreign key, unique constraints.
Table Design
A DB table has two views
Design (Schema) view: defines table attributes, attributes data type, PK
or CPK, FK
Datasheet (data) view: shows table data or records
Table design means deciding on the table schema
The following steps are to be used to determine table schema (both
attributes and data types):
Decide on tables needed for the DB
Identify the attributes for each table
Select meaningful names for tables and attributes
Populate the DB tables with data
Attribute data types of attributes: integer, text, numeric, real, etc.
Table Naming Convention
Table attribute are all lower case, use first the full table name as a
prefix
Example:
employee_id employee_last_name employee_first_name
Do not use plural names for table names, i.e. do not use employees as
a name, instead use ‘employee'
Creating an Employee Tables
Now that we have “retailapp.db” created in sqlite3, let’s add a table to it
The data type for employee_id is integer, and it is designated as the PK.
employee_first_name, is the employee’s first name in text and employee_last_name is the employee’s
last name in text. Both employee_first_name and employee_last_name cannot be null.
Task: Create additional tables in your DB, create a product Table with product_code,
product_description, and product_price
Opening a Upon starting the CLI, the .database command does not
show the retailapp.db that we have created
Database
You have created the
retailapp.db, the employee table
and have inserted some records
into it.
Suppose you want to close out of
the terminal and want to resume
your work later, you may notice
that the database that we have
created isn’t immediately
accessible.
To access our DB, we can use the
“.open” command and provide the Upon using the .open command and specifying
file path to where the database is the file path, we can see that the DB has been loaded
stored
This will keep you in the CLI but exit from the sqlite3 program.
You can restart the program by typing “sqlite3”
select Statement
There are two types of select statements in SQL: Unconditional select
and Conditional select statements
Unconditional select
Class discussion
Why and when must you use the dot notation?
See Example 3
Example 2: Unconditional select Statement
Conditional select
select column name, column name, …
from table name, table name, …
where clause;
Operators used in where
Relational: =, !=, <, >, <=, >=
Logical: not, and, or
Other: in, between
Filters
enable you to control the result of an SQL
statement
Different types of filters exist in SQL
distinct Filter: filter repetitions in query results; Example 4
like and not like; Example 5
order by filter; Example 6
Class discussion
What does each filter do?
Example 4: distinct Filter
Ex) distinct Filter:
A very important filter is group by: allows you to include table and scalar
query results in one query
Class discussion
What happens if you do not use the group by filter?
Example 8: insert
Example 9: delete
Example 10: create
Example 8: Action Queries - insert
Ex) Sequential
insert into employee
values (100, “zeid”, “abe”);
Ex) Non-Sequential
insert into employee (emp_lname)
values (“smith”);
Class discussion
What does sequential mean?
Example 9 Action Queries – delete and
update
Ex) delete
Ex) update
Class discussion:
update employee What are the results?
set emp_lname = “smith”
where emp_lname = “zeid”;
Example 10 Action Queries – create and
group by
Ex) create table
create employee
(employee_id int primary key, employee_last_name text);
Web-based
https://www.geeksforgeeks.org/introduction-to-sqlite/
https://www.javatpoint.com/sqlite-tutorial
https://www.techonthenet.com/sqlite/index.php