Download presentation
Presentation is loading. Please wait.
Published byAmbrose Taylor Modified over 8 years ago
1
1 COMP 1100 Basic SQL David J. Stucki
2
Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2
3
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
4
SQL Terminology Table Equivalent to relational model’s relation Row Equivalent to tuple Column Equivalent to attribute CREATE statement Used for data definition 4
5
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
6
Sample Schema 6
7
SQL Retrieval SQL retrieval operation: SELECT statement SELECT FROM WHERE ; - Projection attributes - Selection condition 7
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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 >= 30000 AND Salary <= 400000; 20
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.