Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 COMP234 – Perl SQL

2 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)

3 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 –http://www.sqlite.org/lang.htmlhttp://www.sqlite.org/lang.html

4 SELECT statement

5 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

6 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

7 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

8 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

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

10 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

11 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 http://www.sqlite.org/lang_aggfunc.html

12 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 http://www.sqlite.org/lang_corefunc.html

13 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

14 Some other Functions http://www.sqlite.org/lang_corefunc.html length(X) lower(X) also upper() max(X,Y,...) also min random() round(X,Y) substr(X,Y,Z)

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

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

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

18 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

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

20 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

21 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

22 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

23 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

24 dbish

25 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

26 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 https://metacpan.org/pod/DBI::Shell

27 sqlite3.exe

28 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 http://www.sqlite.org/download.html http://www.sqlite.org/download.html 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

29 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

30 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:...>

31 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

32 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


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

Similar presentations


Ads by Google