Databases and SQL programming overview

Similar documents
Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Oracle Database 11g: SQL and PL/SQL Fundamentals

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

SQL functions fit into two broad categories: Data definition language Data manipulation language

Oracle Syllabus Course code-r10605 SQL

5. Single-row function

A Unit of SequelGate Innovative Technologies Pvt. Ltd. All Training Sessions are Completely Practical & Real-time

Oracle Database: Introduction to SQL Ed 2

Oracle Database: Introduction to SQL

Oracle SQL & PL SQL Course

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

Learn Well Technocraft

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

AO3 - Version: 2. Oracle Database 11g SQL

Oracle Database 10g: Introduction to SQL

Oracle Database 12c SQL Fundamentals

Today Learning outcomes LO2

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Querying Data with Transact SQL

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Oracle Database: SQL and PL/SQL Fundamentals

Module 9: Managing Schema Objects

CGS 3066: Spring 2017 SQL Reference

Oracle Database: Introduction to SQL/PLSQL Accelerated

EDUVITZ TECHNOLOGIES

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

T-SQL Training: T-SQL for SQL Server for Developers

Bonus Content. Glossary

Oracle Database 11g: Introduction to SQLRelease 2

Sql Server Syllabus. Overview

Course Outline and Objectives: Database Programming with SQL

MTA Database Administrator Fundamentals Course

Introduction to Computer Science and Business

UNIT-IV (Relational Database Language, PL/SQL)

Introduction to SQL/PLSQL Accelerated Ed 2

New ways to migrate from Oracle

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

Chapter-14 SQL COMMANDS

20761 Querying Data with Transact SQL

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Oracle Database 10g: SQL Fundamentals I

Querying Data with Transact-SQL (761)

Oracle Database 10g: SQL Fundamentals I

Database Management System 9

PL/PGSQL AN INTRODUCTION ON USING IMPERATIVE PROGRAMMING IN POSTGRESQL

Model Question Paper. Credits: 4 Marks: 140

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

EE221 Databases Practicals Manual

GridDB Advanced Edition SQL reference

SQL Commands & Mongo DB New Syllabus

Deccan Education Society s FERGUSSON COLLEGE, PUNE (AUTONOMOUS) SYLLABUS UNDER AUTONOMY. FIRST YEAR B.Sc. COMPUTER SCIENCE SEMESTER I

THE UNIVERSITY OF AUCKLAND

EDB Postgres Hadoop Data Adapter Guide

Oracle 12C DBA Online Training. Course Modules of Oracle 12C DBA Online Training: 1 Oracle Database 12c: Introduction to SQL:

Course Outline. MySQL Database Administration & Design. Course Description: Pre-requisites: Course Content:

Deccansoft softwareservices-microsoft Silver Learing Partner. SQL Server Syllabus

Oracle Database 11g: SQL Fundamentals I

SQL Interview Questions

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

Workshop. Introducing SQL: A Foundation of Data Analytics. Robb Sombach University of Alberta Alberta School of Business

Assignment Session : July-March

The Structured Query Language Get Started

Relation Databases. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, PGT CS II Shift Jaipur

1 Prepared By Heena Patel (Asst. Prof)

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

Advanced SQL Tribal Data Workshop Joe Nowinski

Nikolay Samokhvalov.

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Course Details Duration: 3 days Starting time: 9.00 am Finishing time: 4.30 pm Lunch and refreshments are provided.

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

Topics Fundamentals of PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

ORACLE TRAINING. ORACLE Training Course syllabus ORACLE SQL ORACLE PLSQL. Oracle SQL Training Syllabus

INFORMATION TECHNOLOGY NOTES

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Relational Database Systems Part 01. Karine Reis Ferreira

After completing this course, participants will be able to:

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

DB2 SQL Class Outline

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Data about data is database Select correct option: True False Partially True None of the Above

PostgreSQL Documentation. Fast Backward

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Certification Exam Preparation Seminar: Oracle Database SQL

Querying Data with Transact-SQL

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

Brief Contents. Foreword by Sarah Frostenson...xvii. Acknowledgments... Introduction... xxiii. Chapter 1: Creating Your First Database and Table...

Chapter 1 SQL and Data

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

ORACLE PL/SQL DATABASE COURSE

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

MySQL for Beginners Ed 3

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

Transcription:

Databases and SQL programming overview

Databases: Digital collections of data A database system has: Data + supporting data structures The management system (DBMS) Popular DBMS Commercial: Oracle, IBM, Microsoft GUI: Microsoft Access, OpenOffice Base Terminal: MySQL, Postgres, SQLite

Databases: Digital collections of data Why use a database system? Large amounts of diverse data Example: sequence identifiers and expression data

Databases: Digital collections of data

Databases: Digital collections of data Why use a database system? Large amounts of diverse data Example: sequence identifiers and expression data Many concurrent end-users or collaborators

Databases: Digital collections of data

Databases: Digital collections of data

Biological databases Complex data Relationships (1-1, 1-many, many-many) End users

Biological databases

SQL (Structured Query Language) Language for building, accessing, and manipulating a relational database management systems (RDBMS) Database Schema: tables, relationships, permissions The data Access the data with SQL queries

PostgreSQL http://www.postgresql.org/ Install postgres (version 8.4 is the current one in Ubuntu) $ psql --help Log into your psql terminal $ psql -h localhost -U postgres -d my_database Get help with SQL commands my_database=>\h Get help with psql commands my_database=>\?

SQL server basics Keywords are not case sensitive. Table and column names are stored as they are entered (use lower case as naming convention). SQL statements terminated with a semicolon. Elements are comma separated. Comments are enclosed between /* and */ or preceded by --.

SQL (Structured Query Language) Language for building, accessing, and manipulating a relational database management systems (RDBMS) Database Create a new database from your psql terminal Create or drop database from your linux terminal (createdb and dropdb commands) * Mind owner of the database, and permissions!

SQL (Structured Query Language) Language for building, accessing, and manipulating a relational database management systems (RDBMS) Database Schema: tables, relationships, permissions (DDL) The data Access the data with SQL queries

SQL (Structured Query Language) Data definition (DDL) Data manipulation SELECT statements Joins Row functions Aggregate functions Subqueries Views * PostgreSQL, and database connection with Perl

Data definition Data types http://www.postgresql.org/docs/8.4/static/datatype.html

Data definition Create table Defines the structure of the table: CREATE TABLE sample ( sample_id serial NOT NULL PRIMARY KEY, sample_name character varying NOT NULL );

Data definition Create table Defines the structure of the table: Table name CREATE TABLE sample ( sample_id serial NOT NULL PRIMARY KEY, sample_name character varying NOT NULL ); SQL command Column name Data type Constraint(s)

Data definition Constraints are used to enforce valid data in columns modifies the structure of the table: NOT NULL CHECK PRIMARY KEY FOREIGN KEY http://www.postgresql.org/docs/8.4/static/ddl-constraints.html

Data definition Create table CREATE TABLE sample ( sample_id serial NOT NULL PRIMARY KEY, sample_name character varying NOT NULL, species character varying ); Or: CREATE TABLE sample ( sample_id serial NOT NULL PRIMARY KEY, sample_name character varying NOT NULL ); ALTER TABLE sample ADD COLUMN species character varying ;

Data definition Alter table modifies the structure of the table: ALTER TABLE sample ALTER COLUMN species SET NOT NULL; ALTER TABLE sample DROP COLUMN species; ALTER TABLE sample ADD COLUMN species text DEFAULT NULL;

Data definition Drop tables DROP TABLE sample; Oops!

Data definition Drop tables DROP TABLE sample; Oops! ALWAYS USE TRANSACTIONS!! => BEGIN; => SQL statement 1; SQL statement 2... ; -- I made some mistake... => ROLLBACK;

Data definition ALWAYS USE TRANSACTIONS!! => BEGIN; => SQL statement 1; SQL statement 2... ; -- Looks good! => COMMIT;

Data definition Foreign keys CREATE TABLE species ( species_id serial PRIMARY KEY, species_name character varying NOT NULL ); CREATE TABLE sample ( sample_id serial NOT NULL PRIMARY KEY, sample_name character varying NOT NULL, species_id integer REFERENCES species(species_id) );

Foreign key constraint species animal species_id (PK) animal_id (PK) species_name animal_name species_id (FK)

SQL (Structured Query Language) Language for building, accessing, and manipulating a relational database management systems (RDBMS) Database Schema: tables, relationships, permissions (DDL) The data (INSERT, UPDATE, DELETE) Access the data with SQL queries

Data manipulation Insert add new rows to your table INSERT INTO species (species_name) VALUES ('Solanum lycopersicum'); Update modify column value/s of existing rows UPDATE species SET species_name = 'Solanum tuberosum' WHERE species_id = 1; Delete -remove rows from table DELETE FROM species WHERE species_id = 1;

Data manipulation Transactions BEGIN; UPDATE...; DELETE...; INSERT...; COMMIT; or ROLLBACK;

Data manipulation - transactions

Use transactions, sanitize your input http://bobby-tables.com/

Data manipulation copy command Large dataset? Write SQL file with INSERT commands Use COPY with a delimited text file

Data manipulation copy command http://www.postgresql.org/docs/8.4/interactive/app-psql.html

Data manipulation copy command BEGIN; INSERT INTO species (species_name) VALUES ('Solanum melongena'); INSERT INTO species (species_name) VALUES ('Solanum tuberosum'); INSERT INTO species (species_name) VALUES ('Capsicum annuum');.. COMMIT; => \copy species (species_name) FROM 'species_list.txt'

SQL (Structured Query Language) Language for building, accessing, and manipulating a relational database management systems (RDBMS) Database Schema: tables, relationships, permissions (DDL) The data (INSERT, UPDATE, DELETE) Access the data with SQL queries (SELECT)

SQL (Structured Query Language) Data definition Data manipulation (DML) SELECT statements Joins Row functions Aggregate functions Subqueries Views * PostgreSQL, and database connection with Perl

SELECT statements

SELECT statements Select everything: SELECT * FROM sample; Select with a condition, sort results: SELECT sample_name FROM sample WHERE species_id=1 ORDER BY sample_name ASC; Count rows, group by column, with a condition: SELECT count(sample_id), species.species_name FROM sample JOIN species USING (species_id) GROUP BY species_name HAVING species_name like 'Solanum%';

SELECT statements Conditional operators: SELECT... FORM... WHERE...

SQL (Structured Query Language) Data definition Data manipulation (DML) SELECT statements Joins Row functions Aggregate functions Subqueries Views * PostgreSQL, and database connection with Perl

Joins

Joins Inner joins are default, so the word 'inner' can be omitted Natural joins (when foreign key column has the same name as the referenced column) SELECT * FROM sample JOIN species USING (species_id);

Joins

Joins

Row functions

Row functions Math functions http://www.postgresql.org/docs/8.4/interactive/functions-math.html String functions http://www.postgresql.org/docs/8.4/interactive/functions-string.html

Row functions Date and time http://www.postgresql.org/docs/8.4/interactive/functions-datetime.html http://www.postgresql.org/docs/8.4/static/sql-createcast.html Data type conversion # SELECT now(); --this is a timestamp! # SELECT cast (now() AS text) ; --the output is now a 'text' data type

PostgreSQL the basics $ psql -h hostname -U username -d dbname Users: postgres, your_user_name, other_user *user permissions postgres is the database superuser Grant permissions to other users as required

Resources http://en.wikipedia.org/wiki/sql http://www.postgresql.org/docs/8.4/ In your psql terminal: =>\h [SQL command name] =>\?

SQL advanced querying Data definition Data manipulation (DML) SELECT statements Joins Row functions Aggregate functions Subqueries Views * PostgreSQL, and database connection with Perl

Row functions - Case SELECT * FROM test; The SQL CASE expression is a generic conditional expression, similar to if/else statements in other languages: CASE WHEN condition THEN result [WHEN...] [ELSE result] END a --1 2 3 SELECT a, CASE WHEN a=1 THEN 'one' WHEN a=2 THEN 'two' ELSE 'other' END FROM test; a case ---+------1 one 2 two 3 other

Row functions more conditional expressions The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null. It is often used to substitute a default value for null values when data is retrieved for display, for example: SELECT COALESCE(description, short_description, '(none)')... The NULLIF function returns a null value if value1 and value2 are equal; otherwise it returns value1. This can be used to perform the inverse operation of the COALESCE example given above: SELECT NULLIF(value, '(none)')... If value1 is (none), return a null, otherwise return value1.

Aggregate functions

Aggregate functions - grouping GROUP BY will condense into a single row all selected rows that share the same values for the grouped expressions

Aggregate functions - grouping HAVING clause use with aggregate functions instead of 'WHERE'

Subqueries

Subqueries

Views A view is a virtual table based on the results of an SQL statement http://www.postgresql.org/docs/8.4/interactive/tutorial-views.html CREATE VIEW myview AS SELECT city, temp_lo, temp_hi, prcp, date, location FROM weather, cities WHERE city = name;

postgresql Programming with a procedural language Write your own postgresql functions! PL/pgSQL (similar to Oracle's PL/SQL http://en.wikipedia.org/wiki/pl_sql ) Many other languages: PL/Perl http://www.postgresql.org/docs/8.1/static/plperl.html PL/Java, plphp, PL/Python, PL/R and more...