1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.

Slides:



Advertisements
Similar presentations
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Advertisements

SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
The Relational Calculus
COMPANY schema EMPLOYEE
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
Introduction to Standard Query Language Erik Zeitler UDBL
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
FEN More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.
Database Design -- Basic SQL
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
SQL: Schema Definition, Constraints, & Queries and Views.
Relational Algebra - Chapter (7th ed )
Onsdag The concepts in a relation data model SQL DDL DML.
CS 380 Introduction to Database Systems (Chapter 8: SQL-99: Schema Definition, Constraints, Queries, and Views)
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Structured Query Language
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –continue with SQL (chapter.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Introduction to Database Systems
FEN Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.
COMP163 Database Management Systems September 18, 2008 Lecture 8 – Sections SQL Queries.
1 Database Systems Basic SQL. 2Outline  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries in SQL  INSERT,
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.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 A First Course in Database Systems.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
The SQL Database Grammar
Chapter 4 Basic SQL.
6/22/2018.
CS580 Advanced Database Topics
Database Systems Basic SQL
Retrieval Queries in SQL(DML)
376a. Database Design Dept. of Computer Science Vassar College
11/9/2018.
Session - 6 Sequence - 2 SQL: The Structured Query Language:
SQL-99: Schema Definition, Constraints, and Queries and Views
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Manipulating Data Lesson 3.
Presentation transcript:

1 COMP 1100 Basic SQL David J. Stucki

Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2

SQL Structured Query Language  High-level, declarative programming language User specifies what he wants, not how to do it  Comprehensive database language Statements for data definition, queries and updates Defining views, specifying authorization Setting up integrity constraints and transaction controls 3

SQL Terminology Table  Equivalent to relational model’s relation Row  Equivalent to tuple Column  Equivalent to attribute CREATE statement  Used for data definition 4

Schemas Identified by a schema name Can be thought of as a grouping of elements into one database idea  Tables  Constraints  Views  Domains  Authorizations (Roles) 5

Sample Schema 6

SQL Retrieval SQL retrieval operation: SELECT statement  SELECT FROM WHERE ;  - Projection attributes  - Selection condition 7

SQL Retrieval  Example SQL Query SELECTBIRTH_DATE, ADDRESS FROMEMPLOYEE WHEREFIRST_NAME=‘John’ AND LAST_NAME=‘Smith’;  Retrieves every row from the table where the selection condition holds true Anyone with the name “John Smith” will have their birth date and address returned 8

SQL Retrieval  Example select-project-join SQL Query SELECTFname, Lname FROMEMPLOYEE, DEPARTMENT WHEREDname=‘Research’ AND Dnumber=Dno;  Here will retrieve first name and last name of all employees who work in the ‘Research’ department Join condition – combines multiple tables in the WHERE Finds the row in the table DEPARTMENT where DNAME is ‘Reseach’ Matches the DNUMBER for that row to the DNO in the EMPLOYEE table  Only retrieves results where these values match 9

SQL Retrieval  Another example select-project-join SQL Query SELECTPname, Lname FROMEMPLOYEE, DEPARTMENT, PROJECT WHEREDnum=Dnumber AND Mgr_ssn=Ssn AND Plocation = ‘Houston’; Here we find:  Names of projects located in Houston  Last names of the managers of the departments that these projects are associated with Note that this query joins across 3 tables:  Need PROJECT table to find the location  Relate PROJECT to DEPARTMENT via DNUMBER to get Department Manager’s SSN  Use Dpt. Manager’s SSN to find last name in EMPLOYEE table 10

SQL Retrieval  Suppose we had chosen different names for our attributes What if we wanted to use “Dnumber” everywhere for the department number? This would no longer work: SELECTFname, Lname FROMEMPLOYEE, DEPARTMENT WHEREDname=‘Research’ AND Dnumber=Dnumber;  In this case, what do we mean by Dnumber=Dnumber? Ambiguous - we have two tables with the same attribute in them 11

SQL Retrieval  We can specify exactly what we mean SELECTFname, Lname FROMEMPLOYEE, DEPARTMENT WHEREDname=‘Research’ AND EMPLOYEE.Dnumber =DEPARTMENT.Dnumber;  Can also use aliases to make our lives simpler: SELECTFname, Lname FROMEMPLOYEE AS E, DEPARTMENT AS D WHEREDname=‘Research’ AND E.Dnumber=D.Dnumber; 12

SQL Retrieval  Aliases can also help us with circular references: SELECTE.Fname, E.Lname S.Fname, S.Lname FROMEMPLOYEE AS E, EMPLOYEE AS S WHEREE.Super_ssn = S.Ssn; This gets us back a list of employees and their supervisors  Without aliasing, this query would not work  Best practice is to always use aliases, even when you don’t need them  Usually improves understandability of code:  SELECTFname, Lname FROMEMPLOYEE AS EMP, DEPARTMENT AS DEPT WHEREDEPT.Dname=‘Research’ AND DEPT.Dnumber=EMP.Dno; 13

SQL Retrieval Missing “WHERE” clauses  Where clauses are not required  When missing, ALL results are returned: SELECTFname, Lname FROMEMPLOYEE; This would return all employee names in the table 14

SQL Retrieval  Need to be careful with results  When multiple tables involved, you get back the cross product of your tuples: SELECTFname, Lname, Dname FROMEMPLOYEE, DEPARTMENT This actually returns ALL employees crossed with ALL Department names  So if you have two employees and two deparments, ‘John Smith’ in Research and ‘Bob Jones’ in ‘Accounting’, you would get: John Smith Accounting John Smith Research Bob Jones Accounting Bob Jones Research 15

SQL Retrieval Wildcards in the select clause  Retrieve all of the attributes SELECT* FROMEMPLOYEE; This would return all employee data from the EMPLOYEE table 16

SQL Retrieval Duplicate elimination  In a pure relational model, no relation would have duplicate tuples  But SQL does allow duplicate tuples in a relation So long as there is no conflict in their primary keys …but if there’s no primary key defined in a table, it can have duplicate entries 17

SQL Retrieval  Even on tables that have a primary key, sometimes queries will return duplicates If we’re only getting a limited number of attributes, there could be overlap in those attributes If we want to remove duplicates, we use the DISTINCT keyword SELECTDISTINCT Salary FROMEMPLOYEE; This would return only the unique SALARY entries from the EMPLOYEE table, removing duplicate entries 18

SQL Retrieval Wildcards in the WHERE clause  Sometimes we don’t want to match a whole attribute completely A partial match is enough We can use a wildcard in the WHERE clause to accomplish this SELECTFname, Lname FROMEMPLOYEE WHEREAddress LIKE ‘%Houston%’; If we really need a ‘%’ literal character, we need to escape it: SELECTProduct_name FROMPRODUCTS WHEREDiscount LIKE ‘%5\%’; 19

SQL Retrieval Arithmetic in queries SELECTLname, 1.1*Salary AS Increase FROMEMPLOYEE; This would give us back all of the employee last names with their salary increased by 10%  Note that it doesn’t change what’s in the database – just what is reported by the query Ranges SELECT* FROMEMPLOYEE WHERESalary >= AND Salary <= ; 20

SQL Retrieval Ordering  We can force the query to come back in a particular order Very useful for reports and for debugging SELECT* FROMEMPLOYEE ORDER BYLname, Fname, Super_ssn; 21