cop 4540 spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/spring/home… · crazy for the...

17
MICROSOFT ACCESS TUTORIAL COP 4540 Database Management Hsin-Yu Ha

Upload: others

Post on 18-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

MICROSOFT ACCESS TUTORIAL

COP 4540Database Management

Hsin-Yu Ha

Page 2: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

� Create Database

� Create Table

� Set up attribute type, primary key, foreign key

� Query

� SQL Language

Page 3: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

SQL Template

Page 4: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

Example: Library Database

� Tables

Page 5: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

List of Queries

� Query 1. Retrieve the names of all the male Resident

� Query 2. Retrieve the number of female and male residents

� Query 3. Retrieve the names of residents who did not borrow any book in our record

� Query 4. Retrieve the Library card id/names of residents who borrow more than once in record

� Query 5. Retrieve the names of residents who borrow books less than 5 days

Page 6: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

Example: Library Database

� Query 1. Retrieve the names of all the male Resident

SELECT Resident_L_Name,Resident_F_NameFROM ResidentWHERE Sex="M"

Page 7: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

Example: Library Database

� Query 2. Retrieve the number of female and male residents

� Important Function

� Aggregation functions: SUM, COUNT, MAX…etc.

SELECT Sex, count(*) as NumberofRecordFROM ResidentGROUP BY Sex

Page 8: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

Example: Library Database

� Query 3. Retrieve the names of residents who did not borrow any book in our record

� Important Function

� Nested Select

� Please notice that you can always use inner join or EXIST as an alternative resolution.

SELECT Resident_L_Name,Resident_F_NameFROM ResidentWHERE LibraryCard_ID not in (SELECT LibraryCard_ID

FROM Record)

Page 9: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

Example: Library Database

� Query 4. Retrieve the Library card id/names of residents who borrow more than once in record

� Important Function� HAVING: The HAVING clause is used in combination with the GROUP BY

clause

� Step1

� Step2

SELECT LibraryCard_IDFROM RecordGROUP BY LibraryCard_IDHaving count(*) > 1

SELECT Resident_L_Name,Resident_F_NameFROM ResidentWHERE LibraryCard_ID in (SELECT LibraryCard_ID

FROM Record GROUP BY LibraryCard_IDHaving count(*) > 1)

Page 10: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

Example: Library Database

� Query 5. Retrieve the names of residents who borrow books less than 5 days

� Option1 SELECT Resident_L_Name,Resident_F_NameFROM ResidentWHERE LibraryCard_ID in (SELECT LibraryCard_ID

FROM Record WHERE Date_of_return - Date_of_borrow<5)

Page 11: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

Example: Library Database

� Query 5. Retrieve the names of residents who borrow books less than 5 days

� Important Function

� Datediff

� Option2

SELECT Resident_L_Name,Resident_F_NameFROM ResidentWHERE LibraryCard_ID in (SELECT LibraryCard_ID

FROM Record WHERE datediff(“d”,Date_of_return, Date_of_borrow)< 5

Page 12: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

INNER JOIN

� Inner Join� SELECT A.Books_title, A.Books_price as A_price , B.Books_price as B_price

� FROM Amazon as A INNER JOIN FIU_bookstore as B

� ON A.books_title = B.Books_title

Books_title Books_price

Let the Great World Spin 45

Brooklyn 20

The City & the City 33

Stitches 24

Crazy for the Storm 19

Books_title Books_price

Let the Great World Spin 20

The Lost Symbol 30

The City & the City 29

Little Oink 18

Crazy for the Storm 23

Page 13: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

INNER JOIN(cont.)

� Inner Join

Books_title Books_price

Let the Great World Spin 45

Brooklyn 20

The City & the City 33

Stitches 24

Crazy for the Storm 19

Books_title Books_price

Let the Great World Spin 20

The Lost Symbol 30

The City & the City 29

Little Oink 18

Crazy for the Storm 23

Books_title A_price B_price

Let the Great World Spin 45 20

The City & the City 33 29

Crazy for the Storm 19 23

Page 14: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

LEFT JOIN

� Left Join� SELECT A.Books_title as A_Books , B.Books_title as B_Books

� FROM Amazon as A LEFT JOIN FIU_bookstore as B

� ON A.books_title = B.Books_title

Books_title Books_price

Let the Great World Spin 45

Brooklyn 20

The City & the City 33

Stitches 24

Crazy for the Storm 19

Books_title Books_price

Let the Great World Spin 20

The Lost Symbol 30

The City & the City 29

Little Oink 18

Crazy for the Storm 23

Page 15: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

LEFT JOIN(cont.)

� Left Join

Books_title Books_price

Let the Great World Spin 45

Brooklyn 20

The City & the City 33

Stitches 24

Crazy for the Storm 19

Books_title Books_price

Let the Great World Spin 20

The Lost Symbol 30

The City & the City 29

Little Oink 18

Crazy for the Storm 23

A_Books B_Books

Let the Great World Spin Let the Great World Spin

Brooklyn

The City & the City The City & the City

Stitches

Crazy for the Storm Crazy for the Storm

Page 16: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

RIGHT JOIN

� Right Join� SELECT A.Books_title as A_Books , B.Books_title as B_Books

� FROM Amazon as A RIGHT JOIN FIU_bookstore as B

� ON A.books_title = B.Books_title

Books_title Books_price

Let the Great World Spin 45

Brooklyn 20

The City & the City 33

Stitches 24

Crazy for the Storm 19

Books_title Books_price

Let the Great World Spin 20

The Lost Symbol 30

The City & the City 29

Little Oink 18

Crazy for the Storm 23

Page 17: COP 4540 Spring2010 - users.cs.fiu.educhens/courses/cop4540/2011/Spring/Home… · Crazy for the Storm 19 Books_title Books_ price Let the Great World Spin 20 The Lost Symbol 30 The

RIGHT JOIN (cont.)

� Right Join

Books_title Books_price

Let the Great World Spin 45

Brooklyn 20

The City & the City 33

Stitches 24

Crazy for the Storm 19

Books_title Books_price

Let the Great World Spin 20

The Lost Symbol 30

The City & the City 29

Little Oink 18

Crazy for the Storm 23

A_Books B_Books

Let the Great World Spin Let the Great World Spin

The Lost Symbol

The City & the City The City & the City

Little Oink

Crazy for the Storm Crazy for the Storm