SlideShare a Scribd company logo
SQL – Simple Queries


   Chapter 3.1
   V3.0
   Copyright @ Napier University
   Dr Gordon Russell
Introduction

•   SQL is the Structured Query Language
•   It is used to interact with the DBMS
•   SQL can
      – Create Schemas in the DBMS
      – Alter Schemas
      – Add data
      – Remove data
      – Change data
      – Access Data
DSL

•   SQL is a Data Sub Language – DSL
•   This is a combination of two languages
     – DDL – Data Definition Language
     – DML – Data Manipulation Language

•   The main way of accessing data is using the DML command
    SELECT.
•   The abilities of the SELECT command forms the majority of
    this material on SQL
Database Models

A data model comprises
• a data structure
• a set of integrity constraints
• operations associated with the data structure

Examples of data models include:
• hierarchic
• network
• relational
Relational Databases

The relational data model comprises:

• relational data structure
• relational integrity constraints
• relational algebra or equivalent (SQL)
   – SQL is an ISO language based on relational
     algebra
   – relational algebra is a mathematical formulation
Relational Data Structure

A relational data structure is a collection of tables or
   relations.

• A relation is a collection of rows or tuples
• A tuple is a collection of columns or attributes
• A domain is a pool of values from which the actual
  attribute values are taken.
Relational Structure cont

         M E N U R elatio n o r
         T ab le                                A ttrib u tes


           D escrip tio n         P rice


                                     T u p le


                         D o m ain
Domain and Integrity Constraints

•   Domain Constraints
     – limit the range of domain values of an attribute
     – specify uniqueness and ‘nullness’ of an attribute
     – specify a default value for an attribute when no value is
       provided.
•   Entity Integrity
     – every tuple is uniquely identified by a unique non-null
       attribute, the primary key.
•   Referential Integrity
     – rows in different tables are correctly related by valid key
       values (‘foreign’ keys refer to primary keys).
Example Database

•   In order to better understand SQL, all the example queries
    make use of a simple database.
•   The database is formed from 2 tables, CAR and DRIVER.
•   Each car may be owned by a DRIVER.
•   A DRIVER may own multiple CARs.



           DRIVER                            CAR
DRIVER



  NAME        DOB
  Jim Smith   11 Jan 1980
  Bob Smith   23 Mar 1981
  Bob Jones   3 Dec 1986
CAR


 REGNO      MAKE    COLOUR   PRICE   OWNER
 F611 AAA FORD      RED      12000   Jim Smith
 J111 BBB   SKODA   BLUE     11000   Jim Smith
 A155 BDE MERCED    BLUE     22000   Bob Smith
          ES
 K555 GHT FIAT      GREEN    6000    Bob Jones
 SC04 BFE SMART     BLUE     13000
•   Each column holds data of a particular type
      – Integer, string, decimal, blobs
      – The range of values can be further constrained
•   If a column in a row contains no data, it is NULL.
•   It can indicate no possible value or unavailable data.

•   All rows must differ from each other in some way
•   Sometimes a row is called a tuple
•   Cardinality is the number of rows of a table
•   Arity is the number of columns of a table
Primary Keys

•   In the design section the idea of a Primary Key is defined.
•   A Primary Key is a group of 1 or more columns which, when
    taken together, is unique in the table
•   No part of a primary key can be NULL.
•   In our example,
     – DRIVER: the primary key is NAME
     – CAR: the primary key is REGNO
•   In our example this means that no two drivers can have the
    same name. In the real world this would be a problem, but
    this is just an example.
Referential Integrity

•   Note that there is a link between CAR and DRIVER via
    OWNER.
•   If there is a value in OWNER, then this value must also
    appear somewhere in DRIVER.
•   If you change a driver’s name in DRIVER, you must make
    sure the same change is made in OWNER of CAR.
•   The DBMS enforces the rules.
•   If you try to break the rules the DBMS reports the problem as
    a REFERENTIAL INTEGRITY error.
SQL Basics

•   Basic SQL statements include
     – CREATE – a data structure
     – SELECT – read one or more rows from a table
     – INSERT – one of more rows into a table
     – DELETE – one or more rows from a table
     – UPDATE – change the column values in a row
     – DROP – a data structure

•   In this lecture the focus is on SELECT.
Simple SELECT

•   SELECT column FROM tablename
•   SELECT column1,column2,column3 FROM tablename
•   SELECT * from tablename
•   SELECT * from CAR;

REGNO        MAKE         COLOUR   PRICE       OWNER
F611 AAA     FORD         RED      12000       Jim Smith
J111 BBB     SKODA        BLUE     11000       Jim Smith
A155 BDE     MERCEDES     BLUE     22000       Bob Smith
K555 GHT     FIAT         GREEN    6000        Bob Jones
SC04 BFE     SMART        BLUE     13000
SELECT regno from CAR;


        REGNO
        F611 AAA
        J111 BBB
        A155 BDE
        K555 GHT
        SC04 BFE
SELECT colour,owner from CAR;


         COLOUR   OWNER
         RED      Jim Smith
         BLUE     Jim Smith
         BLUE     Bob Smith
         GREEN    Bob Jones
         BLUE
Formatting

•   SPACES do not matter
•   NEWLINES do not matter
•   Good practice to put ; at the end of the query.
•   CASE (except between single quotes) does not matter.
•   These are all valid:

SELECT REGNO FROM CAR;
SElecT regno
  From Car
;
Comments

•   To give you the ability to make notes in queries you are
    allowed to have comments.
•   Comments are not executed
•   A comment starts with -- and ends with a newline
•   They are only permitted within a query.


SELECT regno -- The registration number
FROM car -- The car storage table
;
SELECT filters

•   You can have rules in your queries
•   These rules are tested for each row your query produces
•   If the rule is true, the row is displayed
•   If the rule is false, the row is not displayed
•   The rule starts with WHERE

SELECT columns
FROM table
WHERE rule
Simple Rule

•   A simple rule might be to look for a car with a colour of RED.
•   The rule would be colour = 'RED'

SELECT regno FROM CAR             SELECT regno from CAR
                                  WHERE colour = 'RED'
       REGNO
       F611 AAA
                                         REGNO
       J111 BBB
                                         F611 AAA
       A155 BDE
       K555 GHT
       SC04 BFE
Note

• Things between quotes is CASE SENSITIVE.
• ‘RED’ is not the same as ‘Red’ or ‘red’

• Rules which mention fields – they can be used if
  they appear on the SELECT line or not.

                            REGNO    COLOUR
SELECT regno from CAR
                            F611 AAA RED
WHERE colour = 'RED'
Comparisons

•   Valid comparisons include =,!=,<>,<,<=,>,>=
     – Colour = ‘RED’ The colour must be red
     – Colour != ‘RED’ The colour is not red
     – Colour <> ‘Red’ Same as !=
     – Price > 10000    More than 10000
     – Price >= 10000 More than or equal to 10000
     – Price < 10000    Cheaper than 10000
     – Price <=10000 Cheaper or the same as 10000
•   Numbers – You can say ‘10000’ or 10000. Strings always
    have quotes…
DATE

•    Date comparisons can be tricky
•    You can use all the normal comparators with dates.

SELECT name,dob                  SELECT name,dob from driver
from driver                      where DOB = ‘3 Jan 1986’

    NAME        DOB
                                     NAME         DOB
    Jim Smith   11 Jan 1980
                                     Bob Jones    3 Dec 1986
    Bob Smith   23 Mar 1981
    Bob Jones   3 Dec 1986
•   The tricky part with dates is remembering that dates get
    bigger as you move into the future.
•   DATE1>DATE2 indicates DATE1 is in the future after
    DATE2.

SELECT name,dob from driver
WHERE DOB >= ‘1 Jan 1981’
        NAME          DOB
        Bob Smith     23 Mar 1981
        Bob Jones     3 Dec 1986
DATE Syntax

•   It must be in quotes
•   Each DBMS handles dates in a slightly different way
•   Dates like ‘1 Jan 2003’ work quite well.
•   Oracle permits dates like ‘1-Jan-2003’
•   Oracle also permits dates like ‘1-Jan-03’
      – Be caseful … if you type this it will assume 2003.
      – If you mean 1984 type 1984 not –04.
•   You must always specify a day and a month. If you do not
    the DBMS will report an error.
BETWEEN

•   When dealing with dates sometimes you want to test to see
    if a field value falls between two dates.
•   The easiest way to do this is with BETWEEN

•   Find all drivers born between 1995 and 1999
         SELECT name,dob from driver
         WHERE DOB between ‘1 Jan 1985’ and ’31 Dec 1999’
•   Between works for other things, not just dates…
         SELECT regno from CAR
         where price between 5000 and 10000;
NULL

•   NULL indicates that something has no value
•   It is not a value, and you cannot use normal comparison
    operators.
•   For instance, looking for cars without owners…

Wrong:           SELECT regno from car where owner = NULL
Wrong:           SELECT regno from car where owner = ‘NULL’

•   Instead there are two special operators, IS NULL, and
    IS NOT NULL
SELECT regno from car     REGNO
WHERE OWNER is null       SC04 BFE



                          REGNO
SELECT regno from car     F611 AAA
WHERE OWNER is not null   J111 BBB
                          A155 BDE
                          K555 GHT
                          SC04 BFE
LIKE

• Sometimes you want to have a rule involving
  partial strings, substrings, or wildcards
• LIKE does this, and is a slot-in replacement for ‘=‘
• If the string contains ‘%’ or ‘_’, LIKE uses them to
  support wildcards.
   – % - Matches 0 or more characters in the string
   – _ - Matches exactly 1 character in the string
Examples

•   Name LIKE ‘Jim Smith’                 e.g. Jim Smith
•   Name LIKE ‘_im Smith’                 e.g. Tim Smith
•   Name LIKE ‘___ Smith’                 e.g. Bob Smith
•   Name LIKE ‘% Smith’                   e.g. Frank Smith
•   Name LIKE ‘% S%’                      e.g. Brian Smart
•   Name LIKE ‘Bob %’                     e.g. Bob Martin
•   Name LIKE ‘%’                         i.e. match anyone

•   LIKE is more expensive than =
•   If you are not using wildcards, always use = rather than
    LIKE.

More Related Content

Similar to Sql1

http://boxinglatestnews.com
http://boxinglatestnews.comhttp://boxinglatestnews.com
http://boxinglatestnews.com
David Lopez
 
Relational_Model.ppt
Relational_Model.pptRelational_Model.ppt
Relational_Model.ppt
Abhimanyu Verma
 
Four Basic SQL Programming for Learners
Four  Basic SQL Programming for LearnersFour  Basic SQL Programming for Learners
Four Basic SQL Programming for Learners
JcGarcia53
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
Achievers Tech
 
Practical 03 (1).pptx
Practical 03 (1).pptxPractical 03 (1).pptx
Practical 03 (1).pptx
solangiirfan92
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSFOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
HITIKAJAIN4
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
Mahir Haque
 
ADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database Systems
Joshua Costa
 
Dbms &amp; oracle
Dbms &amp; oracleDbms &amp; oracle
Dbms &amp; oracle
J VijayaRaghavan
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
Nilt1234
 
Chapter_8_SQL.pdf
Chapter_8_SQL.pdfChapter_8_SQL.pdf
Chapter_8_SQL.pdf
YoelSisay
 
SQL
SQLSQL
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
Amin Omi
 
Kenny_Automobile_EDA.pdf
Kenny_Automobile_EDA.pdfKenny_Automobile_EDA.pdf
Kenny_Automobile_EDA.pdf
KennyDevarapalli
 

Similar to Sql1 (15)

http://boxinglatestnews.com
http://boxinglatestnews.comhttp://boxinglatestnews.com
http://boxinglatestnews.com
 
Relational_Model.ppt
Relational_Model.pptRelational_Model.ppt
Relational_Model.ppt
 
Four Basic SQL Programming for Learners
Four  Basic SQL Programming for LearnersFour  Basic SQL Programming for Learners
Four Basic SQL Programming for Learners
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
Practical 03 (1).pptx
Practical 03 (1).pptxPractical 03 (1).pptx
Practical 03 (1).pptx
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONSFOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
ADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database Systems
 
Dbms &amp; oracle
Dbms &amp; oracleDbms &amp; oracle
Dbms &amp; oracle
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Chapter_8_SQL.pdf
Chapter_8_SQL.pdfChapter_8_SQL.pdf
Chapter_8_SQL.pdf
 
SQL
SQLSQL
SQL
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Kenny_Automobile_EDA.pdf
Kenny_Automobile_EDA.pdfKenny_Automobile_EDA.pdf
Kenny_Automobile_EDA.pdf
 

Recently uploaded

Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
heathfieldcps1
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
Celine George
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
National Information Standards Organization (NISO)
 

Recently uploaded (20)

Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
 

Sql1

  • 1. SQL – Simple Queries Chapter 3.1 V3.0 Copyright @ Napier University Dr Gordon Russell
  • 2. Introduction • SQL is the Structured Query Language • It is used to interact with the DBMS • SQL can – Create Schemas in the DBMS – Alter Schemas – Add data – Remove data – Change data – Access Data
  • 3. DSL • SQL is a Data Sub Language – DSL • This is a combination of two languages – DDL – Data Definition Language – DML – Data Manipulation Language • The main way of accessing data is using the DML command SELECT. • The abilities of the SELECT command forms the majority of this material on SQL
  • 4. Database Models A data model comprises • a data structure • a set of integrity constraints • operations associated with the data structure Examples of data models include: • hierarchic • network • relational
  • 5. Relational Databases The relational data model comprises: • relational data structure • relational integrity constraints • relational algebra or equivalent (SQL) – SQL is an ISO language based on relational algebra – relational algebra is a mathematical formulation
  • 6. Relational Data Structure A relational data structure is a collection of tables or relations. • A relation is a collection of rows or tuples • A tuple is a collection of columns or attributes • A domain is a pool of values from which the actual attribute values are taken.
  • 7. Relational Structure cont M E N U R elatio n o r T ab le A ttrib u tes D escrip tio n P rice T u p le D o m ain
  • 8. Domain and Integrity Constraints • Domain Constraints – limit the range of domain values of an attribute – specify uniqueness and ‘nullness’ of an attribute – specify a default value for an attribute when no value is provided. • Entity Integrity – every tuple is uniquely identified by a unique non-null attribute, the primary key. • Referential Integrity – rows in different tables are correctly related by valid key values (‘foreign’ keys refer to primary keys).
  • 9. Example Database • In order to better understand SQL, all the example queries make use of a simple database. • The database is formed from 2 tables, CAR and DRIVER. • Each car may be owned by a DRIVER. • A DRIVER may own multiple CARs. DRIVER CAR
  • 10. DRIVER NAME DOB Jim Smith 11 Jan 1980 Bob Smith 23 Mar 1981 Bob Jones 3 Dec 1986
  • 11. CAR REGNO MAKE COLOUR PRICE OWNER F611 AAA FORD RED 12000 Jim Smith J111 BBB SKODA BLUE 11000 Jim Smith A155 BDE MERCED BLUE 22000 Bob Smith ES K555 GHT FIAT GREEN 6000 Bob Jones SC04 BFE SMART BLUE 13000
  • 12. Each column holds data of a particular type – Integer, string, decimal, blobs – The range of values can be further constrained • If a column in a row contains no data, it is NULL. • It can indicate no possible value or unavailable data. • All rows must differ from each other in some way • Sometimes a row is called a tuple • Cardinality is the number of rows of a table • Arity is the number of columns of a table
  • 13. Primary Keys • In the design section the idea of a Primary Key is defined. • A Primary Key is a group of 1 or more columns which, when taken together, is unique in the table • No part of a primary key can be NULL. • In our example, – DRIVER: the primary key is NAME – CAR: the primary key is REGNO • In our example this means that no two drivers can have the same name. In the real world this would be a problem, but this is just an example.
  • 14. Referential Integrity • Note that there is a link between CAR and DRIVER via OWNER. • If there is a value in OWNER, then this value must also appear somewhere in DRIVER. • If you change a driver’s name in DRIVER, you must make sure the same change is made in OWNER of CAR. • The DBMS enforces the rules. • If you try to break the rules the DBMS reports the problem as a REFERENTIAL INTEGRITY error.
  • 15. SQL Basics • Basic SQL statements include – CREATE – a data structure – SELECT – read one or more rows from a table – INSERT – one of more rows into a table – DELETE – one or more rows from a table – UPDATE – change the column values in a row – DROP – a data structure • In this lecture the focus is on SELECT.
  • 16. Simple SELECT • SELECT column FROM tablename • SELECT column1,column2,column3 FROM tablename • SELECT * from tablename • SELECT * from CAR; REGNO MAKE COLOUR PRICE OWNER F611 AAA FORD RED 12000 Jim Smith J111 BBB SKODA BLUE 11000 Jim Smith A155 BDE MERCEDES BLUE 22000 Bob Smith K555 GHT FIAT GREEN 6000 Bob Jones SC04 BFE SMART BLUE 13000
  • 17. SELECT regno from CAR; REGNO F611 AAA J111 BBB A155 BDE K555 GHT SC04 BFE
  • 18. SELECT colour,owner from CAR; COLOUR OWNER RED Jim Smith BLUE Jim Smith BLUE Bob Smith GREEN Bob Jones BLUE
  • 19. Formatting • SPACES do not matter • NEWLINES do not matter • Good practice to put ; at the end of the query. • CASE (except between single quotes) does not matter. • These are all valid: SELECT REGNO FROM CAR; SElecT regno From Car ;
  • 20. Comments • To give you the ability to make notes in queries you are allowed to have comments. • Comments are not executed • A comment starts with -- and ends with a newline • They are only permitted within a query. SELECT regno -- The registration number FROM car -- The car storage table ;
  • 21. SELECT filters • You can have rules in your queries • These rules are tested for each row your query produces • If the rule is true, the row is displayed • If the rule is false, the row is not displayed • The rule starts with WHERE SELECT columns FROM table WHERE rule
  • 22. Simple Rule • A simple rule might be to look for a car with a colour of RED. • The rule would be colour = 'RED' SELECT regno FROM CAR SELECT regno from CAR WHERE colour = 'RED' REGNO F611 AAA REGNO J111 BBB F611 AAA A155 BDE K555 GHT SC04 BFE
  • 23. Note • Things between quotes is CASE SENSITIVE. • ‘RED’ is not the same as ‘Red’ or ‘red’ • Rules which mention fields – they can be used if they appear on the SELECT line or not. REGNO COLOUR SELECT regno from CAR F611 AAA RED WHERE colour = 'RED'
  • 24. Comparisons • Valid comparisons include =,!=,<>,<,<=,>,>= – Colour = ‘RED’ The colour must be red – Colour != ‘RED’ The colour is not red – Colour <> ‘Red’ Same as != – Price > 10000 More than 10000 – Price >= 10000 More than or equal to 10000 – Price < 10000 Cheaper than 10000 – Price <=10000 Cheaper or the same as 10000 • Numbers – You can say ‘10000’ or 10000. Strings always have quotes…
  • 25. DATE • Date comparisons can be tricky • You can use all the normal comparators with dates. SELECT name,dob SELECT name,dob from driver from driver where DOB = ‘3 Jan 1986’ NAME DOB NAME DOB Jim Smith 11 Jan 1980 Bob Jones 3 Dec 1986 Bob Smith 23 Mar 1981 Bob Jones 3 Dec 1986
  • 26. The tricky part with dates is remembering that dates get bigger as you move into the future. • DATE1>DATE2 indicates DATE1 is in the future after DATE2. SELECT name,dob from driver WHERE DOB >= ‘1 Jan 1981’ NAME DOB Bob Smith 23 Mar 1981 Bob Jones 3 Dec 1986
  • 27. DATE Syntax • It must be in quotes • Each DBMS handles dates in a slightly different way • Dates like ‘1 Jan 2003’ work quite well. • Oracle permits dates like ‘1-Jan-2003’ • Oracle also permits dates like ‘1-Jan-03’ – Be caseful … if you type this it will assume 2003. – If you mean 1984 type 1984 not –04. • You must always specify a day and a month. If you do not the DBMS will report an error.
  • 28. BETWEEN • When dealing with dates sometimes you want to test to see if a field value falls between two dates. • The easiest way to do this is with BETWEEN • Find all drivers born between 1995 and 1999 SELECT name,dob from driver WHERE DOB between ‘1 Jan 1985’ and ’31 Dec 1999’ • Between works for other things, not just dates… SELECT regno from CAR where price between 5000 and 10000;
  • 29. NULL • NULL indicates that something has no value • It is not a value, and you cannot use normal comparison operators. • For instance, looking for cars without owners… Wrong: SELECT regno from car where owner = NULL Wrong: SELECT regno from car where owner = ‘NULL’ • Instead there are two special operators, IS NULL, and IS NOT NULL
  • 30. SELECT regno from car REGNO WHERE OWNER is null SC04 BFE REGNO SELECT regno from car F611 AAA WHERE OWNER is not null J111 BBB A155 BDE K555 GHT SC04 BFE
  • 31. LIKE • Sometimes you want to have a rule involving partial strings, substrings, or wildcards • LIKE does this, and is a slot-in replacement for ‘=‘ • If the string contains ‘%’ or ‘_’, LIKE uses them to support wildcards. – % - Matches 0 or more characters in the string – _ - Matches exactly 1 character in the string
  • 32. Examples • Name LIKE ‘Jim Smith’ e.g. Jim Smith • Name LIKE ‘_im Smith’ e.g. Tim Smith • Name LIKE ‘___ Smith’ e.g. Bob Smith • Name LIKE ‘% Smith’ e.g. Frank Smith • Name LIKE ‘% S%’ e.g. Brian Smart • Name LIKE ‘Bob %’ e.g. Bob Martin • Name LIKE ‘%’ i.e. match anyone • LIKE is more expensive than = • If you are not using wildcards, always use = rather than LIKE.