SQL Fundamentals
© copyright : spiraltrain@gmail.com
Course Schedule
• SQL Intro
• SQL Select Queries
• SQL Data Definition
• SQL Functions
• SQL Data Manipulation
• SQL Joins
• What is SQL?
• History of SQL
• SQL Standard
• SQL Parts
• SQL Environment
• Database Creation
• DDL Create Table
• SQL Data Types
• SQL Language Elements
• DML Insert Into
• SQL Errors
• Select Query
2
www.spiraltrain.nl
What is SQL?
• Abbreviation of Structured Query Language :
• Standard language to access relational database management systems (RDBMS)
• Actually three languages can be distinguished
• Declarative query language with procedural elements :
• Used to create database schemas and to insert, update, delete and query data
• Data Definition Language (DDL) :
• Language that defines database structure (relation schemas)
• This includes creating, altering, and dropping tables and establishing constraints
• Data Manipulation Language (DML) :
• Query language to create, read, update and delete tuples (CRUD operations)
• Data Control Language (DCL) :
• Used to controls a database includes administering privileges and committing data
• SQL language further deals with the following issues :
• Transaction control , integrity constraints, views, embedded SQL and dynamic SQL
3SQL Intro
www.spiraltrain.nl
History of SQL
• SQL started with name SEQUEL in the 1970's :
• Abbreviation of structured english query language
• Developed by Raymond F. Boyce and Donald D. Chamberlin
• Access data stored in IBM's System R relational database
• In 1979 Oracle introduced first commercial version of SQL :
• Oracle V2 (Version2) for VAX computers.
• In 1986 SQL first became an ANSI standard :
• SQL-89 known as SQL 1 was rather incomplete
• SQL-92 is known as first solid standard known as SQL 2
• SQL-99 known as SQL 3 :
— Recursive queries, triggers, object-oriented features
• SQL-2003 :
— Window functions, XML-related features
• SQL-2006 offers XML Query Language (XQuery) support
• SQL-2011 has improved support for temporal databases
SQL Intro 4
www.spiraltrain.nl
SQL Standard
• SQL is standard but is not completely portable among RDBMS :
• This is caused by ambiguities and vendor extensions
• Specific SQL implementation by database vendor is SQL dialect
• Vendors implement parts of the SQL standard, e.g. most implement SQL-92
• Vendors add their vendor-specific extensions
• Most vendors conform to a set of Core SQL features :
• Portability might still be limited due to missing or additional features
• Purpose of SQL-92 and SQL-99 Standards :
• Specify syntax/semantics for data definition and manipulation
• Define data structures
• Enable portability
• Specify minimal (level 1) and complete (level 2) standards
• Allow for later growth/enhancement to standard
5SQL Intro
www.spiraltrain.nl
SQL Parts
6SQL Intro
www.spiraltrain.nl
SQL Environment
• Catalog :
• A set of schemas that constitute the description of a database
• Schema :
• Structure containing descriptions of objects created by a user
• This includes base tables, views, constraints
SQL Intro 7
www.spiraltrain.nl
Database Creation
• Creating of database differs for different relational databases :
• Often done through a GUI interface particular for a database
• According to the SQL standard :
• SQL environment contains one or more catalogues :
• Each catalogue manages various metadata :
• Set of schemas consisting of :
— Relations/tables
— Views
— Assertions
— Indexes
• Users and user groups
• Many database use client-server model :
• You must connect to a remote host on certain port, log in and manage database
• SQLite database does not use this model, databases are files
8SQL Intro
Demo01
Create Database
www.spiraltrain.nl
DDL Create Table
• Relational databases contain data in tables :
• Uniquely identified by their names and are comprised of columns and rows
• Columns contain the column name, data type, and other attributes
• Rows contain the records or data for the columns
• Tables are created through CREATE TABLE statement of SQL DDL :
CREATE TABLE inventory (
StockNumber INTEGER PRIMARY KEY,
Descrip VARCHAR(50),
OnHandQuan INTEGER,
PackQty INTEGER,
PackCost FLOAT
);
• Typically columns are created of certain data type
9SQL Intro
Demo02
DDL Create Table
www.spiraltrain.nl
SQL Data Types
• Columns in a table are of certain data type :
• Often used data types are character strings, numbers and date types
• Character String types :
• CHAR(n) – fixed-length character data, n characters long bytes
• VARCHAR(n) – variable length character data
• LONG – variable-length character data
• Numeric types :
• NUMBER(p,q) – general purpose numeric data type
• INTEGER(p) – signed integer, p digits wide
• INTEGER - signed integer
• FLOAT(p) – floating point in scientific notation with p binary digits precision
• FLOAT – floating point in scientific notation
• Date/time type :
• DATE – fixed-length date/time in dd-mm-yy form
10SQL Intro
www.spiraltrain.nl
SQL Language Elements
• Clauses :
• Constituent components of statements and queries, in some cases optional
• Expressions :
• Can produce either scalar values, or tables consisting of columns and rows of data
• Predicates :
• Specify conditions that can be evaluated to true, false or unknown) values
• Used to limit the effects of statements and queries or to change program flow
• Queries :
• Retrieve the data based on specific criteria
• Statements :
• May have a persistent effect on schemata and data
• May control transactions, program flow, connections, sessions or diagnostics
• SQL statements also include the semicolon (";") statement terminator
• Insignificant whitespace is generally ignored :
• Makes it easier to format SQL code for readability
11SQL Intro
www.spiraltrain.nl
DML Insert Into
• Database table is filled with SQL INSERT INTO statements :
INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost)
VALUES (51002,'AA Dry Cells 4 Pack',173,12,9.00);
INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost)
VALUES (51004,'AA Dry Cells 8 Pack',5,12,16.80);
INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost)
VALUES (43512,'10W-30 Motor Oil, Quart',36,12,18.20);
INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost)
VALUES (51013,'D Dry Cells 8 Pack',19,12,90.20);
INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost)
VALUES (23155,'Shovel Pointed Long Handle',1500,1,9.82);
INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost)
VALUES (51001,'AAA Dry Cells 4 Pack ',92,12,9.00);
INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost)
VALUES (43111,'White Gas Gallon Can',14,4,14.75);
• Values listed in INSERT INTO statement :
• Must be in the same order as appearing in CREATE TABLE
• Character strings are surrounded with quotes, number not
12SQL Intro
Demo03
DML Insert Values
www.spiraltrain.nl
SQL Errors
• SQL State Error Codes are defined by ISO/ANSI SQL Standards :
• 5-character string consisting of 2-characters followed by 3-character subclass value
• "00" indicates success, "01" indicates a warning
• Other class values normally indicate an exception
• Example Error Code :
• SQL0208 - ORDER BY column not in result table
• Typical error is duplicate primary key error :
• Trying to insert row with key already present
13SQL Intro
Demo04
Duplicate Key Error
www.spiraltrain.nl
Select Query
• SELECT clause is used to retrieve data from database tables :
SELECT StockNumber,Descrip,OnHandQuan,PackQty,PackCost from inventory
• SELECT is followed by column names separated by comma’s
• Alternatively wild cards may be used to retrieve all columns :
SELECT * FROM inventory;
• SELECT can be combined with clauses WHERE, ORDER_BY etc.
14SQL Intro
Demo05
Select Query
© copyright : spiraltrain@gmail.com
Summary : SQL Intro
• SQL is a declarative language to access RDBM Systems :
• Used to create database schemas and to query insert, update and delete data
• SQL is standard but is not completely portable among RDBMS’s :
• This is caused by ambiguities and vendor extensions
• SQL consists of three parts :
• Data definition, data manipulation and data control language
• SQL consist of several language elements :
• Clauses, expressions, predicates, queries and statements
• SQL Environments have catalogues with schema’s and users :
• Each schema defines tables, relations, views, assertions and indexes
• SQL Standard defines SQL State Error Codes :
• 5-character string consisting of 2-characters followed by 3-character subclass value
• SQL Standard defines data types :
• Columns in tables are of a certain data type
SQL Intro 15
Exercise 1
SQL Intro

SQL Intro

  • 1.
  • 2.
    © copyright :spiraltrain@gmail.com Course Schedule • SQL Intro • SQL Select Queries • SQL Data Definition • SQL Functions • SQL Data Manipulation • SQL Joins • What is SQL? • History of SQL • SQL Standard • SQL Parts • SQL Environment • Database Creation • DDL Create Table • SQL Data Types • SQL Language Elements • DML Insert Into • SQL Errors • Select Query 2
  • 3.
    www.spiraltrain.nl What is SQL? •Abbreviation of Structured Query Language : • Standard language to access relational database management systems (RDBMS) • Actually three languages can be distinguished • Declarative query language with procedural elements : • Used to create database schemas and to insert, update, delete and query data • Data Definition Language (DDL) : • Language that defines database structure (relation schemas) • This includes creating, altering, and dropping tables and establishing constraints • Data Manipulation Language (DML) : • Query language to create, read, update and delete tuples (CRUD operations) • Data Control Language (DCL) : • Used to controls a database includes administering privileges and committing data • SQL language further deals with the following issues : • Transaction control , integrity constraints, views, embedded SQL and dynamic SQL 3SQL Intro
  • 4.
    www.spiraltrain.nl History of SQL •SQL started with name SEQUEL in the 1970's : • Abbreviation of structured english query language • Developed by Raymond F. Boyce and Donald D. Chamberlin • Access data stored in IBM's System R relational database • In 1979 Oracle introduced first commercial version of SQL : • Oracle V2 (Version2) for VAX computers. • In 1986 SQL first became an ANSI standard : • SQL-89 known as SQL 1 was rather incomplete • SQL-92 is known as first solid standard known as SQL 2 • SQL-99 known as SQL 3 : — Recursive queries, triggers, object-oriented features • SQL-2003 : — Window functions, XML-related features • SQL-2006 offers XML Query Language (XQuery) support • SQL-2011 has improved support for temporal databases SQL Intro 4
  • 5.
    www.spiraltrain.nl SQL Standard • SQLis standard but is not completely portable among RDBMS : • This is caused by ambiguities and vendor extensions • Specific SQL implementation by database vendor is SQL dialect • Vendors implement parts of the SQL standard, e.g. most implement SQL-92 • Vendors add their vendor-specific extensions • Most vendors conform to a set of Core SQL features : • Portability might still be limited due to missing or additional features • Purpose of SQL-92 and SQL-99 Standards : • Specify syntax/semantics for data definition and manipulation • Define data structures • Enable portability • Specify minimal (level 1) and complete (level 2) standards • Allow for later growth/enhancement to standard 5SQL Intro
  • 6.
  • 7.
    www.spiraltrain.nl SQL Environment • Catalog: • A set of schemas that constitute the description of a database • Schema : • Structure containing descriptions of objects created by a user • This includes base tables, views, constraints SQL Intro 7
  • 8.
    www.spiraltrain.nl Database Creation • Creatingof database differs for different relational databases : • Often done through a GUI interface particular for a database • According to the SQL standard : • SQL environment contains one or more catalogues : • Each catalogue manages various metadata : • Set of schemas consisting of : — Relations/tables — Views — Assertions — Indexes • Users and user groups • Many database use client-server model : • You must connect to a remote host on certain port, log in and manage database • SQLite database does not use this model, databases are files 8SQL Intro Demo01 Create Database
  • 9.
    www.spiraltrain.nl DDL Create Table •Relational databases contain data in tables : • Uniquely identified by their names and are comprised of columns and rows • Columns contain the column name, data type, and other attributes • Rows contain the records or data for the columns • Tables are created through CREATE TABLE statement of SQL DDL : CREATE TABLE inventory ( StockNumber INTEGER PRIMARY KEY, Descrip VARCHAR(50), OnHandQuan INTEGER, PackQty INTEGER, PackCost FLOAT ); • Typically columns are created of certain data type 9SQL Intro Demo02 DDL Create Table
  • 10.
    www.spiraltrain.nl SQL Data Types •Columns in a table are of certain data type : • Often used data types are character strings, numbers and date types • Character String types : • CHAR(n) – fixed-length character data, n characters long bytes • VARCHAR(n) – variable length character data • LONG – variable-length character data • Numeric types : • NUMBER(p,q) – general purpose numeric data type • INTEGER(p) – signed integer, p digits wide • INTEGER - signed integer • FLOAT(p) – floating point in scientific notation with p binary digits precision • FLOAT – floating point in scientific notation • Date/time type : • DATE – fixed-length date/time in dd-mm-yy form 10SQL Intro
  • 11.
    www.spiraltrain.nl SQL Language Elements •Clauses : • Constituent components of statements and queries, in some cases optional • Expressions : • Can produce either scalar values, or tables consisting of columns and rows of data • Predicates : • Specify conditions that can be evaluated to true, false or unknown) values • Used to limit the effects of statements and queries or to change program flow • Queries : • Retrieve the data based on specific criteria • Statements : • May have a persistent effect on schemata and data • May control transactions, program flow, connections, sessions or diagnostics • SQL statements also include the semicolon (";") statement terminator • Insignificant whitespace is generally ignored : • Makes it easier to format SQL code for readability 11SQL Intro
  • 12.
    www.spiraltrain.nl DML Insert Into •Database table is filled with SQL INSERT INTO statements : INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost) VALUES (51002,'AA Dry Cells 4 Pack',173,12,9.00); INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost) VALUES (51004,'AA Dry Cells 8 Pack',5,12,16.80); INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost) VALUES (43512,'10W-30 Motor Oil, Quart',36,12,18.20); INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost) VALUES (51013,'D Dry Cells 8 Pack',19,12,90.20); INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost) VALUES (23155,'Shovel Pointed Long Handle',1500,1,9.82); INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost) VALUES (51001,'AAA Dry Cells 4 Pack ',92,12,9.00); INSERT INTO inventory(StockNumber,Descrip,OnHandQuan,PackQty,PackCost) VALUES (43111,'White Gas Gallon Can',14,4,14.75); • Values listed in INSERT INTO statement : • Must be in the same order as appearing in CREATE TABLE • Character strings are surrounded with quotes, number not 12SQL Intro Demo03 DML Insert Values
  • 13.
    www.spiraltrain.nl SQL Errors • SQLState Error Codes are defined by ISO/ANSI SQL Standards : • 5-character string consisting of 2-characters followed by 3-character subclass value • "00" indicates success, "01" indicates a warning • Other class values normally indicate an exception • Example Error Code : • SQL0208 - ORDER BY column not in result table • Typical error is duplicate primary key error : • Trying to insert row with key already present 13SQL Intro Demo04 Duplicate Key Error
  • 14.
    www.spiraltrain.nl Select Query • SELECTclause is used to retrieve data from database tables : SELECT StockNumber,Descrip,OnHandQuan,PackQty,PackCost from inventory • SELECT is followed by column names separated by comma’s • Alternatively wild cards may be used to retrieve all columns : SELECT * FROM inventory; • SELECT can be combined with clauses WHERE, ORDER_BY etc. 14SQL Intro Demo05 Select Query
  • 15.
    © copyright :spiraltrain@gmail.com Summary : SQL Intro • SQL is a declarative language to access RDBM Systems : • Used to create database schemas and to query insert, update and delete data • SQL is standard but is not completely portable among RDBMS’s : • This is caused by ambiguities and vendor extensions • SQL consists of three parts : • Data definition, data manipulation and data control language • SQL consist of several language elements : • Clauses, expressions, predicates, queries and statements • SQL Environments have catalogues with schema’s and users : • Each schema defines tables, relations, views, assertions and indexes • SQL Standard defines SQL State Error Codes : • 5-character string consisting of 2-characters followed by 3-character subclass value • SQL Standard defines data types : • Columns in tables are of a certain data type SQL Intro 15 Exercise 1 SQL Intro