COMP234 – Perl SQL. SQL Syntax Keywords upper case: CREATE TABLE English like syntax Multi-word verbs –INSERT INTO Parentheses and other syntactical guides.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

CC SQL Utilities.
Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Mary K. Olson PS Reporting Instance – Query Tool 101.
Concepts of Database Management Sixth Edition
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
MBAC 611.  We have been using MS Access to query and modify our databases.  MS Access provides a GUI (Graphical User Interface) that hides much of the.
State of Connecticut Core-CT Project Query 4 hrs Updated 1/21/2011.
Introduction to SQL J.-S. Chou Assistant Professor.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Databases Chapter 6: Understanding the SQL Language.
Chapter 3 Single-Table Queries
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
PeopleSoft Financials Advanced Query Training Financial Information Systems and Reporting Controller’s Division
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Aggregator Stage : Definition : Aggregator classifies data rows from a single input link into groups and calculates totals or other aggregate functions.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Introduction to Database Programming with Python Gary Stewart
Retrieving Data Using the SQL SELECT Statement
SQL Query Getting to the data ……..
SQL and SQL*Plus Interaction
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
SQL in Oracle.
Using SQL*Plus.
ORACLE SQL Developer & SQLPLUS Statements
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Contents Preface I Introduction Lesson Objectives I-2
Using SQL*Plus.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

COMP234 – Perl SQL

SQL Syntax Keywords upper case: CREATE TABLE English like syntax Multi-word verbs –INSERT INTO Parentheses and other syntactical guides rarely required Comma lists used instead of parentheses –SELECT source, port ORDER BY source Except where required –INSERT INTO table_name (column1, column2,..., columnN) VALUES (value1, value2,..., valueN) Keywords uppercase:CREATE TABLEEnglish like syntaxMulti-word verbsINSERT INTOParentheses and othersyntacticalguides rarely requiredComma lists used instead ofparentheses SELECT source,portORDER BY sourceExceptwhere requiredINSERT INTO table_name (column1,column2,...,columnN)VALUES (value1,value2,...,valueN)Keywords uppercase:CREATE TABLEEnglish like syntaxMulti-word verbsINSERT INTOParentheses and othersyntacticalguides rarely requiredComma lists used instead ofparentheses SELECT source,portORDER BY sourceExceptwhere requiredINSERT INTO table_name (column1,column2,...,columnN)VALUES (value1,value2,...,valueN)

SQLite Syntax SQL standardized in 72 (ANSI SQL 72) –Then each DBMS vendor began implementing extensions New standard is SQL 99 –But many vendor's extensions vary considerably from 99 Have to check the particular DBMS extensions and exceptions to know syntax For SQLite see –

SELECT statement

SELECT SELECT is the statement used to retrieve data from SQL database tables SELECT creates a new table (the result set) SELECT statement can be used in other statements instead of the name of a stored table Specifies data to be retrieved and some processing to be applied

SELECT Syntax SELECT column1, column2,..., columnN FROM table1, table2,..., tableN JOIN condition WHERE condition GROUP BY expresson HAVING condition ORDER BY expression LIMIT expression OFFSET expression

ORDER BY, LIMIT, OFFSET These clauses apply to the result set ORDER BY sorts the result set according to a list of result set columns –SELECT source, port FROM probes ORDER BY source LIMIT limits the rows in the result set OFFSET skips a number of rows at the start –Like start and length in substring function

GROUP BY Groups records according to a key SELECT source FROM probes GROUP BY source –Will be one row in result set per source value Enables the use of aggregate functions applied to the columns not in the GROUP BY clause –SELECT source, COUNT(port) FROM probes GROUP BY source

GROUP BY.... HAVING HAVING condition evaluated for each group –Uses one arbitrarily selected row if necessary If condition evaluates to false, group is discarded

COUNT function The COUNT function used in the previous example is an aggregate function Meaning that it operates within a group COUNT( port) counts the number of times port is not NULL in the group COUNT( DISTINCT port) counts unique values COUNT (*) counts the rows in the group

AGGREGATE functions Operate within a group MAX (x) returns largest x in the group MIN(x) return smallest AVG(x) returns average TOTAL(x) or SUM (X) return total –TOTAL returns 0 if all values NULL Without GROUP BY, functions operate on entire table

Other Functions Most functions (or other expressions) can be used in place of column names in SELECT statements Many functions change data formats, or apply simple transformations Some expressions can perform complex calculations with the results occupying a column in the result set

Date Functions date(timestring, modifier, modifier,...) time(timestring, modifier, modifier,...) datetime(timestring, modifier, modifier,...) SELECT date('now'); SQLite doesn't support date datatypes, so timestamps from files have to be stored as integers Date manipulation requires some computation

Some other Functions length(X) lower(X) also upper() max(X,Y,...) also min random() round(X,Y) substr(X,Y,Z)

Expressions -- Literals 'text literal' 1, 1.2,.4E-5 X'17ac89e4' NULL CURRENT_TIME CURRENT_DATE CURRENT_TIMESTAMP

Expressions -- Columns Column name source Table.column name probes.source Database.Table.column name assign2.probes.source Useful in SELECT from multiple JOINed tables

Expressions – Operators Unary operators + - ~ NOT Binary operators >= = == != <> || = concatonate * / % + - AND OR

Expressions – SELECT Statements A SELECT returning a single value, enclosed in parentheses, is an expression EXISTS SELECT... –True if any rows returned Expression [NOT] IN (list of scalars, or one column SELECT) –True if expression is in the result set, or in the list

Expressions – Misc expression ISNULL ( NOTNULL, NOT NULL) expression [NOT] BETWEEN expression AND expression Lots more, including a CASE statement

JOIN Join links one table to another Suppose a table called students has information about students including course_id Another table called course has course_id and course_name We could use JOIN to connect the two tables Then we could access student data with course_name rather than id

JOIN Example SELECT course.course_name, student.student.name FROM course, student WHERE course.course_id = student.course_id In this example JOIN is implied by the WHERE clause Some SQL implementations require explicit JOIN clause

SELECT Syntax SELECT column1, column2,..., columnN FROM table1, table2,..., tableN JOIN condition WHERE condition GROUP BY expression HAVING condition ORDER BY expression LIMIT expression OFFSET expression

WHERE WHERE clause of SELECT statement contains an expression involving columns from the FROM tables that evaluates to true or false Rows are included in the result set only if the WHERE clause evaluates to true

dbish

DBI::Shell module Installed with CPAN (takes a while to build) Invoked by dbish dbi:SQLite:file-name Provides a shell where SQL commands can be executed Like many SQL browsers command is entered and then executed SELECT ending in / is run right away

dbish commands /help /current - show the current command /edit - edit the current command /do - run the current command /clear - clear the current command /spool file-name –Starts sending output to file-name /spool off /save file-name –Current buffer saved to file

sqlite3.exe

sqlite3 sqlite3.exe is a stand alone command line utility that provides a means of running SQL statements against an sqlite database It can be obtained in a zip file from Unzip the file and copy sqlite3.exe to your flash drive Put it in a directory that's in your PATH, or Put it where you like and put that directory in your path or Put it in your project folder

Sqlite3 shell sqlite3 provides a shell with a prompt like this: sqlite> You can enter SQL statements or SQLite commands. SQLite commands start with "." A useful one is.help.tables shows you the name of the tables in the database.schema table-name shows you the create statement for the table

Running SQL Statements SQL statements can run over as many lines as you wish They aren't executed until you end them with a ; If you start to enter an SQL statement and press enter before typing the ; you will get a continuation prompt asking you to finish the command like this:...>

Output from SELECT Statement Output from select statement is sent to the screen.headers ON includes column names at the start.mode column provides neatest output.output filename sends output to a file.output stdout sends output to the screen LIMIT 20 at the end of your SELECT statement limits number of lines of output

Labs In today's lab we use SQL select statement to extract data Next class is last new lab Week 13 is a workshop –Students may use the class period to work on missed labs in class. Lab test and then written test in the last two classes