Relational databases and SQL



Similar documents
SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Using SQL Server Management Studio

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

SQL. Short introduction

Financial Data Access with SQL, Excel & VBA

How To Create A Table In Sql (Ahem)

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Database Migration from MySQL to RDM Server

Instant SQL Programming

A Brief Introduction to MySQL

IT2304: Database Systems 1 (DBS 1)

MS ACCESS DATABASE DATA TYPES

Darshan Institute of Engineering & Technology PL_SQL

Apache Cassandra Query Language (CQL)

IT2305 Database Systems I (Compulsory)

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

Introduction to Triggers using SQL

Database Administration with MySQL

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Introduction to SQL and database objects

CSC 443 Data Base Management Systems. Basic SQL

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

!"# $ %& '( ! %& $ ' &)* + ! * $, $ (, ( '! -,) (# *&23. mysql> select * from from clienti;

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Information Systems SQL. Nikolaj Popov

Information Systems Modelling Information Systems I: Databases

CIS 631 Database Management Systems Sample Final Exam

Introduction This document s purpose is to define Microsoft SQL server database design standards.

Comparison of Open Source RDBMS

Kaseya 2. Quick Start Guide. for VSA 6.3

Linas Virbalas Continuent, Inc.

not at all a manual simply a quick how-to-do guide

EECS 647: Introduction to Database Systems

2/3/04 Doc 7 SQL Part 1 slide # 1

Language Reference Guide

Database Query 1: SQL Basics

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

SQL Server An Overview

Programming Database lectures for mathema

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

Ontrack PowerControls User Guide Version 8.0

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

Databases 2011 The Relational Model and SQL

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

The Guru's Guide to Transact-SQL

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

5.1 Database Schema Schema Generation in SQL

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

DBMS / Business Intelligence, SQL Server

SQL: joins. Practices. Recap: the SQL Select Command. Recap: Tables for Plug-in Cars

4 Logical Design : RDM Schema Definition with SQL / DDL

Ontrack PowerControls V8.1 for SQL ReadMe

Relational Database: Additional Operations on Relations; SQL

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

A table is a collection of related data entries and it consists of columns and rows.

Porting from Oracle to PostgreSQL

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Oracle Database: SQL and PL/SQL Fundamentals

Database Extensions Visual Walkthrough. PowerSchool Student Information System

BCA. Database Management System

A basic create statement for a simple student table would look like the following.

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

Sharding with postgres_fdw

Introduction to SQL for Data Scientists

ODBC Client Driver Help Kepware, Inc.

SQL Server 2008 Core Skills. Gary Young 2011

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Introduction to Database. Systems HANS- PETTER HALVORSEN,

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

DDL is short name of Data Definition Language, which deals with database schemas and descriptions, of how the data should reside in the database.

Monitoring Agent for PostgreSQL Fix Pack 10. Reference IBM

Working with DB2 UDB objects

SQL Server Table Design - Best Practices

3.GETTING STARTED WITH ORACLE8i

B.1 Database Design and Definition

MYSQL DATABASE ACCESS WITH PHP

Oracle Database: SQL and PL/SQL Fundamentals NEW

6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

Partitioning under the hood in MySQL 5.5

Conventional Files versus the Database. Files versus Database. Pros and Cons of Conventional Files. Pros and Cons of Databases. Fields (continued)

1 SQL Data Types and Schemas

ENHANCEMENTS TO SQL SERVER COLUMN STORES. Anuhya Mallempati #

Procedural Extension to SQL using Triggers. SS Chung

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Oracle Database: SQL and PL/SQL Fundamentals

A methodology for Data Migration between Different Database Management Systems

Advance DBMS. Structured Query Language (SQL)

Inside the PostgreSQL Query Optimizer

Once the schema has been designed, it can be implemented in the RDBMS.

Transcription:

Relational databases and SQL Matthew J. Graham CACR Methods of Computational Science Caltech, 29 January 2009

relational model Proposed by E. F. Codd in 1969 An attribute is an ordered pair of attribute name and type (domain) name An attribute value is a specific valid value for the attribute type A tuple is an unordered set of attribute values identified by their names A relation is defined as an unordered set of n-tuples

databases A relation consists of a heading (a set of attributes) and a body (n-tuples) A relvar is a named variable of some specific relation type and is always associated with some relation of that type A relational database is a set of relvars and the result of any query is a relation A table is an accepted representation of a relation: attribute => column, tuple => row

structured query language Appeared in 1974 from IBM First standard published in 1986; most recent in 2006 SQL92 is taken to be default standard Different flavours: Microsoft/Sybase MySQL Oracle PostgreSQL Transact-SQL MySQL PL/SQL PL/pgSQL

CREATE DATABASE databasename CREATE TABLE tablename (name1 type1, name2 type2,... ) create CREATE TABLE star (name varchar(20), ra float, dec float, vmag float) Data types: boolean, bit, tinyint, smallint, int, bigint; real/float, double, decimal; char, varchar, text, binary, blob, longblob; date, time, datetime, timestamp CREATE TABLE star (name varchar(20) not null, ra float default 0,...)

keys CREATE TABLE star (name varchar(20), ra float, dec float, vmag float, CONSTRAINT PRIMARY KEY (name)) A primary key is a unique identifier for a row and is automatically not null CREATE TABLE star (name varchar(20),..., stellartype varchar(8), CONSTRAINT stellartype_fk FOREIGN KEY (stellartype) REFERENCES stellartypes(id)) A foreign key is a referential constraint between two tables identifying a column in one table that refers to a column in another table.

insert INSERT INTO tablename VALUES(val1, val2,... ) INSERT INTO star VALUES('Sirius', 101.287, -16.716, -1.47) INSERT INTO star(name, vmag) VALUES('Canopus', -0.72) INSERT INTO star SELECT...

delete DELETE FROM tablename WHERE condition TRUNCATE TABLE tablename DROP TABLE tablename DELETE FROM star WHERE name = 'Canopus' DELETE FROM star WHERE name LIKE 'C_n%' DELETE FROM star WHERE vmag > 0 OR dec < 0 DELETE FROM star WHERE vmag BETWEEN 0 and 5

update UPDATE tablename SET columnname = val1 WHERE condition UPDATE star SET vmag = vmag + 0.5 UPDATE star SET vmag = -1.47 WHERE name LIKE 'Sirius'

select SELECT selectionlist FROM tablelist WHERE condition ORDER BY criteria SELECT name, constellation FROM star WHERE dec > 0 ORDER by vmag SELECT * FROM star WHERE ra BETWEEN 0 AND 90 SELECT DISTINCT constellation FROM star SELECT name FROM star LIMIT 5 ORDER BY vmag

joins Inner join: combining related rows SELECT * FROM star s INNER JOIN stellartypes t ON s.stellartype = t.id SELECT * FROM star s, stellartypes t WHERE s.stellartype = t.id Outer join: each row does not need a matching row SELECT * from star s LEFT OUTER JOIN stellartypes t ON s.stellartype = t.id SELECT * from star s RIGHT OUTER JOIN stellartypes t ON s.stellartype = t.id SELECT * from star s FULL OUTER JOIN stellartypes t ON s.stellartype = t.id

aggregate functions COUNT, AVG, MIN, MAX, SUM SELECT COUNT(*) FROM star SELECT AVG(vmag) FROM star SELECT stellartype, MIN(vmag), MAX(vmag) FROM star GROUP BY stellartype SELECT stellartype, AVG(vmag), COUNT(id) FROM star GROUP BY stellartype HAVING vmag > 14

views CREATE VIEW viewname AS... CREATE VIEW region1view AS SELECT * FROM star WHERE ra BETWEEN 150 AND 170 AND dec BETWEEN -10 AND 10 SELECT id FROM region1view WHERE vmag < 10 CREATE VIEW region2view AS SELECT * FROM star s, stellartypes t WHERE s.stellartype = t.id AND ra BETWEEN 150 AND 170 AND dec BETWEEN -10 AND 10 SELECT id FROM regionview2 WHERE vmag < 10 and stellartype LIKE 'A%'

indexes CREATE INDEX indexname ON tablename(columns) CREATE INDEX vmagindex ON star(vmag) A clustered index is one in which the ordering of data entries is the same as the ordering of data records Only one clustered index per table but multiple unclustered indexes Typically implemented as B+ trees but alternate types such as bitmap index for high frequency repeated data

spatial indexing Find all objects near a particular point Remember spherical geometry R-tree - multi-dimensional index supported by PostgreSQL and Oracle Pixellation algorithms: HTM, HealPix Zone approach

zones Simple idea often more efficient than technological solution: SQL can evaluate 10 6 spatial distance calculations per sec. per CPU GHz but function calls costs 100 more Divide declination into zones of equal height: zone = dec / zoneheight create table ZoneIndex ( zone int, objid bigint, ra float, dec float,..., primary key (zone, ra, objid))

zone crossmatch select z1.objid as objid1, z2.objid as objid2 into #answer from zone z1 join zone z2 where z1.zone = z2.zone and z1.objid <> z2.objid and z1.margin = 0 and ra between ra - @maxalpha and @ra + @maxalpha and dec beween @dec - @theta and @dec + @theta and (cx+@x + cy*@y + cz+@z) > cos(radians(@theta)) insert #answer select objid2, objid1 from #answer

normalisation First normal form: no repeating elements or groups of elements table has a unique key (and no nullable columns) Second normal form: no columns dependent on only part of the key Star Name Constellation Area Third normal form: no columns dependent on other non-key columns Star Name Magnitude Flux

partitions Horizontal: different rows in different tables Vertical: different columns in different tables (normalisation) Range: rows where values in a particular column are inside a certain range List: rows where values in a particular column match a list of values Hash: rows where a hash function returns a particular value

transactions BEGIN WORK... COMMIT / ROLLBACK BEGIN WORK INSERT INTO star VALUES(...) COMMIT BEGIN WORK ROLLBACK UPDATE star SET vmag = NULL

cursors DECLARE cursorname CURSOR FOR SELECT... OPEN cursorname FETCH cursorname INTO... CLOSE cursorname A cursor is a control structure for successive traversal of records in a result set Slowest way of accessing data

cursors example For each row in the result set, update the relevant stellar model DECLARE @name varchar(20) DECLARE @mag float DECLARE starcursor CURSOR FOR SELECT name, AVG(vmag) FROM star GROUP BY stellartype OPEN starcursor FETCH starcursor INTO @name, @mag EXEC updatestellarmodel @name, @mag / CALL updatestellarmodel(@name, @mag) CLOSE starcursor

triggers CREATE TRIGGER triggername ON tablename... A trigger is procedural code that is automatically executed in response to certain events on a particular table: INSERT UPDATE DELETE CREATE TRIGGER startrigger ON star FOR UPDATE AS IF @@ROWCOUNT = 0 RETURN IF UPDATE (vmag) EXEC refreshmodels GO

stored procedures CREATE PROCEDURE procedurename @param1 type,... AS... CREATE PROCEDURE findnearestneighbour @starname varchar(20) AS BEGIN END DECLARE @ra, @dec float DECLARE @name varchar(20) SELECT @ra = ra, @dec = dec FROM star WHERE name LIKE @starname SELECT name FROM getnearestneighbour(@ra, @dec) EXEC findnearestneighbour 'Sirius'

programming Java import java.sql.*... String dburl = "jdbc:mysql://127.0.0.1:1234/test"; Connection conn = DriverManager.getConnection(dbUrl, "mjg", "mjg"); Statement stmt = conn.createstatement(); ResultSet res = stmt.executequery("select * FROM star");... conn.close();

programming Python: import MySQLdb Con = MySQLdb.connect(host="127.0.0.1", port=1234, user="mjg", passwd="mjg", db="test") Cursor = Con.cursor() sql = "SELECT * FROM star" Cursor.execute(sql) Results = Cursor.fetchall()... Con.close()

skynodes Distributed query across heterogeneous databases using common programmatic interface Poll each database to get estimate of cost of query (QueryCost) Execution plan consists of ordered list of node identifier, node URL and query for that node Node receives execution plan: if there are subsequent nodes, submit execution plan to downline node, ingest result table and perform query

examples NVO SQL tutorial examples http://www.us-vo.org/summer-school/2006/proceedings/ presentations/sql2006.html SDSS SkyServer SQL examples http://cas.sdss.org/dr6/en/help/docs/realquery.asp CasJobs http://casjobs.sdss.org/casjobs/