Interactive Powerpoint_How to Master effective communication
Rekayasa perangkat lunak(Jilid3)
1. Aunur R. Mulyanto
SOFTWARE
ENGINEERING
Book 3
Vocational Education
Directorate Technical and Vocational Education
Directorate General of Management Education Basic and
Middle
Department of National Education
Copy Right@Department of National Education
All right reserve
2. SOFTWARE
ENGINEERING
Book 3
For Vocational Education
Author : Aunur R. Mulyanto
Cover Design : Team
Book Size : 17,6 x 25 cm
MUL MULYANTO,Aunur R.
Software Engineering Book3 For SMK / by Aunur
R. Mulyanto ---- Jakarta : Directorate Technical and Vocational
Education
Directorate General for Basic and Middle Education
Department of National Education, 2008.
xii. 163 pages
Libraries : A1-A2
Glosarium : B1-B6
ISBN : 978-979-060-007-2
ISBN : 978-979-060-008-9
Published by:
Directorate Technical and Vocational Education
Directorate General Primary and Secondary Education
Department of National Education
2008
4. FOREWORD
We thank to Allah SWT, blessing and gift of His mercy, the Government, in this case,
the Directorate of Development of Vocational High School of Directorate General of
Primary and Secondary Education Management, Ministry of Education, has conducted
the activities of the book trade as a form of purchase of copyright books text for
students learning vocational SMK. Because textbooks vocational very difficult to get in
on the market. Text book lesson this has been through the process of assessment by
the National Education Standards as a text book lesson to SMK and have been
declared eligible to be used in the feasibility of the learning process through the
Ministry of National Education Regulation No. 45 Year 2008 on 15 August 2008.
We delivered the award at a high level to all the writers who have been well switch
copyright works to the Ministry of National Education to be used widely by teachers
and students SMK. Text book lessons that have been transferred the copyright to the
Ministry of National Education is, can be downloaded , duplicated, printed, media
transfer, or photocopy by the community. But for the multiplication of commercial sales
price must meet conditions set by the Government. With the soft copy of this show will
be easier for the public especially teachers and students of vocational school (SMK) in
Indonesia as well as abroad to access and utilize as a reference.
We hope all parties can support this policy. To the students good luck in your study and
hopefully can use this book as well as possible. We realize that this book still needs to
be improved quality. Therefore, suggestions and criticisms are we expected.
Jakarta, 17 Agustus 2008
Director of Technical and Vocational Education
6. vi Softwae Engineering
EDITORIAL
With all humility, we give thanks to Allah SWT. Because only with patronage, blessing
and gift of His mercy , then this book can be completed.
Book entitled 'Software Engineering' is organized to meet the needs handbook for
students Vocational Education. Especially on the program expertise Software
Engineering. This book includes a description refers to the standard of competence
and basic competencies for Software Engineering students from SMK class X, XI to
class XII.
Each chapter contains a theory must be understood correctly by students and
accompanied by examples of problems that are relevant to the theory. In addition there
is also a matter that is based on the concept and discussed the theory test as a tool to
measure the ability of students in the control of these materials.
In developing this book, the author seeks to be the material that is presented in
accordance with the needs of competency to be achieved. Therefore, apart from the
result of thought and experience as writers and practitioners of Software Engineering,
which developed the material with other appropriate reference
In this moment I wish to thank to all parties that support this book can be published.
Hopefully this book can be useful for students in developing capabilities. The author
realized that this book still needs to be developed continuously, so that the suggestions
from various parties, this book is useful.
The Author,
Directorate Technical Vocational Education
7. vii Softwae Engineering
Table of Contents
FOREWORD......................................................................................................iv
EDITORIAL........................................................................................................vi
HOW TO USE THIS BOOK..............................................................................ix
CHAPTER 11. DATABASE APPLICATION OF MICROSOFT ACCESS....1
11.1. DATABASE APPLICATION MENU ................................................2
11.2. TABLE.................................................................................................9
11.2.1 Field and Data Type Definition .......................................................9
11.2.2. Data Entry To The Table...............................................................13
11.3. QUERY ................................................................................................15
11.3.1. Relations between tables ..............................................................15
11.3.2. Making Query ..............................................................................17
11.4. FORM ..................................................................................................27
11.4.1. Create Form .................................................................................28
11.4.2. Modification of the Form .............................................................34
11.5. REPORT ..............................................................................................40
11.6. SUMMARY ........................................................................................49
11.7. EXERCISES ........................................................................................50
CHAPTER 12 SQL BASED DATABASE........................................................51
12.1. An Overview of Structured Query Language (SQL)............................52
12.1.1. Data Definition Language (DDL) ................................................52
12.1.2. Data Manipulation Language (DML)...........................................52
12.2. PREPARING SOFTWARE IN SQL BASE .....................................53
12.2.1. Operation Requirements...............................................................53
12.2.2. Operation of SQL based Database software ................................54
12.3. MAIN MENU / FEATURES.............................................................56
12.4 . CREATION AND DATA ENTRY IN TABLE....................................58
12.4.1. Database Creation.........................................................................58
12.4.2. Table Creation..............................................................................59
12.4.3. Fill in Data into Table...................................................................60
12.5. ABLE VIEW AND OPERATION........................................................61
12.5.1. Relation Between Tables ..............................................................61
12.5.2. View Creation...............................................................................62
12.6. T-SQL USAGE.....................................................................................68
12.6.1. Defining Table using T-SQL ........................................................69
12.6.2. Fill in, Change and Remove Table contents by SQL ...................72
Directorate Technical Vocational Education
8. viii Softwae Engineering
12.6.3. Search and View Data by SQL ....................................................74
12.7. FUNCTION, PROCEDURE DAN TRIGGER .................................78
12.7.1. Function .......................................................................................78
12.7.2. Procedure and Stored Procedure ..................................................81
12.7.3. Trigger ..........................................................................................83
12.8. ADMINISTRASION SQL SERVER....................................................83
12.8.1. Security and Authentication..........................................................83
12.8.2. Permissions ..................................................................................85
12.8.2. Permissions ..................................................................................85
12.9. SUMMARY ........................................................................................86
12.10. EXERCISE.........................................................................................87
CHAPTER 13 DESAIN WEB STATIS DAN HTML ......................................88
13.1. BASIC CONCEPT AND WEB TECHNOLOGY ..............................89
13.1.1 Web Technology Standard.............................................................89
13.1.2. Static and Dynamic Web. .............................................................90
13.2. PREPARATION TO CREATE A WEB ..............................................91
13.2.2. The Hardware ...............................................................................91
13.2.3. The Software.................................................................................91
13.3. CREATION AND EVALUATION OF WEB PAGES .......................97
13.4. HTML ................................................................................................100
13.4.1. Understanding the HTML ..........................................................100
13.4.2. General File Structure in HTML Language................................103
13.4.3. Document Format ......................................................................107
13.4.4. Adding Objects ...........................................................................112
13.4.5. Table............................................................................................114
13.4.6. Link between Documents............................................................118
13.5. SUMMARY .......................................................................................119
13.6. EXERCISE.........................................................................................119
CHAPTER 14 DYNAMIC JSP BASED..........................................................121
14.1 THE BASIC OF DYNAMIC WEB ....................................................122
14.2 SUMMARY.........................................................................................149
14.3 EXERCISE .........................................................................................149
REFERENCES.................................................................................................151
APPENDIX 1 GLOSARY..............................................................................153
APPENDIX 2 SITE LIST................................................................................159
APPENDIX 3 FUNCTIONS BUILD-IN ON VISUAL BASE.......................161
Directorate Technical Vocational Education
9. ix Softwae Engineering
HOW TO USE THIS BOOK
A. Global Description
This book is given the title quot;Software Engineeringquot;, together with one of the program
expertise in the Vocational School (SMK). However, the actual contents of this book is
not specifically discuss on Software Engineering. From the perspective of Computer
Science field of five sub-areas covered in this book, the sub-field of Engineering
Software, Operating System, Algorithm and Data Structure, Programming Languages
and Database. This curriculum tailored to the level of expertise SMK Program for
Software Engineering.
Main subject of Engineering Software in general, describe the basics of Software
Engineering, and solving problems, and methods of software development. The
discussion about the sub-field of Operating System contains a computer system, the
system operating and working in the computer network. The scope of the material
basic algorithms include algorithms and advance algorithms. Sub field Programming
Languages take a big portion, including GUI programming with VB & VB.Net, Java
programming, C + + programming, Programming of object-oriented and web-based.
Sub-sector which is the last part of this book is a data base with the scope of the
system databases, conceptual modeling, relational database, Microsoft Access and
SQL.
B. Competency Map
In general, this book refers to the Standard and Competence
Basic Competency (SKKD) for Vocational School (SMK) as follows :
1. Using a basic level of programming algorithm
2. Using advanced programming algorithm
3. Operate the database application
4. Create an application-based Microsoft Access
5. Mastering the basic techniques electronics
6. Mastering the digital electronics technology
7. Create file with HTML according to specifications
8. Applying the basics of creating a basic level of static web
9. Make the application using VB and VB.NET
10. Make the software application package
11. Doing programming data description (Structured-SQL Query Language) basic
level
12. Operates programming data description language (SQL) advanced
Directorate Technical Vocational Education
10. x Softwae Engineering
13. Create a web page dynamic basic levels
14. Create a web page more dynamic level
15. Create a web application program using JSP Software Engineering
16. Make the application database using XML
17. Make a database program using Microsoft (SQL Server)
18. Make a database program using PL / SQL (Oracle)
19. Make the application using C + +
20. Explaining the system peripherals
21. Make the program in object-oriented programming language
22. Make the application using Java
23. Operate the computer operating system and text-based GUI
In this book, chapters not compiled based on SKKD, but the materials are developed
based on the sequence of basic subject. So that in some chapters contain a mix of
several standards of competence. Or a basic competency may not be on the
competency standards groups such as SKKD on the list, but is on the other sub-
chapters.
SKKD suitability and content of the chapter can be seen in the table below :
Competency Code Competency Relation
ELKA-MR.UM.001.A Mastering the basic techniques electronics 3
ELKA.MR.UM.004.A Mastering the digital electronics technology 3
and Computer
TIK.PR02.001.01 Using a basic level of programming algorithm 5
TIK.PR02.002.01 Using advanced programming algorithm 6
HDW.OPR.103.(1).A Operate the computer operating system and text- 4
based
HDW.OPR.104.(1).A Operate the computer operating system based 4
GUI
TIK.PR02.020.01 Operate the database application 10 and 11
TIK.PR08.004.01 Create an application-based Microsoft Access 11
TIK.PR08.024.01 Create file with HTML according to specifications 13
TIK.PR08.027.01 Applying the basics of creating a basic level of 13
static web
TIK.PR08.003.01 Make the application using VB and VB.NET 7
TIK.PR02.016.01 Make the software application package 7
TIK.PR03.001.01 Doing programming data description (Structured - 12
Directorate Technical Vocational Education
11. xi Softwae Engineering
SQL Query Language) basic level
TIK.PR03.002.01 Operates programming data description language 12
(SQL) advanced
TIK.PR04.002.01 Create a web page dynamic basic levels 13
TIK.PR04.003.01 Create a web page dynamic advance levels 13
Competency Code Competency Relation
TIK.PR02.009.01 Operate the program in object-oriented 8
programming language
TIK.PR08.012.01 Make application program using Java 8
TIK.PR08.001.01 Make application program using C++ 9
TIK.PR06.003.01 Describe Peripheral system 3
TIK.PR08.005.01 Make database using PL/SQL 10 and 12
TIK.PR08.006.01 Make database program using SQL server 12
TIK.PR08.008.01 Make JSP based we application web program 14
C. How to use this book
This book is specifically aimed at students and teachers for the vocational school
(SMK) expertise in RPL. However, this book is also open for general readers who are
interested in the RPL, Algorithm and Programming, Database and the Internet. For
students, this book can be a handbook, because this book is to provide learning
materials that are complete enough for the subjects for three years at the school.
Some parts of this book may need books to help enrich more insight and capacity
building. Whereas for teachers, this book can be used as reference books to prepare
teaching modules for the students.
This book is structured in such a way so that students can independently learn and
encourage to try. Therefore, in this book, many will be found either in the form of
illustration image, the scheme and program listings. This is so that students can easily
understand the explanation or the application of a particular concept. The chapter will
be ended with exercise of the subject.
Directorate Technical Vocational Education
13. 1 Sftware Engineering
CHAPTER 11. DATABASE APPLICATION OF MICROSOFT ACCESS
Figure 11.1. Microsoft Access 2007.
The above Figure shows the starting windows of the newest Microsoft Access version
2007. The software is part of Microsoft Office Suite that rarely used but available in
Microsoft Office package and is very useful.
This chapter discuss about two standard of competences. First is operating an
application database and, second, making the application based on Microsoft Access.
The first standard consists of three (3) basic competences, namely, explaining the
application menu of the database, making table and creating view or query. And the
second standard consists of four (4) basic competences, namely, explaining Database
Management System, describing the Data Definition Language, applying query and
reporting. Database Management System is explained in the Chapter 10, and
Definition Language Data is in the Chapter 12. It is important to review the principle of
problem solving, operating system, and the foundation of the database in the previous
chapters.
Directorate Technical Vocational Education
14. 2 Sftware Engineering
OBJECTIVES
After reading this chapter, the readers are hoped to be able to,
Explain the general menu of database application.
●
Create table.
●
Create and apply View / Query.
●
Create Form
●
Create Report
●
11.1. DATABASE APPLICATION MENU
As mentioned in the previous chapters, Microsoft Office 2007 is Microsoft's DBMS.
The latest version of Microsoft Access is version 2007 which is part of Microsoft Office
2007. The default data format for the recent version is different from the previous
version. The previous file extension is .mdb, whereas the current extension is .accdb.
The recent version database file may not be read by the previous version. However,
the recent version may read the database file from the previous version.
To start the Microsoft Access, click Start on Windows select Programs then Microsoft
Access. The first Windows of Microsoft Access is shown in Figure 11,2. Microsoft
Access has several options in creating a database as shown in Figure 11.2. The mostly
use database option is Blank database. If we click this section, then a request window
to define the database will appear as shown in Figure 11.3. Make sure that the name
and the location of the storage is confirm.
Directorate Technical Vocational Education
15. 3 Sftware Engineering
Figure 11.2. First showing of the Microsoft Access.
Figure 11.3. The specific name and location of the database.
Directorate Technical Vocational Education
16. 4 Sftware Engineering
When we press the Create button meaning we create a database. However, there is
no table or data in it as shown in Figure 11.4. We can see the name file of the
database is latihan 01 and the database format files uses Access 2000. In the Figure is
shown the database objects.
Figure 11.4. Parts of a Database of the Microsoft Access.
There are six (6) important Access objects in this DBMS, namely,
Table. It is the place where we keep the data. All the tables in Access follow
●
the database relational rules with lines and column. Each database may
contain more than one table.
Queries. Queries feature is provided to choose the data to be shown. While
●
queries of the Access is available both GUI and SQL language.
Forms. Form feature is provided to create Interesting appearance of the
●
database. Both while editing as well as output the data on the screen.
Reports. This feature is provided to create to make printing layout for printer.
●
Macros. Macro feature is usually used to keep an automatic specific
●
Directorate Technical Vocational Education
17. 5 Sftware Engineering
order related to the created database. Programming ability is needed to
be able to use this facility.
Modules. This feature is more elaborated than macros and provides
●
many programming aspects in Microsoft Access.
In addition to the main objects, Microsoft Access also provides a set of tools to support
and make easy in using the database and its applications. The following is some
figures of the the tool bar in the Microsoft Access as well as its usage.
Figure 11.5. Tool bar on menu Create.
Figure 11.6. Tool bar on menu External Data.
Directorate Technical Vocational Education
18. 6 Sftware Engineering
Figure 11.7. Tool bar on menu Database Tool.
In this book, we will observe four (4) objects, namely, table, query, form and report.
However, prior to describe how to use these objects in the Microsoft Access, we will
briefly discuss a database case sample. It is Database of the Book Sale. In this case,
each book buyer will purchase a book and will pay suitable amount of money for the
book. The buyer's data will be recorded. In addition, each data, both summary and
detailed items are also recorded. The data contains the buyer who do the order, the
total purchase order and the date of order. Whereas the item data contains the ordered
book data and the number of each order.
For the above case, we make ER Diagram, the first step is to identify of the candidate
of involved entities. From the text, we identify there is minimum of four (4) entities
candidate, they are the buyer, the book, the order and the item ordering. Whereas the
identified relations are as follows.
● the buyer do the ordering
● each order contain items
● each item contain the list of books order.
Attributes for each entity candidate describes as follows.
● The buyer entity is attributed with id_pembeli, nama, alamat, and telepone.
● The book entity is attributed with ISBN, pengarang, and judul
● The order entity is attributed with id_pesanan, jumlah pembelian, tanggal
pembelian.
● The purchase entity is attributed with total books order.
After all information is completed then we may draw the ER Diagram for that case as
shown in Figure 11.8.
Directorate Technical Vocational Education
19. 7 Sftware Engineering
Figure 11.8. ER Diagram for the case of the book sale Database
Base on the ER Diagram, we may decide the needed tables for book sale database.
There are four (4) tables: pembeli, buku, pesanan, and item pesanan. To
accommodate relations in ER Diagram, then we use attributes to create the
relationship. Table 11.1 shows the attribute in the book sales database.
Table 11.1. Table and attribute in book sale database.
Table Attribute Comments
Customer Customer ID Customer ID is the primary key due to its
uniqueness.
Name
Address
Telephone
Book ISBN ISBN is the primary key due to its uniqueness
Author
Title
Price
Directorate Technical Vocational Education
20. 8 Sftware Engineering
Order Order ID Order ID is the primary key due to its
uniqueness
Customer ID Purchase ID is the foreign key to link the buyer
to the order. This is to relate the buyer who do
the order.
Amount of
Purchase
Date of Purchase
Order Item Order ID Order ID and isbn are both the primary key.
IDBN Order ID is the foreign key to relate order to
Order item. While IDBN relates to the book in
the Order Item
Quantity
Table 11.1 should be completed with data type and constraint / domain such as in
Table 11.2. This is to make it easer to create table in DBMS. While we check whether
tables are in normal form or not, then we will see all tables in the third normal form
(3NF).
Directorate Technical Vocational Education
21. 9 Sftware Engineering
Table 11.2. Table, attribute, data type, and constraint / domain in the book sale
database.
Table Attribute Data Type Constraint
Customer Customer ID Integer Not Null
Name Char/Text (30)
Address Char/Text (60)
Telephone Char/Text (15)
BOOK ISBN Char/Text (15) Not Null
Author Char/Text (30)
Title Char/Text (50)
Price Real/Float (10,2)
Order Order ID Integer Not Null
Customer ID Integer Not Null
Amount of Purchase Real/Float(10,2)
Date of Purchase Date
Order Item Order ID Integer Not Null
IDBN Char/Text (30) Not Null
Quantity Integer Not Null dan >0
11.2. TABLE
When you finish create the database file, please see Figures 11.3 and 11.4, we have a
database file but empty as we have not create tables in the database. As explain in
Chapter 10, the table contains column and row. Column is called field while row is
called record in Microsoft Access.
11.2.1 Field and Data Type Definition
The first step in creating the table is defining the field then fill up data rows. Steps are
as follow:
1. In the Database window click the Table (Figure 11.9).
2. Double click to Create table in Design View (Figure 11.9). After that, the
window appears to define fields as we need (Figure 11.9)
Directorate Technical Vocational Education
22. 10 Sftware Engineering
Figure 11.9. The First Step in Table Creation
3. We may begin to create the needed fields. For example, we will put the fields
for table Pembeli as defined in Table 11.2 Please examine Figure 11.10.
Directorate Technical Vocational Education
23. 11 Sftware Engineering
Figure 11.10. Field Definition and the Type of Data, Constraint and Domain.
Microsoft Access provides good facilities to define fields in a table. Figure
11.10 shows the process of defining field. The upper part is to determine the
name of the field, the data type and information. Whereas the lower side is the
place to define the data width, the format, the domain or constraint from a data
type. In this figure, we can see that the field nama is definite as text data, with
the field size is 30 (see the picture underside).
4. After all field for table pembeli is completely defined, then we must determine
which field should be the primary key. As described earlier, id_pembeli is the
primary key. Select id_pembeli, then click key simbol, as shown in Figure
11.11, in the Microsoft Access tool bar.
Directorate Technical Vocational Education
24. 12 Sftware Engineering
Figure 11.11. Toolbar Microsoft Access.
5. We can save the table and give the name by pressing the diskette button as
shown in Figure 11.11. Then we can close the Design View window. In the
same way, other tables, such as, table buku, table pesanan, and table
item_pesanan, may be defined. These pictures show the results of a complete
definition of the tables.
Figure 11.12. Structure of table pembeli
Figure 11.13. Structure of table buku
Directorate Technical Vocational Education
25. 13 Sftware Engineering
Figure 11.14. Structure of table pesanan
Figure 11.15. Structure of table item_pesanan
11.2.2. Data Entry To The Table
The newly created tables is still empty. It has only the structures. It is very easy to fill in
the data, just double click the table's name and fill the data in. After the window
opened as shown in Figure 11.16, we could fill in the data. The steps to fill in the data
is as same as if we work with Microsoft Excel or other spreadsheet software. We can
save the results of the data by pressing the diskette button in the tool bar.
Figure 11.16. Result of the data in table pembeli.
Figure 11.16 shows the contents of the data of the table pembeli. Using the same way,
we can fill the data in other tables. Examine the results of the data filling in below
Figures.
Directorate Technical Vocational Education
26. 14 Sftware Engineering
Figure 11.17. Result of the fill data in table buku.
Figure 11.18. Result of the fill data in table pesanan
Figure 11.19. Result of the fill data in table item_pesanan
Directorate Technical Vocational Education
27. 15 Sftware Engineering
11.3. QUERY
In this section, we will apply query theories that we have explained, Query is basically
“data request”. With query, we can show the data from one or more tables, or do data
calculation from the table. It would be better to understand table relations so that make
it easier to study on query.
11.3.1. Relations between tables
In Figure 11.8, we define relations between the entities in ER Diagram. The Figure is
the base to create relation between tables. To create relation between tables, select
menu Tools then relationship so that Windows as Figure 11.20 is shown. At the
window relationships – do right click – selection menu will be shown and select Show
Table. After window similar to Figure 11.21 is shown, we can decide which table to
make the relationships.
Figure 11.20. Relationships Windows. Figure 11.21. Show Table Windows
In Figure 11.21, select table to create the relationship then click Add button. After
completing the selection process click Close button. In our case, the four (4) tables are
directly or indirectly connected. We chose all the tables to make the relationship.
Figure 11.22 shows the result of the table selection in the Relationships window. In the
Figure is shown the structure of each tables, not the content. Column name is boldly
printed is the primary key. We can manage table position by drag & drop.
Directorate Technical Vocational Education
28. 16 Sftware Engineering
Figure 11.22. Tables Relationship
To make the relations between the table, we can chose primary key on a table then
drag the mouse to the key with the same name in other table (foreign key to the other
table). For example, in table pembeli, with primary key – id_pembeli and in table
pesanan, id_pembeli is foreign key. Click id_pembeli in table pembeli then drag the
mouse towards id_pembeli in table pesanan. If the procedure is correctly performed,
the windows as shown in Figure 11.23 will be shown. Please click on the Enforce
Referential Integrity and also click on Create button. We can do the same procedure
with other relations. So that the Relationships window will be similar to Figure 11.24.
Figure 11.23. Windows to Edit Relationships
Directorate Technical Vocational Education
29. 17 Sftware Engineering
Figure 11.24. Relations to All Tables
Figure 11.24 shows the relations between the tables and can be compared with ER
Diagram in Figure 11.8. In the table relationship, we may decide cardinality between
tables. Please note the line connecting table pembeli with table pesanan. At the table
pembeli end it is marked 1, while at the table pesanan end, it its marked by ∞. It means
one- to-many relations between the table pembeli and table pesanan. Similarly with the
relation to other tables.
11.3.2. Making Query
Microsoft Access provides query facility to make the user easier. As it is GUI based, the
procedure is fairly simple. Here are some samples in making query in Microsoft
Access.
Query in one table
●
Query in one table only involves in one table. For example we will do query to
the table buku. The procedure is as follows:
1. At the Database window select Query then click twice on Create Query
In Design View. The screen as shown in Figure 11.25 will be shown.
Directorate Technical Vocational Education
30. 18 Sftware Engineering
Select table buku on window Show Table, click Add then Close. The
Show Table window will close and we begin to do query.
Figure 11.25. Windows query at mode design view
2. In Query window as shown in Figure 11.25 underside, there are
several important matters and useful in query, namely,
Field : Field Name to show
Table : Table name of the Field
Sort : Sort the Query results
Show : Manage Field to be shown or not.
Criteria: the Condition of the data to be shown.
3. Example, the first query how to show all the data, such as,
Show, all data in the table buku.
To show all data in table buku, on the Filed, drag down the arrow and
click on buku. * (see Figure 11.26). To execute query, we can select
the Query menu then click Run, or directly click “!” button in Toolbar.
The query results can be seen in Figure 11.26. Keep query by any
name, e.g. query_of_all_book_data, then close the Query window.
Directorate Technical Vocational Education
31. 19 Sftware Engineering
Figure 11.26. Procedure and result Query of table book.
4. Example: the second query to select certain columns to be displayed,
such as,
Show the name of authors and the book title.
The Query does not show all the data but only column of pengarang
and judul. Figure 11.27 shows how to do the query. The table buku is
selecte from window Show Table, then in Query window, in the Field
section select Field pengarang and judul. The query results will be all
the author and the title of the book.
Directorate Technical Vocational Education
32. 20 Sftware Engineering
Figure 11.27. Query of the writers and the titles
5. Example: The third query, how to show a selected / specific data, such
as,
Show all the titles of the book and its price, and sort from the
expensive to cheapest.
This query is not to show all data, but only he title of the book and its
price. The print out must be sorted. Figure 11.28 shows how to do the
query. Table buku is selected from window Show Table, then in
window Query, in the Field section selects field judul and harga. In the
section Sort, in column harga, use option Descending to short from the
high to low price. The result of the query is the list of the most
expensive books to cheapest. Compare the list sequence to the query
result in Figure 11.26.
Directorate Technical Vocational Education
33. 21 Sftware Engineering
Figure 11.28. Query for price sorted book titles
Example: The fourth query, how to select certain row to be printed, such as,
Collect all books written by Harry Redknapp.
This query is using the data of field judul buku and pengarang.
However, not all book title will be used, only those written by 'Harry
Redknapp'. Figure 11.28 shows how to do the query. Table buku is
selected from window Show Table, then in window Query, in Field
section selects pengarang and judul. In the criteria section put in the
criteria, namely, = ‘Harry Redknapp’. Results of the execution query is
the lists books written by ‘Harry Redknapp’.
Directorate Technical Vocational Education
34. 22 Sftware Engineering
Figure 11.29. Query with some criteria
7. Example: the fifth query, how to use the operator in the criteria.
Collect all the titles of the books with writes are Harry Redknapp and
Arsene Wenger
Similar to no 6, this query only relates to field judul and pengarang.
However, not all title will be used only those written by ‘Harry
Redknapp and Arsene Wenger’. Figure 11.30 shows how to do the
query. Similarly to no 6, only at the criteria we put = ‘Harry Redknapp’
and ‘Arsene Wenger’. Results of the execution is unsatisfactory, Why?
Because there is no book written together by ‘Harry Redknapp’ and
‘Arsene Wenger’. What is the result, if we substitute and with or?
Examine Figure 11.31, there are three (3) appropriate records.
Directorate Technical Vocational Education
35. 23 Sftware Engineering
Figure 11.30. Query with and operator.
Figure 11.31. Query with or operator.
Directorate Technical Vocational Education
36. 24 Sftware Engineering
Query for more than one table
●
In query for more than one table, relations between tables is becoming very
important. These relations will impact on the query results. Procedure to make
the query is the same as query in one table, but the table at the window Show
Table may be more than one, depending on the requirement. Here is several
samples of query with more than one tables.
1. Example, the first query involves two tables, such as,
show name and address of the buyers for purchase more than
100000.
In this query, we need table pembeli as column nama and alamat of
the buyer is in table pembeli. We also need table pesanan as column
jumlah pembelian is in this table. From the relation between tables
shown in Figure 11.23, we know that there is a relation between table
pembelian and table pesanan with cardinality one-to-many. Procedure
to make a query is as follows,
In window Database selects Query and double click on Create Query
In Design View. Window Show Table will pop-up. In window of Show
Table selects table pembeli and pesanan as shown in Figure 11.32.
Click Add then close. Window Show Table closes then we can make
query.
Figure 11.32. Selection Table for query from two tables
Directorate Technical Vocational Education
37. 25 Sftware Engineering
As we select table pembeli and pesanan as shown in Figure 11.33,
Microsoft Access will automatically show the line of the relations
between these two tables. If we have not make any relations, the line
of the relations will not be shown. Figure 11.33 shows the Field for
column nama and alamat in table pembeli. While column
jumlah_pembelian is in table pesanan. In addition, in column
jumlah_pembelian, we add criteria, namely, must be more than
100000. The query execution shows that there are two buyers with
total purchases more than 100000.
Figure 11.33. Two Tables query
2. Example in the second query involves three tables.
Show the book title and its writers bought on 22 July 2007 or 25 July
2007.
In this query, we need table buku as column judul and pengarang is in
table buku. While column tanggal_pembelian is in table pesanan.
However from Figure 11.24, it is shown that there is no direct relation
between table pesanan and table buku. However, table pesanan has a
direct relation with table item_pesanan, and table item_pesanan has a
direct relation with table buku. All relations are cardinally one-to- many.
The query is as follow.
Directorate Technical Vocational Education
38. 26 Sftware Engineering
Select table buku, table item_pesanan and table pesanan in windows
Show Table. Click Add then Close. Figure 11.34 shows the relation
between the three (3) tables. The query results shows three (3)
records as shown in the Figure.
Figure 11.34. Three tables query.
3. Example, the third query involves all tables in the book sale database,
such as,
Show the name and address of buyers who buy the Theory of Modern
Football book.
In this query, we need table pembeli as column nama and alamat is in
table pembeli. We also need table buku, as column judul is in table
buku. From the table relation in Figure 11.24, there is no direct relation
between table pembeli and table buku, and must through table
pesanan and item_pesanan. Thus, we need four (4) tables to do the
query. Figure 11.35 shows how makes the query and produce the
Directorate Technical Vocational Education
39. 27 Sftware Engineering
three (3) records.
Figure 11.35. Four tables query.
11.4. FORM
Form is one of the database objects in Microsoft Access and use for data input /
output. For a beginner, to input data as Figure 11.16 up to 11.19 is quite complicated..
It would be better to use form. In Microsoft Access, there are three (3) models of form,
namely, data entry form (Figure 11.36 no. 1), switchboard form (no. 2) and custom
dialog form (no.3).
There are two ways to produce form in Microsoft Access; namely, using Wizard and
Design View. Wizard is the easiest as Microsoft Access help automatically creates the
form. While in Design View, we may manually design the form. We may also modify
the result from the Wizard using Design View.
Directorate Technical Vocational Education
40. 28 Sftware Engineering
Figure 11.36. Form types.
11.4.1. Create Form
In this section, we will create form using the Wizard. The first form to be made is to
input data for table Pembeli.
1. At Database window, click object Form, then double click on Create form by
using Wizard. The Form Wizard window will open as shown in Figure 11.37.
Figure 11.37. Open Form Wizard Window
Directorate Technical Vocational Education
41. 29 Sftware Engineering
2. In Form Wizard window, click on combo box Table/ Queries and select table
pembeli as shown in Figure 11.38, and click on button >> so all field in the
Available Fields moves to Selected Fields as shown in Figure 11.39. Then,
click Next to pop-up the window and select the model view form as in Figure
11.40.
Figure 11.38. Table Selection for Form
Directorate Technical Vocational Education
42. 30 Sftware Engineering
Figure 11.39. Selection Field for form.
Directorate Technical Vocational Education
43. 31 Sftware Engineering
Figure 11.40. Window for The Model View Form.
3. In Figure 11.40, there are several model view form options. We will try to
create using Columnar model. However, the reader may be freely try any
available models. Click on Next button to select the style as shown in Figure
11.41. In this windows, we may select our best suited form and completed by
clicking Next to pop-up Title form windows. Fill in the title form in the available
text box as shown in Figure 11.42.
Directorate Technical Vocational Education
44. 32 Sftware Engineering
Figure 11.41. The window to select style form.
Figure 11.42. The window to set the title form.
Directorate Technical Vocational Education
45. 33 Sftware Engineering
4. Click on Finish button as shown in Figure 11.42. Buyer's form is completed as
shown in Figure 11.43. The important things from this form is the navigation
button and the new record button. The function of The navigation button is
print and sort the data based on id_pembeli. If the add button is clicked, all text
in the Text box will emptied and new data may be filled up.
Figure 11.43. The Buyer's Form
Using the same way, we can create the other forms. For example the following
is the book form.
Directorate Technical Vocational Education
46. 34 Sftware Engineering
Figure 11.44. Book's Form.
11.4.2. Modification of the Form
Create Form with wizard mode gives two benefits, namely, easiness and faster in
creating one. On the other hand, it has a disadvantage as all formats are automatically
set. A good example as shown in Figure 11.43 and 11.44, the name of the Fields on
the form is made the same as the Field on the tables. Fortunately, Microsoft Access
provides facility to modify the form as follow.
1. In Database window, select Form. Then do right click on the name of the form
to be modified. Select Design View in pop-up menu to show the form to be
modified is shown as in Figure 11.45.
Figure 11.45. Window Buyer's Form in Mode Design View.
Directorate Technical Vocational Education
47. 35 Sftware Engineering
2. In Figure 11.45, we will modify the Buyer's Form. Prior to any modification, we
need to know the element / section of the form. Figure 11.56 shows the main
section of the form, namely, Form Header, Detail and Form Footer. Form
Header contains the title and the specific logo. The Detail section is the main
section, as all the Field is here. The Form Footer is optional. Form Footer
contains information, such as, the creator, the data entry operator, etc.
Figure 11.46. Section of the Form.
3. Microsoft Access uses the same controls as the GUI base programming
language as described in Chapter 13. In the form, we will find control Label,
Text Box, Combo Box, Command Button etc.
4. The first modification is to fix the view of the name fields. Click on the Label to
be fixed. Then change or substitute with new name. After all completed, the
preview of buyer's form will be show as Figure 11.47. Examine the label Fields
in Figure 11.45 and Figure 11.47.
Directorate Technical Vocational Education
48. 36 Sftware Engineering
Figure 11.47. Change the Label Fields in Buyer's Form
5. The next modification is to add switches to make easy for the user in filling up
the data. To do this we must arrange form as Figure 11.48. Then we add
switch by clicking the icon Command Button at Toolbar to place it in the Form.
The Command Bottom Wizards will be poped-up as shown in Figure 11.49. In
Categories select Record Operations and in Actions select Add New Record.
Click Next to continue. The next window as shown in Figure 11.50 select Text
and type Tambah then click Next. The following window as shown in Figure
11.51, type the name to this command button, e.g., cmdTambah, then click
Finish.
Directorate Technical Vocational Education
50. 38 Sftware Engineering
Figure 11.50. Define a Command Button text
Figure 11.51. Define a Command Button Name
Directorate Technical Vocational Education
51. 39 Sftware Engineering
6. We add button to delete data. We can do this in the same way as the
above procedure and use the Command Bottom Wizards window, in the
categories select Record Operations and select Actions to Delete Record
as in Figure 11.52. Set text and the name of the appropriate command
button.
Figure 11.52. Define a Command Button name
7. The final view of the modified form is shown in Figure 11.53.
Directorate Technical Vocational Education
52. 40 Sftware Engineering
Figure 11.53. Modified result of Buyer's Form
We may add more switches or controls using a fairly the same method.
11.5. REPORT
Report, as same as form, can use to show the data processing result into printer ready
information. We can always print directly from the database table, however, the printer
result may not be satisfactory. The best way to print a report is using the report model
provided by Report facilities. There are two (2 ) ways in making Report, namely,
manual or using wizards. Similar to form, the wizard provides an easy way to
automatically produce report. Here is a sample in making report in daily sale /
transactions. This report includes the transaction date, ISBN and the book title, the
book price and the total sale value per day / date.
1. To create this report, we cannot directly get the data from the table, as the
report is a combination from several tables. Thus, we must use query as data
source for the report. In figure 11.54. we select table pesanan, table
item_pesanan, and table buku. Then we select tanggal_pembelian, isbn, judul
and harga in the section Field. Then we store it in a file called
query_rpt_penjualan_harian (or other name if you like).
Directorate Technical Vocational Education
53. 41 Sftware Engineering
Figure 11.54. Query window for source report
2. In Database window select object Report and double click on Create Report by
using Wizard. The Wizard report window will be opened and in Tables/Queries
select query that has been created in no 1 (Figure 11.55). Then click button
>> so that all field in the Available Fields move to the Selected Fields, as
shown in Figure 11.56.
Figure 11.55. Selection query as the report data source.
Directorate Technical Vocational Education
54. 42 Sftware Engineering
Figure 11.56. Selection of involved fields.
3. Click Next to open the next window as shown in Figure 11.57. In this window
we decide the base preview report, whether it is by the table buku or table
pesanan. As we will create a daily report, so the review will be based on table
pesanan. Remember that field tanggal_pembelian is in table pesanan.
4. Click Next to continue to the next window as shown in Figure 11.58. This
window is used to grouping data in the same field. In this particular example,
we will not do any grouping so that nothing to be done in the window. Click
Next to continue to the next window.
Directorate Technical Vocational Education
55. 43 Sftware Engineering
Figure 11.57. Making window to viewing report
Figure 11.58. Making window to grouping data.
Directorate Technical Vocational Education
56. 44 Sftware Engineering
5. In Figure 11.59, we may select to sort the data or not. In this section, we will
sort the data based on its ISBN number than based on the book title. At
Combo Box no 1 select isbn and in Combo Box no 2 select judul. In addition,
in this section, we may set whether to create report summary or not. Click on
the Summary Options to open summary option window as shown in Figure
11.60. In the field harga, we can check on the box below the Sum. It means,
we will report on the number of purchases or daily transaction and total
transaction. Close the Summary Option window by clicking OK when we finish
it. Then click Next to continue the next window.
Figure 11.59. Window to set data sequence.
Directorate Technical Vocational Education
57. 45 Sftware Engineering
Figure 11.60. Window to manage the summary
6. Figure 11.61 shows the window to manage the report lay-out. We may try
various lay-outs. In this example, we use block and landscape orientation lay-
out. Then, click Next to show the report style window as in Figure 11.62. In
this section, we may select the appropriate style. Click next to open the final
window of Report Wizard. In the Text Box, we may set the report title. Click
Finish to complete the report creation process. The final report result is shown
in Figure 11.63.
Directorate Technical Vocational Education
58. 46 Sftware Engineering
Figure 11,61. Window to arrange lay-out and orientation
Figure 11,62. Window to arrange style the report
Directorate Technical Vocational Education
59. 47 Sftware Engineering
Figure 11,63. Report by Wizard
7. Figure 11.63 shows that the print-out report is unsatisfactory. The column title
is using the field name of the table. In addition, the summary wordings,
purchase data etc. are a little bit annoying in the view report. To fix it, do right
click on the report name in object report window then select Design View.
Window similar to Figure 11.64 will be opened.
Figure 11.64. Report in mode Design View mode.
Directorate Technical Vocational Education
60. 48 Sftware Engineering
8.
Figure 11.65. Re-Design Report
9. The first fix in this case is column title. We change it to be the same as Figure
11.65. To replace column title, double-click on the column title, and type the
new name. We also could arrange the column width by clicking in the highest
row to select the whole the column, bring the cursor to the right side of the
column to widen the column similar to Microsoft Word.
9. The second fix is to eliminate the Summary words for the date of purchase etc.
as shows Figure 11.64. Please click on the Summary section, press Del button
on keyboard. In addition, to delete Sum words and change Grand Total with
Total Sale. To add line select control line in Toolbar Report, then put it in the
selected position. The final Design View will be similar to Figure 11.65.
Whereas The Print Preview will be as in Figure 11.66.
Directorate Technical Vocational Education
61. 49 Sftware Engineering
Figure 11.66. Print Preview after Re-Design Report
11.6. SUMMARY
There are six important things in Microsoft Access objects; Table, Queries,
●
Forms, Reports, Macros and Modules.
Table in Microsoft Access consist of record (the row) and field (the column).
●
Table may be made by Field definitions with data type, domain and set the
primary key.
Query or the data request may show the data from one table or several related
●
tables. Query in Microsoft Access may be done by using GUI facilities.
Form is a database object in Microsoft Access that mostly use by beginner to
●
input or output data. Form may be made by Wizard method or manual Design
View.
Report is usually used to present processed data into printer ready
●
information. Report may be manually created or using Wizard facilities.
Directorate Technical Vocational Education
62. 50 Sftware Engineering
11.7. EXERCISES
1. Please see the tables above. Add one of the table named publisher. The
attributes are ID Publisher, the publisher name, the address, the city and
contact person. Please decide on the type data and data width for this table.
Fill in the tables with the publishers you know. You may want to check on your
books to check information on the publisher.
2. In the real condition, the publishers relates to the book. A publisher could
publish more than one book. However, one book may only published by one
publisher. Please create a relation between the new table between table buku
and table publisher. If needed, feel free to do some changes in table buku.
3. Please make queries below and attention for the results.
a. Show all the publisher's data.
b. Show the publisher's data from Bandung city.
c. Show the publisher's data with the book price above RP. 150.000,-.
d. Show the publisher which book buy by Cristiano Ronaldo.
e. Show the book which buy by Cristiano Ronaldo and Wayne Rooney.
4. Create form to input the publisher's data.
5. Create report to show the list of the publisher and publications.
Directorate Technical Vocational Education
63. 51 Sftware Engineering
CHAPTER 12 SQL BASED DATABASE
Figure 12.1. Hardware as Computer Server
Figure 12.1 shows computer servers used at internet service provider or corporate's
information technology department. One of the function of these equipments is to act
as user's data center. Thus, a strong database application or DBMS is needed. DBMS
tool such as Microsoft SQL Server, Oracle, MySQL, PostgreSQL is usually the main
choice. The additional functions of DBMS may help increase working speed, security
and data accuracy.
In this chapter, three (3) standard of competences, namely, basic Structured Query
Language (SQL), advanced SQL programming, and creation of database program for
Directorate Technical Vocational Education
64. 52 Sftware Engineering
Microsoft SQL Server. The last two (2) standard of competences is merged due to its
similarity. The chapter is structured indirectly refers to the basic competence, but more
towards the flow of materials. The summary is in the end of the chapter. Please review
the previous chapters on problem solving, basic database fundamental, and database
application using Microsoft Access.
OBJECTIVES
To Prepare and operate SQL software
●
To Know SQL application menu.
●
To Make and fill in table
●
To Operate table and view
●
To Use T-SQL
●
To Use fixtures Microsoft SQL Server such as procedures, functions, trigger
●
and XML support
To Administer a Microsoft SQL Server
●
12.1. An Overview of Structured Query Language (SQL)
DBMS provides language package language to organize the database, such as, the
Data Definition Language (DDL) and the Data Manipulation Language (DML).
12.1.1. Data Definition Language (DDL)
Data Definition Language (DDL) is one of language package in DBMS to specify the
database scheme. Results of DDL compilation is a table set stored in a special file
called Data Directory / Dictionary. In general, the DDL commands are related to the
creation of new database, drop database, new data table creation, index creation,
change structure tables. Examples of DLL are Create Table, Create Index, Alter, and
Drop Database,
12.1.2. Data Manipulation Language (DML)
Data Manipulation Language (DML) Is a DBMS package to provide user to access or
manipulate data to manage the data in a correct data model. DML may do the
following activities,
Take information from the database file.
●
Insert new information in database.
●
Delete information from table
●
There are two DML types, namely, procedural and non procedural. In procedural DML
, it needs user to specify the needed data and how to get it. While in non procedural
Directorate Technical Vocational Education
65. 53 Sftware Engineering
DML, it needs user to specif icy the needed data without knowing how get it.
SQL is short for Structured Query Language. SQL is closer to DML than DDL, it
doesn't mean SQL not provide DDL commands. SQL put more stress to search
capability in the table. Searching is an important aspect as it is the focus of data
management. Data in database is organized in such way to make it easier in
searching it.
As a language, SQL has standardized and several times changed for perfection. SQL
is for the first time found in 1970 by name Sequel (is being used until now). The first
standard is made in 1986 by ANSI (American National Standards Institute) and ISO
(International Standard Organization) called SQL-86. In 1989, SQL-86 is renewed into
SQL-89. The last standard is SQL-92.
The SQL statements is used for several tasks, namely, update data in database or
show ata from database. Several RDBMS using SQL are Oracle, Sybase, Microsoft
SQL Server, MySQL, Microsoft Access, Ingres, etc.. Each database software may have
different command / syntax, but in principle, it has the same meaning and function.
The main command in SQL is Select. The main command structure is as follows,
Select < column >
From < table >
Where < condition >
We will use above commands in the following section.
12.2. PREPARING SOFTWARE IN SQL BASE
As mentioned earlier, the software used is Microsoft SQL Server. In this chapter, we
will use Microsoft SQL Server use 2005 Developers Edition. Brief explanation of
Microsoft SQL is in Chapter 10.
12.2.1. Operation Requirements
Microsoft SQL Server is a software for Microsoft Windows platform, so automatically
using Windows operation system, especially the Server version and has good
integration. Also give better indication for security access and allocation work from
client-server model..
Microsoft SQL Server has to be activated as server to Windows. The default
installation, Microsoft SQL Server will be automatically installed, and it will
automatically run every time the Windows is starting. To make sure, please check the
Directorate Technical Vocational Education
66. 54 Sftware Engineering
Control Panel, and select Administrative Tool as shown in Figure 12.2. double click at
Service icon to open a window as shown in Figure 12.3.
Figure 12.2 Administrative Tool window
Figure 12.3. Services window
At Service window look for SQL server service then check the column status whether it
has been started or not. If not started, then start the service.
12.2.2. Operation of SQL based Database software
After the installation of Microsoft SQL Server and find GUI client as SQL Server
Management Studio. The screen will be shown as Figure 12.4.
Directorate Technical Vocational Education
67. 55 Sftware Engineering
Figure 12.4. SQL Server Management Studio
Every time opening SQL Server Management Studio, it will check whether we have
right to use SQL Server or not. If SQL Server is run on local machine and uses
Windows Authentication then we may press Connect to connect to the SQL Server
environment. However, if the SQL server is run on other machine / other server, it will
ask for user name and password.
Back to Figure 12.4, there are some important information. It shows that when we will
access server type Database Engine, I.e., a server that connects to a database
application. SQL Server provides several server types to be accessed but not all will be
explained in this book. In the Figure, the example server name is ARM- BLACKMATE.
As the SQL Server is installed in a computer named ARM-BLACKMATE and gives the
same name as the server for the SQL server.
Directorate Technical Vocational Education
68. 56 Sftware Engineering
12.3. MAIN MENU / FEATURES.
As we enter the SQL Server environment, SQL Server Management Studio will be
shown as in Figure12.5.
Figure 12.5. SQL Server Management Studio.
Figure 12.6. Object Databases
As shown in Figure 12.5, there are two important parts in SQL Management Studio,
namely, Object Explorer and Summary Panel. Object Explorer is a place where we see
object available in SQL Server. For the time being, the important object for us is the
Directorate Technical Vocational Education
69. 57 Sftware Engineering
Databases. While Summary panel is a place for information or summary of object
Explorer. Please click to one objects in Object Explorer, so Summary Panel's contents
will change according to our object,
We will focus to Databases object. Please click on the sign + (or sign node) in front of
Databases object as shown in Figure 12.6. In the Figure, there are four (4) available
databases in SQL Server, namely, Adventure Works, Adventure Works DW,
latihan01SQL and Northwind2007SQL. These four (4) databases are user object as
there are made by the user. SQL Server also make its own database during installtion
process. This database is normally called system object. Some of the available system
object are Master, Model, MSDB, Resource, TempDB, and Distribution. These
databases cannot be removed as it may create an unstable system. To check, please
click node in front of System Databases.
For the time being, we will not touch System Databases. We will focus on user object.
Click one of databases, see that screen Summary Panel change. Figure12,7 shows
database AdventureWorks contains various component Database Diagrams, Tables,
Views and others. Each database we make in SQL server will have these components
by default. Although we do not make Database Diagrams for example, they will be
there.
Figure 12.7. Database content in SQL Server.
Directorate Technical Vocational Education
70. 58 Sftware Engineering
12.4 . CREATION AND DATA ENTRY IN TABLE
12.4.1. Database Creation
Prior to any table creation, a database must be firstly created. Right click on
Databases and choose New Database. Window to create new database will be
opened as shown in Figure 12.8. In the textbox in front of Database name, type the
database name. In the example, the database name is Lat-01. Click OK. Examine the
Object Explorer, new database has formed and has same components as other
database.
Directorate Technical Vocational Education
71. 59 Sftware Engineering
Figure 12.8. Define new database.
12.4.2. Table Creation.
After database is created, we may create table. Right click on Tables component in
database Lat-01 and select New Table. Summary panel will be changed as shown in
Figure 12.9. You may start put in attribute or column table. The process as same as
Microsoft Access. Also for data type, data width and primary key are not too different.
The difference is SQL Server provides more data type than Microsoft Access. For the
primary key, select the attribute and press the sign key in toolbar.
Figure 12.9. Table Creation
In Figure 12.9, we create table of five (50columns, namely, Student No, Student Name,
Address, City, Telephone. With type nchar and varying width data. To fix the data
width, please see in the column property. Then save the table in certain name. In the
example, the table is save and named siswa. By default, SQL server will give name
prefix dbot. However, we can always change it. So table name will become dbo.siswa.
Directorate Technical Vocational Education
72. 60 Sftware Engineering
Remove table dbo.siswa by using right click then Delete. Now, create the following
table by using the above described process.
Table 12,1. Table, column, type data.
Table Name Column Type and Wide Data Information
Student - Student No. Nchar (10)
- Student Name Nchar (20)
- Address Nchar (30)
- City Nchar (20)
- Telephone Nchar (15)
- IdProgram Smallint
Teacher - NIP Nchar (15) NIP as
- TeacherName Nchar (20) Primary Key
- Address Nchar (30)
- City Nchar (20)
- Telephone Nchar (15)
Program - IdProgrM Smallint IdProgrAM as
- Program'Name Nchar (20) Primary Key
- Description Nchar (100)
Field - IdField Smallint IdField as Primary
- FieldName Nchar (20) Key
- Description
Program Teacher - NIP Nchar (15) Both as Primary
- IdProgram Smallint Key
Field Teacher - NIP Nchar(15) Both as Primary
- idField smallint Key
12.4.3. Fill in Data into Table
After making tables, we can fill in data, By using GUI SQL Server Management Studio
we fill data as same as Microsoft Access. Right click in table's name chose Open
Table. New window at Summary panel will open and ready to fill data. Figure 12,10
shows how fill data to a table. Every time we fill in one line, then make sure to press
Enter. Try to fill in the other tables.
Directorate Technical Vocational Education
73. 61 Sftware Engineering
Figure 12.10. Filling table
12.5. ABLE VIEW AND OPERATION
12.5.1. Relation Between Tables
So far, our tables are basically independent tables and no relation between table. If we
examine, we can create relation between table as shown in Chapter 10 and Chapter 11
on the relation between table. Table siswa relates to table program, table teacher
relates to table field and table program. To create relation between table, we may use
Database Diagrams components. Right click on Database Diagrams then select New
Database Diagram. A new windows as shown in Figure 12.11 will be opened.
Figure 12.11. Window for relation between table
Directorate Technical Vocational Education
74. 62 Sftware Engineering
On Add Table window, click Add to select the tables for relations. In this example, we
select all tables as they are all related. Click many times until no table in Add Table
window. After all tables selected, create the relation as shown in Figure 12.12. To relate
one table to the other is similar to Microsoft Access. Keep the diagram.
Figure 12.12. Relations between tables
12.5.2. View Creation
View is same as Query on Microsoft Access. SQL Server provides GUI feature same
as GUI Query on Microsoft Access. To create View, right click at View and select New
View as in Figure 12.13. Select Add Table and add one or more table. For example we
use table siswa. Click Close to close window as see Figure 12.14.
There are four important parts as shown in Figure 12.14, namely, Diagram window,
Criteria window, SQL window and Result window. Diagram window uses as source
tables to be used in View. Criteria window is a place for criteria in View. SQL window
is a place to write SQL command for View. Result window is a place to see View.
Directorate Technical Vocational Education
75. 63 Sftware Engineering
Figure 12.13. Window for making table then View.
Directorate Technical Vocational Education
76. 64 Sftware Engineering
Figure 12.14. Window making View.
In Figure 12.14, we select table siswa as View source and it shows in the Diagrams
window. We will make simple View based on this table. Click *(All Columns) under
table siswa. Contents of the SQL window will automatically changed. It will be the
same as what we have done in Diagram and Criteria window. Click on ! Mark in toolbar
to execution View or right click in one of Figure 12.14 it may be Diagram / Criteria /
SQL / Result window then select Execute SQL. See Figure 12.15. Save this View by
clicking diskette icon on Toolbar and provides the View name.
Directorate Technical Vocational Education
77. 65 Sftware Engineering
Figure 12.15. Results View execution.
Please note that all steps in making View is the same as making Query to Microsoft
Access. Let us now create a more complex examples. Examine the following
examples.
Example 12.1. View to show Teacher's name and their address outside Malang
City.
Use table Teacher as View source. Create View as shown in Figure 12.13 windows.
Select table Teacher then Close. Click Name, Address and City in box of table Teacher.
At Criteria window, select City, select row City and in column Filter type statement <>
‘Malang’. Execute this View, you will get the results as Figure 12.16.
Directorate Technical Vocational Education
78. 66 Sftware Engineering
Figure 12.16. Results of View execution of example 12.1.
The Filter Column in Criteria window is used to select the row to our need. In the
above example, we use row to select City that is not ‘Malang’. We use sign <> to state
not equal.
Example 12.2. View to see Student's name, Id number, and field of study in
alphabetical order.
For this example, we need a minimum of two (2) tables, namely, table siswa and table
Program. See relations between table in Figure 12.12. Table siswa directly connects to
table Program using the same process as in Figure 12.13, In Student box in table
siswa select name and NoInduk while in Program box select NamaProgram. In Criteria
windows, select Name row and click on Sort Type column then select Ascending.
Execute the View. We will see the result similar to Figure 12.17. Examine the Result
windows, name column is shown in alphabetical order.
Directorate Technical Vocational Education
79. 67 Sftware Engineering
Figure 12.17. Results of View execution of example 12,2.
Example 12,3. View to show Teacher's name and field of expertise in alphabetical
order.
Base on table relations in Figure 12.12, it needs three (3) tables, namely, table
Teacher, Teacher's Field and Field. Column Name is in table Teacher, while Field
column is in table Field. Select the three (3) tables. At Teacher's box in window
Diagram select Name. At Field's box select Field Name. Finally, at Criteria window
select Name row and click on Sort Type – then Ascending. Execute this View.
Directorate Technical Vocational Education
80. 68 Sftware Engineering
Figure 12,18. Results of View execution of example 12.3.
12.6. T-SQL USAGE
Transact-SQL (T-SQL) is a variant of SQL database language from Microsoft company
and Sybase. Microsoft SQL Server recognizes onlyT-SQL SQL language. Thus, to use
Microsoft SQL Server, we must understand T-SQL.
In General, T-SQL is not too different with the standard SQL. In fact, T-SQL is a
standard SQL language with addition of several special commands to help work in SQL
Server. The commands SELECT, FROM and WHERE mentioned earlier, is some of
the main command in T-SQL.
To create T-SQL commands, we may use the Query window in the SQL Server by
clicking New Query button in the Menu Bar as shown in Figure 12.19. Prior to it, select
the database to bused by clicking the database name in the Object Explorer. In the
opened Query window, we may type the SQL commands.
Directorate Technical Vocational Education
81. 69 Sftware Engineering
Figure 12.19. The query window.
12.6.1. Defining Table using T-SQL
Almost all activities in SQL Server may be done by using T-SQL commands, including
create table. In previous chapter, it has been explained that we can create table by
using GUI facilities or SQL commands. Table definition commands are part of DDL.
Command to define or create a new table is CREATE TABLE. While command to drop
or remove the table is DROP TABLE.
Create a new database called Lat-01 SQL. Then select it. Open Query window as
shown in Figure 12.18. We will make the same tables as Lat-01 but using SQL
command. The first table is the table Siswa, see the structure in the above table 12.1.
Type in the following command, then execute by clicking (!).
CREATE TABLE [dbo].[Field](
[IdField] [smallint] NOT NULL,
[Field'sName] [nchar](20)NULL,
[Description] [nchar](100)NULL,
Directorate Technical Vocational Education
82. 70 Sftware Engineering
CONSTRAINT [PK_Field] PRIMARY KEY CLUSTERED
(
[IdField] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE commands follows with table name ([dbo]. [Field]) then follows by
column list on the table. In the column list, data type, data width, and other condition
(NULL NOTE or NULL) must be listed. After that then write CONSTRAINT section of
the table. In the CONSTRAINT, the Primary Key is defined. Examine how to write the
commands.
After execute it, examine the table node section to see if the table created or not. Right
Click on Tables database in Object Explorer, and select Refresh. You will find Object
Tables. Create the other tables in the same way. Here are the commands to create
tables.
Table Program
CREATE TABLE [dbo].[Program](
[IdProgram] [smallint] NOT NULL,
[Program'sName] [nchar](20) NULL,
[Descriptions] [nchar](100) NULL,
CONSTRAINT [PK_Program] PRIMARY KEY CLUSTERED
(
[IdProgram] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Table Teacher
CREATE TABLE [dbo].[Teacher](
[NIP] [nchar](15) NOT NULL,
[Nama] [nchar](20) NULL,
[Alamat] [nchar](30) NULL,
[Kota] [nchar](20) NULL,
[Telepon] [nchar](15) NULL,
CONSTRAINT [PK_Teacher] PRIMARY KEY CLUSTERED
(
[NIP] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Table Student
CREATE TABLE [dbo].[Student](
[IdNumber] [nchar](10) NOT NULL,
[Name] [nchar](20) NULL,
[Address] [nchar](30) NULL,
Directorate Technical Vocational Education
83. 71 Sftware Engineering
[City] [nchar](20) NULL,
[Telephone] [nchar](15) NULL,
[IdProgram] [smallint] NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[IdNumber] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Table TeacherProgram
CREATE TABLE [dbo].[TeacheProgram](
[NIP] [nchar](15) NOT NULL,
[IdProgram] [smallint] NOT NULL,
CONSTRAINT [PK_TeacheProgram] PRIMARY KEY CLUSTERED
(
[NIP] ASC,
[IdProgram] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Table Teacher Field
CREATE TABLE [dbo].[TeacherField](
[IdBidang] [smallint] NOT NULL,
[NIP] [nchar](15) NOT NULL,
CONSTRAINT [PK_TeacherField] PRIMARY KEY CLUSTERED
(
[IdField] ASC,
[NIP] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
In the above commands to create tables, we have not yet done any relation between
table. Relation between table in SQL is usually set by using FOREIGN KEY command.
Please review the FOREIGN KEY in Chapter 10 and 11. We need to modify table by
adding CONSTRAINT so that the table could understand relations between tables.
Open again Query window then type in the following commands.
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT
[FK_Student_Program] FOREIGN KEY([IdProgram])
REFERENCES [dbo].[Program] ([IdProgram])
ALTER TABLE [dbo].[TeacherProgram] WITH CHECK ADD
CONSTRAINT [FK_TeacherProgram_Teacher] FOREIGN KEY([NIP])
REFERENCES [dbo].[Teacher] ([NIP])
ALTER TABLE [dbo].[TeacherField] WITH CHECK ADD CONSTRAINT
Directorate Technical Vocational Education
84. 72 Sftware Engineering
[FK_TeacherField_Field] FOREIGN KEY([IdField])
REFERENCES [dbo].[Field] ([IdField])
ALTER TABLE [dbo].[TeacheField] WITH CHECK ADD CONSTRAINT
[FK_TeacherField_Teacher] FOREIGN KEY([NIP])
REFERENCES [dbo].[Teacher] ([NIP])
Commands to modify or change table structure is ALTER TABLE, then follow by table
name to change. Examine the command ALTER TABLE [dbo]. [Student] WITH
CHECK ADD CONSTRAINT [FK Student_Program] FOREIGN KEY ([IdProgram]). The
table to be modified is dbo.Student to connect with table Program, so will write
FK_Student_Program. IdProgram Column in table Student is the FOREIGN KEY, and
will write as FOREIGN KEY ([IdProgram]). IdProgram column is originated from table
Program, thus at the last command must be written the table reference (write:
REFERENCES [dbo]. [Program] ([IdProgram])). All relations between tables make as
same way above.
The DROP TABEL command is very easy to do, just add table name in front of
command. For example: DROP TABLE [dbo]. [Field].
12.6.2. Fill in, Change and Remove Table contents by SQL
After making all tables, we can fill in data on each table by using INSERT. This
command is in DML group. We will try to use it at table Field. Check the contents of
table Field by right click at table name, then select Open Table. Table contents may be
seen in the Summary window as shown in Figure 12.20. Close again Open Table
window.
Figure 12.20. Contents table Field
Open Query window, type command as follow:.
Directorate Technical Vocational Education
85. 73 Sftware Engineering
INSERT INTO [Lat-01].[dbo].[Field]
([IdField]
,[FieldName]
,[Description])
VALUES
(8, 'Networking Wireless', NULL)
INSERT command follow by database name and table name ([Lat- 01]. [dbo]. [Field] ),
then follow by column name at that table. VALUES key word is used to provide
guidance that the next keyword would the data entry. Please note that as there is three
(3) column in table Bidang, we need to enter three (3) data. Th first data (8) must be
filled in the definition of table Bidang, IdBidang is NOT NULL. And the third column
Description, we may fill NULL as we want to empty column contents.
Do the above command. Click right at table Field, select Refresh. Click right again at
table Field and select Open Table. All data in table Field is shown in Figure 12.21.
Figure 12.21. Contents of table Field after INSERT data.
As shown above, INSERT command may be too long. Try to remove name list using
the command. Replace data under VALUES to (9, 'Digital Animation', NULL). Execute
and see the results. What happen? After Refresh, you will see your data also can input
without calling names list column at INSERT command.
Sometimes, when we make mistake during data entry. It may be typing error typing or
reading error. If we want to fix the data, it may be done bu using data update. SQL
provides UPDATE command to perform this process. All command is included in DML
group.
For example, we want to change the 5th content of FiledName on IdField column as
shown in Figure 12.21. From 'Photography' into 'Digital Photography', we type the
Directorate Technical Vocational Education
86. 74 Sftware Engineering
following command.
UPDATE [Lat-01].[dbo].[Field]
SET [FieldName] = 'Fotografi Digital'
WHERE [IdField] = 5
Recheck contents of table Field. Don forget to refresh table Field before clicking
Open Table.
To remove data using SQL, it can be done by using DELETE command. Please note
that DELETE command will delete the contents in a row. If you only want to make
empty only part of contents in a row or one cell just use UPDATE command. For
example, we want to remove row with IdField equal to 8 then we have to type the
following command.
DELETE FROM [Lat-01].[dbo].[Field]
WHERE [IdField] = 8
12.6.3. Search and View Data by SQL
As mention at early chapter, main command in SQL is SELECT, FROM, and WHERE.
Most of our activities will use these SQL commands. To use these commands, open
window and then query, type your command. Run by pressing mark (!).
SELECT command use to select column. If we want to show all column, we simply
use the sign *. FROM command is used to select which column for data source.
WHERE command is used to limit the row to be shown to maintain the performance.
WHERE may be from table or from other View results. Examine the following example
of using SQL.
Example 12.4. Show all data in table Filed.
In this example, we use table Field with all column used. We use sign * or register all
columns in table Field. Here is the SQL command and the execution result.
If you want to save the above View command, before SELECT command, add
CREATE VIEW <name_view> AS. Replace <name_view> with the name that you
want.
Example 12.5. Show Student's name and address.
In this example, we use table Student. In table Student, we use name, address, and
city column. We can not use * sign, but register name and address column after
Directorate Technical Vocational Education
87. 75 Sftware Engineering
SELEC command. Note the following SQL command.
Example 12.6. Show student's name who live in Batu.
In this example, we use WHERE statement to limit the shown row. We use table
Student and column name. As shown in Figure 12.2, there are only two (2) students in
Batu, namely, Pepe Richard and Bubi Red. WHERE statement should be followed by
the condition to met. In this case is City = 'Batu'. Here are the full SQL command and
output. Please examine how to write it.
Example 12.7. Show data of name, address and city for teacher who not living in
Malang
Directorate Technical Vocational Education
88. 76 Sftware Engineering
In this example, we use WHERE statement to limit the data shown. We use table
Teacher and name, address and city column. Statement, not living in Malang is the
condition to be met. The complete statements is as follows
Example 12.8. Show student's name and program.
In this example, we will use two related tables, namely, table Student and table
Program. We want to show name column from table Student and NamaProgram
Column from table Program. In this case, database name should be added prior to
column name in SELECT command. Please note that as shown in Table 12.1, in table
Student there is a Foreign Key, namely, IdProgram that is the Primary Key in table
Program. Id Program is used as the connector between these two tables. Please
examine the following SQL command and its result.
Directorate Technical Vocational Education
89. 77 Sftware Engineering
Example 12.9. Show student's names who takes Software Engineering Program.
This is the continuation of Example 12.4. We add WHERE statement to show the
name who take Software Engineering Program. We want to show name of from table
Student. Examine the following SQL statement and the result.
Example 12:10. Show teacher's name and field of expertise.
This example is more complex than others. It involves three (3) tables, table Teacher,
table Teacher_Field and table Field. See the relations between table in Picture 12:11.
There is two INNER JOIN to be used, namely, INNER JOIN table Teacher and table
Teacher_Field and INNER JOIN Teacher_Field and table Field. Examine how to make
INNER JOIN statement.
Example 12.11. Show teacher's name whose expertise is in Web or Database
Programming
This example is an expansion of example 12.10. We will show teachers whose
expertise in Web and Database Programming. We need to add WHERE command to
limit the row. In addition, we will use OR operator in WHERE statement. Examine the
SQL commands and the execution result.
Directorate Technical Vocational Education
90. 78 Sftware Engineering
Compare with the result in Figure 12.10. Try to replace OR with AND and re-run SQL
command. How is the results?
All above examples may be expanded into many variations. With some practice, you
will understand how use SQL commands.
12.7. FUNCTION, PROCEDURE DAN TRIGGER
Microsoft SQL Server provides some advanced facilities that are not provided by
Microsoft Access, such as create functions, store procedure and trigger. This is
because SQL Server is aimed to be used as database server which requires stronger
performance as compared with Microsoft Access.
12.7.1. Function
T-SQL provides many functions to make easy in database management tasks. Some
of the important functions will be presented here. For more information please read the
manual or online help from the SQL Server.
Function related to numeric
●
The important functions related to numeric (number) are isNumeric and Round.
Function IsNumeric is used to check whether a data contains data number or
not. Open Queries window, type command as shown in Figure 12.12, see the
results. Table Student has phone column where the data seems to be
numbers. We can check by function isNumerik. In the execution results, it
shows that all are 0 that means false. Thus, all phone's data is not numeric
data.
Directorate Technical Vocational Education
91. 79 Sftware Engineering
Example 12.12. Usage of is Numeric function.
ROUND function is used to round a number fraction to the nearest round
number. For example 13:58 to 14:00. Examine the following example.
Example 12.13. Round Function
Functions related to String
●
Functions related to character (string) are LEFT, RIGHT, LEN, LOWER,
UPPER, LTRIM, and RTRIM.
LEFT is used to select a certain number of character on the left, while RIGHT
is similar to LEFT for the right character. The result depends in the data width
during table creation. To know the length of data, LEN may be used. Please
examine the following example.
Example 12:14. Use of LEFT, RIGHT and LEN.
Directorate Technical Vocational Education
92. 80 Sftware Engineering
LOWER used to alter the character into small case, while UPPER is to convert
into capital case. LTRIM use to remove space on the left of data string, while
RTRIM on the right of data string. Please examine the following example.
Example 12:15. Use of UPPER and LOWER.
Directorate Technical Vocational Education
93. 81 Sftware Engineering
Functions related to TIME
●
Some of the important functions related to time are GETDATE, MONTH, DAY,
YEAR, DATENAME, DATEADD, and DATEDIFF. GETDATE use to get date
from current system computer. MONTH use to retrieve month from data date.
DAY use to retrieve date from data date. YEAR use to retrieve year from data
date. DATENAME use to get name day from data date. DATEADD use to
increase or reduce data date DATEDIFF use to see difference between two
data date. Examine the following example.
Example 12:16. Use time function.
12.7.2. Procedure and Stored Procedure
Beside the above functions, SQL Server introduces user-defined function. These
functions can be created to speed up access for management database. There are
two types user defined functions, namely, scalar and inline-table value. Scalar function
produces an output by Return keywords. Inline Table-Value produces a new table
when execute. Examine the following example.
Example 12.17. Create a scalar function
The function in Figure 12.17 is called dbo.FullName. It uses two parameters, namely,
@firstname and @LastName. Examine how to define the function and its parameters.
The output from function has types nVarChar data with data width of 35. This function
provides a value output such as combination from @LastName and @Firstname. Both
of them in scalar function.
The blocked code in Figure 12.17 shows how the function is called in a query.
There will be a new column contains a combination of LastName and
firstname.
Directorate Technical Vocational Education
94. 82 Sftware Engineering
Stored procedure is a code that can receive input parameter and produce one or more
output parameters. In generally, Stored procedure is used for operations in database.
Usually a complicated, long and complex SQL commands may be stored as
procedure. Thus, we do not need to retype it again. All we need to do is call the name
of Stored procedure and execute it.
Command to create Stored procedure is CREATE PROCEDURE, follow by name
procedure. Examine the following example.
Example 12.18. Create Stored Procedure
Open Query window, then type the following code.
CREATE PROCEDURE deletelines
@IdNumber smallint
Directorate Technical Vocational Education
95. 83 Sftware Engineering
AS
DELETE
FROM dbo.Field Where dbo.Field.IdField = @IdNumber
Procedure we create call deleteLine with one parameter @ IdNumber with type data
smallint. When Stored procedure process, statement after AS is same as statement
SQL.
12.7.3. Trigger
Trigger is special type of Stored procedure and will execute if something happen. For
example, additional data (INSERT), remove data (DELETE) or change data
(UPDATE). Trigger is usually component of a table.
To create a trigger, click on node in front of the selected table. After it shows the list of
table component, do right click on Trigger and select New Trigger. SQL Server will
show new windows containing Trigger template. Delete all text in the window and type
following example.
Example 12.19. Create a Trigger.
CREATE TRIGGER try_trigger
ON dbo.Field FOR INSERT
AS
DECLARE @varName Varchar(20)
SELECT @varName = NameField FROM INSERTED
PRINT 'you input data : ' + @varName
The above Trigger is in table Field. The Trigger name is try_trigger and will be
executed in the INPUT event in table table dbo.Field.
12.8. ADMINISTRASION SQL SERVER
12.8.1. Security and Authentication.
Database Security is an important component. The SQL as a server may theoretically
be accessed by anyone. This will be very harmful if irresponsible people do some
changes, do destruction or even delete important databases.
SQL Server provides Authentication mechanism to limit the privilege o access the
database SQL Server. There are two authentication models, namely, Windows
Authentication and SQL Server Authentication.
Directorate Technical Vocational Education