Vous êtes sur la page 1sur 107

Exam Prep :

Microsoft Technology Associate 98-364:


Database Fundamentals

Rikky Wisnu Nugraha


Agenda

Introduction Core Database Concepts Manipulating Data

Relational Concepts Administering a Database

Creating Database Objects


Introduction Core Database
Concepts
Database
Tables
Types of databases
Relational databases
Simple Relational Structure
Common Terms
Database Management System
Database Server
Relational Concepts
Normalizing a database
Normalization Advantages
First normal form (1NF)
Second normal form (2NF)
Third normal form (3NF)
Other normalization forms
Example of Normalization
Example of Normalization
Example of Normalization
Product Color ColorSupplier
SupplierSupplier
ProductID Name
Name Supplier
Color
Phone Phone
Supplier
ColorID Color
SupplierColorID SupplierID
ColorSupplierID
Supplier
SupplierID
Supplier
PhoneSupplier
1 Widget
Widget 1 Blue
555-12345555-12345
11 Blue
1 1 1Blue 1 Contoso
1 Contoso
555-12345
Contoso
2 Thingybob
Thingybob2 Blue
555-54321555-54321
22 Red
2 2 2Red 2 Northwind
2 Northwind
555-54321
Northwind
3
Product
Knicknack
Knicknack 1 Red
555-12345555-12345
13 Black
1 3 Black
4 Thingybob
Name Red Color555-54321 2 Supplier Phone
5 Knicknack Black 555-12345 1
Widget Blue Contoso 555-12345
ProductColor
ProductColor
Thingybob BlueProduct
Blue, Red ColorNorthwind
Product 555-54321
Color
Knicknack Red,1Black
Red 1 1Contoso 1 555-12345
2 1 2 1
Thingybob Red Northwind 555-54321
2 2 2 2
Knicknack Black3 2 3Contoso 2 555-12345
3 3 3 3
Product Color Supplier
ProductID Name Supplier ColorID Color SupplierID Supplier Phone
1 Widget 1 1 Blue 1 Contoso 555-12345
2 Thingybob 2 2 Red 2 Northwind 555-54321
3 Knicknack 1 3 Black

ProductColor
Product Color
1 1
2 1
2 2
3 2
3 3
Databases are used to store data in tables
Usually, we normalize these tables to create a relational schema
The tables are stored on disk, and managed in a database server
Client applications access the database across a network
Increasingly, the database server is hosted in the cloud
Access to data in a database is restricted using firewalls and
authentication
Referential Integrity
Referential integrity
Methods for enforcing referential integrity
Contraints
Primary Key
Foreign Key
Unique Key
Composite Key
Relational Structure with Keys
Clustered index and Non-clustered index
A clustered index determines the order in which rows are stored

A non-clustered index stores pointers


Gggggggggg Aaaaaaaaaa
Mmmmmm
Hhhhhhhhh Bbbbbbbbb
Nnnnnnnnn
Abcdef…..1
Iiiiiiiiiiiiiiiiiiiii Ccccccccccc
Ooooooooo
Bcdefg.....1
Jjjjjjjjjjjjjjjjjjjjj Ddddddddd
Ppppppppp
Hijklm……3
Kkkkkkkkkkk Eeeeeeeeee
Qqqqqqqqq
Opqrs…….2
Llllllllllllllllllll Ffffffffffffffff
Rrrrrrrrrrrrr
2 31
Sales.Product
ProductID Name Price Supplier
1 Widget 12.99 1
2 Thingybob 3.75 2
3 Knicknack NULL 1
4 Wotsit NULL 1

CREATE CLUSTERED INDEX


NONCLUSTERED idx_ProductID
INDEX idx_Name
(ProductID);
ON Sales.Product (Name);
Creating Database Objects
Data Type
Data Type
Data Type
Data Type
Data Type
Data Type
Character Strings Phone Email Transcript LastName
555-12345 joe@contoso.com {…} Smith
• Fixed length 555-54321 mary@adatum.org {…} Ásbjörnsson

• Variable length 555-11111 u1@northwind.com {…} 愛佳


555-55555 bill5@litwareinc.com {…} ‫عبد الحميد‬
• Large text
• Unicode Lorem ipsum
Lorem ipsum
dolor sit amet,
Lorem ipsum
dolor sit amet,
consectetuer
Lorem ipsum
dolor sitadipiscing
amet,
consectetuer elit.
dolor sit amet,
consectetuer
adipiscing
Maecenas elit.
consectetuer
adipiscing elit.
Maecenas
porttitor
adipiscing
Maecenas elit.
porttitor
congue massa.
Maecenas
porttitor
congue massa.
porttitor
congue massa.
congue massa.
Numbers UnitsInStock Cost MarketInMillions
198 129.2701 23.5
• Integers 12 109.8923 12.1

• Exact decimals 67 27.6555 5.2


103 4.7600 69.3
• Approximate decimals
Temporal Values BirthDate ShiftStart OrderDate MeetingTime
1971-03-07 09:00 2016-04-01 09:17:00 2016-04-01 09:00 +8:00
• Dates 1987-12-18 10:30 2016-04-01 09:19:00 2016-04-01 09:00 +100

• Times 1967-07-22 11:15 2016-04-01 10:01:35 2016-04-01 09:00 +6:00


2005-03-17 12:05 2016-04-01 10:32:23 2016-04-01 09:00 +3:00
• Date and Time
• Offsets
Others InStock Photo StockID Spec Location Log
1 110101.. 12F51A28-12B.. <prd id=“1”/> 55.95,-3.188 1460077
• Bit (True/False) 0 111011.. 32BC762E-569.. <prd id=“3”/> 47.59,-122.3 1460198

• Binary 1 001010.. 734736BD-125.. <prd id=“4”> 51.51,-0.102 1476352


1 111110.. 329DE521-BC4.. <prd id=“2”/> -33.86,151.2 1479734
• Unique Identifier
• XML
• Spatial
• Timestamp
Creating a Table
Data Definition Language
Common DDL Statements

Database Procedure
Table Trigger
Default View
Index User
Login Role
Sales.Product
ProductID Name Price
1 Widget 12.99
2 Thingybob 3.75
3 Knicknack
4 Wotsit

CREATE TABLE Sales.Product


(ProductID INTEGER IDENTITY PRIMARY KEY,
Name VARCHAR(20),
Price DECIMAL NULL);
Sales.Product
ProductID Name Price Supplier
1 Widget 12.99 1
2 Thingybob 3.75 2
3 Knicknack 1
4 Wotsit 1

DROP
ALTERTABLE
TABLESales.Product;
Sales.Product
ADD Supplier INTEGER NOT NULL
CONSTRAINT def_supplier DEFAULT 1;
Manipulating Data
Data Manipulation Language
Common DML Statements
SELECT

SELECT* FROM tablename


Sample SELECT
SELECT first_name, last_name, salary FROM employees
WHERE salary >= 50,000
Multiple WHERE clauses
SELECT first_name, last_name FROM employees WHERE
department = ‘shipping’ AND gender = ‘F’ AND hired
>= ‘2000-JAN-01’

SELECT first_name, last_name FROM employees WHERE


department = ‘shipping’ OR employee_id <= 610007
Using the BETWEEN Clause
SELECT first_name, last_name, hire_date FROM
employees WHERE hire_date >= ‘1-Jan-1990’ AND
hire_date <= ‘1-Jan-2000’

SELECT first_name, last_name, hire_date FROM


employees WHERE hire_date BETWEEN ‘1-Jan-1990’ AND
‘1-Jan-2000’
Using the ORDER BY Clause
SELECT first_name, last_name, hire_date FROM
employees WHERE hire_date BETWEEN ‘1-Jan-1990’ AND
‘1-Jan-2000’ ORDER BY first_name

SELECT first_name, last_name, hire_date FROM


employees WHERE hire_date BETWEEN ‘1-Jan-1990’ AND
‘1-Jan-2000’ ORDER BY first_name ASC

SELECT first_name, last_name, hire_date FROM


employees WHERE hire_date BETWEEN ‘1-Jan-1990’ AND
‘1-Jan-2000’ ORDER BY first_name DESC
Using the NOT Clause
SELECT first_name, last_name FROM employees WHERE
NOT department = ‘shipping’

SELECT first_name, last_name FROM employees WHERE


department <> ‘shipping’
Using the UNION Clause

SELECT first_name, last_name FROM employees WHERE


department = ‘shipping’
UNION
SELECT first_name, last_name FROM employees WHERE
hire_date BETWEEN ‘1-Jan-1990’ AND ‘1-Jan-2000’
Using the EXCEPT and INTERSECT Clauses

SELECT ProductID FROM Production.Product


EXCEPT
SELECT ProductID FROM Production.WorkOrder;

SELECT ProductID FROM Production.Product


INTERSECT
SELECT ProductID FROM Production.WorkOrder;
Using the JOIN Clause
Using the JOIN Clause
INNER JOIN
SELECT employee.first_name, employee.last_name,
department.first_name, department.last_name FROM
employee INNER JOIN department ON employee.department
= department.department_id
OUTER JOIN
SELECT employee.first_name, employee.last_name,
department.first_name, department.last_name FROM
employee LEFT OUTER JOIN department ON
employee.department = department.department_id
Inserting Data
INSERT INTO <table_name> (<columns>)
VALUES (<values>)

INSERT INTO employee (first_name, last_name,


employee_id, department)
VALUES (‘David’, ‘Clark’, 610008, ‘shipping’),
(‘Arnold’, ‘Davis’, 610009, ‘accounting’)
Updating Data
UPDATE <table_name>
SET <attribute> = <value>
WHERE <conditions>

UPDATE employee SET department = 4 WHERE department IS


NULL

UPDATE employee SET department = 4 WHERE department =


1
Deleting Data
DELETE FROM <table_name>
WHERE <conditions>

DELETE FROM employee


WHERE department = ‘accounting’
Truncating a Table
TRUNCATE TABLE <table_name>
Using Transaction

BEGIN TRAN
DELETE FROM <table_name>
Table View
Table View

CREATE VIEW vwCustomer


AS
SELECT CustomerId, Company Name, Phone
FROM Customers
Product
ProductID Name Price Supplier
1 Widget 12.99 1
2 Thingybob 3.75 2
3 Knicknack NULL 1
4 Wotsit NULL 1

CREATE VIEW vw_ProductPrice


AS
SELECT Name, Price
Name Price
FROM Product
Thingybob 3.75
WHERE Supplier = 2;
SELECT Name, Price
FROM vw_ProductPrice;
Stored Procedures
Stored Procedures
Savings
CREATE PROCEDURE transferFunds AccountID Balance
AS 1 1000
BEGIN TRANSACTION 2 2000
3 1000
1500
UPDATE SAVINGS 4 2000
SET Balance += 500
WHERE AccountID = 3; Checking
AccountID Balance
UPDATE CHECKING 1 1000
SET Balance -= 500 2 2000
1000
WHERE AccountID = 3; 3 500
1000
COMMIT TRANSACTION 4 2000
1000

-- or if some error occurs


ROLLBACK TRANSACTION EXEC transferFunds;
Functions
Functions
Aggregate functions

SELECT
COUNT(DISTINCT SalesOrderlD) AS UniqueOrders,
AVG(UnitPrice) AS Avg_UnitPrice,
MIN(OrderQty)AS Min_OrderQty,
MAX(LineTotal) AS Max_LineTotal
FROM Sales.SalesOrderDetail;
Scalar functions

SELECT UCASE(Name), Price


FROM Product
User-define functions
Administering a Database
Security

Authentication Authorization Encryption Auditing


Security
Planning roles and permissions
Database Security
Security Level
Logins and Accounts
Server-level security
Fixed Server Roles
Database-level Security
Fixed Database Roles
Guest logon accounts
Managing object permissions
Object permissions
Database Backup
Database Backup

Full and partial Transaction log backups Back up locally


database backups for point-in-time or to the cloud
recovery
Database backups
Common types of backups
Database Restore
Restore scenarios
Monitoring and Maintenance
Monitoring and Maintenance
Server and database health
Resource usage
Job execution

Validate backups
Tune indexes

Updates to OS and SQL


Server
© 2016 Microsoft Corporation. All rights reserved. The text in this document is available under the Creative Commons Attribution 3.0 License, additional terms may apply. All other content contained in this
document (including, without limitation, trademarks, logos, images, etc.) are not included within the Creative Commons license grant. This document does not provide you with any legal rights to any
intellectual property in any Microsoft product. You may copy and use this document for your internal, reference purposes.
This document is provided "as-is." Information and views expressed in this document, including URL and other Internet Web site references, may change without notice. You bear the risk of using it. Some
examples are for illustration only and are fictitious. No real association is intended or inferred. Microsoft makes no warranties, express or implied, with respect to the information provided here.