1 / 18

CpSc 3220 The Language of SQL

CpSc 3220 The Language of SQL. Chapters 1-4. SQL Background. SQL was created at IBM as a language for SYSTEM-R, an early RDBM based on Codd’s work ( Rockoff says RDBMs are called relational to indicate their tables are related. Not true.) Later used in other RDBMs

aquarius
Download Presentation

CpSc 3220 The Language of SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CpSc 3220The Language of SQL Chapters 1-4

  2. SQL Background • SQL was created at IBM as a language for SYSTEM-R, an early RDBM based on Codd’s work (Rockoffsays RDBMs are called relational to indicate their tables are related. Not true.) • Later used in other RDBMs • Standardized by ANSI and ISO in various versions; SQL92, SQL99, … ,SQL2011 • Commercial RDBMs rarely implement a fully-standard version of SQL

  3. Textbook Covers Three RDBMs • SQL Server from Microsoft • Oracle from Oracle • MySQL from Oracle

  4. We Will Concentrate on MySQL • A link to an online MySQL reference manual http://dev.mysql.com/doc/refman/5.5/en/index.html

  5. SQL is a Declarative Language • Statements say WHAT we want, not HOW to get it • Has some procedural elements • Three ‘sub-languages’: DML, DDL, DCL • We will spend most of our time on the DML part • There are several statement types in the DML part of SQL • We will spend most of our time on the SELECT statement

  6. But First . . . • Rockoff discusses SQL datatypes • There are a bunch – we concentrate on a few: • int • decimal • float • char • varchar • date • datetime • time

  7. Back to the SELECT Statement • We review the full SELECT syntax • We will only study part of it • We start with a very small part • First, we review the syntax notation used for describing SQL statements

  8. Syntax of the SELECT statement SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]]

  9. Syntax Notation • Information in upper case is ‘reserved’, must be spelled as given (but upper case isn’t required in actual SQL statements) • Information in lower case must be further defined • The information inside [ ] is optional • The | symbol is used to indicate alternatives • The . . . symbol indicates (zero or more) repetitions • All other symbols are used as is

  10. Some Syntax Notes • SQL statements are generally case-insensitive • SQL is generally free-format • SQL identifier names are made up of letters, numbers, special symbols, and spaces. (names with special symbols and spaces may require delimiters.) Fully defined in Reference Manual. • Some DBMS (including MySQL) require statement terminators (;)

  11. select_expr • Made be either * a comma-separated list of fields

  12. fields • A field may be a column-name field a literal-value field a calculated or generated fieldmade up of column-names, literal-values, operators, and/or functions • Fields may have aliases

  13. SQL allows Aliases DB elements • Any select_expr can be given a new name: SELECT colexp AS aliasNameFROM table; The alias will be used in column headings • Tables can also be given aliases SELECT colexp FROM tableAS tableAlias;

  14. Samples • Suppose we have a database with two tables; orders and customers • We use this notation to describe this database orders(orderID, customerID, OrderAmount) customer(customerID, FirstName, LastName)

  15. The Customer Table from Ch 1

  16. The Orders Table from Ch 1

  17. SQL Select Examples • We can write SQL statements for this database SELECT “Hello, World”; SELECT * FROM orders; SELECT firstName FROM customers; SELECT 2*OrderAmount FROM orders; SELECT “Amt doubled =”,2*OrderAmount FROM orders; SELECT concat(“Dr. ”, LastName) FROM customers; SELECT concat(“Dr. ”, LastName) AS Name FROM customers;

  18. The DBs from the Rockoff book are available online • Download the appropriate (MySQL) file from the site given in the CourseMaterials link on BB • Use a text editor, open a blank file, cut and paste the script for a specific chapter to your open text file. • Modify this file by adding the statements below at the beginning of the script DROP DATABASE IF EXISTS yourDBname; CREATE DATABASE yourDBname; USE yourDBname; • Save it with any name you choose with a suffix .sql • Use PHPMyAdmin to import and run the saved script

More Related