C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database...

75
CSC 330 Object Oriented CSC 330 Object Oriented Programming Programming C# and Databases C# and Databases

Transcript of C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database...

Page 1: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

CSC 330 Object Oriented CSC 330 Object Oriented jjProgrammingProgramming

C# and DatabasesC# and Databases

Page 2: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

IntroductionIntroductionIntroductionIntroduction

Database:Integrated collection of dataDatabase management system (DBMS)Database management system (DBMS)

Provides mechanisms for storing and organizing data in a way that is consistent with database’s formatAllows storage and access to database without knowledge of internal representation

Relational Databases most popularRelational Databases most popularUse Structured Query Language to perform queries (search) and manipulate dataProgramming languages need an interface to interact with

2

Programming languages need an interface to interact with relational databases

Page 3: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

What are Databases?What are Databases?What are Databases?What are Databases?

Features of Database management system (DBMS)Definition of data structure Definition of data structure Maintenance facilityAllow access to and storage of data independently of the g p yinternal representation of data

Main job of the DBMS is to ensureensure datadata integrityintegrity

3

Page 4: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Systems and SQLRelational Database Systems and SQLRelational Database Systems and SQLRelational Database Systems and SQL

Relational database systems are the most popular today (Microsoft Access is a relational database system)SQL is international standard language used with relational database systems to perform queries (i.e. to request information that satisfies given criteria) and to manipulate information that satisfies given criteria) and to manipulate dataSome popular relational database management systems Some popular relational database management systems (RDBMS) are Microsoft SQL Server, Oracle, MySQL, Sybase

4

Page 5: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database ModelRelational Database ModelRelational Database ModelRelational Database Model

Logical representation of data:Relationships can be considered without concern for physical structure of dataphysical structure of data

Composed of tablesRows called recordsRows called recordsColumns called fieldsPrimary key: field that contains unique dataPrimary key: field that contains unique data

Each record can be identified by at least one distinct value

N t d f i ll d lt t

5

New sets made from queries called result sets

Page 6: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database ModelRelational Database ModelRelational Database ModelRelational Database ModelRelational Database ModelRelational Database ModelRelational Database ModelRelational Database Model

All information is contained in tables (or relationsrelations)All information is contained in tables (or relationsrelations)

TupleTuple or row (record)AttributeAttribute or column (field)Relationships [not why called relational]

6

Page 7: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relation InstanceRelation InstanceThe current values (relation instance) of a relation are specified by a tablep yAn element t of r is a tuple, represented by a row in a table

attributes

customer_name customer_street customer_city

attributes(or columns)

JonesSmithCurry

MainNorthNorthP k

HarrisonRyeRyePitt fi ld

tuples(or rows)

Lindsay Park Pittsfield

customer

7

Page 8: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database ModelRelational Database ModelRelational Database ModelRelational Database Model

Relationships are logical representation of data:Relationships are logical representation of data:Not concerned with physical structure

PrimaryPrimary keykey: field that contains unique dataPrimaryPrimary keykey: field that contains unique dataEach record can be identified by at least one distinct valuenumber name department salary location

23603 Jones 413 1100 New Jersey

24568 Kerwin 413 2000 New Jersey

34589 Larson 642 1800 Los Angelesrecord/row

35761 Myers 611 1400 Orlando

47132 Neumann 413 9000 New Jersey

78321 Stephens 611 8500 Orlando

8

field/columnprimary key

Page 9: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Primary KeysPrimary KeysPrimary KeysPrimary Keys

R l ti l DBMS f l t i t d tRelational DBMS enforces several constraints on dataRuleRule ofof EntityEntity IntegrityIntegrity: every record must have a unique value in its primary key fieldunique value in its primary-key fieldPrimary key does not have to be a single fieldCompoundCompound PrimaryPrimary KeyKey: when a record has a unique CompoundCompound PrimaryPrimary KeyKey: when a record has a unique key based on a combination of two fields

9

Page 10: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Foreign KeysForeign KeysForeign KeysForeign Keys

A foreignforeign keykey is used to create relationships:Field for which every entry has a unique value in another table and where the field in the other table is the primary table and where the field in the other table is the primary key for that table

RuleRule ofof ReferentialReferential IntegrityIntegrity: every foreign-key field RuleRule ofof ReferentialReferential IntegrityIntegrity: every foreign-key field value must appear in another table’s primary-key fieldOneOne toto manymany relationshiprelationship: A foreign key can appear OneOne toto manymany relationshiprelationship: A foreign key can appear many times in its own table, but only once as primary key in another table

10

y

Page 11: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: Books DatabaseBooks Database

Rule of Entity Integrity: every record must have a unique y g y y qvalue in its primary-key fieldCompound Primary key: when a record has a unique key based on a combination of two fieldsbased on a combination of two fieldsForeign 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 tableRule of Referential Integrity: every foreign-key field value must appear in another table’s primary-key fieldOne to many relationship: A foreign key can appear many times in its own table, but only once as primary

11

y , y p ykey in another table

Page 12: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: BooksBooks DatabaseDatabase

Fi ld D i tiField Desc rip tionauthorID Author’s ID number in the database. In the Books database, this int field is

defined as an auto-incremented field. For each new record inserted in this table, the database increments the authorID value, ensuring that each record has a unique authorID This field represents the table’s primary keyunique authorID. This field represents the table s primary key.

firstName Author’s first name (a string). lastName Author’s last name (a string). Fig. 19.3 Authors tab le from Books.

12

Page 13: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: BooksBooks DatabaseDatabaseBooksBooks DatabaseDatabase

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

13

Fig. 19.4 Data from the Authors tab le of Books.

Page 14: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: BooksBooks DatabaseDatabaseBooksBooks DatabaseDatabase

Field Description publisherID The publisher’s ID number in the database. This auto-incremented int

field is the table’s primary-key field.

publisherName The name of the publisher (a string). Fig 19 5 Publishers table from BooksFig. 19.5 Publishers table from Books.

publisherID publisherName 1 Prentice Hall

2 Prentice Hall PTG Fig. 19.6 Data from the Publishers tab le of Books.

Fie ld Desc rip tionFie ld Desc rip tionauthorID The author’s ID number, which allows the database to associate each

book with a specific author. The integer ID number in this field must also appear in the Authors table.

14

isbn The ISBN number for a book (a string). Fig. 19.7 AuthorISBN ta b le from Books.

Page 15: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: kk D t bD t bBooksBooks DatabaseDatabase

a u t h o r I D i s b n a u t h o r I D i s b n 1 0 1 3 0 8 9 5 7 2 5 2 0 1 3 9 1 6 3 0 5 0

1 0 1 3 2 2 6 1 1 9 7 2 0 1 3 0 2 8 4 1 9 x 1 0 1 3 0 8 9 5 7 1 7 2 0 1 3 0 1 6 1 4 3 8 1 0 1 3 5 2 8 9 1 0 6 2 0 1 3 0 8 5 6 1 1 8 1 0 1 3 9 1 6 3 0 5 0 2 0 1 3 0 1 2 5 0 7 5 1 0 1 3 0 2 8 4 1 9 x 2 0 1 3 8 9 9 3 9 4 71 0 1 3 0 1 6 1 4 3 8 2 0 1 3 0 8 5 2 4 7 3 1 0 1 3 0 8 5 6 1 1 8 2 0 1 3 0 8 2 9 2 7 7 1 0 1 3 0 1 2 5 0 7 5 2 0 1 3 4 5 6 9 5 5 5 1 0 1 3 8 9 9 3 9 4 7 2 0 1 3 0 8 2 9 2 9 31 0 1 3 0 8 5 2 4 7 3 2 0 1 3 0 2 8 4 1 7 3 1 0 1 3 0 8 2 9 2 7 7 2 0 1 3 0 2 8 4 1 8 1 1 0 1 3 4 5 6 9 5 5 5 2 0 1 3 0 8 9 5 6 0 1 1 0 1 3 0 8 2 9 2 9 3 3 0 1 3 0 2 8 4 1 9 x 1 0 1 3 0 2 8 4 1 7 3 3 0 1 3 0 1 6 1 4 3 81 0 1 3 0 2 8 4 1 7 3 3 0 1 3 0 1 6 1 4 3 81 0 1 3 0 2 8 4 1 8 1 3 0 1 3 0 8 5 6 1 1 8 1 0 1 3 0 8 9 5 6 0 1 3 0 1 3 4 5 6 9 5 5 5 2 0 1 3 0 8 9 5 7 2 5 3 0 1 3 0 8 2 9 2 9 3 2 0 1 3 2 2 6 1 1 9 7 3 0 1 3 0 2 8 4 1 7 3

15

2 0 1 3 0 8 9 5 7 1 7 3 0 1 3 0 2 8 4 1 8 1 2 0 1 3 5 2 8 9 1 0 6 4 0 1 3 0 8 9 5 6 0 1

Fig . 1 9 .8 D a t a f ro m A u t h o r I S B N t a b le in B o o k s .

Page 16: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: BooksBooks DatabaseDatabase

Fie ld Desc rip tion isbn ISBN number of the book (a string).

title Title of the book (a string). editionNumber Edition number of the book (a string). copyright Copyright year of the book (an int). publisherID Publisher’s ID number (an int). This value must correspond to an ID

number in the Publishers table.number in the Publishers table.imageFile Name of the file containing the book’s cover image (a string). price Suggested retail price of the book (a real number). [Note: The prices

shown in this database are for example purposes only.]

Fig. 19.9 Titles ta b le from Books.

16

Page 17: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: BooksBooks DatabaseDatabaseFig. 19.10 part 1 BooksBooks DatabaseDatabase

i s b n t i t l e e d i t i o n -N u m b e r

p u b l i s h - e r I D

c o p y -r i g h t

i m a g e F i l e p r i c e

0 1 3 0 9 2 3 6 1 3 P y t h o n H o w t o P r o g r a m

1 1 2 0 0 2 p y t h o n . j p g $ 6 9 . 9 5

0 1 3 0 6 2 2 2 1 4 C # H o w t o P r o g r a m

1 1 2 0 0 2 c s h t p . j p g $ 6 9 . 9 5

0 1 3 0 3 4 1 5 1 7 J a v a H o w t o P r o g r a m

4 1 2 0 0 2 j h t p 4 . j p g $ 6 9 . 9 5

0 1 3 0 6 4 9 3 4 1 T h e C o m p l e t e J a v a T r a i n i n g C o u r s e

4 2 2 0 0 2 j a v a c t c 4 . j p g $ 1 0 9 . 9 5

0 1 3 0 8 9 5 6 0 1 A d v a n c e d J a v a 2 P l a t f o r m H o t o

1 1 2 0 0 2 a d v j h t p 1 . j p g $ 6 9 . 9 5 P l a t f o r m H o w t o P r o g r a m

0 1 3 0 3 0 8 9 7 8 I n t e r n e t a n d W o r l d W i d e W e b H o w t o P r o g r a m

2 1 2 0 0 2 i w 3 h t p 2 . j p g $ 6 9 . 9 5

0 1 3 0 2 9 3 6 3 6 V i s u a l B a s i c . N E T H o w t o P r o g r a m

2 1 2 0 0 2 v b n e t . j p g $ 6 9 . 9 5

0 1 3 0 8 9 5 6 3 6 T h C l C 3 2 2 0 0 1 t 3 j $ 1 0 9 9 50 1 3 0 8 9 5 6 3 6 T h e C o m p l e t e C + + T r a i n i n g C o u r s e

3 2 2 0 0 1 c p p c t c 3 . j p g $ 1 0 9 . 9 5

0 1 3 0 8 9 5 5 1 2 T h e C o m p l e t e e -B u s i n e s s & e -C o m m e r c e P r o g r a m m i n g T r a i n i n g C o u r s e

1 2 2 0 0 1 e b e c c t c . j p g $ 1 0 9 . 9 5

0 1 3 0 8 9 5 6 1 X T h e C o m p l e t e I n t e r n e t & W o r l d W i d e W e b P r o g r a m m i n g T r a i n i n g C o u r s e

2 2 2 0 0 1 i w 3 c t c 2 . j p g $ 1 0 9 . 9 5

0 1 3 0 8 9 5 5 4 7 T h e C o m p l e t e P e r l T r a i n i n g C o u r s e

1 2 2 0 0 1 p e r l . j p g $ 1 0 9 . 9 5

0 1 3 0 8 9 5 5 6 3 T h e C o m p l e t e 1 2 2 0 0 1 x m l c t c . j p g $ 1 0 9 . 9 5

17

pX M L P r o g r a m m i n g T r a i n i n g C o u r s e

j p g

Page 18: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: BooksBooks DatabaseDatabaseFig. 19.10 part 2 BooksBooks DatabaseDatabase

0 1 3 0 8 9 5 7 2 5 C H o w t o P r o g r a m 3 1 2 0 0 1 c h t p 3 . j p g $ 6 9 . 9 5

0 1 3 0 8 9 5 7 1 7 C + + H o w t o P

3 1 2 0 0 1 c p p h t p 3 . j p g $ 6 9 . 9 5 P r o g r a m

0 1 3 0 2 8 4 1 9 X e - B u s i n e s s a n d e -C o m m e r c e H o w t o P r o g r a m

1 1 2 0 0 1 e b e c h t p 1 . j p g $ 6 9 . 9 5

0 1 3 0 6 2 2 2 6 5 W i r e l e s s I n t e r n e t a n d M o b i l e B u s i n e s s H o w t o

1 1 2 0 0 1 w i r e l e s s . j p g $ 6 9 . 9 5

P r o g r a m

0 1 3 0 2 8 4 1 8 1 P e r l H o w t o P r o g r a m

1 1 2 0 0 1 p e r l h t p 1 . j p g $ 6 9 . 9 5

0 1 3 0 2 8 4 1 7 3 X M L H o w t o P r o g r a m

1 1 2 0 0 1 x m l h t p 1 . j p g $ 6 9 . 9 5

0 1 3 0 8 5 6 1 1 8 T h e C o m p l e t e I d W l d

1 2 2 0 0 0 i w 3 c t c 1 . j p g $ 1 0 9 . 9 5 I n t e r n e t a n d W o r l d W i d e W e b P r o g r a m m i n g T r a i n i n g C o u r s e

0 1 3 0 1 2 5 0 7 5 J a v a H o w t o P r o g r a m ( J a v a 2 )

3 1 2 0 0 0 j h t p 3 . j p g $ 6 9 . 9 5

0 1 3 0 8 5 2 4 8 1 T h e C o m p l e t e J a v a 2 T r a i n i n g C o u r s e

3 2 2 0 0 0 j a v a c t c 3 . j p g $ 1 0 9 . 9 5 2 T r a i n i n g C o u r s e

0 1 3 0 3 2 3 6 4 0 e - B u s i n e s s a n d e -C o m m e r c e f o r M a n a g e r s

1 1 2 0 0 0 e b e c m . j p g $ 6 9 . 9 5

0 1 3 0 1 6 1 4 3 8 I n t e r n e t a n d W o r l d W i d e W e b H o w t o P r o g r a m

1 1 2 0 0 0 i w 3 h t p 1 . j p g $ 6 9 . 9 5

18

0 1 3 0 1 3 2 4 9 7 G e t t i n g S t a r t e d w i t h V i s u a l C + + 6 w i t h a n I n t r o d u c t i o n t o M F C

1 1 1 9 9 9 g s v c . j p g $ 4 9 . 9 5

Page 19: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database Overview: Relational Database Overview: BooksBooks DatabaseDatabaseFig. 19.10 part 3 BooksBooks DatabaseDatabase

0 1 3 0 8 2 9 2 9 3 T h e C o m p le t e V is u a l B a s ic 6 T ra in in g C o u rs e

1 2 1 9 9 9 v b c t c 1 . j p g $ 1 0 9 .9 5

0 1 3 4 5 6 9 5 5 5 V i l B i 6 1 1 1 9 9 9 b h t 1 j $ 6 9 9 50 1 3 4 5 6 9 5 5 5 V is u a l B a s ic 6 H o w to P r o g r a m

1 1 1 9 9 9 v b h t p 1 . j p g $ 6 9 .9 5

0 1 3 2 7 1 9 7 4 6 J a v a M u lt im e d ia C yb er C la s s r o o m

1 2 1 9 9 8 j a v a c t c . j p g $ 1 0 9 .9 5

0 1 3 6 3 2 5 8 9 0 J a v a H o w to P ro g ra m

1 1 1 9 9 8 j h t p 1 . j p g $ 6 9 .9 5 P ro g ra m

0 1 3 9 1 6 3 0 5 0 T h e C o m p le t e C + + T ra in in g C o u rs e

2 2 1 9 9 8 c p p c t c 2 . j p g $ 1 0 9 .9 5

0 1 3 5 2 8 9 1 0 6 C + + H o w t o P ro g ra m

2 1 1 9 9 8 c p p h t p 2 . j p g $ 4 9 .9 5

0 1 3 7 9 0 5 6 9 6 T h C l t J 2 2 1 9 9 8 j t 2 j $ 1 0 9 9 50 1 3 7 9 0 5 6 9 6 T h e C o m p le t e J a v a T ra in in g C o u rs e

2 2 1 9 9 8 j a v a c t c 2 . j p g $ 1 0 9 .9 5

0 1 3 0 8 2 9 2 7 7 T h e C o m p le t e J a v a T ra in in g C o u rs e (J a v a 1 .1 )

2 2 1 9 9 8 j a v a c t c 2 . j p g $ 9 9 .9 5

0 1 3 8 9 9 3 9 4 7 J a v a H o w to 2 1 1 9 9 8 j h t p 2 j p g $ 4 9 9 50 1 3 8 9 9 3 9 4 7 J a v a H o w to P ro g ra m (J a v a 1 .1 )

2 1 1 9 9 8 j h t p 2 . j p g $ 4 9 .9 5

0 1 3 1 1 7 3 3 4 0 C + + H o w t o P ro g ra m

1 1 1 9 9 4 c p p h t p 1 . j p g $ 6 9 .9 5

0 1 3 2 2 6 1 1 9 7 C H o w t o P r o g ra m 2 1 1 9 9 4 c h t p 2 . j p g $ 4 9 .9 5

0 1 3 1 1 8 0 4 3 6 C H t P 1 1 1 9 9 2 h t j g $ 6 9 9 5

19

0 1 3 1 1 8 0 4 3 6 C H o w t o P r o g ra m 1 1 1 9 9 2 c h t p . j p g $ 6 9 .9 5

Fig . 1 9 .1 0 D a t a f ro m t h e T i t l e s t a b le o f B o o k s .

Page 20: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

The Books DatabaseThe Books DatabaseThe Books DatabaseThe Books Database

Entity-relationship diagram for the Book database

AuthorISBNAuthorsTitles

isbn¥¥1 authorID

isbn

authorID

firstName

lastName

isbn

titleeditionNumber

copyright

¥11

Publishers

publisherID

publisherName

copyright

publisherIDimageFile

price

1

¥

20

Page 21: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Relational Database QueriesRelational Database QueriesRelational Database QueriesRelational Database QueriesRelational Database QueriesRelational Database QueriesRelational Database QueriesRelational Database Queries

Q iQ i l QueriesQueries : return a new result setBased upon relationalrelational algebraalgebra

Uses 3 operations to Uses 3 operations to build a new relation (a result-set)ProjectionProjection ProjectionProjection, selectionselection, and joinjoin

21

Page 22: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

What is SQL?What is SQL?What is SQL?What is SQL?

Structured Query Language (SQLSQL)Keywords and rules used to implement relational algebra operationsoperations

22

Page 23: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

SQL KeywordsSQL KeywordsSQL KeywordsSQL Keywords

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 p g qevery SELECT and DELETE statement.

WHERE Specifies criteria that determine the rows to be retrieved. INNER JOIN Joins records from multiple tables to produce a single set of records. GROUP BY Specifies criteria for grouping records. ORDER BY Specifies criteria for ordering records. INSERT Inserts data into a specified table.pUPDATE Updates data in a specified table. DELETE Deletes data from a specified table.

Fig 19 12 SQLquery keywords

23

Fig. 19.12 SQL query keywords.

Page 24: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Basic Select QueryBasic Select QueryBasic Select QueryBasic Select Query

Extracts information from one or more tables in a databaseFormat:

Basic: select * from tableselect * from table--namenameExample: select * from authorsselect * from authors* means "e tract all col mns"* means "extract all columns"

To get a projection (specific columns), use a list:l t fi t l t f thl t fi t l t f thselect firstname, lastname from authorsselect firstname, lastname from authors

Column, table names may be case sensitiveSQL keywords should not be case sensitive

24

SQL keywords should not be case sensitive

Page 25: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Basic Basic SelectSelect QueryQueryBasic Basic SelectSelect QueryQuery

authorID lastName authorID lastName 1 Deitel 8 M cPhie

2 Deitel 9 Yaegerg

3 Nieto 10 Zlatkina

4 Steinbuhler 12 W iederm ann

5 Santry 12 Liperi

6 Li 13 Li tfi ld6 Lin 13 Listfield7 Sadhu Fig . 19.13 authorID a n d lastName fro m th e Authors ta b le .

25

Page 26: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

WhereWhere ClauseClauseWhereWhere ClauseClause

Used to specify certain criteria in a queryBasic form:

SELECT * FROM tableName WHERE criteriaExample:

SELECT * FROM Titles WHERE copyright > 1999Can use LIKE clause

Used for pattern matchingUses wildcards

*: zero or more characters take its place

26

: zero or more characters take its place?: exactly one character takes its place

Page 27: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

PatternPattern matching with Wherematching with WherePatternPattern--matching with Wherematching with Where

Can use likelike clause for string pattern matchingUses wildcards

%: zero or more characters take its place (*)%: zero or more characters take its place ( )_: exactly one character takes its place (?)

Examples:Select * from authors where firstname like '_e%'Select * from authors where firstname like '_e%'Select * from authors where lastname like 'D%'Select * from authors where lastname like 'D%'

27

Page 28: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

WHEREWHERE ClauseClauseT i t l e e d i t i o n N u m b e r c o p y r i g h t

I n t e r n e t a n d W o r l d W i d e W e b H o w to P r o g r a m 2 2 0 0 2

J a v a H o w t o P r o g r a m 4 2 0 0 2

T h e C o m p l e t e J a v a T r a i n i n g C o u r s e 4 2 0 0 2p g

T h e C o m p l e t e e - B u s i n e s s & e - C o m m e r c e P r o g r a m m i n g T r a i n i n g C o u r s e

1 2 0 0 1

T h e C o m p l e t e I n t e r n e t & W o r l d W i d e W e b P r o g r a m m i n g T r a i n in g C o u r s e

2 2 0 0 1

T h e C o m p l e t e P e r l T r a i n in g C o u r s e 1 2 0 0 1 T h C l X M L P i T i i C 1 2 0 0 1T h e C o m p l e t e X M L P r o g r a m m i n g T r a i n i n g C o u r s e 1 2 0 0 1

C H o w t o P r o g r a m 3 2 0 0 1

C + + H o w to P r o g r a m 3 2 0 0 1

T h e C o m p l e t e C + + T r a i n i n g C o u r s e 3 2 0 0 1

e - B u s i n e s s a n d e - C o m m e r c e H o w t o P r o g r a m 1 2 0 0 1 I t t d W l d W i d W b H t P 1 2 0 0 0I n t e r n e t a n d W o r l d W i d e W e b H o w to P r o g r a m 1 2 0 0 0

T h e C o m p l e t e I n t e r n e t a n d W o r l d W i d e W e b P r o g r a m m i n g T r a i n in g C o u r s e

1 2 0 0 0

J a v a H o w t o P r o g r a m ( J a v a 2 ) 3 2 0 0 0

T h e C o m p l e t e J a v a 2 T r a in i n g C o u r s e 3 2 0 0 0

X M L H o w t o P r o g r a m 1 2 0 0 1X M L H o w t o P r o g r a m 1 2 0 0 1

P e r l H o w t o P r o g r a m 1 2 0 0 1

A d v a n c e d J a v a 2 P l a t f o r m H o w t o P r o g r a m 1 2 0 0 2 e - B u s i n e s s a n d e - C o m m e r c e f o r M a n a g e r s 1 2 0 0 0

W i r e l e s s I n t e r n e t a n d M o b i l e B u s i n e s s H o w t o P r o g r a m 1 2 0 0 1

C # H o w T o P r o g r a m 1 2 0 0 2

28

C # H o w T o P r o g r a m 1 2 0 0 2

P y t h o n H o w t o P r o g r a m 1 2 0 0 2 V i s u a l B a s i c . N E T H o w to P r o g r a m 2 2 0 0 2

Fig . 1 9 . 1 4 Tit le s w i t h c o p y r ig h t s a f t e r 1 9 9 9 f r o m t a b le T i t l e s .

Page 29: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

WHEREWHERE ClauseClauseWHEREWHERE ClauseClause

th ID fi tN l tNauthorID firstName lastName 1 Harvey Deitel

2 Paul Deitel Fi 19 15 A th f th t b l h l t t t ith

Fig. 19.15 Authors from the Authors table whose last names start with D.

authorID firstName lastName 3 Tem Nieto3 Tem Nieto

6 Ted Lin

11 Ben Wiedermann

12 Jonathan Liperi

13 Jeffrey Listfield Fig. 19.16 Authors from table Authors whose last names contain i as the

29

second letter.

Page 30: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Sorting the ResultsSorting the ResultsSorting the ResultsSorting the Results

SQL queries return their data in indeterminate orderSame query may return rows in different order each time

Use the order by clause to arrange results of a queryCan be ascending (asc) or descending (desc) order

Examples:select * from authors order by lastname descselect * from authors order by lastname descselect * from authors select * from authors select * from authors select * from authors

order by lastname, firstname ascorder by lastname, firstname asc

30

Page 31: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

ORDER BYORDER BY ClauseClauseORDER BYORDER BY ClauseClause

Used to arrange results of a queryCan be ascending or descending order

Uses ASC and DESC respectivelyUses ASC and DESC respectively

Example:SELECT authorID FROM Authors ORDER BYSELECT authorID FROM Authors ORDER BY authorID ASC

Can be used to sort by multipleCan be used to sort by multiple fields

31

Page 32: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

ORDER BYORDER BY ClauseClauseauthorID firstName lastName 2 Paul Deitel

1 Harvey Deitel

6 Ted Lin

12 h i i12 Jonathan Liperi

13 Jeffrey Listfield

8 David McPhie 3 Tem Nieto

7 Praveen Sadhu

5 Sean Santry

4 Kate Steinbuhler11 Ben Wiedermann

9 Cheryl Yaeger

32

10 Marina ZlatkinaFig. 19.17 Authors from table Authors in ascending order by lastName.

Page 33: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

ORDER BYORDER BY ClauseClauseauthorID firstName lastName 10 Marina Zlatkina

9 Cheryl Yaeger

11 Ben Wiedermann

4 Kate Steinbuhler4 Kate Steinbuhler

5 Sean Santry

7 Praveen Sadhu 3 Tem Nieto3 Tem Nieto

8 David McPhie

13 Jeffrey Listfield

12 Jonathan Liperi

6 Ted Lin 2 Paul Deitel

33

1 Harvey Deitel Fig. 19.18 Authors from table Authors in descending order by lastName.

Page 34: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

ORDER BYORDER BY ClauseClauseauthorID 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

34

Fig. 19.19 Authors from table Authors in ascending order by lastName and by firstName.

Page 35: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

ORDER BYORDER BY ClauseClausei s b n t i t l e e d i t i o n -

N u m b e r c o p y -r i g h t

p r i c e

0 1 3 0 8 9 5 6 0 1 A d v a n c e d J a v a 2 P l a t f o r m H o w t o P r o g r a m

1 2 0 0 2 $ 6 9 . 9 5

0 1 3 1 1 8 0 4 3 6 C H o w t o P r o g r a m 1 1 9 9 2 $ 6 9 . 9 5

0 1 3 0 8 9 5 7 2 5 C H o w t o P r o g r a m 3 2 0 0 1 $ 6 9 . 9 5

0 1 3 2 2 6 1 1 9 7 C H o w t o P r o g r a m 2 1 9 9 4 $ 4 9 . 9 5

0 1 3 0 6 2 2 2 1 4 C # H o w T o P r o g r a m 1 2 0 0 2 $ 6 9 . 9 5

0 1 3 5 2 8 9 1 0 6 C + + H o w t o P r o g r a m 2 1 9 9 8 $ 4 9 . 9 5 0 1 3 1 1 7 3 3 4 0 C + + H o w t o P r o g r a m 1 1 9 9 4 $ 6 9 . 9 5

0 1 3 0 8 9 5 7 1 7 C + + H o w t o P r o g r a m 3 2 0 0 1 $ 6 9 . 9 5

0 1 3 0 2 8 4 1 9 X e - B u s i n e s s a n d e - C o m m e r c e H o w t o P r o g r a m

1 2 0 0 1 $ 6 9 . 9 5

0 1 3 0 3 0 8 9 7 8 I n t e r n e t a n d W o r l d W i d e W e b H o w t o P r o g r a m

2 2 0 0 2 $ 6 9 . 9 5 g

0 1 3 0 1 6 1 4 3 8 I n t e r n e t a n d W o r l d W i d e W e b H o w t o P r o g r a m

1 2 0 0 0 $ 6 9 . 9 5

0 1 3 0 3 4 1 5 1 7 J a v a H o w t o P r o g r a m 4 2 0 0 2 $ 6 9 . 9 5

0 1 3 6 3 2 5 8 9 0 J a v a H o w t o P r o g r a m 1 1 9 9 8 $ 4 9 . 9 5 0 1 3 0 2 8 4 1 8 1 P e r l H o w t o P r o g r a m 1 2 0 0 1 $ 6 9 . 9 5

0 1 3 0 9 2 3 6 1 3 P y t h o n H o w t o P r o g r a m 1 2 0 0 2 $ 6 9 . 9 5

0 1 3 0 2 9 3 6 3 6 V i s u a l B a s i c . N E T H o w t o P r o g r a m

2 2 0 0 2 $ 6 9 . 9 5

0 1 3 4 5 6 9 5 5 5 V i s u a l B a s i c 6 H o w t o P r o g r a m

1 1 9 9 9 $ 6 9 . 9 5

0 1 3 0 6 2 2 2 6 5 W i r e l e s s I n t e r n e t a n d M o b i l e 1 2 0 0 1 $ 6 9 9 5

35

0 1 3 0 6 2 2 2 6 5 W i r e l e s s I n t e r n e t a n d M o b i l e B u s i n e s s H o w t o P r o g r a m

1 2 0 0 1 $ 6 9 . 9 5

0 1 3 0 2 8 4 1 7 3 X M L H o w t o P r o g r a m 1 2 0 0 1 $ 6 9 . 9 5

F ig . 1 9 . 2 0 B o o k s f r o m t a b le T i t l e s w h o se t i t le s e n d w it h H o w t o P r o g r a m i n a sc e n d in g o rd e r b y t i t l e .

Page 36: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Merging Data from Multiple Tables: Merging Data from Multiple Tables: g g pg g pINNER JOININNER JOIN

Merges records from multiple tables into a single recordTests for matching values in a common field

General Form:select * from table1 select * from table1 inner join table 2 on table1 field = table2 fieldinner join table 2 on table1 field = table2 fieldinner join table 2 on table1.field = table2.fieldinner join table 2 on table1.field = table2.field

Example:select firstname isbn from select firstname isbn from select firstname, isbn from select firstname, isbn from authors inner join authorISBNauthors inner join authorISBNon authors.authorID = authorsISBN.authorIDon authors.authorID = authorsISBN.authorID

36

Page 37: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Merging Data from Multiple Tables: Merging Data from Multiple Tables: INNER JOININNER JOININNER JOININNER JOIN

fir s t N a m e l a s t N a m e i s b n f i r s t N a m e l a s t N a m e i s b n H a r v e y D e i te l 0 1 3 0 8 9 5 6 0 1 H a r v e y D e i te l 0 1 3 0 8 2 9 2 9 3

H a r v e y D e i te l 0 1 3 0 2 8 4 1 8 1 H a r v e y D e i te l 0 1 3 4 5 6 9 5 5 5

H D it l 0 1 3 0 2 8 4 1 7 3 H D it l 0 1 3 0 8 2 9 2 7 7H a r v e y D e i te l 0 1 3 0 2 8 4 1 7 3 H a r v e y D e i te l 0 1 3 0 8 2 9 2 7 7

H a r v e y D e i te l 0 1 3 0 8 5 2 4 7 3 P a u l D e i te l 0 1 3 0 1 2 5 0 7 5

H a r v e y D e i te l 0 1 3 8 9 9 3 9 4 7 P a u l D e i te l 0 1 3 0 8 5 6 1 1 8

H a r v e y D e i te l 0 1 3 0 8 5 6 1 1 8 P a u l D e i te l 0 1 3 0 1 6 1 4 3 8 H a r v e y D e i te l 0 1 3 0 1 6 1 4 3 8 P a u l D e i te l 0 1 3 0 2 8 4 1 9 xH a r v e y D e i te l 0 1 3 0 1 6 1 4 3 8 P a u l D e i te l 0 1 3 0 2 8 4 1 9 x

H a r v e y D e i te l 0 1 3 0 2 8 4 1 9 x P a u l D e i te l 0 1 3 9 1 6 3 0 5 0

H a r v e y D e i te l 0 1 3 9 1 6 3 0 5 0 P a u l D e i te l 0 1 3 0 8 9 5 6 0 1

H a r v e y D e i te l 0 1 3 5 2 8 9 1 0 6 P a u l D e i te l 0 1 3 5 2 8 9 1 0 6 H a r v e y D e i te l 0 1 3 0 8 9 5 7 1 7 P a u l D e i te l 0 1 3 0 8 9 5 7 1 7

H a r v e y D e i te l 0 1 3 2 2 6 1 1 9 7 P a u l D e i te l 0 1 3 2 2 6 1 1 9 7

H a r v e y D e i te l 0 1 3 0 8 9 5 7 2 5 P a u l D e i te l 0 1 3 0 8 9 5 7 2 5

H a r v e y D e i te l 0 1 3 0 1 2 5 0 7 5 T e m N ie to 0 1 3 0 2 8 4 1 8 1

P a u l D e i te l 0 1 3 0 2 8 4 1 8 1 T e m N ie to 0 1 3 0 2 8 4 1 7 3 P a u l D e i te l 0 1 3 0 2 8 4 1 7 3 T e m N ie to 0 1 3 0 8 2 9 2 9 3P a u l D e i te l 0 1 3 0 2 8 4 1 7 3 T e m N ie to 0 1 3 0 8 2 9 2 9 3

P a u l D e i te l 0 1 3 0 8 2 9 2 9 3 T e m N ie to 0 1 3 4 5 6 9 5 5 5

P a u l D e i te l 0 1 3 4 5 6 9 5 5 5 T e m N ie to 0 1 3 0 8 5 6 1 1 8

P a u l D e i te l 0 1 3 0 8 2 9 2 7 7 T e m N ie to 0 1 3 0 1 6 1 4 3 8

P a u l D e i te l 0 1 3 0 8 5 2 4 7 3 T e m N ie to 0 1 3 0 2 8 4 1 9 x

37

P a u l D e i te l 0 1 3 8 9 9 3 9 4 7 Fig . 1 9 .2 1 A u t h o rs f ro m t a b le A u t h o r s a n d ISBN n u m b e rs o f t h e a u t h o rs’ b o o k s,

so rt e d in a sc e n d in g o rd e r b y l a s t N a m e a n d f i r s t N a m e .

Page 38: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Joining Data from Tables Authors, Joining Data from Tables Authors, ggAuthorISBN, Titles and PublishersAuthorISBN, Titles and Publishers

Tables produced by INNER JOIN can be used as arguments for another INNER JOINarguments for another INNER JOIN

38

Page 39: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Joining Data from Tables Joining Data from Tables AuthorsAuthors, , AuthorISBNAuthorISBN, , TitlesTitles and and PublishersPublishers

1 SELECT Titles.title, Titles.isbn, Authors.firstName, 2 Authors.lastName, Titles.copyright, 3 Publishers.publisherName4 FROM5 ( Publishers INNER JOIN Titles

Join Publishers and Titles tables if the publisherID matches

5 ( Publishers INNER JOIN Titles 6 ON Publishers.publisherID = Titles.publisherID ) 7 INNER JOIN8 ( Authors INNER JOIN AuthorISBN 9 ON Authors.authorID = AuthorISBN.authorID ) 10 ON Titles.isbn = AuthorISBN.isbn

Join Authors and AuthorISBN if

11 ORDER BY Titles.titleu o S N

authorID matches

Join two created tables if titlesISBN matches authorsISBN

Sort new table by title

39

Fig. 19.22 TitleAuthor query of Books database.

Page 40: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Joining Data from Tables Joining Data from Tables AuthorsAuthors, , AuthorISBNAuthorISBN, , TitlesTitles and and PublishersPublishers

Fig. 19.23 part 1

AuthorISBNAuthorISBN, , TitlesTitles and and PublishersPublishersTitle i s b n f i r s t -

N a m e l a s t -N a m e

c o p y -r i g h t

p u b l i s h e r -N a m e

A d v a n c e d J a v a 2 P la tfo rm H o w to P ro g ra m

0 1 3 0 8 9 5 6 0 1 P a u l D e ite l 2 0 0 2 P re n tic e H a ll H o w to P ro g ra m

A d v a n c e d J a v a 2 P la tfo rm H o w to P ro g ra m

0 1 3 0 8 9 5 6 0 1 H a rv e y D e ite l 2 0 0 2 P re n tic e H a ll

A d v a n c e d J a v a 2 P la tfo rm H o w to P ro g ra m

0 1 3 0 8 9 5 6 0 1 S e a n S a n tr y 2 0 0 2 P re n tic e H a ll g

C H o w to P ro g ra m 0 1 3 1 1 8 0 4 3 6 H a rv e y D e ite l 1 9 9 2 P re n tic e H a ll

C H o w to P ro g ra m 0 1 3 1 1 8 0 4 3 6 P a u l D e ite l 1 9 9 2 P re n tic e H a ll

C H o w to P ro g ra m 0 1 3 2 2 6 1 1 9 7 H a rv e y D e ite l 1 9 9 4 P re n tic e H a ll

C H o w to P ro g ra m 0 1 3 2 2 6 1 1 9 7 P a u l D e ite l 1 9 9 4 P re n tic e H a llC H o w to P ro g ra m 0 1 3 2 2 6 1 1 9 7 P a u l D e ite l 1 9 9 4 P re n tic e H a ll

C H o w to P ro g ra m 0 1 3 0 8 9 5 7 2 5 H a rv e y D e ite l 2 0 0 1 P re n tic e H a ll

C H o w to P ro g ra m 0 1 3 0 8 9 5 7 2 5 P a u l D e ite l 2 0 0 1 P re n tic e H a ll

C # H o w T o P ro g ra m 0 1 3 0 6 2 2 2 1 4 T e m N ie to 2 0 0 2 P re n tic e H a ll

C # H o w T o P ro g ra m 0 1 3 0 6 2 2 2 1 4 P a u l D e ite l 2 0 0 2 P re n tic e H a llC # H o w T o P ro g ra m 0 1 3 0 6 2 2 2 1 4 P a u l D e ite l 2 0 0 2 P re n tic e H a ll

C # H o w T o P ro g ra m 0 1 3 0 6 2 2 2 1 4 Je f fre y L is t fie ld 2 0 0 2 P re n tic e H a ll

C # H o w T o P ro g ra m 0 1 3 0 6 2 2 2 1 4 C h e r yl Y a e g e r 2 0 0 2 P re n tic e H a ll

C # H o w T o P ro g ra m 0 1 3 0 6 2 2 2 1 4 M a rin a Z la tk in a 2 0 0 2 P re n tic e H a ll

C # H o w T o P ro g ra m 0 1 3 0 6 2 2 2 1 4 H a rv e y D e ite l 2 0 0 2 P re n tic e H a ll

40

C # H o w T o P ro g ra m 0 1 3 0 6 2 2 2 1 4 H a rv e y D e ite l 2 0 0 2 P re n tic e H a ll

C + + H o w to P ro g ra m 0 1 3 0 8 9 5 7 1 7 P a u l D e ite l 2 0 0 1 P re n tic e H a ll

Page 41: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Joining Data from Tables Joining Data from Tables AuthorsAuthors, , AuthorISBNAuthorISBN, , TitlesTitles and and PublishersPublishers

Fig. 19.23 part 2

AuthorISBNAuthorISBN, , TitlesTitles and and PublishersPublishers

C++ How to Program 0130895717 Harvey Deitel 2001 Prentice Hall

C++ How to Program 0131173340 Paul Deitel 1994 Prentice Hall

C++ How to Program 0131173340 Harvey Deitel 1994 Prentice Hall

C++ How to Program 0135289106 Harvey Deitel 1998 Prentice Hall

C++ How to Program 0135289106 Paul Deitel 1998 Prentice Hall

B i d C 0130323640 H D it l 2000 P ti H lle-Business and e-Commerce for Managers

0130323640 Harvey Deitel 2000 Prentice Hall

e-Business and e-Commerce for Managers

0130323640 Kate Steinbuhler

2000 Prentice Hall

B i d C 0130323640 P l D i l 2000 P i H lle-Business and e-Commercefor Managers

0130323640 Paul Deitel 2000 Prentice Hall

e-Business and e-Commerce How to Program

013028419X Harvey Deitel 2001 Prentice Hall

e-Business and e-Commerce How to Program

013028419X Paul Deitel 2001 Prentice Hall

e-Business and e-Commerce How to Program

013028419X Tem Nieto 2001 Prentice Hall

41

g

Fig . 19.23 Po rt io n o f the re su lt se t p ro d uc e d b y the q ue ry in Fig . 19.22.

Page 42: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

The Selection OperationThe Selection OperationThe Selection OperationThe Selection Operation

The selection operation is accomplished using wherewhereBasic form:

select * from tableselect * from table--name where criterianame where criteriaExample:

select * from titles where copyright > 1999select * from titles where copyright > 1999Make sure String criteria are inside single quotesCan use expected relational operators

select * from authors where firstname > 'B'select * from authors where firstname > 'B'

42

Page 43: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Insert StatementInsert StatementInsert StatementInsert Statement

Inserts a new record into a tableGeneral form:

insert into tableinsert into table--name(fieldname1, fieldname2,...) name(fieldname1, fieldname2,...) values(value1, value2)values(value1, value2)

E lExample:insert into authors(authorID, lastname, firstname)insert into authors(authorID, lastname, firstname)values(15 'Gilbert' 'Steve')values(15 'Gilbert' 'Steve')values(15, Gilbert , Steve )values(15, Gilbert , Steve )

Values must match field names in order and type

43

Page 44: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

INSERTINSERT StatementStatementINSERTINSERT StatementStatementauthorID firstName lastName 1 Harvey Deitel

2 Paul Deitel

3 Tem Nieto

4 Kate Steinbuhler

5 Sean Santry

6 Ted Lin 7 Praveen Sadhu

8 D id M Phi8 David McPhie

9 Cheryl Yaeger

10 Marina Zlatkina i d11 Ben Wiedermann

12 Jonathan Liperi

13 Jeffrey Listfield

14 Sue Smith

44

14 Sue Smith Fig. 19.24 Authors a fte r a n INSERT op era tion to a d d a rec ord .

Page 45: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

The Update StatementThe Update StatementThe Update StatementThe Update Statement

Modifies data in a tableGeneral Form:

update tableupdate table--name set field1=value1 name set field1=value1 where criteriawhere criteria

E lExample:update authors set firstname='Stephen' update authors set firstname='Stephen' where lastname='Gilbert' and firstname='Steve'where lastname='Gilbert' and firstname='Steve'where lastname= Gilbert and firstname= Stevewhere lastname= Gilbert and firstname= Steve

What happens if you violate an integrity rule?

45

Page 46: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

UPDATEUPDATE StatementStatementUPDATEUPDATE StatementStatementauthorID 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 Wiedermann11 Ben Wiedermann12 Jonathan Liperi

13 Jeffrey Listfield

14 Sue Jones

46

14 Sue Jones Fig. 19.25 Ta b le Authors a fte r a n UPDATE op era tion to c ha nge a rec ord .

Page 47: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

The Delete StatementThe Delete StatementThe Delete StatementThe Delete Statement

Removes data from a tableGeneral Form:

delete from tabledelete from table--name where criterianame where criteriaDon't forget the criteria!!!

Example:delete from authors where lastname='Gilbert'delete from authors where lastname='Gilbert'

47

Page 48: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

DELETEDELETE StatementStatementDELETEDELETE StatementStatementauthorID 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

48

Fig. 19.26 Table Authors after a DELETE operation to remove a record .

Page 49: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Programming with ADO .NET: Programming with ADO .NET: g gg gExtracting Information from a DBMSExtracting Information from a DBMS

Examples that demonstrate how to connect to a database, query the database and display the results of the query

49

Page 50: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Displa ing a Database Table in a DataGridVieDispla ing a Database Table in a DataGridVieDisplaying a Database Table in a DataGridViewDisplaying a Database Table in a DataGridView

Step 1: Creating the ProjectStep 2: Adding a Data Source to the ProjectStep 3: Choosing the Data Source Type to Add to the ProjectStep 4: Adding a New Database ConnectionStep 5: Choosing the Books.mdf Data Connectionp gStep 6: Saving the Connection String

50

Page 51: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 1: Creating the ProjectStep 1: Creating the ProjectC t Wi d A li ti d C t Wi d A li ti d Di l T blDi l T bl Ch th F t Ch th F t Di l T bl FDi l T bl FCreate a new Windows Application named Create a new Windows Application named DisplayTableDisplayTable. Change the Form name to . Change the Form name to DisplayTableFormDisplayTableForm

and change the source file name to and change the source file name to DisplayTable.csDisplayTable.cs. Then set the Form's Text property to Display Table.. Then set the Form's Text property to Display Table.

51

Page 52: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 2: Adding a Data Source to the ProjectStep 2: Adding a Data Source to the ProjectStep 2: Adding a Data Source to the ProjectStep 2: Adding a Data Source to the Project

T i i h d ( d b ) To interact with a data source (e.g., a database), you must add it to the project using the Data Sources window, which lists the data that your project can access. Open the Data y p j pSources window by selecting Data > Show Data Sources or by clicking the tab to the right of the tab for the Solution Explorer In the Data Sources window click Add New Data Explorer. In the Data Sources window, click Add New Data Source... to open the Data Source Configuration Wizard. This wizard guides you through connecting to a database

d h i th t f th d t b ill t t and choosing the parts of the database you will want to access in your project.

52

Page 53: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 2: Adding a Data Source to the ProjectStep 2: Adding a Data Source to the ProjectStep 2: Adding a Data Source to the ProjectStep 2: Adding a Data Source to the Project

53

Page 54: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step Step 3: Choosing the Data Source Type to Add to the 3: Choosing the Data Source Type to Add to the P jP jProjectProject

The first screen of the Data Source Configuration Wizard asks you to choose the data source type you wish to include in the project Select Database and click wish to include in the project. Select Database and click Next >.

54

Page 55: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 4: Adding a New Database ConnectionStep 4: Adding a New Database ConnectionStep 4: Adding a New Database ConnectionStep 4: Adding a New Database Connection

Y h h i h ill b d You must next choose the connection that will be used to connect to the database (i.e., the actual source of the data). Click New Connection... to open the Add Connection dialog. p gIf the Data Source is not set to Microsoft SQL Server Database File (SqlClient), click Change..., select Microsoft SQL Server Database File and click OK In the Add SQL Server Database File and click OK. In the Add Connection dialog, click Browse..., locate the Books.mdf database file on your computer, select it and click Open. Y li k T t C ti t if th t th IDE You can click Test Connection to verify that the IDE can connect to the database through SQL Server. Click OK to create the connection.

55

Page 56: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 4: Adding a New Database ConnectionStep 4: Adding a New Database Connection

56

Page 57: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 5: Choosing the Books mdf Data ConnectionStep 5: Choosing the Books mdf Data ConnectionStep 5: Choosing the Books.mdf Data ConnectionStep 5: Choosing the Books.mdf Data Connection

Now that you have created a connection to the Books.mdf database, you can select and use this connection to access the database Click Next > to set connection to access the database. Click Next > to set the connection, then click Yes when asked whether you want to move the database file to your project.want to move the database file to your project.

57

Page 58: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 5: Choosing the Books mdf Data ConnectionStep 5: Choosing the Books mdf Data ConnectionStep 5: Choosing the Books.mdf Data ConnectionStep 5: Choosing the Books.mdf Data Connection

58

Page 59: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 6: Saving the Connection StringStep 6: Saving the Connection StringStep 6: Saving the Connection StringStep 6: Saving the Connection String

Th k h h h The next screen asks you whether you want to save the connection string to the application configuration file. A connection string specifies the path to a database file on g p pdisk, as well as some additional settings that determine how to access the database. Saving the connection string in a configuration file makes it easy to change the connection configuration file makes it easy to change the connection settings at a later time. Leave the default selections and click Next > to proceed.

59

Page 60: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 6: Saving the Connection StringStep 6: Saving the Connection StringStep 6: Saving the Connection StringStep 6: Saving the Connection String

60

Page 61: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Displaying a Database Table in a DataGridView cont’dDisplaying a Database Table in a DataGridView cont’dDisplaying a Database Table in a DataGridView cont dDisplaying a Database Table in a DataGridView cont d

Step 7: Selecting the Database Objects to Include in Your DatasetStep 8: Viewing the Data Source in the Data Sources WindowStep 9: Viewing the Database in the Solution Explorer

61

Page 62: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 7: Selecting the Database Objects to Include in Step 7: Selecting the Database Objects to Include in p g jp g jYour Your DataSetDataSet

The IDE retrieves information about the database you selected and prompts you to y p p yselect the database objects (i.e., the parts of the database) that you want your project to be able to access. Recall that programs typically access a database's contents through a cache of the data, which is stored in a DataSet. In response to your selections in this screen the IDE will generate a class derived from selections in this screen, the IDE will generate a class derived from System.Data.DataSet that is designed specifically to store data from the Books database. Click the checkbox to the left of Tables to indicate that the custom DataSetshould cache (i.e., locally store) the data from all the tables in the Books databaseAuthors, AuthorISBN and Titles. By default, the IDE names the DataSetBooksDataSet, though it is possible to specify a different name in this screen. Finally, click Finish to complete the process of adding a data source to the project.

62

Page 63: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 7: Selecting the Database Objects to Step 7: Selecting the Database Objects to p g jp g jInclude in Your Include in Your DataSetDataSet

63

Page 64: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 8: Viewing the Data Source in the Data Step 8: Viewing the Data Source in the Data p gp gSources WindowSources Window

Notice that a BooksDataSet node now appears in the Data Sources window with child nodes for each table in the Books databasethese nodes represent table in the Books databasethese nodes represent the DataTables of the BooksDataSet. Expand the Authors node and you will see the table's Authors node and you will see the table s columnsthe DataSet's structure mimics that of the actual Books database

64

Page 65: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 8: Viewing the Data Source in the Step 8: Viewing the Data Source in the Data Sources WindowData Sources WindowData Sources WindowData Sources Window

65

Page 66: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 9: Viewing the Database in the Solution Step 9: Viewing the Database in the Solution p gp gExplorerExplorer

B k df i li t d d i th S l ti E l i di ti Books.mdf is now listed as a node in the Solution Explorer, indicating that the database is now part of this project. In addition, the Solution Explorer now lists a new node named BooksDataSet.xsd. A file with the xsd extension is an XML Schema document which specifies the .xsd extension is an XML Schema document, which specifies the

structure of a set of XML documents. The IDE uses an XML Schema document to represent a DataSet's structure, including the tables that comprise the DataSet and the relationships among them When you comprise the DataSet and the relationships among them. When you added the Books database as a data source, the IDE created the BooksDataSet.xsd file based on the structure of the Books database. The IDE then generated class BooksDataSet from the schema (i.e., g ( ,structure) described by the .xsd file.

66

Page 67: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Step 9: Viewing the Database in the Step 9: Viewing the Database in the p gp gSolution ExplorerSolution Explorer

67

Page 68: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Displaying the Authors TableDisplaying the Authors TableDisplaying the Authors TableDisplaying the Authors Table

Now that you have added the Books database as a data source you can display the data from the Now that you have added the Books database as a data source, you can display the data from the database's Authors table in your program. The IDE provides design tools that allow you to display data from a data source on a Form without writing any code. Simply drag and drop items from the Data Sources window onto a Form, and the IDE generates the GUI controls and code necessary to display the selected data source's content.

To display the Authors table of the Books database, drag the Authors node from the Data Sources window to the Form. The IDE generates two GUI controls that appear on DisplayTableFormauthorsBindingNavigator and authorsDataGridView. The IDE also generates several p y g g gadditional non-visual components that appear in the component traythe gray region below the Form in Design view. We use the IDE's default names for these autogenerated components (and others throughout the chapter) to show exactly what the IDE creates. We briefly discuss the authorsBindingNavigator and authorsDataGridView controls here. The next section discusses all of the autogenerated components in detail and explains how the IDE uses these components to connect the autogenerated components in detail and explains how the IDE uses these components to connect the GUI controls to the Authors table of the Books database.

68

Page 69: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Design view after dragging the Authors data source node to the FormDesign view after dragging the Authors data source node to the Form

69

Page 70: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Displaying the Authors table in a Displaying the Authors table in a p y gp y gDataGridViewDataGridView

A D G idVi di l d i d i d A DataGridView displays data organized in rows and columns that correspond to the rows and columns of the underlying data source. In this case, the DataGridViewy g ,displays the data of the Authors table, so the control has columns named AuthorID, FirstName and LastName. In Design view the control does not display any rows of actual Design view, the control does not display any rows of actual data below the column headers. The data is retrieved from the database and displayed in the DataGridView only at

ti E t th Wh th F l d th runtime. Execute the program. When the Form loads, the DataGridView contains four rows of dataone for each row of the Authors table

70

Page 71: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Displaying the Authors table in a Displaying the Authors table in a p y gp y gDataGridViewDataGridView

71

Page 72: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Binding NavigatorBinding NavigatorBinding NavigatorBinding Navigator

The strip of buttons below the title bar of the window is a BindingNavigator which enables users to browse and The strip of buttons below the title bar of the window is a BindingNavigator, which enables users to browse and manipulate data displayed by another GUI control (in this case, a DataGridView) on the Form. A BindingNavigator's buttons resemble the controls on a CD or DVD player and allow you to move to the first row of data, the preceding row, the next row and the last row. The control also displays the currently selected row number in a text box. You can use this text box to enter the number of a row that you want to select. The yauthorsBindingNavigator in this example allows you to "navigate" the Authors table displayed in the authorsDataGridView. Clicking the buttons or entering a value in the text box causes the DataGridView to select the appropriate row. An arrow in the DataGridView's leftmost column indicates the currently selected row.A BindingNavigator also has buttons that allow you to add a new row, delete a row and save changes back to th d l i d t (i thi th A th t bl f th B k d t b ) Cli ki th b tt ith th the underlying data source (in this case, the Authors table of the Books database). Clicking the button with the yellow plus icon () adds a new row to the DataGridView. However, simply typing values in the FirstName and LastName columns does not insert a new row in the Authors table. To add the new row to the database on disk, click the Save button (the button with the disk icon, ). Clicking the button with the red X () deletes the currently selected row from the DataGridView. Again, you must click the Save button to make the change in the g , y gdatabase. Test these buttons. Execute the program and add a new row, then save the changes and close the program. When you restart the program, you should see that the new row was saved to the database and appears in the DataGridView. Now delete the new row and click the Save button. Close and restart the program to see that the new row no longer exists in the database.

72

Page 73: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Querying the Books DatabaseQuerying the Books DatabaseQuerying the Books DatabaseQuerying the Books Database

Step 1: Creating the ProjectStep 2: Adding a Data Source to the ProjectStep 3: Creating a DataGridView to Display the Titles TableStep 4: Adding Custom Queries to the TitlesTableAdaptorStep 5: Choosing How the TableAdapter Schould Access the Database

73

Page 74: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Querying the Books Database cont’dQuerying the Books Database cont’dQuerying the Books Database cont dQuerying the Books Database cont d

Step 6: Choosing the Query RypeStep 7: Specifying a SELECT Statement for the QueryStep 8: Building a Query with Query BuilderStep 9: Closing the Query Builderp g yStep 10: Setting the Names of the Autogenerated Methods That Perform the QueryyStep 11: Adding an Additional Query

74

Page 75: C# and Databasesnatacha/TeachSpring_09/CSC330/CSharp/Lec5/DatabasesCSharp.pdfRelational Database Systems and SQL Relational database systems are the most popular today (Microsoft Access

Querying the Books Database cont’dQuerying the Books Database cont’dQuerying the Books Database cont dQuerying the Books Database cont d

Step 12: Adding a ComboBox to the St 13 C t i i th F ’ L d E t H dlStep 13: Customizing the Form’s Load Event HandlerStep 14: Programming an Event Handler for the C b BComboBox

75