Introduction to the Structured Query Language (SQL) UFCE8K-15-M: Data Management.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

SQL Rohit Khokher.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL). Origins & history Early 1970’s – IBM develops Sequel as part of the System R project at its San Hose.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 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.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
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.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
1 Structured Query Language (SQL) CIS*2450 Advanced Programming Concepts.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
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.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Lecture 17 Introduction to Structured Query Language (SQL)
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
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.
Big Data Yuan Xue CS 292 Special topics on.
SQL, the Structured Query Language
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 5 Introduction to SQL.
Lecture 3 : Structured Query Language (SQL)
ATS Application Programming: Java Programming
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Database systems Lecture 3 – SQL + CRUD
Chapter 7 Introduction to Structured Query Language (SQL)
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

Introduction to the Structured Query Language (SQL) UFCE8K-15-M: Data Management

Origins & history Early 1970’s – IBM develops Sequel as part of the System R project at its San Hose Research Lab; ANSI & ISO publish the standard SQL-86; 1987 – IBM publishes its own “standard” SQL called Systems Architecture Database Interface (SAA-SQL); 1989 – SQL-89 published by ANSI (extended version of SQL-86); 1992 – SQL-92 published with better support for algebraic operations; 1999 – SQL-1999 published with support for typing, stored procedures, triggers, BLOBs etc. SQL-92 remains the most widely implemented standard – and most database vendors also provide their own (proprietary) extensions.

Components of SQL The SQL language has several parts: Data-definition language (DDL). The SQL DDL provides commands for defining relation schemas, deleting relations, and modifying relation schemas. Interactive data-manipulation language (DML). The SQL DML includes a query language based on both the relational algebra and the tuple relational calculus. It includes also commands to insert tuples into, delete tuples from, and modify tuples in the database. View definition. The SQL DDL includes commands for defining views. Transaction control. SQL includes commands for specifying the beginning and ending of transactions. Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how SQL statements can be embedded within general-purpose programming languages, such as C, C++, Java, PL/I, Cobol, Pascal, and Fortran. Integrity. The SQL DDL includes commands for specifying integrity constraints that the data stored in the database must satisfy. Updates that violate integrity constraints are disallowed. Authorization. The SQL DDL includes commands for specifying access rights to relations and views.

SQL Example (example db) The Supplier-Parts Database sidsnamestatuscity 1Smith20London 2Jones10Paris 3Blake30Paris 4Clark20London 5Adams30Athens s pid pnamecolour weightcity 1NutRed12.0London 2BoltGreen17.0Paris 3ScrewBlue17.0Oslo 4ScrewRed14.0London 5CamBlue12.0Paris 6CogRed19.0London sidpid qty p sp

SQL Example (project) Project the columns sname Smith Jones Blake Clark Adams SELECT sname FROM s computed columns: SELECT sname, status * 5 FROM s sname status * 5 Smith100 Jones50 Blake150 Clark100 Adams150 renamed columns: SELECT sname AS Supplier, status * 5 AS 'Status times Five' FROM s SupplierStatus times Five Smith100 Jones50 Blake150 Clark100 Adams150

SELECT statement (restrict) Restrict the rows SELECT * FROM s WHERE city=‘London’ sidsnamestatuscity s1Smith20London s4Clark20London complex condition: SELECT * FROM s WHERE city=‘London’ OR status = 30 sidsname status city s1Smith20London s3Blake30Paris s4Clark20London s5Adams30Athens

SELECT statement (restrict & project)  Restrict & Project city London SELECT city FROM s WHERE sname='smith' OR status='20' remove duplicate rows: SELECT DISTINCT city FROM s WHERE sname='smith' OR status='20' city London

SELECT statement (group by & having) Use the ‘GROUP BY’ clause to aggregate related rows cityTotal Status Athens30 London40 Paris40 SELECT city, SUM(status) AS 'Total Status' FROM s GROUP BY city Group By and Having Use the ‘HAVING’ clause to restrict rows aggregated with ‘GROUP BY’ cityTotal Status London40 Paris40 SELECT city, SUM(status) AS 'Total Status' FROM s GROUP BY city HAVING SUM(status) > 30

For many of the modern uses of databases, it is often necessary to select some subset of the records from a table, and let some other program manipulate the results. In SQL the SELECT statement is the workhorse for these operations. A summary of the SELECT statement: SELECT columns or computations FROM table WHERE condition GROUP BY columns HAVING condition ORDER BY column [ASC | DESC] LIMIT offset,count; SELECT statement summarized :

In SQL, the WHERE clause is used to operate on subsets of a table. The following comparison operators are available: Usual logical operators: = = <> BETWEEN used to test for a range IN used to test group membership Keyword NOT used for negation LIKE operator allows wildcards _ means single character, % means anything SELECT salary WHERE name LIKE ’Fred %’; SQL Comparison operators :

SQL supports a very large number of data types & formats for internal storage of data. Numeric INTEGER, SMALLINT, BIGINT NUMERIC(w,d), DECIMAL(w,d) - numbers with width w and d decimal places REAL, DOUBLE PRECISION - machine and database dependent FLOAT(p) - floating point number with p binary digits of precision SQL data types :

Character CHARACTER(L) - a fixed-length character of length L CHARACTER VARYING(L) or VARCHAR(L) - supports maximum length of L Binary BIT(L), BIT VARYING(L) - like corresponding characters BINARY LARGE OBJECT(L) or BLOB(L) Temporal DATE TIME TIMESTAMP SQL data types (cont.) :

SQL Functions : o SQL provides a wide range of predefined functions to perform data manipulation. o Four types of functions: arithmetic (sqrt(), log(), mod(), round() …) date (sysdate(), month(), dayname() …) character (length(), lower(), upper()…) aggregate (min(), max(), avg(), sum() …)

Database & Table description commands : Since a single server can support many databases, each containing many tables, with each table having a variety of columns, it’s often necessary to view which databases are available and what the table structures are within a particular database. The following SQL commands are often used for these purposes : SHOW DATABASES; SHOW TABLES IN database; SHOW COLUMNS IN table; DESCRIBE table; - shows the columns and their types

Inserting Records : Individual records can be entered using the INSERT command: INSERT INTO s VALUES(6, Thomas, 40, Cardiff); Using the column names: INSERT INTO s (sno, sname, status, city) VALUES(6, Thomas, 40, Cardiff); Insert multiple records: INSERT INTO s (sno, sname, status, city) VALUES(6, Thomas, 40, Cardiff), (7, Hamish, 30, Glasgow); Upload from file: LOAD DATA INFILE ’supplier.tab’ INTO TABLE s FIELDS TERMINATED BY ’\t’;

Updating (Editing) Existing Records : To change one or more values of columns of a table, the UPDATE command can be used. Edits are provided as a comma-separated list of column/value pairs. UPDATE s SET status=status + 10 WHERE city=’London’; Note that the UPDATE command without a WHERE clause will update all the rows of a table.

Deleting Records : To delete existing record/s the DELETE FROM command is used. Note the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If the WHERE clause is omitted, all records will be deleted! DELETE FROM s WHERE city=’London’;

Joins (1) o The m-f database idnameage 1tom23 2dick20 3harry30 idnameage 1mary23 2anne30 3sue34 mf Joins are used to re-combine records which have data spread across many tables. The following simple example database with two tables - m, f – is used to illustrate the various kinds of joins.

Joins (2) Product (or Cartesian Product) idname age idnameage 1tom231mary23 2dick201mary23 3harry301mary23 1tom232anne30 2dick202anne30 3harry302anne30 1tom233sue34 2dick203sue34 3harry303sue34 SELECT * FROM m, f Synonymous with the CROSS JOIN, hence: SELECT * FROM m CROSS JOIN f; would return the same result. This is not very useful but is the basis for all other joins.

Joins (3) Natural join Joins tables using some shared characteristic – usually (but not necessarily) a foreign key. SELECT * FROM m,f WHERE m.age = f.age idnameage idnameage 1tom231mary23 3harry302anne30

Joins (4) Inner joins The previous example, besides being a natural join, is also an example of an inner join. An inner join retrieves data only from those rows where the join condition is met. idnameageidnameage 3harry301mary23 SELECT * FROM m,f WHERE m.age > f.age

Joins (5) Outer joins Unmatched rows can be included in the output using as outer join. id name ageidname age 1tom231mary23 2dick20NULL 3harry302anne30 idname age idname age 1tom231mary23 3harry302anne30 NULL 3sue34 Right outer join: SELECT * FROM m RIGHT OUTER JOIN f ON m.age = f.age Left outer join: SELECT * FROM m LEFT OUTER JOIN f ON m.age = f.age

Joins (6) Self Join Special case of the inner join – here the table employee shows employees and their managers. Ruth manages Joe who manages Tom, Dick and Harry. emp_idemp_namemgr_id 1Tom4 2Dick4 3Harry4 4Joe5 5RuthNULL EmployeeManager TomJoe DickJoe HarryJoe Ruth Show who manages who by name: SELECT E1.emp_name AS Employee, E2.emp_name AS Manager FROM employee AS E1 INNER JOIN employee AS E2 ON E1.mgr_id = E2.emp_id

o A database daemon is a process that looks after a number of DATABASES each of which comprises a number of TABLES each of which comprises data arranges in ROWS and COLUMNS. o You don't access database information directly on the disc from any application program - you access it via your database daemon (a program called mysqld) which is usually running all the time and MUST be running whenever you want to access your data. o You SHOW to look at the structure but you SELECT to look at the content. o You UPDATE the content of table rows but you ALTER the structure of a table. o You choose rows from a result set with a WHERE clause but you choose rows after you've grouped them with a HAVING clause. o You add tables to each other "side by side" with a JOIN and you add results sets above/below each other with a UNION. MySQL & SQL – some key facts (1)

o You use a USE command (or other function call) to set the current DATABASE but there is no concept of current TABLE to carry through from one query to the next on a connection. o REPLACE gets rid of existing rows and puts new data in their place, but UPDATE changes existing rows in situe and leaves columns that you don't re-specify unaltered. DELETE gets rid of rows completely, and INSERT puts in completely new rows. o Every row in a table has the same structure (homogeneous) but different columns will have different structures (hetrogeneous). o If you JOIN tables, you tell MySQL how the tables are to be connected to each other (which row(s) in the first table match which row(s) in the second) using an ON clause. o A regular JOIN return only result rows where the ON clause matches up a row from each table exactly, and a LEFT JOIN return the same results and IN ADDITION any records from the left hand table specified, NULL padded to make a complete result record. o You use = to check whether something has an exact value, but you use IS NULL to check for the total absence of any defined value. MySQL & SQL – some key facts (2)