06_database_design_part_II
06_database_design_part_II
SQL
Spatial databases
Implementation
What is SQL?
3
SQL syntaxes used in different databases are almost similar, though few RDBMS use a few different commands and
even proprietary SQL syntaxes.
It allows you to define the data in a database and manipulate that specific
data.
With the help of SQL, you can create and drop databases and tables.
SQL offers you to use the function in a database, create a view, and stored
procedure.
You can set permissions on tables, procedures, and views.
Characters:
CHAR(20) -- fixed length
VARCHAR(40) -- variable length
Numbers:
BIGINT, INT, SMALLINT, TINYINT
REAL, FLOAT -- differ in precision
MONEY
Times and dates:
DATE
DATETIME -- SQL Server
Others... All are simple
Tables in SQL
14
Tuples or rows
Tables Explained
15
A tuple = a record
Restriction: all attributes are of atomic type
A table = a set of tuples
Like a list…
…but it is unordered: no first(), no next(), no last().
Tables Explained
16
SELECT attributes
FROM relations (possibly multiple)
WHERE conditions (selections)
Simple SQL Query
18
SELECT *
FROM Product
WHERE category=‘Gadgets’
Input Schema
Output Schema
Selections
21
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’
Eliminating Duplicates
23
Category
SELECT DISTINCT category Gadgets
FROM Product
Photography
Household
Compare to:
Category
Gadgets
SELECT category
Gadgets
FROM Product
Photography
Household
Ordering the Results
24
Ties are broken by the second attribute on the ORDER BY list, etc.
Ordering the Results
25
SELECT category
FROM Product
ORDER BY pname
?
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Ordering the Results
26
Category
SELECT DISTINCT category Gadgets
FROM Product
Household
ORDER BY category
Photography
Compare to:
?
SELECT DISTINCT category
FROM Product
ORDER BY pname
Joins in SQL
27
Product Company
PName Price
SingleTouch $149.99
Joins
30
Find all countries that manufacture some product in the ‘Gadgets’ category.
SELECT country
FROM Product, Company
WHERE manufacturer=cname AND category=‘Gadgets’
Joins in SQL
31
Product Company
SELECT country
FROM Product, Company
WHERE manufacturer=cname AND category=‘Gadgets’
Country
What is ??
the problem ? ??
What’s the
solution ?
Joins
32
Find names of people living in Seattle that bought some product in the
‘Gadgets’ category, and the names of the stores they bought such product
from
Find all stores that sold at least one product that the store
‘BestBuy’ also sold:
Answer (store)
Tuple Variables
36
General rule:
tuple variables introduced automatically by the system:
Data Types
Primitives: Integer, Numeric, String, Boolean
Structured: Date/Time, Array, Range, UUID
Document: JSON/JSONB, XML, Key-value (Hstore)
Geometry: Point, Line, Circle, Polygon
Customizations: Composite, Custom Types
Data Integrity
UNIQUE, NOT NULL
Primary Keys
Foreign Keys
Exclusion Constraints
Explicit Locks, Advisory Locks
Extensibility
Stored functions and procedures
Procedural Languages: PL/PGSQL, Perl, Python (and many more)
Processing and analytic functions for both vector and raster data for
splicing, dicing, morphing, reclassifying, and collecting/unioning with the
power of SQL
raster map algebra for fine-grained raster processing
Spatial reprojection SQL callable functions for both vector and raster data
Support for importing / exporting ESRI shapefile vector data via both
commandline and GUI packaged tools and support for more formats via
other 3rd-party Open Source tools
Packaged command-line for importing raster data from many standard
formats: GeoTiff, NetCDF, PNG, JPG to name a few
Rendering and importing vector data support functions for standard textual
formats such as KML,GML, GeoJSON,GeoHash and WKT using SQL
Rendering raster data in various standard formats GeoTIFF, PNG, JPG,
NetCDF, to name a few using SQL
Seamless raster/vector SQL callable functions for extrusion of pixel values
by geometric region, running stats by region, clipping rasters by a
geometry, and vectorizing rasters
3D object support, spatial index, and functions
Network Topology support
Packaged Tiger Loader / Geocoder/ Reverse Geocoder / utilizing US
Census Tiger data
The following operations and many more are specified by the Open Geospatial
Consortium standard:
Spatial Measurements: Computes line length, polygon area, the distance between
geometries, etc.
Spatial Functions: Modify existing features to create new ones, for example by providing
a buffer around them, intersecting features, etc.
Spatial Predicates: Allows true/false queries about spatial relationships between
geometries. Examples include "do two polygons overlap" or 'is there a residence located
within a mile of the area we are planning to build the landfill?' (see DE-9IM)
Geometry Constructors: Creates new geometries, usually by specifying the vertices (points
or nodes) which define the shape.
Observer Functions: Queries which return specific information about a feature such as the
location of the center of a circle
Felix Mutua, PhD
Spatial Database Management System
Spatial Database Management System (SDBMS) provides the
capabilities of a traditional database management system (DBMS)
while allowing special storage and handling of spatial data.
SDBMS:
Works with an underlying DBMS
Allows spatial data models and types
Spatial application
Interface to DBMS
Data types
DBMS
Operations
Query language
Core spatial functionality Algorithms
Access methods
Interface to DBMS
Spatial Query Language
Number of specialized adaptations of SQL
Spatialquery language
Temporal query language (TSQL2)
Find all the counties through which the Merced river runs
SELECT C.Name, R.Name
FROM County C, River R
WHERE Intersect(C.Shape, R.Shape) = 1 AND R.Name = ‘Merced’;
Each student will choose a system for a database design (based on the
previous assignment). You are required to:
1. Design
a) Prepare a revised ER diagram using pgAdmin 4 tool
b) Forward engineer the diagram to SQL
c) Execute the SQL
The revised diagram, the SQL text and an empty database backup(sql)
should be zipped together and uploaded to
https://forms.gle/EL3CRSWhjFuMfeTb7
Deadline next week.
Felix Mutua, PhD