02 Core SQL 4
02 Core SQL 4
02 Core SQL 4
Databases
and
PostgreSQL
Charles Severance
www.pg4e.com
SQL
http://en.wikipedia.org/wiki/SQL
https://en.wikipedia.org/wiki/ANSI-SPARC_Architecture
SQL
Architecture
USING SQL
pgAdmin
(Browser / SQL
Desktop) Database
User
D.B.A. Server
psql PostgresSQL
(Command SQL
Line)
STARTING POSTGRESQL
COMMAND LINE
$ psql –U postgres
Password for user postgres: <password here>
psql (9.3.5, server 11.2)
Type "help" for help.
postgres=#
postgres-#
CREATING A USER AND
DATABASE
postgres=# CREATE USER dbUser WITH PASSWORD 'secret';
CREATE ROLE
CREATE DATABASE
postgres=# \q
https://www.postgresql.org/docs/11/sql-createuser.html
https://www.postgresql.org/docs/11/sql-createdatabase.html
CONNECTING TO A DATABASE
$ psql people
Password for user : <password here>
psql (9.3.5, server 11.2)
people=> \dt
No relations found.
people=>
https://www.postgresql.org/docs/11/app-psql.html
people=> CREATE TABLE users(
people(> name VARCHAR(128),
CREATING A
TABLE
people(> email VARCHAR(128)
people(> );
CREATE TABLE
people=> \dt CREATE TABLE users(
List of relations
Schema | Name | Type | Owner name VARCHAR(128),
--------+-------+-------+------- email VARCHAR(128)
public | users | table |
(1 row) );
people=> \d+ users
Table "public.users"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------------------------+-----------+----------+--------------+-------------
name | character varying(128) | | extended | |
email | character varying(128) | | extended | |
Has OIDs: no
people=>
SQL: INSERT
The INSERT statement inserts a row into a table
https://www.postgresql.org/docs/11/functions.html
INDEXES
As a table gets large (they always do), scanning all the data to
find a single row becomes very costly
When drchuck@gmail.com logs into Twitter, they must find my
password amongst 500 million users
There are techniques to greatly shorten the scan as long as you
create data structures and maintain those structures - like
shortcuts
Hashes or Trees are the most common
B-TREES
A B-tree is a tree data structure that keeps data sorted and allows searches,
sequential access, insertions, and deletions in logarithmic amortized time. The
B-tree is optimized for systems that read and write large blocks of data. It is
commonly used in databases and file systems.
http://en.wikipedia.org/wiki/B-tree
HASHES
A hash function is any algorithm or subroutine that
maps large data sets to smaller data sets, called
keys. For example, a single integer can serve as an
index to an array (cf. associative array). The values
returned by a hash function are called hash values,
hash codes, hash sums, checksums, or simply hashes.
Hash functions are mostly used to accelerate table
lookup or data comparison tasks such as finding
items in a database...
http://en.wikipedia.org/wiki/Hash_function
SUMMARY
SQL allows us to describe the shape of data to be stored and give many hints
to the database engine as to how we will be accessing or using the data.
SQL is a language that provides us operations to Create, Read, Update, and
Delete (CRUD) our data in a database.
ACKNOWLEDGEMENTS / CONTRIBUTIONS
These slides are Copyright 2010- Charles R. Severance (www.dr- Nurlan Shaidullaev, University of Central Asia
chuck.com) as part of www.pg4e.com and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change, feel
free to add your name and organization to the list of contributors
on this page as you republish the materials.