Download select

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Extensible Storage Engine wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Visual C# 2012 How to Program
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights 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 of
internal representation
 Relational Databases popular
 Use Structured Query Language (SQL) to perform queries (search)
and manipulate data
 Programming languages need an interface to interact with relational
databases
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.

Logical representation of data:
 Relationships can be considered without concern for physical
structure of data

Composed of tables
6
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.



Rule of Entity Integrity: every record must have a
unique value in its primary-key field
Compound Primary key: when a record has a unique
key based on a combination of two fields
Foreign key:
 Field for which every entry has a unique value in another table
and where the field in the other table is the primary key for
that table
 Rule of Referential Integrity: every foreign-key field value
must appear in another table’s primary-key field
 One to many relationship: A foreign key can appear many
times in its own table, but only once as primary key in another
table
8
New sets made from queries called result sets
Result set formed by selecting Department and Location data from the Employee table.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.
©1992-2014 by Pearson Education, Inc. All
Rights Reserved.

Used to request data (perform queries) and
manipulate data
18
SQL keyword
Description
SELECT
Selects (retrieves) fields from one or more tables.
FROM
Specifies tables from which to get fields or delete records.
Required in every SELECT and DELETE statement.
Specifies criteria that determine the rows to be retrieved.
Joins records from multiple tables to produce a single set of
records.
Specifies criteria for grouping records.
Specifies criteria for ordering records.
Inserts data into a specified table.
Updates data in a specified table.
Deletes data from a specified table.
WHERE
INNER JOIN
GROUP BY
ORDER BY
INSERT
UPDATE
DELETE
SQL query keywords.
19

Extracts information from one or more tables in a
database

Format:
 Basic: SELECT * FROM tableName
 * extracts all columns
 To get specific fields use a comma separated list instead of *
 Example: SELECT * FROM Authors
20
SELECT * FROM Authors
authorID
lastName
authorID
lastName
1
Deitel
8
McPhie
2
Deitel
9
Yaeger
3
Nieto
10
Zlatkina
4
Steinbuhler
12
Wiedermann
5
Santry
12
Liperi
6
Lin
13
Listfield
7
Sadhu
authorID and lastName from the Authors table.
21


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
22
Title
editionNumber
copyright
Internet and World Wide Web How to Program
2
2002
Java How to Program
4
2002
The Complete Java Training Course
4
2002
The Complete e-Business & e-Commerce Programming
Training Course
1
2001
The Complete Internet & World Wide Web
Programming Training Course
2
2001
The Complete Perl Training Course
1
2001
The Complete XML Programming Training Course
1
2001
C How to Program
3
2001
C++ How to Program
3
2001
The Complete C++ Training Course
3
2001
e-Business and e-Commerce How to Program
1
2001
Internet and World Wide Web How to Program
1
2000
The Complete Internet and World Wide Web
Programming Training Course
1
2000
Java How to Program (Java 2)
3
2000
The Complete Java 2 Training Course
3
2000
XML How to Program
1
2001
Perl How to Program
1
2001
Advanced Java 2 Platform How to Program
1
2002
e-Business and e-Commerce for Managers
1
2000
Wireless Internet and Mobile Business How to Program
1
2001
C# How To Program
1
2002
Python How to Program
1
2002
Visual Basic .NET How to Program
2
2002
Titles with copyrights after 1999 from table
Titles.
23
authorID
firstName
lastName
1
Harvey
Deitel
2
Paul
Deitel
SELECT * FROM Authors WHERE lastName LIKE ‘D*’
authorID
firstName
lastName
3
Tem
Nieto
6
Ted
Lin
11
Ben
Wiedermann
12
Jonathan
Liperi
13
Jeffrey
Listfield
SELECT * FROM Authors WHERE lastName LIKE ‘?i*’
24

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
25
authorID
firstName
lastName
2
Paul
Deitel
1
Harvey
Deitel
6
Ted
Lin
12
Jonathan
Liperi
13
Jeffrey
Listfield
8
David
McPhie
3
Tem
Nieto
7
Praveen
Sadhu
5
Sean
Santry
4
Kate
Steinbuhler
11
Ben
Wiedermann
9
Cheryl
Yaeger
10
Marina
Zlatkina
SELECT * FROM Authors ORDER BY lastName ASC
26
authorID
firstName
lastName
1
Harvey
Deitel
2
Paul
Deitel
6
Ted
Lin
12
Jonathan
Liperi
13
Jeffrey
Listfield
8
David
McPhie
3
Tem
Nieto
7
Praveen
Sadhu
5
Sean
Santry
4
Kate
Steinbuhler
11
Ben
Wiedermann
9
Cheryl
Yaeger
10
Marina
Zlatkina
SELECT * FROM Authors ORDER BY lastName, firstName ASC
27

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 operator
followed by the field name
 Example:
SELECT firstName, isbn FROM Authors INNER JOIN
AuthorISBN ON Authors.authorID= AuthorISBN.authorID
28
firstName
isbn
firstName
isbn
Harvey
0130895601
Harvey
0130829293
Harvey
0130284181
Harvey
0134569555
Harvey
0130284173
Harvey
0130829277
Harvey
0130852473
Paul
0130125075
Harvey
0138993947
Paul
0130856118
Harvey
0130856118
Paul
0130161438
Harvey
0130161438
Paul
013028419x
Harvey
013028419x
Paul
0139163050
Harvey
0139163050
Paul
0130895601
Harvey
0135289106
Paul
0135289106
Harvey
0130895717
Paul
0130895717
Harvey
0132261197
Paul
0132261197
Harvey
0130895725
Paul
0130895725
Harvey
0130125075
Tem
0130284181
Paul
0130284181
Tem
0130284173
Paul
0130284173
Tem
0130829293
Paul
0130829293
Tem
0134569555
Paul
0134569555
Tem
0130856118
Paul
0130829277
Tem
0130161438
Paul
0130852473
Tem
013028419x
Paul
0138993947
29

Tables produced by INNER JOIN can be used as
arguments for another INNER JOIN
30
1
2
3
4
5
6
7
8
9
10
11
Join Publishers and
Titles tables if the
publisherID
matches
SELECT Titles.title, Titles.isbn, Authors.firstName,
Authors.lastName, Titles.copyright,
Publishers.publisherName
FROM
( Publishers INNER JOIN Titles
ON Publishers.publisherID = Titles.publisherID )
INNER JOIN
( Authors INNER JOIN AuthorISBN
ON Authors.authorID = AuthorISBN.authorID )
ON Titles.isbn = AuthorISBN.isbn
ORDER BY Titles.title
Join Authors and
AuthorISBN if
authorID
matches
Join two created tables if
Sort new table by title
titlesISBN matches authorsISBN
TitleAuthor query of Books database.
31
Part 1
Title
isbn
firstName
lastName
copyright
publisherName
Advanced Java 2 Platform
How to Program
0130895601
Paul
Deitel
2002
Prentice Hall
Advanced Java 2 Platform
How to Program
0130895601
Harvey
Deitel
2002
Prentice Hall
Advanced Java 2 Platform
How to Program
0130895601
Sean
Santry
2002
Prentice Hall
C How to Program
0131180436
Harvey
Deitel
1992
Prentice Hall
C How to Program
0131180436
Paul
Deitel
1992
Prentice Hall
C How to Program
0132261197
Harvey
Deitel
1994
Prentice Hall
C How to Program
0132261197
Paul
Deitel
1994
Prentice Hall
C How to Program
0130895725
Harvey
Deitel
2001
Prentice Hall
C How to Program
0130895725
Paul
Deitel
2001
Prentice Hall
C# How To Program
0130622214
Tem
Nieto
2002
Prentice Hall
C# How To Program
0130622214
Paul
Deitel
2002
Prentice Hall
C# How To Program
0130622214
Jeffrey
Listfield
2002
Prentice Hall
C# How To Program
0130622214
Cheryl
Yaeger
2002
Prentice Hall
C# How To Program
0130622214
Marina
Zlatkina
2002
Prentice Hall
C# How To Program
0130622214
Harvey
Deitel
2002
Prentice Hall
C++ How to Program
0130895717
Paul
Deitel
2001
Prentice Hall
32


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’)
33
authorID
firstName
lastName
1
Harvey
Deitel
2
Paul
Deitel
3
Tem
Nieto
4
Kate
Steinbuhler
5
Sean
Santry
6
Ted
Lin
7
Praveen
Sadhu
8
David
McPhie
9
Cheryl
Yaeger
10
Marina
Zlatkina
11
Ben
Wiedermann
12
Jonathan
Liperi
13
Jeffrey
Listfield
14
Sue
Smith
Authors after an INSERT operation to add a record.
34


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’
35
authorID
firstName
lastName
1
Harvey
Deitel
2
Paul
Deitel
3
Tem
Nieto
4
Kate
Steinbuhler
5
Sean
Santry
6
Ted
Lin
7
Praveen
Sadhu
8
David
McPhie
9
Cheryl
Yaeger
10
Marina
Zlatkina
11
Ben
Wiedermann
12
Jonathan
Liperi
13
Jeffrey
Listfield
14
Sue
Jones
Table Authors after an UPDATE operation to change a record.
36



Removes data from a table
Form:
DELETE FROM tableName WHERE criteria
Example:
DELETE FROM Authors WHERE lastName = ‘Jones’
AND firstName = ‘Sue’
37
authorID
firstName
lastName
1
Harvey
Deitel
2
Paul
Deitel
3
Tem
Nieto
4
Kate
Steinbuhler
5
Sean
Santry
6
Ted
Lin
7
Praveen
Sadhu
8
David
McPhie
9
Cheryl
Yaeger
10
Marina
Zlatkina
11
Ben
Wiedermann
12
Jonathan
Liperi
13
Jeffrey
Listfield
Table Authors after a DELETE operation to remove a record.
38