SlideShare a Scribd company logo
Trinity College



                       Basic SQL
                           Timothy Richards




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372
SQL Language
• Standard for Relational DBs
 • Enabled success
• Easy to migrate
 • From one DBMS to another
 • SQL is the “same”
• In practice...
 • Differences between relational DBMSs do exist!
 • However, using those features that are part of the standard
   makes migration easier.

    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   2
SQL Language
• Declarative Language




   Trinity College, Hartford CT • Department of Computer Science • CPSC 372   3
SQL Language
• Declarative Language
  A user only specifies what the result is to be...




   Trinity College, Hartford CT • Department of Computer Science • CPSC 372   4
SQL Language
• Declarative Language
  A user only specifies what the result is to be...

The database figures out how to retrieve the result!




   Trinity College, Hartford CT • Department of Computer Science • CPSC 372   5
SQL Language
• Declarative Language
    A user only specifies what the result is to be...

 The database figures out how to retrieve the result!

   This allows for greater flexibility in the language
     and more opportunity for an SQL compiler
to optimize queries to achieve increased performance!



     Trinity College, Hartford CT • Department of Computer Science • CPSC 372   6
SQL Language
• SQL: Structured Query Language
• Originally called SEQUEL
 • Structured English Query Language
 • Designed/Implemented at IBM Research for an
   experimental DBMS called System R.

• SQL is now a standard
 • American National Standards Institute (ANSI)
 • International Standards Organization (ISO)

    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   7
SQL Language
• Standards
 • SQL-86 (SQL1)
 • SQL-92 (SQL2)
 • SQL:1999 (SQL3)
 • SQL:2003 and SQL:2006 (XML support)
 • SQL:2008 added object database features
• Later Standards
 • Core specification
 • Plus Extensions (Optional)
    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   8
SQL Language
• Comprehensive DB Language
 • Data Definition Statements
 • Updates Statements (Insert, Update, Delete)
 • Query Statements
• Both DDL and DML
 • Data Definition Language
 • Data Manipulation Language
• SQL is Important and Large
    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   9
SQL Language
• Comprehensive DB Language
 • Data Definition Statements
 • Updates Statements (Insert, Update, Delete)
 • Query Statements
                                                     We’ll start with the
• Both DDL and DML                                   basic DDL and DML
 • Data Definition Language                                statements
 • Data Manipulation Language
• SQL is Important and Large
    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   10
Create Schema
• A Schema Elements
 • Tables, Constraints,Views, Domains, and more!
• Identifies By:
 • A schema name
 • An authorization identifier

    CREATE SCHEMA COMPANY AUTHORIZATION ‘trichards’;




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   11
Catalogs
• Catalog:
 • A named collection of schemas in an SQL environment.
 • An SQL environment is an installation of an SQL-
   compliant RDBMS on a computer system.




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   12
Create Table
• Create Table Command
 • Specifies a new relation
  • With a given name
  • And a set of attributes
 • Attributes are given names, types, and constraints




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   13
Create Table
CREATE TABLE COMPANY.EMPLOYEE (
 Fname VARCHAR(15) NOT NULL,
 Lname VARCHAR(15) NOT NULL,
 Ssn   CHAR(9) NOT NULL,
 Bdate DATE,
 Dno   INT NOT NULL,
 PRIMARY KEY (Ssn),
 FOREIGN KEY (Dno) REFERENCES DEPT(no)
);




  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   14
Create Table
CREATE TABLE COMPANY.EMPLOYEE (
 Fname VARCHAR(15) NOT NULL,
 Lname VARCHAR(15) NOT NULL,
 Ssn   CHAR(9) NOT NULL,
 Bdate DATE,
 Dno   INT NOT NULL,
 PRIMARY KEY (Ssn),
 FOREIGN KEY (Dno) REFERENCES DEPT(no)
);
                          Not Required


  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   15
Create Table
CREATE TABLE COMPANY (
 Fname VARCHAR(15) NOT NULL,
 Lname VARCHAR(15) NOT NULL,
 Ssn   CHAR(9) NOT NULL,
 Bdate DATE,
 Dno   INT NOT NULL,
 PRIMARY KEY (Ssn),
 FOREIGN KEY (Dno) REFERENCES DEPT(no)
);
           As long as you create the table
           within the schema environment.

  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   16
Create Table
CREATE TABLE COMPANY (
 Fname VARCHAR(15) NOT NULL,
 Lname VARCHAR(15) NOT NULL,
 Ssn   CHAR(9) NOT NULL,
 Bdate DATE,
 Dno   INT NOT NULL,
 PRIMARY KEY (Ssn),
 FOREIGN KEY (Dno) REFERENCES DEPT(no)
);
         Base command for creating a table


  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   17
Create Table
CREATE TABLE COMPANY (
 Fname VARCHAR(15) NOT NULL,
 Lname VARCHAR(15) NOT NULL,
 Ssn   CHAR(9) NOT NULL,
 Bdate DATE,
 Dno   INT NOT NULL,
 PRIMARY KEY (Ssn),
 FOREIGN KEY (Dno) REFERENCES DEPT(no)
);
                   Non-null Constraints


  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   18
Create Table
CREATE TABLE COMPANY (
 Fname VARCHAR(15) NOT NULL,
 Lname VARCHAR(15) NOT NULL,
 Ssn   CHAR(9) NOT NULL,
 Bdate DATE,
 Dno   INT NOT NULL,
 PRIMARY KEY (Ssn),
 FOREIGN KEY (Dno) REFERENCES DEPT(no)
);
             Primary Key Constraint
          entity integrity constraint
              (non-null primary key)
  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   19
Create Table
CREATE TABLE COMPANY (
 Fname VARCHAR(15) NOT NULL,
 Lname VARCHAR(15) NOT NULL,
 Ssn   CHAR(9) NOT NULL,
 Bdate DATE,
 Dno   INT NOT NULL,
 PRIMARY KEY (Ssn),
 FOREIGN KEY (Dno) REFERENCES DEPT(no)
);
           Foreign Key Constraint
     referential integrity constraint
            (non-null foreign key)
  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   20
Attribute Domains
• Data Types
 • Numeric
 • Character string
 • Bit string
 • Boolean
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   21
Attribute Domains
•   Data Types                       Integer (Int)
                                     SmallInteger
    • Numeric                        Float
    • Character string               Real
    • Bit string                     Double
                                     Decimal(i,j) (j is precision)
    • Boolean
    • Time



       Trinity College, Hartford CT • Department of Computer Science • CPSC 372   22
Attribute Domains
• Data Types                      Char(n)
 • Numeric                        Varchar(n)
 • Character string               Clob
 • Bit string
 • Boolean
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   23
Attribute Domains
• Data Types                      Char(n)                   Fixed-length
 • Numeric                        Varchar(n)                Padded if string
                                                            is shorter than n
 • Character string               Clob
 • Bit string
 • Boolean
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372    24
Attribute Domains
• Data Types                      Char(n)                Variable-length
 • Numeric                        Varchar(n)             No Padding
 • Character string               Clob
 • Bit string
 • Boolean
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   25
Attribute Domains
• Data Types                      Char(n)                Character
 • Numeric                        Varchar(n)             Large
 • Character string               Clob                   Object
                                                         Large text values
 • Bit string                                            such as documents
 • Boolean
                                                         Maximum length
 • Time                                                  specified as K, M,
                                                         or G.



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   26
Attribute Domains
• Data Types                      Char(n)
 • Numeric                        Varchar(n)
 • Character string               Clob
 • Bit string
                                 Format
 • Boolean                       ‘Smith’
 • Time                          Concatenation
                                 ‘Bob ’ || ‘Smith’
                                 ‘Bob Smith’




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   27
Attribute Domains
• Data Types
 • Numeric
 • Character string               Bit(n)
 • Bit string                     Bit Varying(n)
 • Boolean                        Blob
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   28
Attribute Domains
• Data Types
 • Numeric
 • Character string               Bit(n)         Fixed Length
 • Bit string                     Bit Varying(n)
 • Boolean                        Blob
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   29
Attribute Domains
• Data Types
 • Numeric
 • Character string               Bit(n)         Variable
 • Bit string                     Bit Varying(n) Length
 • Boolean                        Blob
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   30
Attribute Domains
• Data Types
 • Numeric
 • Character string               Bit(n)         Binary
 • Bit string                     Bit Varying(n) Large
                                                 Object
 • Boolean                        Blob
                                                 Images, Objects
 • Time                                          Max Size
                                                 as K, M, G:

                                                           Blob(30G)

    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   31
Attribute Domains
• Data Types
 • Numeric
 • Character string               Bit(n)         Format
 • Bit string                     Bit Varying(n) ‘B0010110’
 • Boolean                        Blob
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   32
Attribute Domains
• Data Types
 • Numeric
 • Character string                                        Format
 • Bit string                                              TRUE
                                                           FALSE
 • Boolean                        Boolean
                                                           NULL (Unknow)
 • Time                                                    three-valued logic




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372    33
Attribute Domains
• Data Types
 • Numeric
 • Character string
 • Bit string
 • Boolean
 • Time                           Date
                                  Time




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   34
Attribute Domains
• Data Types
 • Numeric
 • Character string
 • Bit string
 • Boolean
 • Time                           Date          Format
                                  Time          YYYY-MM-DD
                                                DATE‘2010-09-30’




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   35
Attribute Domains
• Data Types
 • Numeric
 • Character string
 • Bit string
 • Boolean
 • Time                           Date          Format
                                  Time          HH:MM:SS
                                                TIME’13:30:27’




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   36
Attribute Domains
• Data Types
 • Numeric
                                 Format
 • Character string              DATE TIME
 • Bit string                    TIMESTAMP’2010-09-30 13:30:27’

 • Boolean
 • Time                           Date
                                  Time
                                  Timestamp

                               Extension Type
    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   37
Attribute Domains
• Data Types
 • Numeric                        Create your own domain!
 • Character string               CREATE DOMAIN SSN_TYPE AS CHAR(9);
 • Bit string
 • Boolean
 • Time



    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   38
Attribute Constraints
• Default
 • Specifies a default value for an attribute
   Dno INT DEFAULT 75




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   39
Attribute Constraints
• Default
 • Specifies a default value for an attribute
   Dno INT DEFAULT 75


• Check
 • Impose a restriction on values
   Dno INT NOT NULL CHECK (Dno > 0 AND Dno < 100)




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   40
Attribute Constraints
• Default
 • Specifies a default value for an attribute
   Dno INT DEFAULT 75


• Check
 • Impose a restriction on values
   Dno INT NOT NULL CHECK (Dno > 0 AND Dno < 100)


• Key Constraint
   PRIMARY KEY (Dno)
   Dno INT PRIMARY KEY


    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   41
Attribute Constraints
• Unique
 • Indicates uniqueness, but not a primary key
   Dname Varchar(15) UNIQUE




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   42
Attribute Constraints
• Unique
 • Indicates uniqueness, but not a primary key
   Dname Varchar(15) UNIQUE


         Other constraints can be specified,
        but often they depend on the RDBMS.




    Trinity College, Hartford CT • Department of Computer Science • CPSC 372   43
Retrieval Queries
SELECT-FROM-WHERE Structure

           SELECT          <attribute list>
           FROM            <table list>
           WHERE           <condition>;




Trinity College, Hartford CT • Department of Computer Science • CPSC 372   44
Query Example
   Retrieve the birth date and address of the
   employee(s) whose name is ‘John B. Smith’

           SELECT          <attribute list>
           FROM            <table list>
           WHERE           <condition>;




Trinity College, Hartford CT • Department of Computer Science • CPSC 372   45
Query Example
   Retrieve the birth date and address of the
   employee(s) whose name is ‘John B. Smith’

           SELECT          Bdate, Address
           FROM            <table list>
           WHERE           <condition>;




Trinity College, Hartford CT • Department of Computer Science • CPSC 372   46
Query Example
   Retrieve the birth date and address of the
   employee(s) whose name is ‘John B. Smith’

           SELECT          Bdate, Address
           FROM            EMPLOYEE
           WHERE           <condition>;




Trinity College, Hartford CT • Department of Computer Science • CPSC 372   47
Query Example
   Retrieve the birth date and address of the
   employee(s) whose name is ‘John B. Smith’

           SELECT          Bdate, Address
           FROM            EMPLOYEE
           WHERE           Fname=‘John’ AND
                           Minit=’B’ AND
                           Lname=‘Smith’;




Trinity College, Hartford CT • Department of Computer Science • CPSC 372   48
Query Example
      Retrieve the name and address of all
employees who work for the ‘Research’ department.

             SELECT          <attribute list>
             FROM            <table list>
             WHERE           <condition>;




  What is different about this query?



  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   49
Query Example
      Retrieve the name and address of all
employees who work for the ‘Research’ department.

             SELECT          Fname, Lname, Address
             FROM            EMPLOYEE, DEPARTMENT
             WHERE           Dname=‘Research’ AND
                             Dnumber=Dno;


  What is different about this query?



  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   50
Query Example
      Retrieve the name and address of all
employees who work for the ‘Research’ department.

             SELECT          Fname, Lname, Address
             FROM            EMPLOYEE, DEPARTMENT
             WHERE           Dname=‘Research’ AND
                             Dnumber=Dno;


  What is different about this query?
         It uses a join condition

  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   51
Query Example
    For every project located in ‘Stafford’, list the
project number, the controlling department number,
and the department manager’s last name, address,
                   and birth date.

              SELECT         <attribute list>
              FROM           <table list>
              WHERE          <condition>;




  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   52
Query Example
    For every project located in ‘Stafford’, list the
project number, the controlling department number,
and the department manager’s last name, address,
                   and birth date.

              SELECT         <attribute list>
              FROM           <table list>
              WHERE          <condition>;




  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   53
Query Example
    For every project located in ‘Stafford’, list the
project number, the controlling department number,
and the department manager’s last name, address,
                   and birth date.

              SELECT         Pnumber, Dnum,
                             Lname, Address,
                             Bdate
              FROM           PROJECT, DEPARTMENT,
                             EMPLOYEE
              WHERE          Dnum=Dnumber AND
                             Mgr_ssn=Ssn AND
                             Plocation=‘Stafford’;

  Trinity College, Hartford CT • Department of Computer Science • CPSC 372   54
More SQL Next Time


                          Questions?




Trinity College, Hartford CT • Department of Computer Science • CPSC 372   55

More Related Content

What's hot (20)

PPT
Data models
Dhani Ahmad
 
PPTX
Object oriented database concepts
Temesgenthanks
 
PPTX
Chapter 4 Classification
Khalid Elshafie
 
PPTX
All data models in dbms
Naresh Kumar
 
PPTX
Lect 08 materialized view
Bilal khan
 
PPTX
Database recovery
Vritti Malhotra
 
PPTX
Recovery techniques
Dr. C.V. Suresh Babu
 
PDF
Triggers and Stored Procedures
Tharindu Weerasinghe
 
PDF
Enhanced Entity-Relationship (EER) Modeling
sontumax
 
PDF
Mining Frequent Patterns And Association Rules
Rashmi Bhat
 
PPTX
Structure of dbms
Megha yadav
 
PDF
Xml databases
Srinivasan R
 
PDF
SQL Joins With Examples | Edureka
Edureka!
 
PDF
Dbms 3: 3 Schema Architecture
Amiya9439793168
 
PPTX
Class viii ch-2 log on to access
jessandy
 
PPTX
Object relational database management system
Saibee Alam
 
PPTX
Data science
SouravSadhukhan6
 
PPTX
Sql parametrized queries
Hadi Fadlallah
 
PPT
Constraints In Sql
Anurag
 
PPTX
Data preprocessing
Gajanand Sharma
 
Data models
Dhani Ahmad
 
Object oriented database concepts
Temesgenthanks
 
Chapter 4 Classification
Khalid Elshafie
 
All data models in dbms
Naresh Kumar
 
Lect 08 materialized view
Bilal khan
 
Database recovery
Vritti Malhotra
 
Recovery techniques
Dr. C.V. Suresh Babu
 
Triggers and Stored Procedures
Tharindu Weerasinghe
 
Enhanced Entity-Relationship (EER) Modeling
sontumax
 
Mining Frequent Patterns And Association Rules
Rashmi Bhat
 
Structure of dbms
Megha yadav
 
Xml databases
Srinivasan R
 
SQL Joins With Examples | Edureka
Edureka!
 
Dbms 3: 3 Schema Architecture
Amiya9439793168
 
Class viii ch-2 log on to access
jessandy
 
Object relational database management system
Saibee Alam
 
Data science
SouravSadhukhan6
 
Sql parametrized queries
Hadi Fadlallah
 
Constraints In Sql
Anurag
 
Data preprocessing
Gajanand Sharma
 

Viewers also liked (17)

PPTX
SQL Basics
Hammad Rasheed
 
PPTX
No sql and sql - open analytics summit
Open Analytics
 
PPTX
Sql basics
Genesis Omo
 
PDF
Basic Sql Handouts
jhe04
 
PDF
Sql basics joi ns and common commands (1)
johnnygoodman
 
PPTX
Sql Basic Selects
Bob Litsinger
 
PDF
Sql basics
pavitra kavali
 
PPTX
1. SQL Basics - Introduction
Varun A M
 
PPT
Writing Basic SQL SELECT Statements
Salman Memon
 
PPT
Tables And SQL basics
Amit Kumar Singh
 
PDF
개발자가 도전하는 MariaDB 서버구축
정해 이
 
PPTX
개발자도 알아야 하는 DBMS튜닝
정해 이
 
PDF
Electrical Technology
Metro Technology Centers
 
PPT
4. SQL in DBMS
koolkampus
 
PPT
Sql ppt
Anuja Lad
 
PPTX
Electrical system
Gulfaraz alam
 
SQL Basics
Hammad Rasheed
 
No sql and sql - open analytics summit
Open Analytics
 
Sql basics
Genesis Omo
 
Basic Sql Handouts
jhe04
 
Sql basics joi ns and common commands (1)
johnnygoodman
 
Sql Basic Selects
Bob Litsinger
 
Sql basics
pavitra kavali
 
1. SQL Basics - Introduction
Varun A M
 
Writing Basic SQL SELECT Statements
Salman Memon
 
Tables And SQL basics
Amit Kumar Singh
 
개발자가 도전하는 MariaDB 서버구축
정해 이
 
개발자도 알아야 하는 DBMS튜닝
정해 이
 
Electrical Technology
Metro Technology Centers
 
4. SQL in DBMS
koolkampus
 
Sql ppt
Anuja Lad
 
Electrical system
Gulfaraz alam
 
Ad

Similar to Lecture 07 - Basic SQL (20)

PDF
Sql tutorial
Axmed Mo.
 
PDF
Sql tutorial
togather111
 
DOC
Dbms Lecture Notes
dM Technologies
 
DOC
Dbms Lecture Notes
dM Technologies
 
PDF
Sql ch 4
Mukesh Tekwani
 
PDF
CS121Lec04.pdf
georgejustymirobi1
 
PDF
Sql 2009
Cathie101
 
PPT
Sql server lab_2
vijay venkatash
 
PPTX
PPT CREATEIVEhahhahahahhahahahahahaha.pptx
DjTayone
 
PDF
SQL Tutorial
ziamd
 
PDF
SQL
kaushal123
 
PDF
SQL
kaushal123
 
PPT
Sql
jyothislides
 
PDF
Unit 3 rdbms study_materials-converted
gayaramesh
 
PPTX
Session 2 - "MySQL Basics & Schema Design"
LogaRajeshwaranKarth
 
PPTX
PPT Lecture 1.1 and 1.2(Database concepts and database system architecture) (...
AbhiGrover10
 
PPTX
Structured Query Language (SQL) _ Edu4Sure Training.pptx
Edu4Sure
 
PDF
UNIT 3 SQL 10.pdf ORACEL DATABASE QUERY OPTIMIZATION
saranyaksr92
 
Sql tutorial
Axmed Mo.
 
Sql tutorial
togather111
 
Dbms Lecture Notes
dM Technologies
 
Dbms Lecture Notes
dM Technologies
 
Sql ch 4
Mukesh Tekwani
 
CS121Lec04.pdf
georgejustymirobi1
 
Sql 2009
Cathie101
 
Sql server lab_2
vijay venkatash
 
PPT CREATEIVEhahhahahahhahahahahahaha.pptx
DjTayone
 
SQL Tutorial
ziamd
 
Unit 3 rdbms study_materials-converted
gayaramesh
 
Session 2 - "MySQL Basics & Schema Design"
LogaRajeshwaranKarth
 
PPT Lecture 1.1 and 1.2(Database concepts and database system architecture) (...
AbhiGrover10
 
Structured Query Language (SQL) _ Edu4Sure Training.pptx
Edu4Sure
 
UNIT 3 SQL 10.pdf ORACEL DATABASE QUERY OPTIMIZATION
saranyaksr92
 
Ad

More from University of Massachusetts Amherst (6)

Lecture 07 - Basic SQL

  • 1. Trinity College Basic SQL Timothy Richards Trinity College, Hartford CT • Department of Computer Science • CPSC 372
  • 2. SQL Language • Standard for Relational DBs • Enabled success • Easy to migrate • From one DBMS to another • SQL is the “same” • In practice... • Differences between relational DBMSs do exist! • However, using those features that are part of the standard makes migration easier. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 2
  • 3. SQL Language • Declarative Language Trinity College, Hartford CT • Department of Computer Science • CPSC 372 3
  • 4. SQL Language • Declarative Language A user only specifies what the result is to be... Trinity College, Hartford CT • Department of Computer Science • CPSC 372 4
  • 5. SQL Language • Declarative Language A user only specifies what the result is to be... The database figures out how to retrieve the result! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 5
  • 6. SQL Language • Declarative Language A user only specifies what the result is to be... The database figures out how to retrieve the result! This allows for greater flexibility in the language and more opportunity for an SQL compiler to optimize queries to achieve increased performance! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 6
  • 7. SQL Language • SQL: Structured Query Language • Originally called SEQUEL • Structured English Query Language • Designed/Implemented at IBM Research for an experimental DBMS called System R. • SQL is now a standard • American National Standards Institute (ANSI) • International Standards Organization (ISO) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 7
  • 8. SQL Language • Standards • SQL-86 (SQL1) • SQL-92 (SQL2) • SQL:1999 (SQL3) • SQL:2003 and SQL:2006 (XML support) • SQL:2008 added object database features • Later Standards • Core specification • Plus Extensions (Optional) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 8
  • 9. SQL Language • Comprehensive DB Language • Data Definition Statements • Updates Statements (Insert, Update, Delete) • Query Statements • Both DDL and DML • Data Definition Language • Data Manipulation Language • SQL is Important and Large Trinity College, Hartford CT • Department of Computer Science • CPSC 372 9
  • 10. SQL Language • Comprehensive DB Language • Data Definition Statements • Updates Statements (Insert, Update, Delete) • Query Statements We’ll start with the • Both DDL and DML basic DDL and DML • Data Definition Language statements • Data Manipulation Language • SQL is Important and Large Trinity College, Hartford CT • Department of Computer Science • CPSC 372 10
  • 11. Create Schema • A Schema Elements • Tables, Constraints,Views, Domains, and more! • Identifies By: • A schema name • An authorization identifier CREATE SCHEMA COMPANY AUTHORIZATION ‘trichards’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 11
  • 12. Catalogs • Catalog: • A named collection of schemas in an SQL environment. • An SQL environment is an installation of an SQL- compliant RDBMS on a computer system. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 12
  • 13. Create Table • Create Table Command • Specifies a new relation • With a given name • And a set of attributes • Attributes are given names, types, and constraints Trinity College, Hartford CT • Department of Computer Science • CPSC 372 13
  • 14. Create Table CREATE TABLE COMPANY.EMPLOYEE ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Trinity College, Hartford CT • Department of Computer Science • CPSC 372 14
  • 15. Create Table CREATE TABLE COMPANY.EMPLOYEE ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Not Required Trinity College, Hartford CT • Department of Computer Science • CPSC 372 15
  • 16. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); As long as you create the table within the schema environment. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 16
  • 17. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Base command for creating a table Trinity College, Hartford CT • Department of Computer Science • CPSC 372 17
  • 18. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Non-null Constraints Trinity College, Hartford CT • Department of Computer Science • CPSC 372 18
  • 19. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Primary Key Constraint entity integrity constraint (non-null primary key) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 19
  • 20. Create Table CREATE TABLE COMPANY ( Fname VARCHAR(15) NOT NULL, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Dno) REFERENCES DEPT(no) ); Foreign Key Constraint referential integrity constraint (non-null foreign key) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 20
  • 21. Attribute Domains • Data Types • Numeric • Character string • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 21
  • 22. Attribute Domains • Data Types Integer (Int) SmallInteger • Numeric Float • Character string Real • Bit string Double Decimal(i,j) (j is precision) • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 22
  • 23. Attribute Domains • Data Types Char(n) • Numeric Varchar(n) • Character string Clob • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 23
  • 24. Attribute Domains • Data Types Char(n) Fixed-length • Numeric Varchar(n) Padded if string is shorter than n • Character string Clob • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 24
  • 25. Attribute Domains • Data Types Char(n) Variable-length • Numeric Varchar(n) No Padding • Character string Clob • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 25
  • 26. Attribute Domains • Data Types Char(n) Character • Numeric Varchar(n) Large • Character string Clob Object Large text values • Bit string such as documents • Boolean Maximum length • Time specified as K, M, or G. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 26
  • 27. Attribute Domains • Data Types Char(n) • Numeric Varchar(n) • Character string Clob • Bit string Format • Boolean ‘Smith’ • Time Concatenation ‘Bob ’ || ‘Smith’ ‘Bob Smith’ Trinity College, Hartford CT • Department of Computer Science • CPSC 372 27
  • 28. Attribute Domains • Data Types • Numeric • Character string Bit(n) • Bit string Bit Varying(n) • Boolean Blob • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 28
  • 29. Attribute Domains • Data Types • Numeric • Character string Bit(n) Fixed Length • Bit string Bit Varying(n) • Boolean Blob • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 29
  • 30. Attribute Domains • Data Types • Numeric • Character string Bit(n) Variable • Bit string Bit Varying(n) Length • Boolean Blob • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 30
  • 31. Attribute Domains • Data Types • Numeric • Character string Bit(n) Binary • Bit string Bit Varying(n) Large Object • Boolean Blob Images, Objects • Time Max Size as K, M, G: Blob(30G) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 31
  • 32. Attribute Domains • Data Types • Numeric • Character string Bit(n) Format • Bit string Bit Varying(n) ‘B0010110’ • Boolean Blob • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 32
  • 33. Attribute Domains • Data Types • Numeric • Character string Format • Bit string TRUE FALSE • Boolean Boolean NULL (Unknow) • Time three-valued logic Trinity College, Hartford CT • Department of Computer Science • CPSC 372 33
  • 34. Attribute Domains • Data Types • Numeric • Character string • Bit string • Boolean • Time Date Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 34
  • 35. Attribute Domains • Data Types • Numeric • Character string • Bit string • Boolean • Time Date Format Time YYYY-MM-DD DATE‘2010-09-30’ Trinity College, Hartford CT • Department of Computer Science • CPSC 372 35
  • 36. Attribute Domains • Data Types • Numeric • Character string • Bit string • Boolean • Time Date Format Time HH:MM:SS TIME’13:30:27’ Trinity College, Hartford CT • Department of Computer Science • CPSC 372 36
  • 37. Attribute Domains • Data Types • Numeric Format • Character string DATE TIME • Bit string TIMESTAMP’2010-09-30 13:30:27’ • Boolean • Time Date Time Timestamp Extension Type Trinity College, Hartford CT • Department of Computer Science • CPSC 372 37
  • 38. Attribute Domains • Data Types • Numeric Create your own domain! • Character string CREATE DOMAIN SSN_TYPE AS CHAR(9); • Bit string • Boolean • Time Trinity College, Hartford CT • Department of Computer Science • CPSC 372 38
  • 39. Attribute Constraints • Default • Specifies a default value for an attribute Dno INT DEFAULT 75 Trinity College, Hartford CT • Department of Computer Science • CPSC 372 39
  • 40. Attribute Constraints • Default • Specifies a default value for an attribute Dno INT DEFAULT 75 • Check • Impose a restriction on values Dno INT NOT NULL CHECK (Dno > 0 AND Dno < 100) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 40
  • 41. Attribute Constraints • Default • Specifies a default value for an attribute Dno INT DEFAULT 75 • Check • Impose a restriction on values Dno INT NOT NULL CHECK (Dno > 0 AND Dno < 100) • Key Constraint PRIMARY KEY (Dno) Dno INT PRIMARY KEY Trinity College, Hartford CT • Department of Computer Science • CPSC 372 41
  • 42. Attribute Constraints • Unique • Indicates uniqueness, but not a primary key Dname Varchar(15) UNIQUE Trinity College, Hartford CT • Department of Computer Science • CPSC 372 42
  • 43. Attribute Constraints • Unique • Indicates uniqueness, but not a primary key Dname Varchar(15) UNIQUE Other constraints can be specified, but often they depend on the RDBMS. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 43
  • 44. Retrieval Queries SELECT-FROM-WHERE Structure SELECT <attribute list> FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 44
  • 45. Query Example Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’ SELECT <attribute list> FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 45
  • 46. Query Example Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’ SELECT Bdate, Address FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 46
  • 47. Query Example Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’ SELECT Bdate, Address FROM EMPLOYEE WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 47
  • 48. Query Example Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’ SELECT Bdate, Address FROM EMPLOYEE WHERE Fname=‘John’ AND Minit=’B’ AND Lname=‘Smith’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 48
  • 49. Query Example Retrieve the name and address of all employees who work for the ‘Research’ department. SELECT <attribute list> FROM <table list> WHERE <condition>; What is different about this query? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 49
  • 50. Query Example Retrieve the name and address of all employees who work for the ‘Research’ department. SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname=‘Research’ AND Dnumber=Dno; What is different about this query? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 50
  • 51. Query Example Retrieve the name and address of all employees who work for the ‘Research’ department. SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname=‘Research’ AND Dnumber=Dno; What is different about this query? It uses a join condition Trinity College, Hartford CT • Department of Computer Science • CPSC 372 51
  • 52. Query Example For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date. SELECT <attribute list> FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 52
  • 53. Query Example For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date. SELECT <attribute list> FROM <table list> WHERE <condition>; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 53
  • 54. Query Example For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birth date. SELECT Pnumber, Dnum, Lname, Address, Bdate FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation=‘Stafford’; Trinity College, Hartford CT • Department of Computer Science • CPSC 372 54
  • 55. More SQL Next Time Questions? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 55