Subido por Ygrem Bekele

Chapter 1 SQL Server 2012 Database Design

Anuncio
Chapter 1: SQL Server 2012
Database Design
1/23/2022
1
Module Overview
•
•
•
•
•
1/23/2022
Data Modeling
Database Creation using GUI
Database Creation using T-SQL Scripts
File locations and Size Parameters
DB Log Files, Growth, and Placement
2
Data Modeling
• The whole process of Data Modeling is called Data Normalization or Database Normalization.
• Normalization is a process of reducing redundant/duplicate data. It means that we have some data element
in a row/record that we may not need in that same table. So, it is a rule of thumb that anytime we find a
duplicate data element in a record, we may need to create another table with all the duplicates.
• Again, Normalization is the process of organizing data in the most optimized way for better performance and
to avoid unnecessary redundant data in a database. This includes creating tables and establishing
relationships between tables to eliminate redundancy and inconsistent data.
1/23/2022
3
Data Modeling
Advantages of Data Normalization:
• Minimizes the amount of space required to store the data by eliminating
redundant data minimizes the risk of data inconsistencies within a database
• Minimizes the introduction of possible updates and delete anomalies
• Maximizes the stability of the data structure
• Eliminates duplicate/redundant data from tables
1/23/2022
4
Data Modeling
• Reduce the chances of data anomaly/inconsistency that occur in a
database Provides efficient management of data and space (HD)
Fewer chances of making mistakes in editing data
• Effective usage of storage
• Writing/inserting and Data update is faster
1/23/2022
5
Data Modeling
Disadvantage of normalization:
• Data Read/Retrieval is slower
• Requires writing more complicated script/queries to retrieve data
1/23/2022
6
Data Modeling
Types of Normalization/Normalization forms:
• There are different normalization forms.
• However, OLTP and OLAP databases for commercial purposes, Data Modeling use only first 3 normalization forms.
The other normalization forms are used for academic and research purposes only.
• The normalization forms are 1NF, 2NF, 3NF, 4NF, 5NF, 6NF etc.
• Among all these normalization forms, only 1NF, 2NF, and 3NF normalization forms are used for data normalization
and commercial databases.
• Edgar F. Codd is the inventor of relational model as he first defined the relational model of databases. Codd
defined First, second, and third normal form or 1NF, 2NF and 3NF respectively.
1/23/2022
7
Data Modeling
• In some schemes/systems/designs such as data warehousing, you keep your design deformalized for performance reasons
and primarily because you are typically not updating the data in a data warehouse.
Denormalization:
• Denormalization is the reverse process of normalization. In Denormalization, we accept data redundancy in a table.
• De-normalization is a logical database design method to merge data from multiple related tables into one table. The
characteristic of a De-normalization database is that it has wide tables with more columns per table.
• It helps to improve the performance of a query. It is because it requires reading related data from a few tables. So, some
calculated values can be kept in the column of a table. In a way, it reduces the overhead of doing calculations with every
query.
1/23/2022
8
Data Modeling
Why Denormalize a Database:
• The only reason to Denormalization a database is to increase performance.
• For example, in an OLAP (On-Line Analytical Processing) application, you could Denormalize the
database for creating reports that are used frequently, such as reports that include summarized
numbers for quarterly profit. Instead of calculating these values each time you need them, you
can use the existing records to be queried again without calculation. This type of denormalization
works best when the data is frequently changed, in the case of historical data.
1/23/2022
9
Normalization Form Example
Un-normalized Students table:
Student# AdvID AdvName AdvRoom Class1 Class2
123
123A James
555
102-8 104-9
124
123B Smith
467
209-0 102-8
1/23/2022
10
Normalization Rules
Rule 1:
• In the First Normal Form (1NF), every entity has a primary key attribute. Each attribute must have only one
value, and not a set of values.
• In simple word:
• Eliminate Repeating Groups or Remove duplicate data/similar data from the same row of a table and store the
repeating records in another table. Use a key to relate the records between the tables created.
• For a database to be in 1NF, it must not have any repeating groups. For a single instance, data in the
repeating group may have multiple values for a given attribute.
1/23/2022
11
1st Normal Form
Example
Un-normalized Students table:
Student# AdvID AdvName AdvRoom Class1 Class2
123
123A James
555
102-8 104-9
124
123B Smith
467
209-0 102-8
Normalized Students table:
Student# AdvID AdvName AdvRoom Class#
1/23/2022
123
123
124
124
123A
123A
123B
123B
James
James
Smith
Smith
555
555
467
467
102-8
104-9
209-0
102-8
12
Normalization Rules
Second Normal Form (2NF):
• Tables to be in 2NF, it must satisfy 1NF and all non-key attributes must fully depend on the primary key.
Reduce data redundancy by extracting redundant data and moving it a new table.
• All requirements for 1st NF must be met.
• Redundant data across multiple rows of a table must be moved to a separate table.
• The resulting tables must be related to each other by use of foreign key.
• Eliminate/Remove Duplicate/Redundant data that is in same column on multiple rows and store these
records in a new table.
1/23/2022
ACT American College of Technology
13
2nd Normal Form Example
Students table
Student#
AdvID
123
123A
124
123B
AdvName
James
Smith
AdvRoom
555
467
Registration table
1/23/2022
Student#
123
123
124
124
Class#
102-8
104-9
209-0
102-8
Class#
102-8
104-9
209-0
14
Normalization Rules
Third Normal Form (3NF):
• A table to be in 3NF, it must satisfy the 1NF, 2NF and require to remove all non-key attributes relying on another non-key attribute
that relies on the primary key.
• Eliminate Columns/Non key attributes dependent on another non key attribute. So, Non key Attributes cannot depend on another
non key attribute. Each non-key attribute must depend on a primary key (exactly like 2NF).
• Eliminate fields that do not depend on the primary key.
• That is, any field that is dependent not only on the primary key but also on another field must be moved to another table.
1/23/2022
ACT American College of Technology
15
3rd Normal Form Example
Students table:
Student# AdvID
123
123A
124
123B
Student table:
1/23/2022
AdvName AdvRoom
James
555
Smith
467
Advisor table:
Student# AdvID
AdvID AdvName AdvRoom
123
123A
123A
James
555
124
123B
123B
Smith
467
16
Normal Form Example Cont.
Students table:
Student# AdvID
123
123A
124
123B
Advisor table:
Registration table:
Student#
123
123
124
124
Class#
102-8
104-9
209-0
102-8
Class table:
Class#
102-8
104-9
209-0
AdvID AdvName AdvRoom
123A James
555
123B Smith
467
1/23/2022
17
ER Diagram
• E-R diagram stands for Entity Relationship Diagram.
• An Entity Relationship Diagram (ERD) is a way to graphically describe the relationship between data in a
table. This relationship is established by using same value of two columns of two tables.
• Relationships describe the connection between two Entities/Tables. There are different types of relationship.
For example, a relationship could be one-to-one, one-to-many, and many-to-many.
• A relationship works by matching data in key columns. A good practice is to use the same name for
connected columns of the tables.
1/23/2022
18
ER-Diagram
• E-R Diagram helps to organize data into entities of a system of any project. It also helps to define relationship
between the entities/tables.
• E-R Diagram helps Data Analyst/Modeler to produce a good database structure so that the data can be
stored and retrieved most efficiently.
• Again, an E-R diagram is a specialized graphical representation of relationship between entities in a
database.
• E-R diagram is a diagram that is used in data modeling for Relational Databases. It shows the structure of
each table including column name, data type, length, constraints, Primary Key and links between entities.
1/23/2022
19
ER-Diagram for Student Advisory Database
1/23/2022
20
Data Dictionary For Student Advisory Database
1/23/2022
21
Database Creation using GUI(Student
Advisory DB)
Connect to Database Server With SSMS
1/23/2022
22
Database Creation using GUI(Student
Advisory DB)
Right Click on the Database Node and Click on New Database
1/23/2022
23
Database Creation using GUI(Student
Advisory DB)
Provide “Student Advisory” on the Database Name
1/23/2022
24
Database Creation using GUI(Student
Advisory DB)
Review the Option for the Database
1/23/2022
25
Database Creation using GUI(Student
Advisory DB)
Review the Filegroup for the Database and the Click Ok
1/23/2022
26
Database Creation using GUI(Student
Advisory DB)
Confirm the “Student Advisory” Database is created
1/23/2022
27
Database Creation using T-SQL Scripts
(Student Advisory DB)
Click “New Query” from the menu
1/23/2022
28
Database Creation using T-SQL Scripts
(Student Advisory DB)
Write the below create script to create the “Student Advisory” with Script
1/23/2022
29
Database Creation using T-SQL Scripts
Genrate
(Student Advisory DB-Copy)
1/23/2022
30
Database Creation using T-SQL Scripts
Genrate
(Student Advisory DB-Copy)
1/23/2022
31
Database Properties (General)
1/23/2022
32
Database Properties (Files)
1/23/2022
33
Database Properties (Filegroup)
1/23/2022
34
Database Properties (Options)
1/23/2022
35
Database Properties (Change Tracking)
1/23/2022
36
Database Properties (Permissions)
1/23/2022
37
Student Advisory Database Tables Defintion
Scripts
1/23/2022
38
Database ER-Diagram Generated from DB
1/23/2022
39
Descargar