SlideShare a Scribd company logo
1 of 56
Database Basics
From developer to developers
Database Basics
 What is a database, DBMS and its types?
 What makes a database?
 Object-Oriented Design vs. Database Design
 Database Normalization
 Structured Query Language (SQL)
What is a database?
Database Definition
What is a database?
 “A database is an organized collection of data. The data is typically
organized to model aspects of reality in a way that supports processes
requiring information”. [Wikipedia.org Definition]
 “A database provides for the storing and control of business information,
independent from (but not separate from the processing requirements of)
one or more applications”. [IBM Definition]
 “A database is a collection of information that is organized so that it can
easily be accessed, managed, and updated”. [WhatIs.com Definition]
 So, What do you think?
Database Models, Types and Formats
 Flat-file Model (Mainframes)
 Hierarchical Model (Tree-Structure, LDAP)
 Relational Model (RDBMS)
 Object-Oriented Model (Apache Derby DB, H2 and In-memory databases)
 Not Only SQL (NoSQL): Semi structured Model, Structure within data
What is a RDBMS?
 RDBMS: Relational Database Management System
 A tool for organizing, storing, retrieving, and communicating groups of
information that have similar characteristics.
 Examples are: ORACLE DB, MySQL, MS Access, MS SQL Server, IBM DB2 and
Sybase
What makes a database?
Database Objects
What makes a database?
Schema
(Owner)
Tables Views Indices Sequences Triggers Constraints Subroutines
Table
 The primary unit of physical storage for data in a database.
 Stores data, so they’re essential building blocks of any database.
 Any database consists of one or more tables (at least one)
 Contains Rows called Records and Columns called Fields.
Cell - ENTRY
Rows – RECORDS
Columns - FIELDS
ID CustomerName Email City
1 Mahmoud mahmoud@test.com Cairo
2 Samy samy@test.com Alex
3 Aly aly@test.com Aswan
View
 Views can represent a subset of the data contained in a table. Consequently,
a view can limit the degree of exposure of the underlying tables to the outer
world: a given user may have permission to query the view, while denied
access to the rest of the base table.
 Views can join and simplify multiple tables into a single virtual table.
 Views can act as aggregated tables, where the database engine aggregates
data (sum, average, etc.) and presents the calculated results as part of the
data.
 Views can hide the complexity of data. For example, a view could appear as
Sales2000 or Sales2001, transparently partitioning the actual underlying
table.
 Views take very little space to store; the database contains only the
definition of a view, not a copy of all the data that it presents.
 It can be read-only or updatable
Index
 A data structure that improves the speed of data retrieval operations on a
database table at the cost of additional writes and storage space to maintain
the index data structure.
 Used to quickly locate data without having to search every row in a database
table every time a database table is accessed.
 Can be created using one or more columns of a database table, providing the
basis for both rapid random lookups and efficient access of ordered records.
 An index is a copy of select columns of data from a table that can be
searched very efficiently that also includes a low-level disk block address or
direct link to the complete row of data it was copied from.
 The Primary Key is merely an index that enforces uniqueness
Sequence
 The sequence is a feature by some database products which just creates
unique values. It just increments a value and returns it.
 The special thing about it is: there is no transaction isolation, so several
transactions can not get the same value, the incremental is also not rolled
back.
 Sequence will allow you to populate primary key with a unique, serialized
number.
 Without a database sequence, it is very hard to generate unique incrementing
numbers.
 Other database products support columns that are automatically initialized
with a incrementing number.
Trigger
 A procedural code that is automatically executed in response to certain
events on a particular table or view in a database.
 The trigger is mostly used for maintaining the integrity of the information on
the database.
 For example, in HRMS database when a new record (representing a new
worker) is added to the employees table, new records should also be created
in the tables of the taxes, vacations and salaries.
 Can run before or after a database change.
 Can run at the cell level, row level, column level, or statement level.
 In some implementations, it can call an external program
Constraints
 It can be a single rule or a set of rules to restrict the data entered in the
table and guarantee its correctness.
 If there is any violation between the constraint and the data action, the
action is aborted by the constraint.
 Can apply to a single column or the entire table
 Can be specified when the table is created or after that
 In SQL, we have the following constraints:
NOT NULL - UNIQUE - PRIMARY KEY - FOREIGN KEY - CHECK – DEFAULT
 Advantages:
1. Input Validation
2. Data Consistency
3. Intelligent Defaults
Subroutines
 A sequence of program instructions that perform a specific task, packaged as
a unit and stored in the database.
 Can consolidate and centralize logic that was originally implemented in
applications
 Sometimes called a stored procedure (SP), function or User Defined Function
(UDF)
 Advantages:
1. Simple, no overhead
2. Avoidance of network traffic
3. Encapsulation of business logic
4. Delegation of access-rights
5. Some protection from SQL injection attacks
Database Design
In comparison with Object-Oriented Design
Entity Structure
Customer
• id: int
• name: String
• birthDate: Date
• salary: double
• jobCode: String
CUSTOMER
• C_ID: INTEGER
• C_NAME: VARCHAR(25)
• C_BIRTH_DATE: DATE
• C_SAL: DOUBLE
• C_JOB_CODE: CHAR(3)
 CUSTOMER TABLE structure can be represented by a Customer Class
 Every COLUMN is mapped to its equivalent class attribute/variable
 Data types may vary according to the database vendor / programming
language
Entity Data
ID Name Birth Date Salary Job Code
5011 Moneim Emad 03/07/1982 5,000.00 SAR
5012 Kareem Hewady 28/06/1993 3,000.00 OSD
 A single, specific occurrence of an entity is an instance.
 The selected record/row represents a single record in CUSTOMER table
 This is mapped to an instance / object of the Customer class
 The entire table data can be mapped to a list of Customer objects
Entity Relationship Diagram (ERD)
 A relationship is a link that relates two entities that share one or more attributes
 An ERD can be mapped to UML Class Diagram
 Database relations between tables can be mapped to UML association between classes
CustomerPublisher Book
Author Inventory Order
Relationship: Primary Key
ID Name Birth Date Salary Job Code
5011 Moneim Emad 03/07/1982 5,000.00 SAR
5012 Kareem Hewady 28/06/1993 3,000.00 OSD
 A primary key is a unique identifier of records in a table.
 A primary key values may be generated manually or automatically
 A primary key can consist of more than one column (composite key)
Relationship: Foreign Key
ID Name Birth Date Salary Job Code
5011 Moneim Emad 03/07/1982 5,000.00 SAR
5012 Kareem Hewady 28/06/1993 3,000.00 OSD
 A foreign key is a column reference to the primary key in its original table
 In contrast to primary key, foreign key may be repeated in the child table
ID Customer ID Location Delivery Date
1 5011 El Dokky 27/03/2015 11:45 AM
2 5011 Nasr City 28/03/2015 09:30 AM
3 5897 El Zamalek 28/03/2015 02:15 PM
ORDER (Child table)
CUSTOMER (Parent table)
Relationship Types
 One-to-one
Customer Basic info  Customer Additional Info
 One-to-many
Customer info  Customer Orders Info
 Many-to-many
Customer Info  Customer Books Interest  Books Info
Normalization
Database Best Practices
Normalization
 A set of recommendations to be followed when designing a database that
helps for organizing data elements into tables and optimizing the database.
 We will see an example for database normalization in three steps using the
three normal forms
1NF 2NF 3NF
Normalization: Case Study (Un-normalized)
SalesOrders
• SalesOrderNo
• Date
• CustomerNo
• CustomerName
• CutomerAddress
• ClerkNo
• ClerkName
• Item1Description
• Item1Quantity
• Item1UnitPrice
• Item2Description
• Item2Quantity
• Item2UnitPrice
• Item3Description
• Item3Quantity
• Item3UnitPrice
• Total
Normalization: Into 1NF
 Separate repeating groups into new tables.
 Start a new table for the repeating data.
 The primary key for the repeating group is usually a composite key.
Normalization: Into 1NF
SalesOrders
• SalesOrderNo
• Date
• CustomerNo
• CustomerName
• CutomerAddress
• ClerkNo
• ClerkName
• Item1Description
• Item1Quantity
• Item1UnitPrice
• Item2Description
• Item2Quantity
• Item2UnitPrice
• Item3Description
• Item3Quantity
• Item3UnitPrice
• Total
• SalesOrderNo
• Date
• CustomerNo
• CustomerName
• CutomerAddress
• ClerkNo
• ClerkName
• Total
SalesOrders
OrderItems
• SalesOrderNo
• ItemNo
• ItemDescription
• ItemQuantity
• ItemUnitPrice
Normalization: Into 2NF
 Remove partial dependencies.
 Start a new table for the partially dependent data and the part of the key it
depends on.
 Tables started at this step usually contain descriptions of resources.
Normalization: Into 2NF
 Functional dependency:
The value of one attribute depends entirely on the value of another.
 Partial dependency:
An attribute depends on only part of the primary key. (The primary key must
be a composite key.)
 Transitive dependency:
An attribute depends on an attribute other than the primary key.
Normalization: Into 2NF
OrderItems
• SalesOrderNo
• ItemNo
• ItemDescription
• ItemQuantity
• ItemUnitPrice
OrderItems
• SalesOrderNo
• ItemNo
• ItemQuantity
• ItemUnitPrice
Item
• ItemNo
• ItemDescription
Normalization: Into 2NF – Problems Solved
 Duplication of data:
ItemDescription would appear for every order.
 Insert anomaly:
To insert an inventory item, you must insert a sales order.
 Delete anomaly:
Information about the items stay with sales order records. Delete a sales
order record, delete the item description.
 Update anomaly:
To change an item description, you must change all the sales order
records that have the item.
Normalization: Into 3NF
 Remove transitive dependencies.
 Start a new table for the transitively dependent attribute and the attribute it
depends on.
 Keep a copy of the key attribute in the original table.
Normalization: Into 3NF
• SalesOrderNo
• Date
• CustomerNo
• CustomerName
• CutomerAddress
• ClerkNo
• ClerkName
• Total
SalesOrders
Clerk
• ClerkNo
• ClerkName
Customer
• CustomerNo
• CustomerName
• CustomerAddress
• SalesOrderNo
• Date
• CustomerNo
• ClerkNo
• Total
SalesOrders
Normalization: Into 3NF – Problems Solved
 Duplication of data:
Customer and Clerk details would appear for every order.
 Insert anomaly:
To insert a customer or clerk, you must insert a sales order.
 Delete anomaly:
Information about the customers and clerks stay with sales order records.
Delete a sales order record, delete the customer or clerk.
 Update anomaly:
To change the details of a customer or clerk, you must change all the sales
order that involve that customer or clerk.
Normalization: Final Results
Clerk
• ClerkNo
• ClerkName
Customer
• CustomerNo
• CustomerName
• CustomerAddress
• SalesOrderNo
• Date
• CustomerNo
• ClerkNo
• Total
SalesOrders
SalesOrders
• SalesOrderNo
• Date
• CustomerNo
• CustomerName
• CutomerAddress
• ClerkNo
• ClerkName
• Item1Description
• Item1Quantity
• Item1UnitPrice
• Item2Description
• Item2Quantity
• Item2UnitPrice
• Item3Description
• Item3Quantity
• Item3UnitPrice
• Total
OrderItems
• SalesOrderNo
• ItemNo
• ItemQuantity
• ItemUnitPrice
Item
• ItemNo
• ItemDescription
Structured Query Language
Database Development
SQL
 SQL is a standard language for accessing and manipulating databases.
 Stands for Structured Query Language.
 SQL can execute administrative, structural and data manipulation statements
 Administrative:
Set permissions on schemas, tables, procedures and views
 Structural:
Create, Drop or Alter tables, procedures and views
 Data Manipulation:
Retrieve data from a database
Insert, update or delete records from a database
SQL
 Although SQL is an ANSI (American National Standards Institute) standard,
there are different versions of the SQL language.
 Most of the big database vendors have their own proprietary extensions in
addition to the SQL standard.
 However, to be compliant with the ANSI standard, they all support at least the
major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a
similar manner.
 SQL is NOT case sensitive: select is the same as SELECT.
 As a coding convention, we should write all SQL keywords in upper-case.
 Semicolon is the standard way to separate each SQL statement in database
systems that allow more than one SQL statement to be executed in the same
call to the server.
 Some database vendors let you choose your statement terminator character.
SQL
 SELECT - extracts data from a database
 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index
SQL Syntax
SELECT col1_name,col2_name FROM table_name;
SELECT * FROM table_name;
 The SELECT DISTINCT statement is used to return only distinct (different) values.
SELECT DISTINCT column1_name,column2_name FROM table_name;
 The WHERE clause is used to extract only those records that fulfill a specified criterion.
SELECT col1_name,col2_name FROM table_name WHERE boolean_condition;
WHERE CITY=‘Cairo’
WHERE (CUSTOMER_ID=5011 OR CITY<>’Alex’)
WHERE CUSTOMER_ID IN(5011,5378,5834)
WHERE (PRICE>=5000 AND PRICE<10000)
WHERE CUSTOMER_NAME LIKE ’%Moneim%’
WHERE HIRE_DATE BETWEEN 2006-12-31 AND 2004-01-01
SQL Syntax
INSERT INTO table_name VALUES (value1,value2,value3,...);
INSERT INTO table_name (col1,col2,col3) VALUES (val1,val2,val3);
UPDATE table_name SET col1=val1,col2=val2 WHERE col5=val5;
DELETE FROM table_name WHERE col3=val5;
 The WHERE clause specifies which record or records that should be updated or deleted.
 If you omit the WHERE clause, all records will be updated or deleted!
 Using Aliases for tables and columns
SELECT CUSTOMER_NAME AS C_NAME FROM CUSTOMER AS C;
SELECT CUSTOMER_NAME C_NAME FROM CUSTOMER C;
SQL Syntax
 Using NOT & Wildcards
WHERE ID NOT IN (5011,5012); or WHERE PROVIDER NOT LIKE ‘%mobinil%’;
WHERE City LIKE '_erlin'; or WHERE City LIKE 'L_n_on';
 City starting with "b", "s", or "p": WHERE City LIKE '[bsp]%'
 starting with "a", "b", or "c": WHERE City LIKE '[a-c]%
 City NOT starting with "b", "s", or "p":
WHERE City LIKE '[!bsp]%'; or WHERE City NOT LIKE '[bsp]%';
WHERE Price NOT BETWEEN 10 AND 20;
WHERE ProductName BETWEEN 'C' AND 'M'; -- beginning with C,D,…,M
 Ordering Data
SELECT col1,col2 FROM table_name ORDER BY col1 ASC|DESC;-- Ascending
ORDER BY col1 ASC|DESC, col2 ASC|DESC;
SQL Syntax - JOINs
 An SQL JOIN clause is used to combine rows from two or more tables, based on a common
field between them.
 The most common type of join is: SQL INNER JOIN (simple join).
 INNER JOIN: Returns all rows when there is at least one match in BOTH tables
 LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
 RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
 FULL JOIN: Return all rows when there is a match in ONE of the tables
SQL Syntax - JOINs
A B A B
A B A B
INNER JOIN LEFT OUTER JOIN
RIGHT OUTER JOIN FULL JOIN
SQL Syntax – INNER JOIN
OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 2 1996-09-19
10310 1 1996-09-20
10311 1996-09-21
ID CustomerName Email City
1 Mahmoud mahmoud@test.com Cairo
2 Samy samy@test.com Alex
3 Aly aly@test.com Aswan
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers [INNER] JOIN Orders
ON Customers.ID=Orders.CustomerID
ORDER BY Customers.CustomerName; CustomerName OrderID
Mahmoud 10310
Samy 10308
Samy 10309
SQL Syntax – LEFT OUTER JOIN
OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 2 1996-09-19
10310 1 1996-09-20
10311 1996-09-21
ID CustomerName Email City
1 Mahmoud mahmoud@test.com Cairo
2 Samy samy@test.com Alex
3 Aly aly@test.com Aswan
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers LEFT JOIN Orders
ON Customers.ID=Orders.CustomerID
ORDER BY Customers.CustomerName; CustomerName OrderID
Aly NULL
Mahmoud 10310
Samy 10308
Samy 10309
SQL Syntax – RIGHT OUTER JOIN
OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 2 1996-09-19
10310 1 1996-09-20
10311 1996-09-21
ID CustomerName Email City
1 Mahmoud mahmoud@test.com Cairo
2 Samy samy@test.com Alex
3 Aly aly@test.com Aswan
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers RIGHT JOIN Orders
ON Customers.ID=Orders.CustomerID
ORDER BY Customers.CustomerName; CustomerName OrderID
Mahmoud 10310
Samy 10308
Samy 10309
NULL 10311
SQL Syntax – FULL OUTER JOIN
OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 2 1996-09-19
10310 1 1996-09-20
10311 1996-09-21
ID CustomerName Email City
1 Mahmoud mahmoud@test.com Cairo
2 Samy samy@test.com Alex
3 Aly aly@test.com Aswan
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers FULL JOIN Orders
ON Customers.ID=Orders.CustomerID
ORDER BY Customers.CustomerName; CustomerName OrderID
Aly NULL
Mahmoud 10310
Samy 10308
Samy 10309
NULL 10311
SQL Syntax - Advanced
 Using SELECT INTO
SELECT Customers.CustomerName, Orders.OrderID
INTO CustomersOrderBackup2013
FROM Customers LEFT JOIN Orders ON Customers.ID=Orders.CustomerID;
 Using INSERT INTO SELECT
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;
 Using SQL Create
CREATE DATABASE dbname;
CREATE TABLE Persons(PersonID int NOT NULL UNIQUE,LastName varchar(255),
FirstName varchar(255),Address varchar(255),City varchar(255));
SQL Aggregate Functions
 AVG() Returns the average value
 COUNT() Returns the number of rows
 FIRST() Returns the first value
 LAST() Returns the last value
 MAX() Returns the largest value
 MIN() Returns the smallest value
 SUM() Returns the sumUsing SELECT INTO
SQL Scalar Functions
 UCASE() Converts a field to upper case
 LCASE() Converts a field to lower case
 MID() Extract characters from a text field
 LEN() Returns the length of a text field
 ROUND() Rounds a numeric field to the number of decimals specified
 NOW() Returns the current system date and time
 FORMAT() Formats how a field is to be displayed
SQL GROUP BY Statement
 The GROUP BY statement is used in conjunction with the aggregate functions
to group the result set by one or more columns.
 It can be used for summary reports
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders
FROM Orders LEFT JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName;
ShipperName NumberOfOrders
Federal Shipping 68
Speedy Express 54
United Package 74
SQL GROUP BY Statement
 Group by two columns
SELECT Shippers.ShipperName, Employees.LastName,
COUNT(Orders.OrderID) AS NumberOfOrders FROM ((Orders
INNER JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID)
INNER JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY ShipperName,LastName;
ShipperName Last Name NumberOfOrders
Federal Shipping Baher 38
Federal Shipping Mona 30
Speedy Express Aly 54
United Package Kareem 70
United Package Yasser 4
SQL HAVING Clause
 The HAVING clause was added to SQL because the WHERE keyword could not be
used with aggregate functions.
 Suppose that we want to find if any of the employees has registered more than 10 orders.
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM (Orders INNER JOIN Employees
ON Orders.EmployeeID=Employees.ID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;
Last Name NumberOfOrders
Aly 54
Baher 38
Kareem 70
Mona 30
SQL Injection
 An SQL injection can destroy your database
 SQL injection is a technique where malicious users can inject SQL commands into an SQL
statement, via web page input.
 Injected SQL commands can alter SQL statement and compromise the security of a web
application.
 txtUserId = getRequestString("UserId");
txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;
 UserId: 105 or 1=1
 SELECT * FROM Users WHERE UserId = 105 or 1=1
 SELECT * FROM Users WHERE UserId = 105 or ""=""
 UserId: 105; DROP TABLE SUPPLIERS
 Use SQL Parameters / PreparedStatements
References
 W3Schools.com
http://www.w3schools.com/sql/sql_default.asp
 Wikipedia.org
https://en.wikipedia.org/
 WhatIs.com
http://whatis.techtarget.com/
 IBM IMS for z/OS – Chapter 12
http://csis.pace.edu/lixin/mainframe/zos-slides/Chapter12%20Database%20slides.ppt
 North Carolina Virtual Public School – Database Fundamentals
http://www.ncvps.org/BbContent/CompApp/6411.08_Apps_1_V2%20Word/6411.08-v2-Unit-B-
5.01/Database-Fundamentals.ppt

More Related Content

What's hot

Object oriented database model
Object oriented database modelObject oriented database model
Object oriented database modelPAQUIAAIZEL
 
12. oracle database architecture
12. oracle database architecture12. oracle database architecture
12. oracle database architectureAmrit Kaur
 
Object Oriented Dbms
Object Oriented DbmsObject Oriented Dbms
Object Oriented Dbmsmaryeem
 
Introduction to database
Introduction to databaseIntroduction to database
Introduction to databaseArpee Callejo
 
Week 3 Classification of Database Management Systems & Data Modeling
Week 3 Classification of Database Management Systems & Data ModelingWeek 3 Classification of Database Management Systems & Data Modeling
Week 3 Classification of Database Management Systems & Data Modelingoudesign
 
MS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database ConceptsMS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database ConceptsDataminingTools Inc
 
File systems versus a dbms
File systems versus a dbmsFile systems versus a dbms
File systems versus a dbmsRituBhargava7
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMSkoolkampus
 
Relational databases vs Non-relational databases
Relational databases vs Non-relational databasesRelational databases vs Non-relational databases
Relational databases vs Non-relational databasesJames Serra
 
Fundamentals of Database ppt ch01
Fundamentals of Database ppt ch01Fundamentals of Database ppt ch01
Fundamentals of Database ppt ch01Jotham Gadot
 

What's hot (20)

Oracle
OracleOracle
Oracle
 
Oracle 10g Introduction 1
Oracle 10g Introduction 1Oracle 10g Introduction 1
Oracle 10g Introduction 1
 
Object oriented database model
Object oriented database modelObject oriented database model
Object oriented database model
 
12. oracle database architecture
12. oracle database architecture12. oracle database architecture
12. oracle database architecture
 
Object Oriented Dbms
Object Oriented DbmsObject Oriented Dbms
Object Oriented Dbms
 
Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
 
Week 3 Classification of Database Management Systems & Data Modeling
Week 3 Classification of Database Management Systems & Data ModelingWeek 3 Classification of Database Management Systems & Data Modeling
Week 3 Classification of Database Management Systems & Data Modeling
 
MS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database ConceptsMS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database Concepts
 
File systems versus a dbms
File systems versus a dbmsFile systems versus a dbms
File systems versus a dbms
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS
 
Object oriented database
Object oriented databaseObject oriented database
Object oriented database
 
Relational databases vs Non-relational databases
Relational databases vs Non-relational databasesRelational databases vs Non-relational databases
Relational databases vs Non-relational databases
 
Dbms slides
Dbms slidesDbms slides
Dbms slides
 
Sql server basics
Sql server basicsSql server basics
Sql server basics
 
Introduction to Database
Introduction to DatabaseIntroduction to Database
Introduction to Database
 
Fundamentals of Database ppt ch01
Fundamentals of Database ppt ch01Fundamentals of Database ppt ch01
Fundamentals of Database ppt ch01
 
NoSql
NoSqlNoSql
NoSql
 
Object oriented databases
Object oriented databasesObject oriented databases
Object oriented databases
 
Database
DatabaseDatabase
Database
 
Xml databases
Xml databasesXml databases
Xml databases
 

Similar to Database Basics

Sqlserver interview questions
Sqlserver interview questionsSqlserver interview questions
Sqlserver interview questionsTaj Basha
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifahalish sha
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database ModelsPrithwis Mukerjee
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database ModelsPrithwis Mukerjee
 
Advance sqlite3
Advance sqlite3Advance sqlite3
Advance sqlite3Raghu nath
 
Sql Interview Questions
Sql Interview QuestionsSql Interview Questions
Sql Interview Questionsarjundwh
 
153680 sqlinterview
153680  sqlinterview153680  sqlinterview
153680 sqlinterviewzdsgsgdf
 
Data Warehouse ( Dw Of Dwh )
Data Warehouse ( Dw Of Dwh )Data Warehouse ( Dw Of Dwh )
Data Warehouse ( Dw Of Dwh )Jenny Calhoon
 
PPT SQL CLASS.pptx
PPT SQL CLASS.pptxPPT SQL CLASS.pptx
PPT SQL CLASS.pptxAngeOuattara
 
Data warehousing interview_questionsandanswers
Data warehousing interview_questionsandanswersData warehousing interview_questionsandanswers
Data warehousing interview_questionsandanswersSourav Singh
 
Introduction to database with ms access.hetvii
Introduction to database with ms access.hetviiIntroduction to database with ms access.hetvii
Introduction to database with ms access.hetvii07HetviBhagat
 
Introduction to database with ms access(DBMS)
Introduction to database with ms access(DBMS)Introduction to database with ms access(DBMS)
Introduction to database with ms access(DBMS)07HetviBhagat
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresSteven Johnson
 

Similar to Database Basics (20)

T-SQL Overview
T-SQL OverviewT-SQL Overview
T-SQL Overview
 
Sqlserver interview questions
Sqlserver interview questionsSqlserver interview questions
Sqlserver interview questions
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifah
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 
BIS06 Physical Database Models
BIS06 Physical Database ModelsBIS06 Physical Database Models
BIS06 Physical Database Models
 
Data warehouse physical design
Data warehouse physical designData warehouse physical design
Data warehouse physical design
 
Advance sqlite3
Advance sqlite3Advance sqlite3
Advance sqlite3
 
Sql
SqlSql
Sql
 
Sql Interview Questions
Sql Interview QuestionsSql Interview Questions
Sql Interview Questions
 
Sql
SqlSql
Sql
 
Sql
SqlSql
Sql
 
153680 sqlinterview
153680  sqlinterview153680  sqlinterview
153680 sqlinterview
 
Data Warehouse ( Dw Of Dwh )
Data Warehouse ( Dw Of Dwh )Data Warehouse ( Dw Of Dwh )
Data Warehouse ( Dw Of Dwh )
 
PPT SQL CLASS.pptx
PPT SQL CLASS.pptxPPT SQL CLASS.pptx
PPT SQL CLASS.pptx
 
Fg d
Fg dFg d
Fg d
 
Database fundamentals
Database fundamentalsDatabase fundamentals
Database fundamentals
 
Data warehousing interview_questionsandanswers
Data warehousing interview_questionsandanswersData warehousing interview_questionsandanswers
Data warehousing interview_questionsandanswers
 
Introduction to database with ms access.hetvii
Introduction to database with ms access.hetviiIntroduction to database with ms access.hetvii
Introduction to database with ms access.hetvii
 
Introduction to database with ms access(DBMS)
Introduction to database with ms access(DBMS)Introduction to database with ms access(DBMS)
Introduction to database with ms access(DBMS)
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome Measures
 

Recently uploaded

vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookvip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookmanojkuma9823
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 

Recently uploaded (20)

vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Bookvip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
vip Sarai Rohilla Call Girls 9999965857 Call or WhatsApp Now Book
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 

Database Basics

  • 2. Database Basics  What is a database, DBMS and its types?  What makes a database?  Object-Oriented Design vs. Database Design  Database Normalization  Structured Query Language (SQL)
  • 3. What is a database? Database Definition
  • 4. What is a database?  “A database is an organized collection of data. The data is typically organized to model aspects of reality in a way that supports processes requiring information”. [Wikipedia.org Definition]  “A database provides for the storing and control of business information, independent from (but not separate from the processing requirements of) one or more applications”. [IBM Definition]  “A database is a collection of information that is organized so that it can easily be accessed, managed, and updated”. [WhatIs.com Definition]  So, What do you think?
  • 5. Database Models, Types and Formats  Flat-file Model (Mainframes)  Hierarchical Model (Tree-Structure, LDAP)  Relational Model (RDBMS)  Object-Oriented Model (Apache Derby DB, H2 and In-memory databases)  Not Only SQL (NoSQL): Semi structured Model, Structure within data
  • 6. What is a RDBMS?  RDBMS: Relational Database Management System  A tool for organizing, storing, retrieving, and communicating groups of information that have similar characteristics.  Examples are: ORACLE DB, MySQL, MS Access, MS SQL Server, IBM DB2 and Sybase
  • 7. What makes a database? Database Objects
  • 8. What makes a database? Schema (Owner) Tables Views Indices Sequences Triggers Constraints Subroutines
  • 9. Table  The primary unit of physical storage for data in a database.  Stores data, so they’re essential building blocks of any database.  Any database consists of one or more tables (at least one)  Contains Rows called Records and Columns called Fields. Cell - ENTRY Rows – RECORDS Columns - FIELDS ID CustomerName Email City 1 Mahmoud mahmoud@test.com Cairo 2 Samy samy@test.com Alex 3 Aly aly@test.com Aswan
  • 10. View  Views can represent a subset of the data contained in a table. Consequently, a view can limit the degree of exposure of the underlying tables to the outer world: a given user may have permission to query the view, while denied access to the rest of the base table.  Views can join and simplify multiple tables into a single virtual table.  Views can act as aggregated tables, where the database engine aggregates data (sum, average, etc.) and presents the calculated results as part of the data.  Views can hide the complexity of data. For example, a view could appear as Sales2000 or Sales2001, transparently partitioning the actual underlying table.  Views take very little space to store; the database contains only the definition of a view, not a copy of all the data that it presents.  It can be read-only or updatable
  • 11. Index  A data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure.  Used to quickly locate data without having to search every row in a database table every time a database table is accessed.  Can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records.  An index is a copy of select columns of data from a table that can be searched very efficiently that also includes a low-level disk block address or direct link to the complete row of data it was copied from.  The Primary Key is merely an index that enforces uniqueness
  • 12. Sequence  The sequence is a feature by some database products which just creates unique values. It just increments a value and returns it.  The special thing about it is: there is no transaction isolation, so several transactions can not get the same value, the incremental is also not rolled back.  Sequence will allow you to populate primary key with a unique, serialized number.  Without a database sequence, it is very hard to generate unique incrementing numbers.  Other database products support columns that are automatically initialized with a incrementing number.
  • 13. Trigger  A procedural code that is automatically executed in response to certain events on a particular table or view in a database.  The trigger is mostly used for maintaining the integrity of the information on the database.  For example, in HRMS database when a new record (representing a new worker) is added to the employees table, new records should also be created in the tables of the taxes, vacations and salaries.  Can run before or after a database change.  Can run at the cell level, row level, column level, or statement level.  In some implementations, it can call an external program
  • 14. Constraints  It can be a single rule or a set of rules to restrict the data entered in the table and guarantee its correctness.  If there is any violation between the constraint and the data action, the action is aborted by the constraint.  Can apply to a single column or the entire table  Can be specified when the table is created or after that  In SQL, we have the following constraints: NOT NULL - UNIQUE - PRIMARY KEY - FOREIGN KEY - CHECK – DEFAULT  Advantages: 1. Input Validation 2. Data Consistency 3. Intelligent Defaults
  • 15. Subroutines  A sequence of program instructions that perform a specific task, packaged as a unit and stored in the database.  Can consolidate and centralize logic that was originally implemented in applications  Sometimes called a stored procedure (SP), function or User Defined Function (UDF)  Advantages: 1. Simple, no overhead 2. Avoidance of network traffic 3. Encapsulation of business logic 4. Delegation of access-rights 5. Some protection from SQL injection attacks
  • 16. Database Design In comparison with Object-Oriented Design
  • 17. Entity Structure Customer • id: int • name: String • birthDate: Date • salary: double • jobCode: String CUSTOMER • C_ID: INTEGER • C_NAME: VARCHAR(25) • C_BIRTH_DATE: DATE • C_SAL: DOUBLE • C_JOB_CODE: CHAR(3)  CUSTOMER TABLE structure can be represented by a Customer Class  Every COLUMN is mapped to its equivalent class attribute/variable  Data types may vary according to the database vendor / programming language
  • 18. Entity Data ID Name Birth Date Salary Job Code 5011 Moneim Emad 03/07/1982 5,000.00 SAR 5012 Kareem Hewady 28/06/1993 3,000.00 OSD  A single, specific occurrence of an entity is an instance.  The selected record/row represents a single record in CUSTOMER table  This is mapped to an instance / object of the Customer class  The entire table data can be mapped to a list of Customer objects
  • 19. Entity Relationship Diagram (ERD)  A relationship is a link that relates two entities that share one or more attributes  An ERD can be mapped to UML Class Diagram  Database relations between tables can be mapped to UML association between classes CustomerPublisher Book Author Inventory Order
  • 20. Relationship: Primary Key ID Name Birth Date Salary Job Code 5011 Moneim Emad 03/07/1982 5,000.00 SAR 5012 Kareem Hewady 28/06/1993 3,000.00 OSD  A primary key is a unique identifier of records in a table.  A primary key values may be generated manually or automatically  A primary key can consist of more than one column (composite key)
  • 21. Relationship: Foreign Key ID Name Birth Date Salary Job Code 5011 Moneim Emad 03/07/1982 5,000.00 SAR 5012 Kareem Hewady 28/06/1993 3,000.00 OSD  A foreign key is a column reference to the primary key in its original table  In contrast to primary key, foreign key may be repeated in the child table ID Customer ID Location Delivery Date 1 5011 El Dokky 27/03/2015 11:45 AM 2 5011 Nasr City 28/03/2015 09:30 AM 3 5897 El Zamalek 28/03/2015 02:15 PM ORDER (Child table) CUSTOMER (Parent table)
  • 22. Relationship Types  One-to-one Customer Basic info  Customer Additional Info  One-to-many Customer info  Customer Orders Info  Many-to-many Customer Info  Customer Books Interest  Books Info
  • 24. Normalization  A set of recommendations to be followed when designing a database that helps for organizing data elements into tables and optimizing the database.  We will see an example for database normalization in three steps using the three normal forms 1NF 2NF 3NF
  • 25. Normalization: Case Study (Un-normalized) SalesOrders • SalesOrderNo • Date • CustomerNo • CustomerName • CutomerAddress • ClerkNo • ClerkName • Item1Description • Item1Quantity • Item1UnitPrice • Item2Description • Item2Quantity • Item2UnitPrice • Item3Description • Item3Quantity • Item3UnitPrice • Total
  • 26. Normalization: Into 1NF  Separate repeating groups into new tables.  Start a new table for the repeating data.  The primary key for the repeating group is usually a composite key.
  • 27. Normalization: Into 1NF SalesOrders • SalesOrderNo • Date • CustomerNo • CustomerName • CutomerAddress • ClerkNo • ClerkName • Item1Description • Item1Quantity • Item1UnitPrice • Item2Description • Item2Quantity • Item2UnitPrice • Item3Description • Item3Quantity • Item3UnitPrice • Total • SalesOrderNo • Date • CustomerNo • CustomerName • CutomerAddress • ClerkNo • ClerkName • Total SalesOrders OrderItems • SalesOrderNo • ItemNo • ItemDescription • ItemQuantity • ItemUnitPrice
  • 28. Normalization: Into 2NF  Remove partial dependencies.  Start a new table for the partially dependent data and the part of the key it depends on.  Tables started at this step usually contain descriptions of resources.
  • 29. Normalization: Into 2NF  Functional dependency: The value of one attribute depends entirely on the value of another.  Partial dependency: An attribute depends on only part of the primary key. (The primary key must be a composite key.)  Transitive dependency: An attribute depends on an attribute other than the primary key.
  • 30. Normalization: Into 2NF OrderItems • SalesOrderNo • ItemNo • ItemDescription • ItemQuantity • ItemUnitPrice OrderItems • SalesOrderNo • ItemNo • ItemQuantity • ItemUnitPrice Item • ItemNo • ItemDescription
  • 31. Normalization: Into 2NF – Problems Solved  Duplication of data: ItemDescription would appear for every order.  Insert anomaly: To insert an inventory item, you must insert a sales order.  Delete anomaly: Information about the items stay with sales order records. Delete a sales order record, delete the item description.  Update anomaly: To change an item description, you must change all the sales order records that have the item.
  • 32. Normalization: Into 3NF  Remove transitive dependencies.  Start a new table for the transitively dependent attribute and the attribute it depends on.  Keep a copy of the key attribute in the original table.
  • 33. Normalization: Into 3NF • SalesOrderNo • Date • CustomerNo • CustomerName • CutomerAddress • ClerkNo • ClerkName • Total SalesOrders Clerk • ClerkNo • ClerkName Customer • CustomerNo • CustomerName • CustomerAddress • SalesOrderNo • Date • CustomerNo • ClerkNo • Total SalesOrders
  • 34. Normalization: Into 3NF – Problems Solved  Duplication of data: Customer and Clerk details would appear for every order.  Insert anomaly: To insert a customer or clerk, you must insert a sales order.  Delete anomaly: Information about the customers and clerks stay with sales order records. Delete a sales order record, delete the customer or clerk.  Update anomaly: To change the details of a customer or clerk, you must change all the sales order that involve that customer or clerk.
  • 35. Normalization: Final Results Clerk • ClerkNo • ClerkName Customer • CustomerNo • CustomerName • CustomerAddress • SalesOrderNo • Date • CustomerNo • ClerkNo • Total SalesOrders SalesOrders • SalesOrderNo • Date • CustomerNo • CustomerName • CutomerAddress • ClerkNo • ClerkName • Item1Description • Item1Quantity • Item1UnitPrice • Item2Description • Item2Quantity • Item2UnitPrice • Item3Description • Item3Quantity • Item3UnitPrice • Total OrderItems • SalesOrderNo • ItemNo • ItemQuantity • ItemUnitPrice Item • ItemNo • ItemDescription
  • 37. SQL  SQL is a standard language for accessing and manipulating databases.  Stands for Structured Query Language.  SQL can execute administrative, structural and data manipulation statements  Administrative: Set permissions on schemas, tables, procedures and views  Structural: Create, Drop or Alter tables, procedures and views  Data Manipulation: Retrieve data from a database Insert, update or delete records from a database
  • 38. SQL  Although SQL is an ANSI (American National Standards Institute) standard, there are different versions of the SQL language.  Most of the big database vendors have their own proprietary extensions in addition to the SQL standard.  However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.  SQL is NOT case sensitive: select is the same as SELECT.  As a coding convention, we should write all SQL keywords in upper-case.  Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.  Some database vendors let you choose your statement terminator character.
  • 39. SQL  SELECT - extracts data from a database  UPDATE - updates data in a database  DELETE - deletes data from a database  INSERT INTO - inserts new data into a database  CREATE DATABASE - creates a new database  ALTER DATABASE - modifies a database  CREATE TABLE - creates a new table  ALTER TABLE - modifies a table  DROP TABLE - deletes a table  CREATE INDEX - creates an index (search key)  DROP INDEX - deletes an index
  • 40. SQL Syntax SELECT col1_name,col2_name FROM table_name; SELECT * FROM table_name;  The SELECT DISTINCT statement is used to return only distinct (different) values. SELECT DISTINCT column1_name,column2_name FROM table_name;  The WHERE clause is used to extract only those records that fulfill a specified criterion. SELECT col1_name,col2_name FROM table_name WHERE boolean_condition; WHERE CITY=‘Cairo’ WHERE (CUSTOMER_ID=5011 OR CITY<>’Alex’) WHERE CUSTOMER_ID IN(5011,5378,5834) WHERE (PRICE>=5000 AND PRICE<10000) WHERE CUSTOMER_NAME LIKE ’%Moneim%’ WHERE HIRE_DATE BETWEEN 2006-12-31 AND 2004-01-01
  • 41. SQL Syntax INSERT INTO table_name VALUES (value1,value2,value3,...); INSERT INTO table_name (col1,col2,col3) VALUES (val1,val2,val3); UPDATE table_name SET col1=val1,col2=val2 WHERE col5=val5; DELETE FROM table_name WHERE col3=val5;  The WHERE clause specifies which record or records that should be updated or deleted.  If you omit the WHERE clause, all records will be updated or deleted!  Using Aliases for tables and columns SELECT CUSTOMER_NAME AS C_NAME FROM CUSTOMER AS C; SELECT CUSTOMER_NAME C_NAME FROM CUSTOMER C;
  • 42. SQL Syntax  Using NOT & Wildcards WHERE ID NOT IN (5011,5012); or WHERE PROVIDER NOT LIKE ‘%mobinil%’; WHERE City LIKE '_erlin'; or WHERE City LIKE 'L_n_on';  City starting with "b", "s", or "p": WHERE City LIKE '[bsp]%'  starting with "a", "b", or "c": WHERE City LIKE '[a-c]%  City NOT starting with "b", "s", or "p": WHERE City LIKE '[!bsp]%'; or WHERE City NOT LIKE '[bsp]%'; WHERE Price NOT BETWEEN 10 AND 20; WHERE ProductName BETWEEN 'C' AND 'M'; -- beginning with C,D,…,M  Ordering Data SELECT col1,col2 FROM table_name ORDER BY col1 ASC|DESC;-- Ascending ORDER BY col1 ASC|DESC, col2 ASC|DESC;
  • 43. SQL Syntax - JOINs  An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.  The most common type of join is: SQL INNER JOIN (simple join).  INNER JOIN: Returns all rows when there is at least one match in BOTH tables  LEFT JOIN: Return all rows from the left table, and the matched rows from the right table  RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table  FULL JOIN: Return all rows when there is a match in ONE of the tables
  • 44. SQL Syntax - JOINs A B A B A B A B INNER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN FULL JOIN
  • 45. SQL Syntax – INNER JOIN OrderID CustomerID OrderDate 10308 2 1996-09-18 10309 2 1996-09-19 10310 1 1996-09-20 10311 1996-09-21 ID CustomerName Email City 1 Mahmoud mahmoud@test.com Cairo 2 Samy samy@test.com Alex 3 Aly aly@test.com Aswan SELECT Customers.CustomerName, Orders.OrderID FROM Customers [INNER] JOIN Orders ON Customers.ID=Orders.CustomerID ORDER BY Customers.CustomerName; CustomerName OrderID Mahmoud 10310 Samy 10308 Samy 10309
  • 46. SQL Syntax – LEFT OUTER JOIN OrderID CustomerID OrderDate 10308 2 1996-09-18 10309 2 1996-09-19 10310 1 1996-09-20 10311 1996-09-21 ID CustomerName Email City 1 Mahmoud mahmoud@test.com Cairo 2 Samy samy@test.com Alex 3 Aly aly@test.com Aswan SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.ID=Orders.CustomerID ORDER BY Customers.CustomerName; CustomerName OrderID Aly NULL Mahmoud 10310 Samy 10308 Samy 10309
  • 47. SQL Syntax – RIGHT OUTER JOIN OrderID CustomerID OrderDate 10308 2 1996-09-18 10309 2 1996-09-19 10310 1 1996-09-20 10311 1996-09-21 ID CustomerName Email City 1 Mahmoud mahmoud@test.com Cairo 2 Samy samy@test.com Alex 3 Aly aly@test.com Aswan SELECT Customers.CustomerName, Orders.OrderID FROM Customers RIGHT JOIN Orders ON Customers.ID=Orders.CustomerID ORDER BY Customers.CustomerName; CustomerName OrderID Mahmoud 10310 Samy 10308 Samy 10309 NULL 10311
  • 48. SQL Syntax – FULL OUTER JOIN OrderID CustomerID OrderDate 10308 2 1996-09-18 10309 2 1996-09-19 10310 1 1996-09-20 10311 1996-09-21 ID CustomerName Email City 1 Mahmoud mahmoud@test.com Cairo 2 Samy samy@test.com Alex 3 Aly aly@test.com Aswan SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL JOIN Orders ON Customers.ID=Orders.CustomerID ORDER BY Customers.CustomerName; CustomerName OrderID Aly NULL Mahmoud 10310 Samy 10308 Samy 10309 NULL 10311
  • 49. SQL Syntax - Advanced  Using SELECT INTO SELECT Customers.CustomerName, Orders.OrderID INTO CustomersOrderBackup2013 FROM Customers LEFT JOIN Orders ON Customers.ID=Orders.CustomerID;  Using INSERT INTO SELECT INSERT INTO Customers (CustomerName, Country) SELECT SupplierName, Country FROM Suppliers;  Using SQL Create CREATE DATABASE dbname; CREATE TABLE Persons(PersonID int NOT NULL UNIQUE,LastName varchar(255), FirstName varchar(255),Address varchar(255),City varchar(255));
  • 50. SQL Aggregate Functions  AVG() Returns the average value  COUNT() Returns the number of rows  FIRST() Returns the first value  LAST() Returns the last value  MAX() Returns the largest value  MIN() Returns the smallest value  SUM() Returns the sumUsing SELECT INTO
  • 51. SQL Scalar Functions  UCASE() Converts a field to upper case  LCASE() Converts a field to lower case  MID() Extract characters from a text field  LEN() Returns the length of a text field  ROUND() Rounds a numeric field to the number of decimals specified  NOW() Returns the current system date and time  FORMAT() Formats how a field is to be displayed
  • 52. SQL GROUP BY Statement  The GROUP BY statement is used in conjunction with the aggregate functions to group the result set by one or more columns.  It can be used for summary reports SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders LEFT JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID GROUP BY ShipperName; ShipperName NumberOfOrders Federal Shipping 68 Speedy Express 54 United Package 74
  • 53. SQL GROUP BY Statement  Group by two columns SELECT Shippers.ShipperName, Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM ((Orders INNER JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID) INNER JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID) GROUP BY ShipperName,LastName; ShipperName Last Name NumberOfOrders Federal Shipping Baher 38 Federal Shipping Mona 30 Speedy Express Aly 54 United Package Kareem 70 United Package Yasser 4
  • 54. SQL HAVING Clause  The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.  Suppose that we want to find if any of the employees has registered more than 10 orders. SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders INNER JOIN Employees ON Orders.EmployeeID=Employees.ID) GROUP BY LastName HAVING COUNT(Orders.OrderID) > 10; Last Name NumberOfOrders Aly 54 Baher 38 Kareem 70 Mona 30
  • 55. SQL Injection  An SQL injection can destroy your database  SQL injection is a technique where malicious users can inject SQL commands into an SQL statement, via web page input.  Injected SQL commands can alter SQL statement and compromise the security of a web application.  txtUserId = getRequestString("UserId"); txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;  UserId: 105 or 1=1  SELECT * FROM Users WHERE UserId = 105 or 1=1  SELECT * FROM Users WHERE UserId = 105 or ""=""  UserId: 105; DROP TABLE SUPPLIERS  Use SQL Parameters / PreparedStatements
  • 56. References  W3Schools.com http://www.w3schools.com/sql/sql_default.asp  Wikipedia.org https://en.wikipedia.org/  WhatIs.com http://whatis.techtarget.com/  IBM IMS for z/OS – Chapter 12 http://csis.pace.edu/lixin/mainframe/zos-slides/Chapter12%20Database%20slides.ppt  North Carolina Virtual Public School – Database Fundamentals http://www.ncvps.org/BbContent/CompApp/6411.08_Apps_1_V2%20Word/6411.08-v2-Unit-B- 5.01/Database-Fundamentals.ppt