atabases in ngineering / lab1 · 2014. 11. 5. · database management system (dbms) is a set of...

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

Upload: others

Post on 01-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

COVER PAGE

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

2014 – 2015 Fall

Page 2: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

1

Page 3: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

2

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

Page 4: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

3

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

Page 5: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

4

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.

Page 6: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

5

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)

Page 7: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

6

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.

Page 8: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

7

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.

Tables in Design View

Students Table (Students)

Lecturer Table (Lecturer)

Page 9: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

8

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.

Page 10: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

9

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

Page 11: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

10

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;

Page 12: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

11

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 Description

fieldlist

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.

table The name of the table from which records are retrieved.

selectcriteria Selection criteria. If the statement includes a WHERE clause, the Microsoft Access database

engine groups values after applying the WHERE conditions to the records.

groupfieldlist 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

Page 13: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

12

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;

Page 14: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

13

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

Page 15: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

14

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?

Page 16: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

15

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.

Page 17: atabases in ngineering / Lab1 · 2014. 11. 5. · Database Management System (DBMS) is a set of programs that enables you to store, modify, and extract information from a database,

16

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"));