Anda di halaman 1dari 23

DBMS

Data and Information


How are data and information related?

Computers process data into


information

Data is a collection of unprocessed


items

Information is data that is organized and


meaningful

File Processing System


Each department or area has its own set of files
Records in one file usually do not related to any records in
other file

WEAKNESS

DATA REDUNDANCY
same data stored in
multiple file waste of
resources

ISOLATED DATA
data
stored in separate file
difficult to share and
update
3

File Processing Vs Database

Database Approach
What is a database?
Collection of data
organized so
you can access,
retrieve, and
use it

Database software
also called Database
Management System
(DBMS)
Database software
allows you to

Create
database
Add,
change,
and delete
data

Sort
and
retrieve
data

Create
forms
and
reports
5

Database
Advantages of the database

Control data redundancy

Sharing Data

Remove data inconsistency

Improve data security

Increased Productivity

Database Administrator (DBA)


Coordinates all the activities of the database system
Database administrator's duties include:

Database definition
Storage structure and access method definition
Database modification
Granting user authority to access the database
Specifying constraints
Monitoring performance and responding to changes
in requirements
Backup & Recovery

Relational Database

Stores data in tables that consist of


rows and columns
Rows called tuples / records
Columns called attributes /
fields.
Each column has unique name
Each row has primary key
Used terminology called relations
8

Relational database
attributes/ fields
Primary key

records/ tuples

Relational database

10

Relational database

SQL statement
SELECT Jersey, PlayerNm, BirthPlace From Player WHERE SponsorID = 1

SQL statement results

Jersey

PlayerNm

BirthPlace

Shanil

Selangor

Aaron

Perak

11

SQL (Structured Query Language) is a computer


language aimed to store, manipulate, and retrieve
data stored in relational databases.
Data Definition Language (DDL)
Commands that define a database, including creating, altering, and dropping
tables and establishing constraints

Data Manipulation Language (DML)


Commands that maintain and query a database

12

Common SQL Commands

Data Definition Language (DDL):


Create
Alter

Drop

Data Manipulation Language (DML):


Select
Insert

13

Update
Delete

Writing SQL Statements

SQL statements are not case sensitive


(but criteria within quotation marks are for some
RDBMS)

SQL statements can be on one or more lines


Keywords cannot be split across lines
Tabs and spaces are allowed to enhance
readability
Each SQL statement (not line) ends with a
semicolon
(;)
14

DDL--Table Creation
General syntax for CREATE TABLE
CREATE TABLE employees (
Employee_id INTEGER(8),
Last_name
VARCHAR(25),
First_name
VARCHAR(25),
Reports_to
INTEGER );
OR
CREATE TABLE customer (
First_Name
char(50),
Last_Name
char(50),
Address
char(50) default 'Unknown',
City
char(50) default 'Mumbai',
Country
char(25),
Birth_Date
date );
15

DDL ALTER Tables

Alter Table Statement: Once a table is created in the database, there are
many occasions where one may wish to change the structure of the table.
Typical cases include the following:
- Add a column
- Drop a column
- Change a column name
- Change the data type for a column
ALTER table customer add Gender char(1) ;
ALTER table customer drop Gender ;
ALTER table customer change Address1 Address2 char(50) ;
ALTER table customer modify Address2 char(30) ;

16

DDL REMOVE Tables

Drop Table Statement:

The syntax for DROP TABLE is

DROP TABLE customer ;

17

DML--Insert Statement
Adds data to a table

Inserting into a table: every attribute is supplied


INSERT INTO CUSTOMER VALUES
(001, India bull, 1355 CP, Delhi, India, 01-10-1992);

Inserting from another table


INSERT INTO CUSTOMER_MASTER
SELECT * FROM CUSTOMER WHERE STATE = Delhi;

18

DML--Delete Statement

19

Removes rows from a table


Delete certain rows
DELETE FROM CUSTOMER WHERE STATE = HP;
Delete all rows
DELETE FROM CUSTOMER;

DML--Update Statement

Modifies data in existing rows


UPDATE PRODUCT SET UNIT_PRICE = 775 WHERE
PRODUCT_ID = 01027;

20

DML- SELECT Example

Find products with price less than 275


SELECT PRODUCT_NAME, PRICE
FROM PRODUCT
WHERE PRICE < 275;

21

When you want to select all columns in the table , use * in the
SELECT Clause
SELECT *
FROM PRODUCT
WHERE PRICE < 275;

Aggregate Functions (Numeric Functions)

COUNT: the number of rows containing the specified column (attribute)

SUM: the sum of all values for a selected column

AVG: the arithmetic mean (average) for the specified column (attribute)

MAX: the largest value in the column

MIN: the smallest value in the column

22

SELECT Example Using Aggregate


Function
SELECT COUNT(*)
FROM Customer;
SELECT AVG (PRICE)
FROM PRODUCT;
SELECT MAX (PRICE) AS MAXIMUM
FROM PRODUCT;
SELECT SUM(ORDERED_QUANTITY)
FROM Product;

23