SlideShare a Scribd company logo
1 of 55
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

introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Databasenehabsairam
 
Mongo Nosql CRUD Operations
Mongo Nosql CRUD OperationsMongo Nosql CRUD Operations
Mongo Nosql CRUD Operationsanujaggarwal49
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)pptGowarthini
 
Introduction to database & sql
Introduction to database & sqlIntroduction to database & sql
Introduction to database & sqlzahid6
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functionsfarwa waqar
 
ETL VS ELT.pdf
ETL VS ELT.pdfETL VS ELT.pdf
ETL VS ELT.pdfBOSupport
 
Structured Query Language (SQL)
Structured Query Language (SQL)Structured Query Language (SQL)
Structured Query Language (SQL)Syed Hassan Ali
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structurekalyanineve
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introductionSmriti Jain
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)Sabana Maharjan
 
NoSQL databases - An introduction
NoSQL databases - An introductionNoSQL databases - An introduction
NoSQL databases - An introductionPooyan Mehrparvar
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraintsmadhav bansal
 

What's hot (20)

introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Database
 
NoSQL databases
NoSQL databasesNoSQL databases
NoSQL databases
 
Mongo Nosql CRUD Operations
Mongo Nosql CRUD OperationsMongo Nosql CRUD Operations
Mongo Nosql CRUD Operations
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
 
Introduction to database & sql
Introduction to database & sqlIntroduction to database & sql
Introduction to database & sql
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
SQOOP PPT
SQOOP PPTSQOOP PPT
SQOOP PPT
 
ETL VS ELT.pdf
ETL VS ELT.pdfETL VS ELT.pdf
ETL VS ELT.pdf
 
Structured Query Language (SQL)
Structured Query Language (SQL)Structured Query Language (SQL)
Structured Query Language (SQL)
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
DISE - Database Concepts
DISE - Database ConceptsDISE - Database Concepts
DISE - Database Concepts
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
NoSQL databases
NoSQL databasesNoSQL databases
NoSQL databases
 
NOSQL vs SQL
NOSQL vs SQLNOSQL vs SQL
NOSQL vs SQL
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
NoSQL databases - An introduction
NoSQL databases - An introductionNoSQL databases - An introduction
NoSQL databases - An introduction
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 

Viewers also liked

Viewers also liked (17)

Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql StatementsLes01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
No sql and sql - open analytics summit
No sql and sql - open analytics summitNo sql and sql - open analytics summit
No sql and sql - open analytics summit
 
Sql basics
Sql  basicsSql  basics
Sql basics
 
Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
 
Sql basics joi ns and common commands (1)
Sql basics  joi ns and common commands (1)Sql basics  joi ns and common commands (1)
Sql basics joi ns and common commands (1)
 
Sql Basic Selects
Sql Basic SelectsSql Basic Selects
Sql Basic Selects
 
Sql basics
Sql basicsSql basics
Sql basics
 
1. SQL Basics - Introduction
1. SQL Basics - Introduction1. SQL Basics - Introduction
1. SQL Basics - Introduction
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Tables And SQL basics
Tables And SQL basicsTables And SQL basics
Tables And SQL basics
 
개발자가 도전하는 MariaDB 서버구축
개발자가 도전하는 MariaDB 서버구축개발자가 도전하는 MariaDB 서버구축
개발자가 도전하는 MariaDB 서버구축
 
개발자도 알아야 하는 DBMS튜닝
개발자도 알아야 하는 DBMS튜닝개발자도 알아야 하는 DBMS튜닝
개발자도 알아야 하는 DBMS튜닝
 
Electrical Technology
Electrical TechnologyElectrical Technology
Electrical Technology
 
4. SQL in DBMS
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMS
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Electrical system
Electrical systemElectrical system
Electrical system
 

Similar to Lecture 07 - Basic SQL

Introduce Apache Cassandra - JavaTwo Taiwan, 2012
Introduce Apache Cassandra - JavaTwo Taiwan, 2012Introduce Apache Cassandra - JavaTwo Taiwan, 2012
Introduce Apache Cassandra - JavaTwo Taiwan, 2012Boris Yen
 
Quick tour all handout
Quick tour all handoutQuick tour all handout
Quick tour all handoutYi-Shin Chen
 
The Land Sharks are on the Squawk Box (Where Did Postgres Come From?)
The Land Sharks are on the Squawk Box (Where Did Postgres Come From?)The Land Sharks are on the Squawk Box (Where Did Postgres Come From?)
The Land Sharks are on the Squawk Box (Where Did Postgres Come From?)EDB
 
search.ppt
search.pptsearch.ppt
search.pptPikaj2
 
Using the Chebotko Method to Design Sound and Scalable Data Models for Apache...
Using the Chebotko Method to Design Sound and Scalable Data Models for Apache...Using the Chebotko Method to Design Sound and Scalable Data Models for Apache...
Using the Chebotko Method to Design Sound and Scalable Data Models for Apache...Artem Chebotko
 
hands on: Text Mining With R
hands on: Text Mining With Rhands on: Text Mining With R
hands on: Text Mining With RJahnab Kumar Deka
 
Spark and cassandra (Hulu Talk)
Spark and cassandra (Hulu Talk)Spark and cassandra (Hulu Talk)
Spark and cassandra (Hulu Talk)Jon Haddad
 
Ontology-based multi-domain metadata for research data management using tripl...
Ontology-based multi-domain metadata for research data management using tripl...Ontology-based multi-domain metadata for research data management using tripl...
Ontology-based multi-domain metadata for research data management using tripl...João Rocha da Silva
 
BTEC- HND In Computing-Creating Table_Week6.pptx
BTEC- HND In Computing-Creating Table_Week6.pptxBTEC- HND In Computing-Creating Table_Week6.pptx
BTEC- HND In Computing-Creating Table_Week6.pptxTTKCreation
 
East Coast Oracle 2018 APEX Charts - Data Viz Now
East Coast Oracle 2018 APEX Charts - Data Viz NowEast Coast Oracle 2018 APEX Charts - Data Viz Now
East Coast Oracle 2018 APEX Charts - Data Viz NowKaren Cannell
 

Similar to Lecture 07 - Basic SQL (18)

Lecture 06
Lecture 06Lecture 06
Lecture 06
 
Introduce Apache Cassandra - JavaTwo Taiwan, 2012
Introduce Apache Cassandra - JavaTwo Taiwan, 2012Introduce Apache Cassandra - JavaTwo Taiwan, 2012
Introduce Apache Cassandra - JavaTwo Taiwan, 2012
 
search engine
search enginesearch engine
search engine
 
[系列活動] 資料探勘速遊
[系列活動] 資料探勘速遊[系列活動] 資料探勘速遊
[系列活動] 資料探勘速遊
 
Quick tour all handout
Quick tour all handoutQuick tour all handout
Quick tour all handout
 
Unit iii
Unit iiiUnit iii
Unit iii
 
The Land Sharks are on the Squawk Box (Where Did Postgres Come From?)
The Land Sharks are on the Squawk Box (Where Did Postgres Come From?)The Land Sharks are on the Squawk Box (Where Did Postgres Come From?)
The Land Sharks are on the Squawk Box (Where Did Postgres Come From?)
 
search.ppt
search.pptsearch.ppt
search.ppt
 
Using the Chebotko Method to Design Sound and Scalable Data Models for Apache...
Using the Chebotko Method to Design Sound and Scalable Data Models for Apache...Using the Chebotko Method to Design Sound and Scalable Data Models for Apache...
Using the Chebotko Method to Design Sound and Scalable Data Models for Apache...
 
hands on: Text Mining With R
hands on: Text Mining With Rhands on: Text Mining With R
hands on: Text Mining With R
 
Spark and cassandra (Hulu Talk)
Spark and cassandra (Hulu Talk)Spark and cassandra (Hulu Talk)
Spark and cassandra (Hulu Talk)
 
Basic SQL Part 2
Basic SQL Part 2Basic SQL Part 2
Basic SQL Part 2
 
Ontology-based multi-domain metadata for research data management using tripl...
Ontology-based multi-domain metadata for research data management using tripl...Ontology-based multi-domain metadata for research data management using tripl...
Ontology-based multi-domain metadata for research data management using tripl...
 
BTEC- HND In Computing-Creating Table_Week6.pptx
BTEC- HND In Computing-Creating Table_Week6.pptxBTEC- HND In Computing-Creating Table_Week6.pptx
BTEC- HND In Computing-Creating Table_Week6.pptx
 
Realizing Semantic Web - Light Weight semantics and beyond
Realizing Semantic Web - Light Weight semantics and beyondRealizing Semantic Web - Light Weight semantics and beyond
Realizing Semantic Web - Light Weight semantics and beyond
 
Web search engines
Web search enginesWeb search engines
Web search engines
 
2015 10-08 - additive manufacturing software 1
2015 10-08 - additive manufacturing software  12015 10-08 - additive manufacturing software  1
2015 10-08 - additive manufacturing software 1
 
East Coast Oracle 2018 APEX Charts - Data Viz Now
East Coast Oracle 2018 APEX Charts - Data Viz NowEast Coast Oracle 2018 APEX Charts - Data Viz Now
East Coast Oracle 2018 APEX Charts - Data Viz Now
 

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

Editor's Notes