databases and sql programming overview · programming with a procedural language write your own...

59
Databases and SQL programming overview

Upload: dinhliem

Post on 29-Aug-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Databases and SQL programming overview

Page 2: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

Page 3: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Databases: Digital collections of data

Why use a database system?

➢ Large amounts of diverse data

Example: sequence identifiers and expression data

Page 4: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Databases: Digital collections of data

Page 5: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

Page 6: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Databases: Digital collections of data

Page 7: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Databases: Digital collections of data

Page 8: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Biological databases

➢ Complex data➢ Relationships (1-1, 1-many, many-many)➢ End users

Page 9: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Biological databases

Page 10: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

Page 11: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

PostgreSQL

http://www.postgresql.org/

➢ Install postgres (version 8.4 is the current one in Ubuntu)

➢ Log into your psql terminal

$ psql --help

$ psql -h localhost -U postgres -d my_database

➢ Get help with SQL commands

my_database=>\h

➢ Get help with psql commands

my_database=>\?

Page 12: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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 -- .

Page 13: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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!

Page 14: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

Page 15: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

SQL (Structured Query Language)

➢ Data definition (DDL)➢ Data manipulation

➢ SELECT statements➢ Joins➢ Row functions➢ Aggregate functions➢ Subqueries➢ Views

➢ * PostgreSQL, and database connection with Perl

Page 16: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data definition

Data types

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

Page 17: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

);

Page 18: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

);

SQL command

Table name

Column name Data type Constraint(s)

Page 19: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

Page 20: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data definition

Create table

CREATE TABLE sample (sample_id serial NOT NULL PRIMARY KEY,sample_name character varying NOT NULL,

species character varying);

Or:

ALTER TABLE sample ADD COLUMN species character varying ;

CREATE TABLE sample (sample_id serial NOT NULL PRIMARY KEY,sample_name character varying NOT NULL

);

Page 21: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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;

Page 22: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data definition

Drop tables

DROP TABLE sample; Oops!

Page 23: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data definition

Drop tables

DROP TABLE sample; Oops!

ALWAYS USE TRANSACTIONS!!

=> BEGIN;=> SQL statement 1; SQL statement 2 .... ;

-- I made some mistake...

=> ROLLBACK;

Page 24: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data definition

ALWAYS USE TRANSACTIONS!!

=> BEGIN;=> SQL statement 1; SQL statement 2 .... ;

-- Looks good!

=> COMMIT;

Page 25: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data definition

Foreign keys

CREATE TABLE sample (sample_id serial NOT NULL PRIMARY KEY,sample_name character varying NOT NULL,

species_id integer REFERENCES species(species_id));

CREATE TABLE species (species_id serial PRIMARY KEY,species_name character varying NOT NULL

);

Page 26: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Foreign key constraint

species

species_id (PK)species_name

animal

animal_id (PK)animal_namespecies_id (FK)

Page 27: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

Page 28: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data manipulation

Insert – add new rows to your table

INSERT INTO species (species_name) VALUES ('Solanum lycopersicum');

Delete -remove rows from table

DELETE FROM species WHERE species_id = 1;

Update – modify column value/s of existing rows

UPDATE species SET species_name = 'Solanum tuberosum' WHERE species_id = 1;

Page 29: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data manipulation

Transactions

BEGIN;

UPDATE.......;

DELETE.....;

INSERT .....;

COMMIT; or ROLLBACK;

Page 30: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data manipulation - transactions

Page 31: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Use transactions, sanitize your input

http://bobby-tables.com/

Page 32: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data manipulation – copy command

Large dataset?

➢ Write SQL file with INSERT commands

➢ Use COPY with a delimited text file

Page 33: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

http://www.postgresql.org/docs/8.4/interactive/app-psql.html

Data manipulation – copy command

Page 34: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Data manipulation – copy command

=> \copy species (species_name) FROM 'species_list.txt'

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;

Page 35: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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)

Page 36: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

SQL (Structured Query Language)

➢ Data definition➢ Data manipulation (DML)➢ SELECT statements

➢ Joins➢ Row functions➢ Aggregate functions➢ Subqueries➢ Views

➢ * PostgreSQL, and database connection with Perl

Page 37: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

SELECT statements

Page 38: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

SELECT statements

Select everything:

SELECT * FROM sample;

Count rows, group by column, with a condition:

SELECT count(sample_id) , species.species_name FROM sampleJOIN species USING (species_id)GROUP BY species_nameHAVING species_name like 'Solanum%';

Select with a condition, sort results:

SELECT sample_name FROM sampleWHERE species_id=1ORDER BY sample_name ASC;

Page 39: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

SELECT statements

Conditional operators:

SELECT .... FORM .... WHERE ....

Page 40: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

SQL (Structured Query Language)

➢ Data definition➢ Data manipulation (DML)➢ SELECT statements

➢ Joins➢ Row functions➢ Aggregate functions➢ Subqueries➢ Views

➢ * PostgreSQL, and database connection with Perl

Page 41: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Joins

Page 42: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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);

Page 43: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Joins

Page 44: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Joins

Page 45: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Row functions

Page 46: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Row functions

Math functions

String functions

http://www.postgresql.org/docs/8.4/interactive/functions-math.html

http://www.postgresql.org/docs/8.4/interactive/functions-string.html

Page 47: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Row functions

Date and time http://www.postgresql.org/docs/8.4/interactive/functions-datetime.html

Data type conversion

# SELECT now(); --this is a timestamp!

# SELECT cast (now() AS text) ; --the output is now a 'text' data type

http://www.postgresql.org/docs/8.4/static/sql-createcast.html

Page 48: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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

Page 49: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Resources

➢ http://en.wikipedia.org/wiki/SQL

➢ http://www.postgresql.org/docs/8.4/

➢ In your psql terminal:

=>\h [SQL command name]

=>\?

Page 50: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

SQL – advanced querying

➢ Data definition➢ Data manipulation (DML)➢ SELECT statements

➢ Joins➢ Row functions➢ Aggregate functions➢ Subqueries➢ Views

➢ * PostgreSQL, and database connection with Perl

Page 51: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Row functions - Case

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

SELECT * FROM test;

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

Page 52: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

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.

Page 53: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Aggregate functions

Page 54: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Aggregate functions - grouping

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

Page 55: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Aggregate functions - grouping

HAVING clause – use with aggregate functions instead of 'WHERE'

Page 56: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Subqueries

Page 57: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Subqueries

Page 58: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

Views

CREATE VIEW myview AS SELECT city, temp_lo, temp_hi, prcp, date, location FROM weather, cities WHERE city = name;

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

Page 59: Databases and SQL programming overview · Programming with a procedural language Write your own postgreSQL functions! PL/pgSQL (similar to Oracle's PL/SQL  )

postgreSQL

Programming with a procedural language Write your own postgreSQL functions!

➢ PL/pgSQL (similar to Oracle's PL/SQLhttp://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...