Professional Documents
Culture Documents
(IT) 08 Physical DM Dan Implementasi DB - DDL - DML
(IT) 08 Physical DM Dan Implementasi DB - DDL - DML
(IT) 08 Physical DM Dan Implementasi DB - DDL - DML
Learning Outcomes
Students can create design Physical Database
Students can explain impact of normalization and index for
database design
3
Topics
Introduction to Physical Database Design
Data Volumes and Usage Analysis
Designing Fields
Introduction to SQL
Data Definition Language
Data Manipulation Language
Data Control Language
VIew
4
Introduction to Physical
Database Design
5
Physical Database Design 1
Purpose
• is to translate the logical description of data into the technical specifications for storing and retrieving data
Goal
• The primary goal of physical database design is data processing efficiency
• to create a design for storing data that will provide adequate performance and ensure database integrity,
security, and recoverability
• to minimize the time required by users to interact with the information system
Output
• Physical database design produces the technical specifications that programmers, database administrators,
and others involved in information systems construction will use during the implementation phase
Scope in this course
• We concentrate in this chapter on the design of a single, centralized database especially in Selecting Data
Type.
• We concentrate on how to make processing of physical files and databases efficient, with less attention on
minimizing the use of space.
6
Physical Database Design 2
Requirements
• Normalized relations, (number of rows in each table)
• Definitions of each attribute
• Descriptions of where and when data are used in various ways
• Expectations or requirements for response time and data security, backup, recovery, retention, and integrity
• Descriptions of the technologies
Decisions
• Choosing the storage format (called data type)
• DBMS guidance regarding how to group attributes from the logical data model into physical records
• DBMS guidance regarding how to arrange similarly structured records in secondary memory (primarily
hard disks)
• Selecting structures (including indexes and the overall database architecture)
• Preparing strategies for handling queries against the database that will optimize performance and take
advantage of the file organizations and indexes that you have specified
7
Designing Fields
12
Background
• A field is the smallest unit of application data recognized by system software
• Field: simple attribute (single component)
• Specifications each field concern:
• Type of data
• Data integrity
• Mechanism to handle missing values
13
What is Data type?
• A data type is a detailed coding scheme recognized by system software, such as a
DBMS, for representing organizational data.
• The bit pattern of the coding scheme is usually transparent to you, but the space to
store data and the speed required to access data are of consequence in physical
database design.
• In this course, we will use the Oracle Data types
14
Why We Should Selecting Data types?
• Selecting a data type involves four objectives that will have
different relative levels of importance for different
applications:
1. Represent all possible values.
2. Improve data integrity.
3. Support all data manipulations.
4. Minimize storage space.
15
Selecting Data types..
• An optimal data type for a field can, in minimal space, represent every
possible value (while eliminating illegal values) for the associated
attribute and can support the required data manipulation (e.g., numeric
data types for arithmetic operations and character data types for string
manipulation).
• Any attribute domain constraints from the conceptual data model are
helpful in selecting a good data type for that attribute.
16
Introduction to Oracle Data types
Each column value and constant in a SQL statement has a data type, which is
associated with a specific storage format, constraints, and a valid range of values.
When you create a table, you must specify a data type for each of its columns.
p e s …
ata ty
o f D
p le
E xa m
17
CHAR Data type
• The CHAR data type stores fixed-length character strings.
• When you create a table with a CHAR column, you must specify a string length (in
bytes or characters) between 1 and 2000 bytes for the CHAR column width.
• The default is 1 byte.
• Oracle then guarantees that:
• When you insert or update a row in the table, the value for the CHAR column has the fixed
length.
• If you give a shorter value, then the value is blank-padded to the fixed length.
• If a value is too large, Oracle Database returns an error.
18
VARCHAR2 and VARCHAR Data types
• The VARCHAR2 data type stores variable-length character strings.
• When you create a table with a VARCHAR2 column, you specify a maximum string length (in bytes
or characters) between 1 and 4000 bytes for the VARCHAR2 column.
• For each row, Oracle Database stores each value in the column as a variable-length field unless a
value exceeds the column's maximum length, in which case Oracle Database returns an error.
• Using VARCHAR2 and VARCHAR saves on space used by the table.
For example, assume you declare a column VARCHAR2 with a maximum size of 50 characters. In
a single-byte character set, if only 10 characters are given for the VARCHAR2 column value in a
particular row, the column in the row's row piece stores only the 10 characters (10 bytes), not 50.
• The VARCHAR data type is synonymous with the VARCHAR2 data type. To avoid possible
changes in behavior, always use the VARCHAR2 data type to store variable-length character
strings.
19
Overview of Numeric Data types
• The numeric data types store positive and negative fixed and
floating-point numbers, zero, infinity, and values that are the
undefined result of an operation (that is, is "not a number" or
NAN).
• This section includes the following topics:
• NUMBER Datatype
• Floating-Point Numbers
20
NUMBER Data type
• The NUMBER data type stores fixed and floating-point numbers.
• The following numbers can be stored in a NUMBER column:
• Positive numbers in the range 1 x 10-130 to 9.99...9 x 10125 with up to 38 significant digits
• Negative numbers from -1 x 10-130 to 9.99...99 x 10125 with up to 38 significant digits
• Zero
• Positive and negative infinity (generated only by importing from an Oracle Database, Version 5)
21
Specifying The Numeric Columns
• For numeric columns, you can specify the column as:
• Optionally, you can also specify a precision (total number of digits) and scale
(number of digits to the right of the decimal point).
• In this case, the precision is 38, and the specified scale is maintained.
• When you specify numeric fields, it is a good idea to specify the precision and scale. This
provides extra integrity checking on input.
• For input and output of numbers, the standard Oracle Database default decimal character is a period,
as in the number 1234.56. The decimal is the character that separates the integer and decimal parts
of a number. You can change the default decimal character with the initialization parameter
NLS_NUMERIC_CHARACTERS. You can also change it for the duration of a session with the
ALTER SESSION statement. To enter numbers that do not use the current default decimal character,
use the TO_NUMBER function.
23
• Oracle Database uses its own internal format to store dates. Date data is stored in
fixed-length fields of seven bytes each, corresponding to century, year, month, day,
hour, minute, and second
25
Formatting DATE Data type
• For input and output of dates, the standard Oracle date format is DD-MON-YY, as follows:
• You can change this default date format for an instance with the parameter NLS_DATE_FORMAT.
You can also change it during a user session with the ALTER SESSION statement. To enter dates
that are not in standard Oracle date format, use the TO_DATE function with a format mask:
• Oracle Database stores time in 24-hour format—HH:MI:SS. By default, the time in a date field is
00:00:00 A.M. (midnight) if no time portion is entered. In a time-only entry, the date portion
defaults to the first day of the current month. To enter the time portion of a date, use the TO_DATE
function with a format mask indicating the time portion, as in:
26
Introduction to SQL
27
Overview SQL
• SQL ( Structured Query Language) is nonprocedural language that provides
database access. It is nonprocedural in that users describe in SQL what they want
done, and the SQL language compiler automatically generates a procedure to
navigate the database and perform the desired task.
• Oracle SQL includes many extensions to the ANSI/ISO standard SQL language,
and Oracle tools and applications provide additional statements. The Oracle tools
SQL*Plus and Oracle Enterprise Manager let you run any ANSI/ISO standard SQL
statement against an Oracle database, as well as additional statements or functions
that are available for those tools.
• Although some Oracle tools and applications simplify or mask SQL use, all
database operations are performed using SQL. Any other data access method
circumvents the security built into Oracle Database and potentially compromises
data security and integrity
28
SQL Statements
• All operations performed on the information in Oracle Database are run using SQL
statements.
• A statement consists of identifiers, parameters, variables, names, datatypes, and
SQL reserved words.
• SQL reserved words have special meaning in SQL and cannot be used for any other
purpose. For example, SELECT and UPDATE are reserved words and cannot be
used as table names.
29
Types of SQL Command
30
• These commands are used to create, alter, and drop tables, views, and indexes.
• There may be other objects controlled by the DDL, depending on the DBMS. For
example, many DBMSs support defining synonyms (abbreviations) for database
objects or a field to hold a specified sequence of numbers (which can be helpful in
assigning primary keys to rows in tables).
• In a production database, the ability to use DDL commands will generally be
restricted to one or more database administrators in order to protect the database
structure from unexpected and unapproved changes.
• In development or student databases, DDL privileges will be granted to more users.
32
Create Table
• We create tables from the Relational Scheme.
• Steps :
1. Create Tables that does not refer to other tables (i.e no Foreign key)
2. Add primary key constraints
3. Add other constraints (i.e unique constraints)
4. Create the rest of tables from the Relational Scheme
5. Add foreign key constraints
33
Create Table Script
Create Table Script 1 Create Table Script 2
MODIFY (
Column_name1 new_data_type, ALTER TABLE Table_name
VIEW
Intro to View
Maxmanroe.com
VIEW
Dynamic View
Maxmanroe.com
• Example:
CREATE VIEW OutstandingStudent AS
SELECT NAME, COURSE, GRADE
FROM Student
WHERE GRADE > B
Dynamic View 43
• The result of query above will be the same like when we execute the
query expression we define for the view:
SELECT NAME, COURSE, GRADE
FROM Student
WHERE GRADE > B
Dynamic View Pros 44
VIEW
Materialized View
Maxmanroe.com
• Example:
CREATE TABLE OutstandingStudent AS
(SELECT NAME, COURSE, GRADE
FROM Student
WHERE GRADE > B)
Materialized View 49
VIEW
Examples
Maxmanroe.com
Data Manipulation
Language
(DML)
56
OVERVIEW DML
• Many consider the DML commands to be the core commands of SQL.
• These commands are used for updating, inserting, modifying, and querying the data
in the database.
• They may be issued interactively, so that a result is returned immediately following
the execution of the statement, or they may be included within programs written in
a procedural programming language, such as C, Java, PHP, or COBOL, or with a
GUI tool (e.g., Oracle’s SQL Developer, SQL Assistant with Teradata, or MySQL
Query Browser).
Use command "COMMIT" to change data permanently to the database
You can use “ROLLBACK” to cancel the change of data
57
INSERT
Use the INSERT statement to add rows to a table.
Use command "COMMIT" to change data permanently to the database
SQL SCRIPT
UPDATE Table_name
SET Column_name = expression_value
[WHERE] Condition;
60
UPDATE ( By Example)
SQL SCRIPT
UPDATE Category
SET category_name = “Marketing”
WHERE category_id = 150;
Marketing
61
DELETE
• Use the DELETE statement to remove rows
• Use command "COMMIT" to change data permanently to the database
SQL SCRIPT
Granting a System Privilege to a User: Example Revoking a System Privilege from a User: Example
Granting System Privileges to a Role: Example Revoking a Role from a User: Example
Reference
Hoffer, Jeffrey A., et.al., "Modern Database Management", Twelfth Edition, Pearson,
2016. Chapter 5
https://docs.oracle.com
68
Questions