Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

The Design Process

continued
1:1 A car can only have
car driver one driver; a driver can
have only one car.

1:M A car can have


car driver more than one driver; a
driver can only have one
car.

1:M A car can have only


car driver one driver; a driver can
have more than one car.

M:N A car can have more than


car driver one driver; a driver can have
more than one car.
many-to-many relationship

A many-to-many relationship cannot be


represented in a relational database.

Take the example from last week:


take orders
Sales Staff Customer

This many-to-many relationship does not enable mapping


between a particular order and a particular member of the sales
staff

Sales Staff Customer

accepts places

Order

The entity Order uniquely defines an instance involving both Sales


Staff and Customer. From a single many-to-many relationship two
one-to-many relationships have been created.
Sales Staff Order Customer
Staffid Orderid Customerid
Name Staffid* Name
DOB Customerid* Address
Employ start date Order details Contact name
Telephone

Foreign keys
Take another example:

takes module
student

Each student has enrolled for


one or more modules
AND
Each module has enrolled on it
zero or more students

This creates the following problem:


STUDENT(studentid,student name,pathway,module1,
module2, module3,module4……..)

MODULE(moduleid,moduleName, student1,student2,
student3,student4………..)
The solution to this problem is to create a third relation
that represents the relationship itself.

Such relations are called intersection relations because


each row documents the intersection of a particular student
with a particular module.
Student Module

is involved is for
Enrolment is an
Enrolment intersection table.

Each student is involved in one or more enrolments


and
Each enrolment involves one student
and
and Each enrolment is for one module
and
Each module is involved in zero or more enrolments
The entity and attributes are represented below:

STUDENT(studentid,studentName,pathway)
MODULE(moduleid,moduleName)
ENROLMENT(moduleid,studentid)

PRIMARY KEY - is an attribute or group of attributes that


uniquely identifies each entity type, and each record in the
corresponding database table.

COMPOSITE KEY – is when two fields together form


the unique identifier.
Top-Down Development

•Entity-Relationship modelling is a top down


approach to database development.

•It begins with study of the strategic goals of the


organisation.

•From such a study, an abstract model is constructed.

•The higher level model is transformed into lower-level


models.

•From these the application is developed.


Bottom-up Development

•Reverse of top-down development.

•Begins with the need to develop a specific system.

•Requirements specification established from which a


data model is built.

•The database is then designed and implemented.


Normalisation
(Bottom-up development)

•Putting all data into one file or table led to logic


problems when trying to manipulate the
contained data.

•Normalisation is a technique for analysing


relations based on:
Primary keys
Functional dependencies
•Series of rules
For example: Orders database
Order
OrderDate
OrderNumber
OrderQuantity1
Productid1 First Normal Form (1NF)
ProductDetail1
Rule:
ProductName1
ProductPrice1
OrderQuantity2 All repeating groups
Productid2 should be eliminated
ProductDetail2 and put into separate
ProductName2 tables
ProductPrice2
Customerid
CustomerName
FROM THIS TO THIS
Order Order
OrderDate
OrderNo OrderNo
OrderQuantity1 OrderDate
Productid1 Customerid
ProductDetail1 CustomerName
ProductName1
OrderDetail
ProductPrice1
OrderQuantity2 Productid
Productid2 ProductDetail
ProductDetail2 ProductPrice
ProductName2 OrderQuantity
ProductPrice2
Customerid
CustomerName
Eliminating Data Repetition in a Database

Order OrderDetail
OrderNo ItemNo
OrderDate Productid
Customerid ProductDetail
CustomerName ProductPrice
OrderQuantity

To enable these tables to relate to one another it is necessary that


both contain a common field.
First Normal Form (1NF) -
continued
OrderDetail
Order
OrderNo Productid
OrderDate OrderNo
Customerid ProductDetail
CustomerName ProductPrice
OrderQuantity

Now a one-to-many relationship has been established.


You are no longer obliged to add a new order every time you
add a new order item.
One row in the OrderDetail table needs both ProductName &
OrderNumber to identify it. Both fields are the primary key i.e.
composite key
Introduction
• Structured Query Language (SQL) is a data
sublanguage that has constructs for defining and
processing a database
• It can be
– Used stand-alone within a DBMS command
– Embedded in triggers and stored procedures
– Used in scripting or programming languages
CREATE TABLE
• CREATE TABLE statement is used for creating
relations
• Each column is described with three parts:
column name, data type, and optional
constraints
• Example
CREATE TABLE PROJECT (
ProjectID Integer Primary Key,
Name Char(25) Unique Not Null,
Department VarChar(100) Null,
MaxHours Numeric(6,1) Default 100);
Data Types
• Standard data types
– Char for fixed-length character
– VarChar for variable-length character
• It requires additional processing than Char data
types
– Integer for whole number
– Numeric
• There are many more data types in the
SQL-92 standard
Constraints
• Constraints can be defined within the CREATE
TABLE statement, or they can be added to the
table after it is created using the ALTER table
statement
• Five types of constraints:
– PRIMARY KEY may not have null values
– UNIQUE may have null values
– NULL/NOT NULL
– FOREIGN KEY
– CHECK
SELECT Statement
• SELECT can be used to obtain values of
specific columns, specific rows,
or both
• Basic format:
SELECT (column names or *)
FROM (table name(s))
[WHERE (conditions)];
WHERE Clause Conditions
• Require quotes around values for Char and VarChar
columns, but no quotes for Integer and Numeric columns
• AND may be used for compound conditions
• IN and NOT IN indicate ‘match any’ and ‘match all’ sets
of values, respectively
• Wildcards _ and % can be used with LIKE to specify a
single or multiple unknown characters, respectively
• IS NULL can be used to test for null values
Example: SELECT Statement
SELECT Name, Department, MaxHours
FROM PROJECT;
• Insert Figure 6-2
(PROJECT Table only)
Example: IS NULL
SELECT Name, Department
FROM EMPLOYEE
WHERE Phone IS NULL;
• Insert Figure 6-2 (EMPLOYEE Table only)
Sorting the Results
• ORDER BY phrase can be used to sort rows
from SELECT statement
SELECT Name, Department
FROM EMPLOYEE
ORDER BY Department;
• Two or more columns may be used for sorting
purposes
SELECT Name, Department
FROM EMPLOYEE
ORDER BY Department DESC, Name ASC;
Built-in Functions
• Five built-in functions for SELECT statement:
– COUNT counts the number of rows in the result
– SUM totals the values in a numeric column
– AVG calculates an average value
– MAX retrieves a maximum value
– MIN retrieves a minimum value
• Result is a single number (relation with a single row
and a single column)
• Column names cannot be mixed with built-in functions
• Built-in functions cannot be used in WHERE clauses
Example
• Tabel Member • Tabel Peminjaman
– ID_Member – ID_Member
– Nama_Member – ID_Buku
– Alamat_Member – Pinjam_Date
– Join_Date – Kembali_Date
– NonActive_Date – Fee
– MemberSince – Denda
• Tabel Buku
– ID_Buku
– Judul_Buku
– Buy_Date
– Status
– Penerbit
– Kode_Buku
Member Buku

Peminjaman
• Member yang sudah Non Aktif
• Buku yang masih belum kembali
• Buku SIM (ID=798751) dipinjam oleh
siapa
• Select ID_Member, Nama_Member
• From Tabel_Member
• Where NonActive_Date is not null

• Select ID_Buku, Judul_Buku


• From Tabel_Buku
• Where Kembali_Date is null

• Select Tabel_Member.ID_Member, Tabel_Member.Nama_Member


• From Tabel_Peminjaman,Tabel_Buku,Table_Member
• Where Tabel_Member.ID_Member=Tabel_Peminjaman.ID_Member
• And Tabel_Peminjaman.ID_Buku=Tabel_Buku.ID_Buku
• And Tabel_Buku.ID_Buku=798751
• And Kembali_Date is null

You might also like