Introduction to Database Systems Microsoft Access, OleDB.

Slides:



Advertisements
Similar presentations
Introduction to SQL, OleDB interface to Access from VB.NET.
Advertisements

What is a Database By: Cristian Dubon.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Introduction to Database Systems. So, what is a database, anyway? An integrated, self-describing collection of data about related sets of things and the.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1.
Copyright © 2014 Pearson Education, Inc. Chapter 10 Working with Databases.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1 Created By Martin Schedlbauer
Introduction to Database Systems. So, what is a database, anyway? An integrated, self-describing collection of data about related sets of things and the.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Access 2007 Database Application Managing Business Information Effectively BCIS 1 and 2.
ASP.NET Programming with C# and SQL Server First Edition
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10 Working with Databases.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Introduction to SQL Steve Perry
1 INTRODUCTION TO DATABASE MANAGEMENT SYSTEM L E C T U R E
Chapter 15: Using LINQ to Access Data in C# Programs.
CHAPTER 8: MANAGING DATA RESOURCES. File Organization Terms Field: group of characters that represent something Record: group of related fields File:
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Database: SQL and MySQL
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
DAY 12: DATABASE CONCEPT Tazin Afrin September 26,
Introduction to Databases Trisha Cummings. What is a database? A database is a tool for collecting and organizing information. Databases can store information.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Instructor: Dema Alorini Database Fundamentals IS 422 Section: 7|1.
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
ADO.NET Data Access. Page  2 SQL  When we interact with the datasource through ADO.NET we use the SQL language to retrieve,modify,update information.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
INFO1408 Database Design Concepts Week 15: Introduction to Database Management Systems.
Database collection of related information stored in an organized form Database program software tool for storage & retrieval of that information.
1 CSE 2337 Introduction to Data Management Access Book – Ch 1.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
Copyright © 2014 Pearson Education, Inc. Chapter 10 Working with Databases.
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
1 A Very Brief Introduction to Relational Databases.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Database Planning Database Design Normalization.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Introduction to Database Programming with Python Gary Stewart
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Fundamentals of DBMS Notes-1.
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
 2012 Pearson Education, Inc. All rights reserved.
JDBC.
Structured Query Language (SQL) William Klingelsmith
Introduction To Structured Query Language (SQL)
Presentation transcript:

Introduction to Database Systems Microsoft Access, OleDB

Introduction to Database Systems So, what is a database, anyway? An integrated, self-describing collection of data about related sets of things and the relationships among them

If you burned down all our plants, and we just kept our people and our information files, we should soon be as strong as ever. Thomas Watson, Jr. Former chairman of IBM

C# and Database Management Systems Simple text files are: – Fine for small amounts of data – But impractical for large amounts of data Businesses must maintain huge amounts of data – A database management system (DBMS) is the typical solution to the data needs of business – Designed to store, retrieve, & manipulate data C# can communicate with several DBMS – Tells DBMS what data to retrieve or manipulate

Layered Approach to Using a DBMS Applications that work with a DBMS use a layered approach – C# application is topmost layer – C# sends instructions to next layer, the DBMS – DBMS works directly with data Programmer need not understand the physical structure of the data – Just need to know how to interact with the DBMS C# Program Database Management System Data

Relational Database Model Introduced in the 60’s and 70’s and is the most common type of DBMS today Data elements stored in simple tables (related) General structure good for many problems Easy to understand, modify, maintain Examples: MySQL, Access, Oracle, SQL Server We will focus on relational databases using Microsoft Access

The Relational Model Views entities as two-dimensional tables – Records are rows – Attributes (fields) are columns Tables can be linked Supports one-to-many, many-to-many, and one-to-one relationships

Terminology Database: a collection of interrelated tables Table: a logical grouping of related data – A category of people, places, or things – For example, employees or departments – Organized into rows and columns Field: an individual piece of data pertaining to an item, an employee name for instance Record: the complete data about a single item such as all information about an employee – A record is a row of a table

Emp_IdFirst_NameLast_NameDepartment IgnacioFletaAccounting ChristianMartinComputer Support OrvilleGibsonHuman Resources BenSmithAccounting AllisonChongComputer Support Database Table Row (Record) ColumnField Each table has a primary key – Uniquely identifies that row of the table – Emp_Id is the primary key in this example – Serves as an index to quickly retrieve the record Columns are also called fields or attributes Each column has a particular data type

C# and SQL Server Data Types C# data types must match table data types SQL Server and C# have similar data types SQL TypeUsageC# Type BitTrue/false valuesbool DateTimeDates and timesDateTime Decimal, MoneyFinancial valuesdecimal FloatReal-number valuesdouble IntInteger valuesint SmallintIntegers -32,768 to 32,767short Varchar(n)Variable length stringsstring TextStrings more than 8000 charstring

Choosing Column Names Define a column for each piece of data Allow plenty of space for text fields Avoid using spaces in column names For the members of an organization: Column NameTypeRemarks Member_IDintPrimary key First_Namevarchar(40) Last_Namevarchar(40) Phonevarchar(30) varchar(50) Date_JoinedsmalldatetimeDate only, no time values Meetings_Attendedsmallint OfficerYes/NoTrue/False values

Database design minimizes redundant data In the following employee table: IDFirst_NameLast_NameDepartment IgnacioFletaAccounting ChristianMartinComputer Support OrvilleGibsonHuman Resources 00300JoseRamirezResearch & Devel BenSmithAccounting AllisonChongComputer Support Same dept name appears multiple times – Requires additional storage space – Causes problems if misspelled – What if a department needs to be renamed? Issues with Redundant Data

Create a department table Dept_IDDept_NameNum_Employees 1Human Resources10 2Accounting5 3Computer Support30 4Research & Development15 Reference department table in employee table IDFirst_NameLast_NameDept_ID IgnacioFleta ChristianMartin OrvilleGibson JoseRamirez BenSmith AllisonChong3 Eliminate Redundant Data by Relating Multiple Tables

Microsoft Access is Unique Provides DBMS functions – Not “industrial-strength”, designed for: Individuals Small workgroups – External application programs work with Access We will use C#.NET and the Structured Query Language (SQL) later Provides built-in tools for reporting and for application development – Forms – Reports – Code modules using Visual Basic for Applications (VBA) which is similar to C# and Visual Basic.NET but not the same Provides flexibility – Small, simple all-in-one environment – Data can be easily transferred to full-fledged DBMS

Introduction to Access Sample databases – Northwind Included with every version of Access since 2.0 Demonstration of Access – Startup – Create tables – Link table relationships – Create queries/reports

Access 2007 Example Student IDLast NameFirst NameDOBAddress 1MockKenrick Somewhere Ave 2CueBarbie A Street 3ObamaBarack Somewhere Ave

Access 2007 Example Student IDGrade 1A 2B 3B CS 109 Table Student IDGrade 1A 2B 3A CS 201 Table

SQL Structured Query Language, abbreviated SQL – Usually pronounced “sequel” but also “ess-cue- ell”) – The common language of client/server database management systems. – Standardized – you can use a common set of SQL statements with all SQL-compliant systems. – Defined by E.F. Codd at IBM research in – Based on relational algebra and predicate logic

SQL Data Retrieval Given an existing database, the SELECT statement is the basic statement for data retrieval. – Both simple and complex, and it may be combined with other functions for greater flexibility. SELECT data_element1 [, {data_element2 | function(..)} ]Or * FROMtable_1, [, table_2, …] [ WHEREcondition_1 [, {not, or, and} condition_2] ] [ GROUP BY data_1, … ] [ HAVINGaggregate function(…)… ] [ORDER BYdata1, … ]

SELECT statement Some sample aggregate functions: – COUNT(*)SUM(item) – AVG(item)MAX(item) – MIN(item) Conditional Operators – =Equal – <Less than – >Greater than – <>,!=Not equal to – <=Less than or equal to – >=Greater than or equal to

SELECT Examples Select every row, column from the table: – SELECT * FROM Orders; – SELECT Orders.cust_id, Orders.prod_id, Orders.cost, Orders.salesperson FROM Orders; Returns a set of all rows that match the query

SELECT If a table has spaces or certain punctuation in it, then Access needs to have the items enclosed in square brackets []. The previous query is identical to the following: – SELECT [orders].[cust_id], orders.prod_id, orders.cost, orders.[salesperson] FROM Orders;

SELECT Query in Access Can flip back and forth between SQL View, Run, and Design Mode SQL Run Design

More SELECT Statements Note that we can have duplicates as a result of the selection. If we want to remove duplicates, we can use the DISTINCT clause: SELECT DISTINCT Orders.cust_id FROM Orders; We can combine a selection and a projection by using the WHERE clause: SELECT Orders.cust_id FROM Orders WHERE Salesperson = “Jones”; This could be used if we wanted to get all the customers that Jones has sold to, in this case, CUST_ID=101 and CUST_ID=100. By default, Access is not case-sensitive, so “jones” would also result in the same table.

More SELECT We can further refine the query by adding AND, OR, or NOT conditions. If we want orders from Jones or from Smith then the query becomes: SELECT Orders.cust_id FROM Orders WHERE Salesperson = “Jones” or Salesperson = “Smith”; Another refinement is to use the BETWEEN operator. If we want only those orders between 10 and 100 then we could define this as: SELECT Orders.cust_id, Orders.cost FROM Orders WHERE Orders.cost >10 and Orders.cost <100; Or use the between operator: SELECT Orders.cust_id, Orders.cost FROM Orders WHERE Orders.cost BETWEEN 10 and 100;

Finally, we might want to sort the data on some field. We can use the ORDER BY clause: SELECT Orders.cust_id, Orders.cost FROM Orders WHERE Orders.cost >10 and Orders.cost <100 ORDER BY Orders.cost; This sorts the data in ascending order of cost. An example is shown in the table: CUST_IDCOST If we wanted to sort them in descending order, use the DESC keyword: SELECT Orders.cust_id, Orders.cost FROM Orders WHERE Orders.cost >10 and Orders.cost <100 ORDER BY Orders.cost DESC; More SELECT

Joining Data from Multiple Tables If our data is in multiple tables we can join them together in one query. – Use a JOIN operator (Access default w/Design view) – Add tables to the FROM, WHERE section (what we will use here) Say we have the following table in addition to Orders:

Multiple Tables SELECT Orders.cust_id, Customer.Cust_Name FROM Orders, Customer WHERE Orders.cost >10 and Orders.cost <100; What do you expect from this query? Result: 100Thomas Jefferson 101Thomas Jefferson 102Thomas Jefferson 100Bill Clinton 101Bill Clinton 102Bill Clinton 100George Bush 101George Bush 102George Bush PRODUCT of two tables!

Multiple Tables Need to link the tables by their common field, the customer ID: SELECT Orders.cust_id, Customer.Cust_Name FROM Orders, Customer WHERE Orders.cust_id = Customer.Cust_Id and Orders.cost >10 and Orders.cost <100; Result: 100Thomas Jefferson 101Bill Clinton 102George Bush

INSERT command Allows you to insert single or multiple rows of data into a table INSERT INTO table [(column-list)] [VALUES (value-list) | sql-query]

INSERT examples Given mytable(field1 as currency, field2 as text, field3 as integer): INSERT INTO mytable (field1, field2, field3) VALUES (12.10, “bah”,20); Adds a new row to the table mytable If you don’t specify every field then fields left out get the default: INSERT INTO mytable (field1, field2) VALUES(24.2, “zot”); Adds only for field1 and field2.

INSERT Examples INSERT INTO ORDERS (CUST_ID, PROD_ID, COST, SALESPESON) VALUES (103, ‘Y338’, 55, ‘Smith’); INSERT INTO ORDERS (PROD_ID, COST, SALESPESON) VALUES (‘Y638’, 155, ‘Smith’); Second might be useful if the CUST_ID is an autonumber field

DELETE Delete will remove a row from the table. DELETE FROM table_name [WHERE search- condition] Examples: DELETE FROM mytable1; Removes all rows! DELETE FROM mytable1 WHERE field1 > 100; Removes only rows with field1>100

UPDATE Update lets you modify the contents of the data. UPDATE table_name SET field_name = expression [, field-name=expression …] [WHERE search-condition] UPDATE mytable SET field1 = 0.0; Changes all field1’s to zero for every row! UPDATE mytable SET field1 = 0.0, field2 = “woof”; Sets field1 to 0 and field2 to woof for all rows! If this is a violation, access will prevent it from happening UPDATE mytable SET field1 = 25.0 WHERE field2=“foo”; Only updates the field where field2 is “foo”

SQL Queries There are a lot more queries, but that should give you an idea of what is possible and how it is done Next we’ll go over an example that uses SQL on an Access Database from C# – Uses OleDB which is different from the book LINQ more modern/powerful but doesn’t work with Access – Database access technology changes rapidly

OleDB in C# Add to the top: using System.Data.OleDb; Set the connection string: – This tells C# where the database is and how to connect to it: For Office 2007 string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\path\\to\\database.accdb";

Example Reading from the DB string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=students.accdb"; OleDbConnection cn = new OleDbConnection(connectionString); cn.Open(); OleDbCommand cmd = new OleDbCommand ("SELECT * From Students WHERE Lastname > 'G'", cn); cmd.ExecuteNonQuery(); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { int ID = Convert.ToInt32(reader["ID"]); string name = Convert.ToString(reader["LastName"]); Console.WriteLine(ID + " " + name); } cn.Close();

Example Writing to the DB string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=students.accdb"; OleDbConnection cn = new OleDbConnection(connectionString); cn.Open(); string newLastName = "Washington"; string sql = "INSERT INTO Students (LastName, FirstName, DOB, Address) " + "VALUES ('" + newLastName + "', 'George', #04/19/2005#, '999 C St.')"; OleDbCommand cmd = new OleDbCommand(sql, cn); cmd.ExecuteNonQuery(); MessageBox.Show("Executed insert query."); cn.Close();