Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 SY306 Web and Databases for Cyber Operations SQL: Structured Query Language.

Similar presentations


Presentation on theme: "1 SY306 Web and Databases for Cyber Operations SQL: Structured Query Language."— Presentation transcript:

1 1 SY306 Web and Databases for Cyber Operations SQL: Structured Query Language

2 2 SQL - The Language of Databases  Developed by IBM in the 1970s  Create and process database data  SQL programming is a critical skill !!!

3 3 Facebook and Databases  Relational databases are accessed in much the same way across the board: SQL. Learning how SQL works is crucial to getting anything done in databases, and any GUI is largely a wrapper around the SQL statements one uses to make those actions happen.  Knowing a little about database design (layout, B-trees, file storage, normalization) is good, mostly for helping you understand good queries.  We run the LAMP stack here, so we primarily use MySQL databases across the site.  I hope this helps a little. Another good motivation may be found in the requirements for most engineering positions here on http://www.facebook.com ;) http://www.facebook.com Thanks! Nick from Facebook (a few years ago)

4 4 Relational Query Languages  A major strength of the relational model:  supports simple, powerful querying of data  Ad-hoc queries  High-level (declarative) languages  Queries can be written intuitively  DBMS is responsible for efficient evaluation.

5 5 SQL DDL and DML  SQL statements can be divided into two categories:  Data definition language (DDL) statements  Used for creating and modifying tables, views, and other structures  CREATE, DROP, ALTER  Data manipulation language (DML) statements.  Used for queries and data modification  INSERT, DELETE, UPDATE, SELECT

6 6 The SQL SELECT Statement  Basic SQL Query: SELECT[DISTINCT] column_name(s) | * FROMtable_name(s) [WHEREconditions] [ORDER BY column_name1 [ASC|DSC], …]

7 7 Selecting All Columns: The Asterisk (*) Keyword SELECT* FROM Students; Student Number Student LastName Student FirstName EmailPhoneNumberMajor 190SmithJohnjsmith@usna.edu410-431-3456SCY 673DoeJanejdoe@usna.eduSCY 312DoeJanejdoe2@usna.edu443-451-7865Math

8 8 Specific Columns and Rows from One Table SELECTStudentNumber, StudentLastName, StudentFirstName FROMStudents WHEREMajor = ‘SCY’; Student Number Student LastName Student FirstName 190SmithJohn 673DoeJane

9 9 The DISTINCT Keyword SELECT StudentLastName FROM Students; StudentLastName Smith Doe SELECT DISTINCT StudentLastName FROM Students; StudentLastName Smith Doe

10 10 Class Exercise  Department(DeptName, ChairName, WebAddress, DivName)  Find the name of the Chair of the ‘Math’ Department

11 11 WHERE Clause Options  AND, OR  IN, NOT IN, BETWEEN  LIKE Wild cards:  SQL-92 Standard (SQL Server, Oracle, etc.):  _ = Exactly one character  % = Any set of characters (zero or more)  MS Access  ? = Exactly one character  * = Any set of characters (zero or more)  Example: Students(SNb, SName, Email, Major) Find alpha and name of SCY or SCS students with SNb starting with ‘16’ SELECT SNb, SName FROM Students WHERE SNb LIKE ’16%’ AND Major IN (‘SCY’, ‘SCS’)

12 12 Sorting the Results SELECT[DISTINCT] column_name(s) | * FROMtable_name(s) [WHEREconditions] [ORDER BY column_name(s) [ASC/DESC]] Example: Students(SNb, SName, Email, Major) SELECT SNb, SName FROM Students ORDER BY SName ASC, SNb DESC

13 13 Summary (partial)  SELECT [DISTINCT] column_name(s) FROM table_name WHERE conditions ORDER BY column_name(s) [ASC/DESC]

14 14 SELECT from Two or More Tables SNbSNameEmail 190Smithjsmith@usna.edu 673Doejdoe@usna.edu 312Doejdoe2@usna.edu CidCNameCDept SY306WebDbScyComSci SY301Data StructuresComSci SM121Calculus1Math SNbCidSemester 190SY301Fall2015 312SY306Spring2015 SELECT SName FROM Students S, Enrolled E WHERE S.Snb = E.SNb AND E.Cid = ‘SY306’ Find the names of students enrolled in SY306 Students Courses Enrolled

15 15 SELECT - Conceptual Evaluation Strategy  Semantics of an SQL query defined in terms of the following conceptual evaluation strategy:  Compute the cross-product of table_names  Discard resulting rows if they fail condition  Delete columns that are not in column_names  If DISTINCT is specified, eliminate duplicate rows  This strategy is probably the least efficient way to compute a query!  An optimizer will find more efficient strategies to compute the same answers.

16 16 Example Conceptual Evaluation S.SNbSNameEmailE.SNbCidSemester 190Smithjsmith@usna.edu190SY301Fall2015 190Smithjsmith@usna.edu312SY306Spring2015 673Doejdoe@usna.edu190SY301Fall2015 673Doejdoe@usna.edu312SY306Spring2015 312Doejdoe2@usna.edu190SY301Fall2015 312Doejdoe2@usna.edu312SY306Spring2015 SELECT SName FROM Students S, Enrolled E WHERE S.Snb = E.SNb AND E.Cid = ‘SY306’

17 17 Example Conceptual Evaluation S.SNbSNameEmailE.SNbCidSemester 190Smithjsmith@usna.edu190SY301Fall2015 190Smithjsmith@usna.edu312SY306Spring2015 673Doejdoe@usna.edu190SY301Fall2015 673Doejdoe@usna.edu312SY306Spring2015 312Doejdoe2@usna.edu190SY301Fall2015 312Doejdoe2@usna.edu312SY306Spring2015 SELECT SName FROM Students S, Enrolled E WHERE S.Snb = E.SNb AND E.Cid = ‘SY306’

18 18 Example Conceptual Evaluation S.SNbSNameEmailE.SNbCidSemester 190Smithjsmith@usna.edu190SY301Fall2015 190Smithjsmith@usna.edu312SY306Spring2015 673Doejdoe@usna.edu190SY301Fall2015 673Doejdoe@usna.edu312SY306Spring2015 312Doejdoe2@usna.edu190SY301Fall2015 312Doejdoe2@usna.edu312SY306Spring2015 SELECT SName FROM Students S, Enrolled E WHERE S.Snb = E.SNb AND E.Cid = ‘SY306’ SName Doe

19 19 Modified Query SELECT S.SNb FROM Students S, Enrolled E WHERE S.SNb = E.SNb AND E.Cid =‘SY306’  Would the result be different with DISTINCT?

20 20 Class Exercise  Students(SNb, SName, Email)  Courses(Cid,CName, Dept)  Enrolled(SNb,Cid, Semester)  Find the student number and name for each student enrolled in ‘Spring2015’ semester  Find the names of all students enrolled in ‘ComSci’ courses

21 21 Summary (partial)  SELECT [DISTINCT] column_name(s) FROM table_name(s) WHERE conditions ORDER BY column_name(s) [ASC/DESC]


Download ppt "1 SY306 Web and Databases for Cyber Operations SQL: Structured Query Language."

Similar presentations


Ads by Google