COP 4540 Database Management MICROSOFT ACCESS TUTORIAL Hsin-Yu Ha
Create Database Create Table Set up attribute type, primary key, foreign key Query SQL Language
SQL Template
Tables Example: Library Database
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
Example: Library Database Query 1. Retrieve the names of all the male Resident SELECT Resident_L_Name,Resident_F_Name FROM Resident WHERE Sex="M"
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 NumberofRecord FROM Resident GROUP BY Sex
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_Name FROM Resident WHERE LibraryCard_ID not in (SELECT LibraryCard_ID FROM Record)
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 SELECT LibraryCard_ID FROM Record GROUP BY LibraryCard_ID Having count(*) > 1 Step2 SELECT Resident_L_Name,Resident_F_Name FROM Resident WHERE LibraryCard_ID in (SELECT LibraryCard_ID FROM Record GROUP BY LibraryCard_ID Having count(*) > 1)
Example: Library Database Query 5. Retrieve the names of residents who borrow books less than 5 days Option1 SELECT Resident_L_Name,Resident_F_Name FROM Resident WHERE LibraryCard_ID in (SELECT LibraryCard_ID FROM Record WHERE Date_of_return - Date_of_borrow<5)
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_Name FROM Resident WHERE LibraryCard_ID in (SELECT LibraryCard_ID FROM Record WHERE datediff( d,date_of_return, Date_of_borrow)< 5
INNER JOIN Let the Great World Spin 45 Brooklyn 20 The City & the City 33 Stitches 24 Crazy for the Storm 19 Let the Great World Spin 20 The Lost Symbol 30 The City & the City 29 Little Oink 18 Crazy for the Storm 23 Inner Join SELECT A., A. as A_, B. as B_ FROM Amazon as A INNER JOIN FIU_bookstore as B ON A.books_title = B.
INNER JOIN(cont.) Let the Great World Spin 45 Brooklyn 20 The City & the City 33 Stitches 24 Crazy for the Storm 19 Let the Great World Spin 20 The Lost Symbol 30 The City & the City 29 Little Oink 18 Crazy for the Storm 23 Inner Join A_ B_ Let the Great World Spin 45 20 The City & the City 33 29 Crazy for the Storm 19 23
LEFT JOIN Let the Great World Spin 45 Brooklyn 20 The City & the City 33 Stitches 24 Crazy for the Storm 19 Let the Great World Spin 20 The Lost Symbol 30 The City & the City 29 Little Oink 18 Crazy for the Storm 23 Left Join SELECT A. as A_Books, B. as B_Books FROM Amazon as A LEFT JOIN FIU_bookstore as B ON A.books_title = B.
LEFT JOIN(cont.) Let the Great World Spin 45 Brooklyn 20 The City & the City 33 Stitches 24 Crazy for the Storm 19 Let the Great World Spin 20 The Lost Symbol 30 The City & the City 29 Little Oink 18 Crazy for the Storm 23 Left Join A_Books Let the Great World Spin Brooklyn The City & the City Stitches Crazy for the Storm B_Books Let the Great World Spin The City & the City Crazy for the Storm
RIGHT JOIN Let the Great World Spin 45 Brooklyn 20 The City & the City 33 Stitches 24 Crazy for the Storm 19 Let the Great World Spin 20 The Lost Symbol 30 The City & the City 29 Little Oink 18 Crazy for the Storm 23 Right Join SELECT A. as A_Books, B. as B_Books FROM Amazon as A RIGHT JOIN FIU_bookstore as B ON A.books_title = B.
RIGHT JOIN (cont.) Let the Great World Spin 45 Brooklyn 20 The City & the City 33 Stitches 24 Crazy for the Storm 19 Let the Great World Spin 20 The Lost Symbol 30 The City & the City 29 Little Oink 18 Crazy for the Storm 23 Right Join A_Books Let the Great World Spin The City & the City Crazy for the Storm B_Books Let the Great World Spin The Lost Symbol The City & the City Little Oink Crazy for the Storm