Title: DML: Data Manipulation Language
 1DML Data Manipulation Language
  2DML Data Manipulation Language
- SELECT Shows real  virtual data 
 - INSERT 
 - UPDATE Modifies physical data 
 - DELETE 
 - Select is also known as DQL (Q for query) 
 - Insert, Update and Delete are also known as the 
Action commands 
  3DML Data Manipulation Language
- SELECT Shows real  virtual data 
 - INSERT 
 - UPDATE Modifies physical data 
 - DELETE 
 - Relative Speed? Select -vs- Action 
 - How can we make them faster? 
 - Frequency of use?
 
  4Select Syntax (elementary version)
- SELECT 
 - FROM 
 - WHERE 
 - ORDER BY 
 - Note SELECT and FROM are mandatory clauses 
 -  WHERE and ORDER BY are optional 
 -  Relative sequence of clauses is fixed
 
- fields 
 - tables 
 - row conditions 
 - fields
 
  5Show everything in the table
- SELECT 
 - FROM 
 - Note The database engine will display the 
records  -  in the fastest possible row and 
column order  
  6Alphabetized list of programmers
- SELECT 
 - FROM 
 - WHERE 
 - ORDER BY 
 - Note 1 No explicit reference to secondary index 
 - Note 2 programmer  Programmer  proGRAMmer
 
-  
 - tblEmployee 
 - JobTitle  'programmer' 
 - LastName, FirstName
 
  7Employees named Jones or Smith
- SELECT 
 - FROM 
 - WHERE 
 - ORDER BY
 
- FirstName, LastName 
 - tblEmployee 
 - LastName  'Jones' 
 -  OR LastName  'Smith' 
 - LastName, FirstName
 
Improper Syntax WHERE LastName  'Jones' OR 
'Smith' 
 8Programmers named Jones or Smith
- SELECT 
 - FROM 
 - WHERE 
 - ORDER BY
 
- FirstName, LastName 
 - tblEmployee 
 - ( LastName  'Jones' 
 -  OR LastName  'Smith' ) 
 -  AND JobTitle  'programmer' 
 - LastName, FirstName
 
  9Annual salaries of programmers
- SELECT 
 - FROM 
 - WHERE 
 - ORDER BY 
 
- FirstName, LastName, AnnualSalary 
 - tblEmployee 
 - JobTitle  'programmer' 
 - LastName, FirstName
 
  10Monthly salaries of programmers
- SELECT 
 - FROM 
 - WHERE 
 - ORDER BY 
 
- FirstName, LastName, AnnualSalary/12 
 - tblEmployee 
 - JobTitle  'programmer' 
 - LastName, FirstName
 
  11Programmers earning over 8K/month
- SELECT 
 - FROM 
 - WHERE 
 - ORDER BY 
 
- FirstName, LastName 
 - tblEmployee 
 - JobTitle  'programmer' 
 -  AND AnnualSalary/12 gt 8000 
 - LastName, FirstName
 
  12Insert Syntax (external source)
- table ( fields ) 
 - ( values )
 
  13New employee
- INSERT INTO tblEmployee ( SSN, FirstName, 
 -  LastName, JobTitle, AnnualSalary, DOB ) 
 - VALUES ( '123-45-6789', 'John', 'Adams', 
 -  'Assistant Manager', 68500, '7/4/1976' )
 
Equivalent INSERT INTO tblEmployee ( DOB, 
AnnualSalary, JobTitle, LastName, 
FirstName, SSN ) VALUES ('7/4/1976', 68500, 
'Assistant Manager', 'Adams', 'John', 
'123-45-6789') 
 14Insert Syntax (internal source)
- INSERT INTO 
 - SELECT STATEMENT
 
  15Management Only
- INSERT INTO tblManagement ( SSN, FN, LN ) 
 - SELECT SSN, FirstName, LastName 
 - FROM tblEmployee 
 - WHERE JobTitle  'Manager' 
 -  OR JobTitle  'Assistant Manager' 
 - Note FN and FirstName, LN and LastName
 
  16Update Syntax
- table 
 - replacement statements 
 - row condition
 
  17Promote the new guy
- tblEmployee 
 - JobTitle  'Manager' , 
 - AnnualSalary  92000 
 - SSN  '123-45-6789'
 
  18Give everyone a 15 raise
- tblEmployee 
 - AnnualSalary  1.15  AnnualSalary
 
  19Give everyone except management a raise
- UPDATE 
 - SET 
 - WHERE 
 -  AND 
 - Alternatives ltgt ! NOT( ) 
 
- tblEmployee 
 - AnnualSalary  1.15  AnnualSalary 
 - JobTitle ltgt 'Manager' 
 - JobTitle ltgt 'Assistant Manager'
 
  20Give everyone except management a raise
- UPDATE 
 - SET 
 - WHERE 
 -  AND 
 - Alternatives ltgt ! NOT( ) 
 
- tblEmployee 
 - AnnualSalary  1.15  AnnualSalary 
 - JobTitle ! 'Manager' 
 - JobTitle ! 'Assistant Manager'
 
  21Give everyone except management a raise
- UPDATE 
 - SET 
 - WHERE NOT 
 -  OR 
 - Alternatives ltgt ! NOT( ) 
 
- tblEmployee 
 - AnnualSalary  1.15  AnnualSalary 
 -  ( JobTitle  'Manager' 
 - JobTitle  'Assistant Manager' )
 
  22Cut management salaries
- tblEmployee 
 - AnnualSalary  0.95  AnnualSalary 
 - JobTitle  'Manager' 
 - JobTitle  'Assistant Manager'
 
  23Delete Syntax
- DELETE 
 - WHERE 
 - Note 1 Delete and Drop are not the same 
 - Note 2 Don't forget the orphans 
 - Note 3 Dont forget cascading
 
  24Fire the new guy
- DELETE tblEmployee 
 - WHERE SSN  '123-45-6789'
 
  25Fire management
- DELETE tblEmployee 
 - WHERE JobTitle  'Manager' 
 -  OR JobTitle  'Assistant Manager'
 
  26Fire everyone
  27SQL Views (Virtual Tables)
  28SQL Structured Query Language
- DDL 
 - CREATE 
 - ALTER 
 - DROP 
 - DCL 
 - GRANT 
 - REVOKE 
 - DENY
 
- DML 
 - SELECT 
 - INSERT 
 - UPDATE 
 - DELETE
 
VIEW 
 29Tables Real and Virtual
- Table 
 - real data, no computations 
 - primary keys for uniqueness 
 - foreign keys for relationships 
 - secondary indices for speed 
 - Virtual Table 
 - data on demand, computations on demand 
 - select query 
 - view - stored select query
 
  30View Syntax
- CREATE VIEW ViewName AS 
 - SELECT STATEMENT 
 - DROP VIEW ViewName
 
  31Sample View
- CREATE VIEW qryManagement AS 
 - SELECT FirstName, LastName, AnnualSalary 
 - FROM tblEmployee 
 - WHERE JobTitle 'Manager' 
 -  OR JobTitle 'Assistant Manager' 
 
  32Using Views
- SELECT FirstName, LastName 
 - FROM qryManagement 
 - WHERE AnnualSalary gt 100000 
 - ORDER BY LastName, FirstName