Chapter 20Databases and LINQ
Visual C# 2012 How to Program
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
cshtp5_22_Database_Page_02
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_03
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_04
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
20.1 Relational Databases
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
Database:
Organized integrated collection of data
Database management system (DBMS)
Provides mechanisms for storing and organizing data
Allows storage and access to database, without knowledge ofinternal representation
Relational Databases popular
Use Structured Query Language (SQL) to perform queries (search)and manipulate data
Programming languages need an interface to interact with relationaldatabases
6
Relational Database Model
Logical representation of data:
Relationships can be considered without concern for physicalstructure of data
Composed of tables
cshtp5_22_Database_Page_05
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
8
Relational Database Overview: Books Database
Rule of Entity Integrity: every record must have aunique value in its primary-key field
Compound Primary key: when a record has a uniquekey based on a combination of two fields
Foreign key:
Field for which every entry has a unique value in another tableand where the field in the other table is the primary key  forthat table
Rule of Referential Integrity: every foreign-key field valuemust appear in another table’s primary-key field
One to many relationship: A foreign key can appear manytimes in its own table, but only once as primary key in anothertable
cshtp5_22_Database_Page_06
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
 New sets made from queries called result sets
Result set formed by selecting Department and Location data from the Employee table.
22.3 A Books Database
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_07
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_08
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_09
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_10
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_11
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_12
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
cshtp5_22_Database_Page_13
©1992-2014 by Pearson Education, Inc. AllRights Reserved.
18
Structured Query Language (SQL)
Used to request data (perform queries) andmanipulate data
Structured Query Language (SQL)
19
20
Basic Select Query
Extracts information from one or more tables in adatabase
Format:
Basic: SELECT * FROM tableName
* extracts all columns
To get specific fields use a comma separated list instead of *
Example: SELECT * FROM Authors
21
Basic Select Query
SELECT * FROM Authors
22
Where Clause
Used to specify certain criteria in a query
Basic form:
SELECT * FROM tableName WHERE criteria
Example:
SELECT * FROM Titles WHERE copyright > 1999
Can use LIKE clause
Used for pattern matching
Uses wildcards
*: zero or more characters take its place
?: exactly one character takes its place
23
WHERE Clause
24
WHERE Clause
25
ORDER BY Clause
Used to arrange results of a query
Can be ascending or descending order
Uses ASC and DESC respectively
Example:
  SELECT * FROM Authors ORDER BY lastName ASC
Can be used to sort by multiple fields
26
ORDER BY Clause
27
ORDER BY Clause
28
Merging Data from Multiple Tables: INNER JOIN
INNER JOIN
Merges records from multiple tables into a single record
Tests for matching values in a common field
General Form:
  SELECT * FROM table1 INNER JOIN table2 ON
         table1.fieldName=table2.fieldName
Fully-qualified names use the table name and dot operatorfollowed by the field name
Example:
  SELECT firstName, isbn FROM Authors INNER JOINAuthorISBN ON Authors.authorID= AuthorISBN.authorID
29
Merging Data from Multiple Tables: INNER JOIN
30
Joining Data from Tables Authors, AuthorISBN, Titles and Publishers
Tables produced by INNER JOIN can be used asarguments for another INNER JOIN
31
Joining Data from Tables Authors, AuthorISBN, Titles and Publishers
TitleAuthor query of Books database.
Join Publishers andTitles tables if thepublisherIDmatches
Join Authors andAuthorISBN ifauthorIDmatches
Join two created tables iftitlesISBN matches authorsISBN
Sort new table by title
32
Joining Data from Tables Authors, AuthorISBN, Titles and Publishers
Part 1
33
Insert Statement
Inserts a new record into a table
General Form:
   INSERT INTO tableName(fieldName1) VALUES (value1)
Values must match field names in order and type
Example:
  INSERT INTO Authors (firstName, lastName) VALUES(‘Sue’, ‘Smith’)
34
INSERT Statement
35
UPDATE Statement
Modifies data in a table
Form:
UPDATE tableName SET fieldName1 = value1 WHERE criteria
Example:
  UPDATE Authors SET lastName = ‘Jones’ WHERE lastName =‘Smith’ AND firstName = ‘Sue’
36
UPDATE Statement
37
DELETE Statement
Removes data from a table
Form:
  DELETE FROM tableName WHERE criteria
Example:
    DELETE FROM Authors WHERE lastName = ‘Jones’AND firstName = ‘Sue’
38
DELETE Statement