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
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
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:
5 First Normal Form A database is in first normal form if there are no repeated fields. That means that there must only be one field for each item of data you want to register. A database is in first normal form if there are no repeated fields. That means that there must only be one field for each item of data you want to register. Each line has a primary key from which the other data belongs. Each line has a primary key from which the other data belongs. (First normal form not only facilitates searching, but is also more space efficient.) (First normal form not only facilitates searching, but is also more space efficient.)
6 Second Normal Form Second Normal Form is already in first normal form is already in first normal form has no fields that aren't dependent on the whole of the key has no fields that aren't dependent on the whole of the key That is to say that all fields are dependent on the whole of the key (where there is a compound key) That is to say that all fields are dependent on the whole of the key (where there is a compound key) A database is said to be in second normal form if it:
7 Third Normal Form 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 )
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.
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!
10 Testing software and test score database I. Entities: Entities: Class Teacher Students Score Grade Test Question Correct answer Software Student answer
11 Testing software and test score database II. Class Student Subject Enrolled Name Id Has Score Has Grade Teacher Name teaches Creates Test
12 Testing software and test score database III. Student Test gives Date Subject Has Student answer Question Has Corresct answer A,B,C.. Number Answer choices
13 Testing software and test score database IV. Software Student answerCorrect answer compare formula generates Score % value correlates Grade A,B,C..
14 What is SQL? SQL is a standard computer language for accessing and manipulating databases. SQL is a standard computer language for accessing and manipulating databases.
15 Notes SQL stands for Structured Query Language SQL stands for Structured Query Language 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
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
17 SQL Data Definition Language (DDL) The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables.
18 The most important DDL statements in SQL are: CREATE TABLE - creates a new database table CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index DROP INDEX - deletes an index
19 SQL Data Manipulation Language (DML) SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records. SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records. These query and update commands together form the Data Manipulation Language (DML) part of SQL: These query and update commands together form the Data Manipulation Language (DML) part of SQL:
20 The most important DML statements in SQL are: SELECT - extracts data from a database table SELECT - extracts data from a database table UPDATE - updates data in a database table UPDATE - updates data in a database table DELETE - deletes data from a database table DELETE - deletes data from a database table INSERT INTO - inserts new data into a database table INSERT INTO - inserts new data into a database table
21 The SELECT Statement The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set). The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set). Syntax Syntax SELECT column_name(s) FROM table_name SELECT column_name(s) FROM table_name
22 SQL The WHERE Clause The WHERE clause is used to specify a selection criterion. The WHERE clause is used to specify a selection criterion. Ex: Ex: SELECT * FROM Persons WHERE City='Sandnes‘ SELECT * FROM Persons WHERE City='Sandnes‘ SELECT * FROM Persons WHERE Year>1965 SELECT * FROM Persons WHERE Year>1965
23 The INSERT INTO Statement The INSERT INTO statement is used to insert new rows into a table. The INSERT INTO statement is used to insert new rows into a table. Syntax Syntax INSERT INTO table_name VALUES (value1, value2,....) INSERT INTO table_name VALUES (value1, value2,....) INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....) INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....)
24 The Update Statement The UPDATE statement is used to modify the data in a table. The UPDATE statement is used to modify the data in a table. Syntax Syntax UPDATE table_name SET column_name = new_value WHERE column_name = some_value UPDATE table_name SET column_name = new_value WHERE column_name = some_value We want to add a first name to the person with a last name of "Rasmussen": We want to add a first name to the person with a last name of "Rasmussen": UPDATE Person SET FirstName = 'Nina' WHERE LastName = 'Rasmussen' UPDATE Person SET FirstName = 'Nina' WHERE LastName = 'Rasmussen'
25 The Delete Statement The DELETE statement is used to delete rows in a table. The DELETE statement is used to delete rows in a table. Syntax Syntax DELETE FROM table_name WHERE column_name = some_value DELETE FROM table_name WHERE column_name = some_value
26 What is a Transaction? Logical unit of work Logical unit of work Must be either entirely completed or aborted Must be either entirely completed or aborted No intermediate states are acceptable No intermediate states are acceptable
27 Examine current account balance SELECT ACC_NUM, ACC_BALANCE FROM CHECKACC WHERE ACC_NUM = ‘ ’; Register credit sale of 100 units of product X to customer Y for $500 UPDATE PRODUCT SET PROD_QOH = PROD_QOH – 100 WHERE PROD_CODE = ‘X’; UPDATE ACCT_RECEIVABLE SET ACCT_BALANCE = ACCT_BALANCE WHERE ACCT_NUM =‘Y’; Consistent state only if both transactions are fully completed
28 TransactionTransaction Properties Transaction Atomicity Atomicity –All transaction operations must be completed –Incomplete transactions aborted Durability Durability –Permanence of consistent database state Serializability Serializability –Conducts transactions in serial order –Important in multi-user and distributed databases Isolation Isolation –Transaction data cannot be reused until its execution complete