28
1 What is database What is database 2 2 ? ? What is What is normalization? normalization? What is SQL? What is SQL? What is What is transaction? transaction?

1 What is database 2? What is normalization? What is SQL? What is transaction?

Embed Size (px)

Citation preview

Page 1: 1 What is database 2? What is normalization? What is SQL? What is transaction?

1

What is databaseWhat is database 2 2??

What is normalization?What is normalization?

What is SQL?What is SQL?

What is transaction?What is transaction?

Page 2: 1 What is database 2? What is normalization? What is SQL? What is transaction?

2

Data Redundancy & Inconsistency

Data Redundancy: a lot of data is „unnecessarily” being duplicated

Inconsistency :lack of internal unity

Page 3: 1 What is database 2? What is normalization? What is SQL? What is transaction?

3

• Anomaly of updated data

• Anomaly of deleted data

• Anomaly of inserted data

The solution to this problem is to use a database that consists of several tables - a relational database

Anomalies in database

Page 4: 1 What is database 2? What is normalization? What is SQL? What is transaction?

4

Normalization

• The process of refining the structure of a database to minimise redundancy and improve integrity is called normalisation.

• When a database has been normalised, it is said to be in normal form.

• There are three normal forms:

Page 5: 1 What is database 2? What is normalization? What is SQL? What is transaction?

5

First Normal FormFirst Normal Form

A database is in first normal form if there A database is in first normal form if there are no repeated fields. That means that there are no repeated fields. That means that there must only be one field for each item of data must only be one field for each item of data you want to you want to registerregister..

Each line has a primary key from which the Each line has a primary key from which the other data belongother data belongss..

((First normal form not only facilitates First normal form not only facilitates searching, but is also more space efficientsearching, but is also more space efficient..))

Page 6: 1 What is database 2? What is normalization? What is SQL? What is transaction?

6

Second Normal Form

is already in first normal formis already in first normal form

has no fields that aren't dependent on the whole of has no fields that aren't dependent on the whole of

the keythe key

That is to say that all fields are dependent on the That is to say that all fields are dependent on the

whole of the key (where there is a compound key) whole of the key (where there is a compound key)

A database is said to be in second normal A database is said to be in second normal

form if it:form if it:

Page 7: 1 What is database 2? What is normalization? What is SQL? What is transaction?

7

Third Normal Form

A database is in third normal form if it:

•is already in second normal form

•it has no non-key dependencies(By non-key dependencies, we mean that there are no fields that are dependent on other fields that are not part of the key )

Page 8: 1 What is database 2? What is normalization? What is SQL? What is transaction?

8

But forget all that! It still sounds a bit complicated to me. Don't try to start at first normal form, and then go to second normal form, and then to third. Once you've got the hang of it, you'll knock out databases in third normal form without even thinking about it. The key thing is to think about what your entities (or tables) are going to be - if you pick the right ones it'll normalise itself.

Page 9: 1 What is database 2? What is normalization? What is SQL? What is transaction?

9

 So, to get to third normal form, your non-repeating fields (first normal form) need to be dependent on the whole of the key (second normal form), and nothing other than the key (third normal form). It works for me!

Well, don't forget it all, however, because you may be asked about normalisation in an exam. Here's a little tip to remember the three stages. - we call it the Normalisation Oath:

Each attribute is dependent on the key, the whole key, and nothing but the

key!

Page 10: 1 What is database 2? What is normalization? What is SQL? What is transaction?

10

Testing software and test score Testing software and test score database I.database I.

Entities:Entities:

ClassTeacher

Students

Score

Grade

Test

Question

Correct answer

Software

Student answer

Page 11: 1 What is database 2? What is normalization? What is SQL? What is transaction?

11

Testing software and test score Testing software and test score database II.database II.

Class

Student

Subject

Enrolled

Name

IdHas

Score

Has

Grade

Teacher

Name

teaches

Creates

Test

Page 12: 1 What is database 2? What is normalization? What is SQL? What is transaction?

12

Testing software and test score Testing software and test score database III.database III.

StudentTest

gives

Date

Subject Has

Student answerQuestion

Has

Corresct answer

A,B,C..

A,B,C..

Number

Answer choices

Page 13: 1 What is database 2? What is normalization? What is SQL? What is transaction?

13

Testing software and test score Testing software and test score database IV.database IV.

Software

Student answer Correct answer

compare

formula

generates

Score% value correlates Grade

A,B,C..

Page 14: 1 What is database 2? What is normalization? What is SQL? What is transaction?

14

What is SQL?What is SQL?

SQL is a standard SQL is a standard computer language for computer language for accessing and accessing and manipulating manipulating databases.databases.

Page 15: 1 What is database 2? What is normalization? What is SQL? What is transaction?

15

NotesNotes

SQL stands for SQL stands for SStructured tructured QQuery uery LLanguage anguage SQL allows you to access a database SQL allows you to access a database SQL is an ANSI standard computer language SQL is an ANSI standard computer language SQL can execute queries against a database SQL can execute queries against a database SQL can retrieve data from a database SQL can retrieve data from a database SQL can insert new records in a database SQL can insert new records in a database SQL can delete records from a database SQL can delete records from a database SQL can update records in a database SQL can update records in a database SQL is easy to learn SQL is easy to learn

Page 16: 1 What is database 2? What is normalization? What is SQL? What is transaction?

16

SQL is a Standard

SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems. SQL statements are used to retrieve and update data in a database. SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, etc

Page 17: 1 What is database 2? What is normalization? What is SQL? What is transaction?

17

SQL Data Definition SQL Data Definition Language (DDL)Language (DDL)

The Data Definition Language The Data Definition Language (DDL) part of SQL permits (DDL) part of SQL permits database tables to be created or database tables to be created or deleted. We can also define deleted. We can also define indexes (keys), specify links indexes (keys), specify links between tables, and impose between tables, and impose constraints between database constraints between database tables.tables.

Page 18: 1 What is database 2? What is normalization? What is SQL? What is transaction?

18

The most important DDL The most important DDL statements in SQL are:statements in SQL are:

CREATE TABLECREATE TABLE - creates a new - creates a new database table database table

ALTER TABLE ALTER TABLE - alters (changes) a - alters (changes) a database table database table

DROP TABLEDROP TABLE - deletes a database - deletes a database table table

CREATE INDEXCREATE INDEX - creates an index - creates an index (search key) (search key)

DROP INDEXDROP INDEX - deletes an index  - deletes an index

Page 19: 1 What is database 2? What is normalization? What is SQL? What is transaction?

19

SQL Data Manipulation SQL Data Manipulation Language (DML)Language (DML)

SQL (Structured Query Language) SQL (Structured Query Language) is a syntax for executing queries. is a syntax for executing queries. But the SQL language also includes But the SQL language also includes a syntax to update, insert, and a syntax to update, insert, and delete records.delete records.

These query and update These query and update commands together form the Data commands together form the Data Manipulation Language (DML) part Manipulation Language (DML) part of SQL:of SQL:

Page 20: 1 What is database 2? What is normalization? What is SQL? What is transaction?

20

The most important DThe most important DMML L statements in SQL are:statements in SQL are:

SELECTSELECT - extracts data from a - extracts data from a database table database table

UPDATEUPDATE - updates data in a - updates data in a database table database table

DELETEDELETE - deletes data from a - deletes data from a database table database table

INSERT INTOINSERT INTO - inserts new data - inserts new data into a database tableinto a database table

Page 21: 1 What is database 2? What is normalization? What is SQL? What is transaction?

21

The SELECT StatementThe SELECT Statement

The SELECT statement is used to The SELECT statement is used to select data from a table. The select data from a table. The tabular result is stored in a result tabular result is stored in a result table (called the result-set).table (called the result-set).

SyntaxSyntax SELECT column_name(s) FROM SELECT column_name(s) FROM

table_nametable_name

Page 22: 1 What is database 2? What is normalization? What is SQL? What is transaction?

22

SQL The WHERE ClauseSQL The WHERE Clause

The WHERE clause is used to The WHERE clause is used to specify a selection criterion.specify a selection criterion.

Ex:Ex: SELECT * FROM Persons SELECT * FROM Persons

WHERE City='Sandnes‘WHERE City='Sandnes‘ SELECT * FROM Persons SELECT * FROM Persons

WHERE Year>1965WHERE Year>1965

Page 23: 1 What is database 2? What is normalization? What is SQL? What is transaction?

23

The INSERT INTO The INSERT INTO StatementStatement

The INSERT INTO statement is The INSERT INTO statement is used to insert new rows into a used to insert new rows into a table.table.

SyntaxSyntax INSERT INTO table_nameINSERT INTO table_name VALUES VALUES

(value1, value2,....)(value1, value2,....) INSERT INTO table_name INSERT INTO table_name

(column1, column2,...) VALUES (column1, column2,...) VALUES (value1, value2,....)(value1, value2,....)

Page 24: 1 What is database 2? What is normalization? What is SQL? What is transaction?

24

The Update StatementThe Update Statement

The UPDATE statement is used to modify The UPDATE statement is used to modify the data in a table.the data in a table.

SyntaxSyntax UPDATE table_name SET column_name UPDATE table_name SET column_name

= new_value= new_value WHERE column_name = WHERE column_name = some_valuesome_value

We want to add a first name to the We want to add a first name to the person with a last name of "Rasmussen":person with a last name of "Rasmussen":

UPDATE Person SET FirstName = 'Nina' UPDATE Person SET FirstName = 'Nina' WHERE LastName = 'RasmussenWHERE LastName = 'Rasmussen''

Page 25: 1 What is database 2? What is normalization? What is SQL? What is transaction?

25

The Delete StatementThe Delete Statement

The DELETE statement is used to The DELETE statement is used to delete rows in a table.delete rows in a table.

SyntaxSyntax DELETE FROM table_name WHERE DELETE FROM table_name WHERE

column_name = some_valuecolumn_name = some_value

Page 26: 1 What is database 2? What is normalization? What is SQL? What is transaction?

26

What is a Transaction?What is a Transaction?

Logical unit of work Logical unit of work Must be either entirely completed or abortedMust be either entirely completed or aborted No intermediate states are acceptableNo intermediate states are acceptable

Page 27: 1 What is database 2? What is normalization? What is SQL? What is transaction?

27

Examine current account balance

SELECT ACC_NUM, ACC_BALANCEFROM CHECKACCWHERE ACC_NUM = ‘0908110638’;

Register credit sale of 100 units of product X to customer Y for $500

UPDATE PRODUCTSET PROD_QOH = PROD_QOH – 100 WHERE PROD_CODE = ‘X’;

UPDATE ACCT_RECEIVABLE

SET ACCT_BALANCE = ACCT_BALANCE + 500 WHERE ACCT_NUM =‘Y’;

•Consistent state only if both transactions are fully completed

Page 28: 1 What is database 2? What is normalization? What is SQL? What is transaction?

28

Transaction Properties Properties

Atomicity Atomicity – All transaction operations must be completedAll transaction operations must be completed– Incomplete transactions abortedIncomplete transactions aborted

Durability Durability – Permanence of consistent database statePermanence of consistent database state

Serializability Serializability – Conducts transactions in serial orderConducts transactions in serial order– Important in multi-user and distributed databasesImportant in multi-user and distributed databases

Isolation Isolation – Transaction data cannot be reused until its execution Transaction data cannot be reused until its execution

completecomplete