workshop a exercises - diuf.unifr.ch · create select statements that answers the statements: -...

14
Mediamatics Workshop A 19.10.2007 Assistant: Darius Zumstein, [email protected] Edy Portmann, [email protected] Daniel Fasel, [email protected] 1. Exercise: Entity Relationship Model A company is producing and distributing advertisement for other companies. They offer to distribute the advertisement to most of the public media institutions in Switzerland. The company has already an Access Database with all the address information of the media institutions. Figure 1 shows the Relation Model of the current database in Microsoft Access. Figure 1 The company aims to extend the current database in a way that it includes: - The customers - The services (e.g. TV spot) delivered to a customer and - Which company published the service Create an Entity Relationship Model (not the Microsoft Access Relational Model) including the 3 extensions mentioned above.

Upload: others

Post on 13-Sep-2019

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Mediamatics Workshop A 19.10.2007 Assistant: Darius Zumstein, [email protected] Edy Portmann, [email protected] Daniel Fasel, [email protected]

1. Exercise: Entity Relationship Model A company is producing and distributing advertisement for other companies. They offer to distribute the advertisement to most of the public media institutions in Switzerland. The company has already an Access Database with all the address information of the media institutions. Figure 1 shows the Relation Model of the current database in Microsoft Access.

Figure 1 The company aims to extend the current database in a way that it includes: - The customers - The services (e.g. TV spot) delivered to a customer and - Which company published the service Create an Entity Relationship Model (not the Microsoft Access Relational Model) including the 3 extensions mentioned above.

Page 2: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

2. Exercise: Creating tables in Microsoft Access

Open the database file: MediaDB.mdb Figure 2 shows the initial window of the Media database

Figure 2 On the left hand side you can navigate through the tables, queries, forms, etc. In the main window you see the existing tables area, city, companies, language and sector. To create a new table, click on New and choose Design View. Figure 3 shows a window for creating a table in Design View. Here you can define the table field names with their data value and further characteristics (e.g. the primary key client_ID for the table client has the data value Integer, it is not allowed to be empty and no duplicates are allowed).

Page 3: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Figure 3 The primary key in Access can be defined by right clicking on the attribute (e.g. client_id) and choosing the primary key icon as shown in Figure 4.

Figure 4 To save the table click on the save icon in the upper left corner of Access (figure 5).

Figure 5

Create the necessary tables in Microsoft Access according the solutions of Exercise 1.

Page 4: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

3. Exercise: Extending the Relational Model in Microsoft Access

In Microsoft Access you can define the relations between the tables in the Relation Model. The Relation Model can be opened with the Relation icon in the upper part of Access shown in Figure 6

Figure 6 You will find the Relation Model of the old database structure (Figure 1). Additionally, the newly created tables are loosely included in the Relational model. To create a relation (e.g. between table client and table city) click on the primary key of the city table and drag it to the foreign key of the client table. A window appears, in which you can define the relation (Figure 7).

Figure 7 After creating the relation, it is shown in the Relation Model of Microsoft Access as in Figure 8.

Page 5: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Figure 8 Open the file: MediaDB_Tables.mdb Create the needed relations in the Relation Model for all the tables of Exercise 2.

Page 6: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

4. Exercise: Introduction into SQL and using SQL in Microsoft Access

Structured Query Language (SQL) is a standard language for querying relational databases. The example below queries all the names and phone numbers of the clients and orders them by name: SELECT Name, Phone FROM client ORDER BY Name; A more complex query is used when querying the all the clients living in a Place that includes the letter n in its name: SELECT client.Name, client.Address, city.ZIP, city.city FROM client, city WHERE client.city_ID=city.city_ID AND city.city LIKE "*n*" ORDER BY client.Name; To omit complex WHERE clauses you can do the relation part in the FROM clause using a JOIN: SELECT client.Name, client.Address, city.ZIP, city.city FROM client left join city on client.city_ID = city.city_ID WHERE city.city LIKE "*n*" ORDER BY client.Name; Additionally, SQL has extension to insert new data tuples in a table. In the following example we will insert the city StUrsen with the ZIP code 1718 into the table city: INSERT INTO city ( ZIP, city ) VALUES ("1718", "StUrsen"); To update an already existing data tuple SQL provides the UPDATE statement: UPDATE city SET ZIP = 1717, city = "St. Ursen" WHERE ZIP=1718; Finally, SQL allows deleting a tuple with the DELETE statement (using the wildcard * deletes the whole tuple): DELETE * FROM city WHERE city="St. Ursen"; In Microsoft Access you can create SQL queries in the Queries section (see Figure 2). To create a SQL query, click on New, choose Design View, click on close in the small window and then click on the icon SQL, as shown in Figure 9, in the upper left corner of Access.

Page 7: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Figure 9 You now get a window where you can enter your SQL statements (Figure 10).

Figure 10 Once entered the SQL statement, when clicking on the table icon in the upper left corner of Access (Figure 11) gives the result table of the SQL Statement (Figure 12).

Figure 11

Page 8: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Figure 12 Here you can find a really good tutorial for SQL: http://www.w3schools.com/sql/default.asp

Create SELECT statements that answers the statements:

- Find all clients and their phone number. - Find all clients which lives in a city that has an „n“ in the name. - Find all companies which are not placed in Zürich - List all companies, the services delivered to this companies, the invoice Date

of the services, the URL and the e-mail address of the companies. - List all services, the clients that asked for the service, the companies where the

service was delivered and the language of the companies.

Create INSERT statements that insert the new data:

- Insert a new service: “Top Spot1”, “15.12.2007” (Invoice Date). - Insert a new client: “Max Mustermann”, “Schwandholz”, “232392389”. - Insert a new city: 1717 St. Ursen. - Insert a new relation Company – Service: company 22 and the newly created

service - Create 5 more clients & 5 more services.

Create UPDATE statements that updates the existing data:

- Update the address of “Max Mustermann” to “Dorf”. - Update the city with the ZIP 1717 to: 1718 Rechthalten. - Update the other 5 clients and services you created in Exercise 6.

Page 9: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Create DELETE statements to delete the data:

- The client Max Mustermann. - The city Rechthalten. - The other 5 clients and services you created in Exercise 6.

Page 10: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

5. Exercise: Using Forms in Microsoft Access

As more and more people starts using the database in the company, the company is looking for an easier way to access the data than browsing the tables or writing SQL statements. In particular for administrating clients and services a front-end is asked. The front end has to support people, with little SQL skills, maintaining the clients and services. Microsoft Access provides forms that can be used to access the database in a graphically. All the Forms are managed in the section Forms (see Figure 2). To create a Form, click on New, choose Form Wizard and choose a query or table (e.g. Appl1 Client Full Address) that the form should use. In the first screen of the wizard, you can choose which fields of the query/table you will use in the form (see Figure 13).

Figure 13 The next two screens (Figure 14 & 15) let you select the layout of your form.

Page 11: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Figure 14

Figure 15 After choosing the name of the form, you can access your first draft version (Figure 16) of the form, in which you can select the clients graphically and edit them easily.

Page 12: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

The navigation in the lower left end of the form is the standard navigation of Access forms.

Figure 16 The standard form allows the user to edit the data fields. In order to use this form as a read only view, the fields have to be edited. Therefore we have to switch in the Design view of the form. The icon on the upper left side of Access shown in Figure 17 is used to switch into the Design View.

Figure 17 To change the read write properties of a field, do a right click on the field and choose Properties. The property window of the field appears (Figure 18).

Figure 18 When choosing the property Locked = no the field will become a read only field. Additionally, to have a more comfortable way to choose the clients a combo box is inserted into the form. The combo box can be chosen from the Toolbox (Figure 19).

Page 13: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Figure 19 After dragging the combo box into the form a wizard will you guide through the configuration of the combo box. In the first screen of the wizard choose “Find a record on my form based in the value I selected in my combo box” (Figure 20).

Figure 20 Select in the next screen name as field and give the combo box a name in the last screen. With this combo box the user can now select a client by choosing the clients name (Figure 21).

Page 14: Workshop A Exercises - diuf.unifr.ch · Create SELECT statements that answers the statements: - Find all clients and their phone number. - Find all clients which lives in a city that

Figure 21 Open the file: MediaDB_Queries.mdb Build the forms that allow you to:

- Administrating all the clients and the necessary client information (address, city, phone…). Use the query “Appl1 Client Full Address”

- Creating new clients. Use the query “Appl2_ Insert Client”. - Administrating the services and the necessary information (invoice date,

client…). Use the query “Appl3 Service”. - Creating new Services. Use the query “Appl4 Insert Service”.