Databases in Engineering / Lab-1 (MS-Access/SQL)



Similar documents
- Suresh Khanal. Microsoft Excel Short Questions and Answers 1

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Use Find & Replace Commands under Home tab to search and replace data.

Using AND in a Query: Step 1: Open Query Design

Oracle 10g PL/SQL Training

Ken Goldberg Database Lab Notes. There are three types of relationships: One-to-One (1:1) One-to-Many (1:N) Many-to-Many (M:N).

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

How To Create A Table In Sql (Ahem)

Introduction to Microsoft Jet SQL

Topic: Relationships in ER Diagram and Relationships in MS Access

SQL. Short introduction

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

MS Access Lab 2. Topic: Tables

A Basic introduction to Microsoft Access

Financial Data Access with SQL, Excel & VBA

DBMS / Business Intelligence, SQL Server

4. The Third Stage In Designing A Database Is When We Analyze Our Tables More Closely And Create A Between Tables

An Overview of Database Management System

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

Table and field properties Tables and fields also have properties that you can set to control their characteristics or behavior.

SQL Server An Overview

Task 1 : Implement a table in your database to represent the CarModel entity

IT2304: Database Systems 1 (DBS 1)

Consider the possible problems with storing the following data in a spreadsheet:

IT2305 Database Systems I (Compulsory)

Microsoft Access Basics

Course: CSC 222 Database Design and Management I (3 credits Compulsory)

Database Database Management System (DBMS)

Filter by Selection button. Displays records by degree to which they match the selected record. Click to view advanced filtering options

Microsoft Access 2007 Module 1

Access Queries (Office 2003)

Intro to Databases. ACM Webmonkeys 2011

User Services. Microsoft Access 2003 II. Use the new Microsoft

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. COURSE CURRICULUM COURSE TITLE: DATABASE MANAGEMENT (Code: ) Information Technology

Creating and Using Databases with Microsoft Access

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Access 2010 Intermediate Skills

Chapter 5. Microsoft Access

SQL Server 2008 Core Skills. Gary Young 2011

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS...

Lab Manual. Databases. Microsoft Access. Peeking into Computer Science Access Lab manual

MS Access. Microsoft Access is a relational database management system for windows. Using this package, following tasks can be performed.

Access Part 2 - Design

ICAB4136B Use structured query language to create database structures and manipulate data

Microsoft Access Glossary of Terms

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

TIM 50 - Business Information Systems

1. INTRODUCTION TO RDBMS

Microsoft Using an Existing Database Amarillo College Revision Date: July 30, 2008

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

Access Creating Databases - Fundamentals

PROJECT ON MICROSOFT ACCESS (HOME TAB AND EXTERNAL DATA TAB) SUBMITTED BY: SUBMITTED TO: NAME: ROLL NO: REGN NO: BATCH:

Relational Databases. Christopher Simpkins

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

Microsoft Access 2010

5.5 Copyright 2011 Pearson Education, Inc. publishing as Prentice Hall. Figure 5-2

Microsoft Access 3: Understanding and Creating Queries

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database 10g: Introduction to SQL

Oracle SQL. Course Summary. Duration. Objectives

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

BCA. Database Management System

Lesson 07: MS ACCESS - Handout. Introduction to database (30 mins)

Check out our website!

Training Needs Analysis

Using SQL Server Management Studio

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Topics. Database Essential Concepts. What s s a Good Database System? Using Database Software. Using Database Software. Types of Database Programs

Introduction to Computing. Lectured by: Dr. Pham Tran Vu

INTRODUCTION TO MICROSOFT ACCESS MINIMAL MANUAL

Microsoft Office 2010

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Microsoft. Access HOW TO GET STARTED WITH

CONTACTS SYNCHRONIZER FOR IPAD USER GUIDE

Database Query 1: SQL Basics

Technology in Action. Alan Evans Kendall Martin Mary Anne Poatsy. Eleventh Edition. Copyright 2015 Pearson Education, Inc.

User Services. Intermediate Microsoft Access. Use the new Microsoft Access. Getting Help. Instructors OBJECTIVES. July 2009

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Microsoft Access 2007 Introduction

Data Tool Platform SQL Development Tools

Oracle Database: SQL and PL/SQL Fundamentals NEW

Microsoft Access 2003 Module 1

ER/Studio 8.0 New Features Guide

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

Information Systems SQL. Nikolaj Popov

A database is a collection of data organised in a manner that allows access, retrieval, and use of that data.

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

MySQL for Beginners Ed 3

Microsoft Access Part I (Database Design Basics) ShortCourse Handout

Querying Microsoft SQL Server

IN THIS PROJECT, YOU LEARN HOW TO

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Instant SQL Programming

T-SQL STANDARD ELEMENTS

Transcription:

COVER PAGE Databases in Engineering / Lab-1 (MS-Access/SQL) ITU - Geomatics 2014 2015 Fall

1

Table of Contents COVER PAGE... 0 1. INTRODUCTION... 3 1.1 Fundamentals... 3 1.2 How To Create a Database File in Access... 4 2. TUTORIAL... 5 2.1 Creating Tables Using SQL... 5 2.2 Creating Tables in Design View... 7 2.3 Creating Relationship Between Tables... 8 2.4 Queries... 10 2.5 More Examples With Queries... 13 2

1. INTRODUCTION 1.1 Fundamentals A database is an organized collection of data. Microsoft Access is a popular software that can create and manage databases. By the help of this system, you can create queries, filters or you can sort your data according to your purpose, analyze your data and create report for another purposes. By the most general description, database is a system that can store your data without any repetation and manage your data for any purposes. Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database, it also provides users with tools to add, delete, access, modify, and analyze data stored in one location. Well known DBMSs include FoxPro, IBM DB2, Linter, Microsoft Access, Microsoft SQL Server, MySQL, Oracle, PostgreSQL and SQLite. Database Table (Relationships, Primary Keys) Field Data Type (Text, Number, Date/Time, Currency, AutoNumber, Yes/No, Hyperlink, Attachment, Calculated) Value Some data types for Microsoft Access 2010; Byte Use for integers that range from 0 to 255. Storage requirement is 1 byte. Integer Use for integers that range from -32,768 to 32,767. Storage requirement is 2 bytes. Long Integer Use for integers that range from -2,147,483,648 to 2,147,483,647. Storage requirement is 4 bytes. Single Use for numeric floating point values that range from -3.4 x 1038 to 3.4 x 1038 and up to seven significant digits. Storage requirement is 4 bytes. Double Use for numeric floating point values that range from -1.797 x 10308 to 1.797 x 10308 and up to fifteen significant digits. Storage requirement is 8 bytes. Decimal Use for numeric values that range from -9.999... x 1027 to 9.999... x 1027. Storage requirement is 12 bytes. Source: http://office.microsoft.com/tr-tr/access-help/ha010341783.aspx#datetime 3

Conceptual View of Acces 2010; Source: http://msdn.microsoft.com/en-us/library/office/ff965871(v=office.14).aspx#dataprogrammingwithaccess2010_where Before Access 2007 - Microsoft Joint Engine Technology (JET) After Access 2007 - Microsoft Access Engine (ACE) 1.2 How To Create a Database File in Access Choose design view to modify your table. Write your field names and choose appropriate data types for each of them. 4

After choosing your data type, you can modify the length of the text, type of the number (integer, single,double), validation rule etc. Create your table with related Field Names and Data Types above. Save your table to the database and you can see related table in the navigation menu. 2. TUTORIAL 7 tables will be created within laboratory work which will demonstrate the example structure of a student otomation system. One of these tables (Student) will be created by using SQL queries and other 6 others will be created by using design view. Tables ; Faculties Programs Lecturer Students Courses StudentCourses (Junction Table) LecturerCourses (Junction Table) 2.1 Creating Tables Using SQL SQL, (Structured Query Language), is a query language based on relational algebra which is used for managing and querying the data in relational database management systems (RDBMS). SQL consists of 3 type of component based on the type of commands; DDL (Data Definition Language): Statements to define data types. (Tables, Fields and Users) DML (Data Manipulation Language) : Statements to Select, deleting, insert and update data. DCL (Data Control Language): Statements to control access to database (User permissions) 5

Microsoft Access 2010 DDL Commands; ADD USER Statement (Microsoft Access SQL) ALTER TABLE Statement (Microsoft Access SQL) ALTER USER or DATABASE Statement (Microsoft Access SQL) CONSTRAINT Clause (Microsoft Access SQL) CREATE INDEX Statement (Microsoft Access SQL) CREATE PROCEDURE Statement (Microsoft Access SQL) CREATE TABLE Statement (Microsoft Access SQL) CREATE USER or GROUP Statement (Microsoft Access SQL) CREATE VIEW Statement (Microsoft Access SQL) DROP Statement (Microsoft Access SQL) DROP USER or GROUP Statement (Microsoft Access SQL) GRANT Statement (Microsoft Access SQL) REVOKE Statement (Microsoft Access SQL) Source: Microsoft Access 2010 SQL reference: http://msdn.microsoft.com/en-us/library/office/ff841694(v=office.14).aspx Microsoft Access 2010 DML Commands; DELETE Statement (Microsoft Access SQL) EXECUTE Statement (Microsoft Access SQL) INNER JOIN Operation (Microsoft Access SQL) INSERT INTO Statement (Microsoft Access SQL) LEFT JOIN, RIGHT JOIN Operations (Microsoft Access SQL) PARAMETERS Declaration (Microsoft Access SQL) PROCEDURE Clause (Microsoft Access SQL) SELECT Statement (Microsoft Access SQL) SELECT INTO Statement (Microsoft Access SQL) SQL Subqueries (Microsoft Access SQL) TRANSACTION Statement (Microsoft Access SQL) TRANSFORM Statement (Microsoft Access SQL) UNION Operation (Microsoft Access SQL) UPDATE Statement (Microsoft Access SQL) WITH OWNERACCESS OPTION Declaration (Microsoft Access SQL) Source: Microsoft Access 2010 SQL reference: http://msdn.microsoft.com/en-us/library/office/ff841695(v=office.14).aspx SQL Standards (SQL-86, SQL-89, SQL-92, SQL:1999, SQL:2003, SQL:2006, SQL:2008, SQL:2011) You need to use SQL view to use SQL codes in MS Access. First choose Create > Query Design and Close the Window because you don t have any tables. Then coose SQL View from Design Tab above. 6

SQL query for Students Table; CREATE TABLE Students ( studentid Long Primary Key, studentprogcode Text(20), studentname Text(70), studentsurname Text(70), studentdob DateTime ); Append a single entry to Students Table INSERT INTO Students VALUES (2014100, 'JDF', 'Alper', 'Esenli', '07.01.1989'); 2.2 Creating Tables in Design View 1. Create following tables with the given data by using design view. (Primary Keys for each table are showed on the figures) 2. Use the same table and field names to follow same procedure with the class. Students Table (Students) Tables in Design View Lecturer Table (Lecturer) 7

Faculty Table (Faculties) Programs Table (Programs) Courses Table (Courses) Courses of Lecturers (LecturerCourses) (Junction Table) Student Courses 2.3 Creating Relationship Between Tables Creating relationships between tables is an important process for database systems, by the help of these relationships, you can link tables and prevent repetition of data. To create relationships between tables; 1. Open tables which you will create relationships between eachother 2. Click Relationships from Database Tools menu 3. Select all tables from Show Tables window 4. Choose the common field from one table and drag the same field of the other table to set up relationships between them. 8

You already know that there are three types of relationships; 1.) One-to-One: You have a one-to-one relationship when a single record in one table is related to a single record in another table, and vice versa. 2.) One-to-Many: You have a one-to-many relationship when one record in a table is related to many records in another table. 3.) Many-to-Many: You have a many-to-many relationship when several records in one table are related to several records in another table. While creating relationships check the (enforce referential integrity) option on relationship window. The purpose of referential integrity is to to prevent orphan records and to keep references synchronized so that you don't have any records that reference other records that no longer exist. You enforce referential integrity by enabling it for a table relationship. Once enforced, Access rejects any operation that would violate referential integrity for that table relationship. This means that Access will reject both updates that change the target of a reference and deletions that remove the target of a reference (Source: http://office.microsoft.com/tr-tr/access-help/ha010072597.aspx#bm9 ) Entity-Relationship Diagram of Student Otomation Database 9

2.4 Queries Creating Queries in Access Query features in Create menu will be used to create queries. You can create your queries by using three methods; Query Design, Query Wizard and SQL. Generally, we will use Query Design to create our queries within laboratory work. Query 1 Courses with program code JDF : To create this query, a single table Courses is selected. Select the Courses table in Show Table window and add courseprogcode, coursecrn and coursename fields to the design view. The criteria will be JDF for courseprogcode field which we are searching for the courses with JDF program code. Save and run the query. (Please consider using only Ascii characters while naming the queries before saving.) Once you save the query you will see the saved queries under All Accessible Objects window. The output of the query will be; 10

SQL for Query 1; SELECT Courses.courseProgCode, Courses.courseCRN, Courses.courseName FROM Courses WHERE (((Courses.courseProgCode)="JDF")); Query 2 Counting (Count,Group By,Having Clause) Number of Lecturers in INS Program Information About Having Clause : Specifies which grouped records are displayed in a SELECT statement with a GROUP BY clause. After GROUP BY combines records, HAVING displays any records grouped by the GROUP BY clause that satisfy the conditions of the HAVING clause. SELECT fieldlist FROM table WHERE selectcriteria GROUP BY groupfieldlist [HAVING groupcriteria] A SELECT statement containing a HAVING clause has these parts: Part fieldlist table selectcriteria groupfieldlist Description The name of the field or fields to be retrieved along with any field-name aliases, SQL aggregate functions, selection predicates (ALL, DISTINCT, DISTINCTROW, or TOP ), or other SELECT statement options. The name of the table from which records are retrieved. Selection criteria. If the statement includes a WHERE clause, the Microsoft Access database engine groups values after applying the WHERE conditions to the records. The names of up to 10 fields used to group records. The order of the field names in groupfieldlist determines the grouping levels from the highest to the lowest level of grouping. groupcriteria An expression that determines which grouped records to display. Source: http://office.microsoft.com/tr-tr/access-help/ha001231483.aspx 11

In this query we will try the find out the number of total lecturers in a program with program code INS. Once again, a single table Lecturers is selected. Select the Lecturers table in Show Table window and add the Primary Key field, lecturerid and lecturerprogid field to the design view. From the Show/Hide Tab click the Totals ( ) button. A new field named Totals will appear in query design view. For the lecturerprogid field select Group By option and type the criteria INS for this field. For lecturerid field select the Count option. The design of the query 2 will look like this; SQL for Query 2; SELECT Count(Lecturer.lecturerID) AS CountOflecturerID, Lecturer.lecturerProgID FROM Lecturer GROUP BY Lecturer.lecturerProgID HAVING (((Lecturer.lecturerProgID)="INS")); Sorgu-2 (more): In addition to this query, we can also obtain the number of Lecturers with the title Prof.Dr. by adding extra criteria to HAVING Clause using AND operator. To achieve this process we have to add the extra field lecturertitle and Group by the criteria Prof.Dr. The design of the new query will look like this; 12

SQL for new query ; SELECT Count(Lecturer.lecturerID) AS CountOflecturerID, Lecturer.lecturerProgID, Lecturer.lecturerTitle FROM Lecturer GROUP BY Lecturer.lecturerProgID, Lecturer.lecturerTitle HAVING (((Lecturer.lecturerProgID)="INS") AND ((Lecturer.lecturerTitle)="Prof.Dr.")); Output of new query; 2.5 More Examples With Queries Querying with a criteria You can use many different criteria to create queries. There are some comparison operators that can use; > Greater than < Less than <> Not equal to <= Less-than or Equal to >= Greater-than or Equal to 13

Query 3 (Query more than one table) The list of students who selected the course with a specific CRN : In this section we will design a query to obtain the values with a given criteria from the tables in one-to-many relation The tables, Courses, Students and the junction table StudentCourses will be used. The specified CRN is 11193 Output of Query 3 ; SQL for Query 3; SELECT Courses.courseCRN, Students.studentProgCode, Students.studentID, Students.studentName, Students.studentSurname FROM Courses INNER JOIN (Students INNER JOIN StudentCourses ON Students.studentID = StudentCourses.studentID) ON Courses.courseCRN = StudentCourses.studentCourseID WHERE (((Courses.courseCRN)=11193)); Note: Notice the INNER JOIN syntax? 14

Query 4 Coursed between hours 09:00 and 13:00: In Access, to query the fields in datetime datatype we write our criteria between # sign, which indicates the field is in datetime data type. Output of Query. 15

Query 5 Lecturer with course out of his or her program: Output of Query 5; SQL for Query 5; SELECT Lecturer.lecturerTitle, Lecturer.lecturerProgID, Lecturer.lecturerName, Lecturer.lecturerSurname, Courses.courseProgCode, Courses.courseCRN, Courses.courseName FROM Courses INNER JOIN (Lecturer INNER JOIN lecturercourses ON Lecturer.lecturerID = lecturercourses.lecturerid) ON Courses.courseCRN = lecturercourses.lecturercoursecrn WHERE (((Lecturer.lecturerProgID)<>"INS") AND ((Courses.courseProgCode)="INS")); 16