Introduction to SQL Structured Query Language Martin Egerhill.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
A Guide to Oracle9i1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Guide to Oracle10G1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Introduction to SQL J.-S. Chou Assistant Professor.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Introduction to Databases Chapter 7: Data Access and Manipulation.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Using Special Operators (LIKE and IN)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Session 1 Module 1: Introduction to Data Integrity
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
There’s a particular style to it… Rob Hatton
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Query Getting to the data ……..
More SQL: Complex Queries,
MySQL Subquery Source: Dev.MySql.com
Relational Database Design
Database Systems: Design, Implementation, and Management Tenth Edition
SQL FUNDAMENTALS CDSE Days 2018.
David M. Kroenke and David J
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Access: SQL Participation Project
Chapter 7 Introduction to Structured Query Language (SQL)
Structured Query Language – The Fundamentals
Database solutions Selected SQL commands – part 1 Marzena Nowakowska Faculty of Management and Computer Modelling Kielce University of Technology rooms:
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Manipulating Data Lesson 3.
Presentation transcript:

Introduction to SQL Structured Query Language Martin Egerhill

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / Disposition Databases and their components What is SQL? The basics JOIN-predicate logic Advanced functionality Subqueries Regular expressions Inserting and updating data GCP – Good Coding Practice

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / Databases and their components Databases are containers for data, relational databases have connections between data points. Database Management Systems (DBMS) handle interactions with the database. Tables are structured arrangements of specific data Schemas define the structures and parameters of the tables. Columns are single fields in a table containing specific information (columns have defined data types). Data types define the content format. Rows represent the records in the table and contain the information for the record in the respective column. Keys define the relational logic.

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / What is SQL? Database communication language with very few commands. Pros: Intuitively constructed syntax (case- and break-insensitive). Widely supported. Supports both data retrieval and manipulation Fast Cons: Several dialects Lack of advanced data manipulation Can be slow if not optimized

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / The basics SELECT-statement –Defines your output data. Rename columns with “AS”. FROM-statement –Defines your data sources and relations. WHERE-clause –Defines dependencies and restrictions on selection. ORDER BY-statement –Defines the sort order (columns delimited by “,”). Can be either ascending (asc) or descendng (desc). Different columns can have different directions! GROUP BY-statement –Used to group calculated columns, for example counts, by data level. Can have multiple levels (delimited by “,”). HAVING-clause –Same functionality as the WHERE-clause but for groups.

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / JOIN-predicate logic There are two main types of joins; inner and outer. There are two ways of writing inner joins: Explicit: Full specification of predicate using JOIN and ON keywords. Implicit: Simply states columns to be joined in the WHERE-clause.

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / JOIN-predicate logic (continued) Different types of inner joins are: Equi-join: Uses equality between columns as comparator. For columns with the same name the shorthand USING (column name) can be used (not supported by SQL server and Sybase). Natural join: Not recommended as it implicitly joins columns with the same name from both tables. Cross join: Returns the Cartesian product of the two tables joined (rows which combine each row from the first table with each row from the second table).

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / JOIN-predicate logic (continued) Different types of outer joins are: Left (outer) join: Preserves all the records in the “left” table while finding matches in the “right” table. Right (outer) join: Preserves all the records in the “right” table while finding matches in the “left” table. Full (outer) join: Retrieves all records regardless of match (combines the effects of both the left and right outer joins). Note: No implicit join-notation for outer joins exists in standard SQL. FROM MASTER A LEFT JOIN LM_SEQ_LENGTH B ON A.SEQ_LENGTH_ID = B.SEQ_LENGTH_ID

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / Advanced functionality Commonly used functions in the SELECT-statement: COUNT(column): counts the number of rows (not NULL values). Should be combined with GROUP BY for stratification. DISTINCT: Selects only unique values. Used on the first selected column if not nested; COUNT(DISTINCT column). SUM(column): Returns the total of values in a column. MIN(column), MAX(column): returns the min/max value. CASE: Converts the returned data using logic: NVL: Converts NULL values, other values are left untouched: DECODE: Specifies conversion of (multiple) string(s): CASE WHEN x=w1 THEN r1 WHEN x=w2 THEN r2 ELSE r3 END NVL(column, replacement_value) DECODE(column [, string, match], default)

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / Advanced functionality (continued) Commonly used functions in the WHERE-clause: LIKE: Matches string in desired column. The % is used as wildcard: EXISTS: Boolean operator used together with subqueries. Is negated with NOT: IN: Used to specify several equality conditions (limited to 1000): AND, OR: Standard logical operators to combine conditions. BETWEEN: Selects a range of data between two values. This argument can behave differently depending on database! WHERE country_name LIKE ‘%JAPAN%’ WHERE (NOT) EXISTS (SELECT * FROM master) WHERE country_id IN (63856, 37465, 98364) WHERE country_id BETWEEN AND 98364

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / Subqueries Subqueries are used to compile a subset of data instead of actually creating a new table. The “virtual” subset-table can then be referenced. This can be especially useful when many operands and longer conditions are needed or when calculated conditions are applied. It is not recommended to use more than 2 nested layers. SELECT CASE WHEN A.LINEAGE_NAME IN (SELECT LINEAGE_NAME FROM (SELECT DISTINCT LINEAGE_NAME FROM HOSTS_AND_SITES WHERE COUNTRY_REGION_ID = 133)) THEN 1 END AS HAWAI FROM master A...

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / Regular expressions Can be used with SQLite3 Requires third party installation Requires session initialization Syntax is slightly different than Perl

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / Inserting and updating data Commonly used functions in the WHERE-clause: INSERT: Inserts specified data into specified table and columns: UPDATE: Will update existing values with possible condition: DELETE: Used to remove specified entries from specified table: There are other modifiers like CREATE, ALTER and DROP but that will not be covered in this presentation. INSERT INTO enterprise (col1, col2, col3, col4) VALUES (‘James T.’, ‘Kirk’, ‘Captain’, 1) UPDATE enterprise SET col1 = ‘Jean-Luc’, col2 = ‘Picard’ WHERE col4 = 1 DELETE FROM enterprise WHERE col3 = ‘Redshirt’

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / GCP – Good Coding Practice Use descriptive table and column names. Use descriptive table aliases. Avoid arbitrary column names or identical column names. Comment your code if necessary. Don’t mix joins if not needed (use explicit outer joins). Keep table relations in the FROM-clause. Use Primary Keys, Secondary Keys and Foreign Keys. Use list maintenance tables. Do not overuse subqueries. Split the code up if too complicated. Always try to avoid writing to the database when retrieving data.

Lund University / Faculty of Science / Department of Biology / MEMEG / SQL / For further reading visit