SQL Relational Database Architecture

32
Relational Database Relational Database Architecture Architecture

description

SQL Relational Database Architecture

Transcript of SQL Relational Database Architecture

Page 1: SQL Relational Database Architecture

Relational Database Relational Database ArchitectureArchitecture

Page 2: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

SQL Server 2000 data is stored in databases.SQL Server 2000 data is stored in databases. Physically, a DB consists of two or more files Physically, a DB consists of two or more files

on one or more disks.on one or more disks. This physical implementation is visible only to This physical implementation is visible only to

database administrators, and is transparent to database administrators, and is transparent to users.users.

Page 3: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

The physical optimization of the DB is The physical optimization of the DB is primarily the responsibility of the DB primarily the responsibility of the DB administrator.administrator.

Logically a DB is structured into components Logically a DB is structured into components that are visible to users, such as tables, views, that are visible to users, such as tables, views, and stored procedures.and stored procedures.

Page 4: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Types of DatabasesTypes of DatabasesSystem Databases

User Databases

mastermodel tempdb msdb

pubs Northwind User1

distribution

Page 5: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

The logical optimization of the database (such The logical optimization of the database (such as design of tables and indexes) is primarily as design of tables and indexes) is primarily the responsibility of the DB designer.the responsibility of the DB designer.

Page 6: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

System & User DBs.System & User DBs. Each instance of SQL Server 2000 has 4 system Each instance of SQL Server 2000 has 4 system

DBs.DBs.1)1) master :master :

Records all of the system-level information for a SQL Records all of the system-level information for a SQL Server 2000 System, including all other databases, login Server 2000 System, including all other databases, login accounts, and system configuration settings.accounts, and system configuration settings.

2) tempdb:2) tempdb:Stores all temporary tables and stored procedures created by Stores all temporary tables and stored procedures created by users, as well as temporary worktables used by the relational users, as well as temporary worktables used by the relational database engine itself.database engine itself.

Page 7: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

3) model :3) model :Servers as the template that is used Servers as the template that is used

whenever a whenever a new DB is created.new DB is created.4) msdb :4) msdb :

SQL Server Agent uses the system SQL Server Agent uses the system database for database for scheduling alerts and jobs, and recording scheduling alerts and jobs, and recording

operators.operators.

Page 8: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

System Tables:System Tables: System Tables Store Information (Metadata) About System Tables Store Information (Metadata) About

the System and Database Objects (only in Master)the System and Database Objects (only in Master) Database Catalog Stores Metadata About a Specific Database Catalog Stores Metadata About a Specific

Database (including Master)Database (including Master) System Catalog Stores Metadata About the Entire System Catalog Stores Metadata About the Entire

System and All Other Databases (only in Master)System and All Other Databases (only in Master) System table examples:System table examples:

Syslogins, sysmessages, sysdatabases, sysusersSyslogins, sysmessages, sysdatabases, sysusers

Page 9: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

In addition to system DBs, each instance of In addition to system DBs, each instance of SQL Server 2000 has one or more user DBs.SQL Server 2000 has one or more user DBs.

The pubs and Northwind user DBs are sample The pubs and Northwind user DBs are sample DBs that comes with SQL Server 2000.DBs that comes with SQL Server 2000.

Given sufficient system resources, each Given sufficient system resources, each instance of SQL Server 2000 can handle instance of SQL Server 2000 can handle thousands of users working in multiple DBs thousands of users working in multiple DBs simultaneously.simultaneously.

Page 10: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

SQL Server working with multiple user DBs.SQL Server working with multiple user DBs.SQL Server 2000

System Databases

User Databases

master tempdb msdb modelPayroll sales employee

Page 11: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Physical Structure of a DatabasePhysical Structure of a Database Each database consists of at least one data file and Each database consists of at least one data file and

one transaction log file.one transaction log file. These files are not shared with any other database.These files are not shared with any other database. To optimize performance and to provide fault To optimize performance and to provide fault

tolerance, data and log files are typically spread tolerance, data and log files are typically spread across multiple drives and frequently use a across multiple drives and frequently use a redundant array of independent disks (RAID)redundant array of independent disks (RAID)

Page 12: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Extents and PagesExtents and Pages Extents:Extents:

SQL Server 2000 allocates space from a data file for tables SQL Server 2000 allocates space from a data file for tables and indexes in 64-KB blocks called extents.and indexes in 64-KB blocks called extents.

Each extent consists of eight contiguous pages of 8KB each.Each extent consists of eight contiguous pages of 8KB each. There are two types of extents:There are two types of extents:

Uniform extentsUniform extents That are owned by a single objectThat are owned by a single object

Mixed extents Mixed extents That are shared by up to eight objects.That are shared by up to eight objects.

Once extent full, next record will take up a whole additional Once extent full, next record will take up a whole additional extent. extent.

Page 13: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Pages:Pages: A page is the fundamental unit of data storage in A page is the fundamental unit of data storage in

SQL Server 2000, with the page size being 8KB.SQL Server 2000, with the page size being 8KB. In general, data pages store data in rows on each In general, data pages store data in rows on each

data page.data page. The maximum amount of data contained in a The maximum amount of data contained in a

single row is 8060 bytes.single row is 8060 bytes. # of records/page varies with bytes/record# of records/page varies with bytes/record

Page 14: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Pages (Conti…)Pages (Conti…)• Types of Pages: Data and Index pagesTypes of Pages: Data and Index pages• Page SplitPage Split

When page becomes full, it splitsWhen page becomes full, it splits New page allocated: ½ data from old page moved to newNew page allocated: ½ data from old page moved to new

Data rows are either organized in some kind of order Data rows are either organized in some kind of order based on a key in a clustered index (Such as zip code), or based on a key in a clustered index (Such as zip code), or stored in no particular order if no clustered index exists.stored in no particular order if no clustered index exists.

Rows : 8060 bytes and 1024 columnsRows : 8060 bytes and 1024 columns The beginning of each page contains a 96-byte header that The beginning of each page contains a 96-byte header that

is used to store system information, such as the type of is used to store system information, such as the type of page, the amount of free space on the page, and the object page, the amount of free space on the page, and the object ID of the object owning the page.ID of the object owning the page.

Page 15: SQL Relational Database Architecture

ExtentExtent 8 contiguous 64K data pages8 contiguous 64K data pages Once extent full, next record will take up a whole Once extent full, next record will take up a whole

additional extent. additional extent. Pre-allocating space saves time.Pre-allocating space saves time.

Page 16: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Transaction Log Files:Transaction Log Files: The transaction log file resides in one or more The transaction log file resides in one or more

separate physical files from the data files and separate physical files from the data files and contains a series of log records, rather than pages contains a series of log records, rather than pages allocated from extents.allocated from extents.

To optimize performance and aid in redundancy, To optimize performance and aid in redundancy, transaction log files are typically placed on transaction log files are typically placed on separate disks from data files, and are frequently separate disks from data files, and are frequently mirrored using RAID.mirrored using RAID.

Page 17: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Physical Database ArchitecturePhysical Database Architecture Pages and ExtentsPages and Extents

Page: 8K blocks of memoryPage: 8K blocks of memory Extent: 8 contiguous pagesExtent: 8 contiguous pages

Database Files and File groupsDatabase Files and File groups Primary data filePrimary data file Secondary data fileSecondary data file Log fileLog file

Table and Index ArchitectureTable and Index Architecture Clustered tablesClustered tables HeapsHeaps

Page 18: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Primary data files Primary data files The primary data file is the starting point of the database and points to the The primary data file is the starting point of the database and points to the

other files in the database. Every database has one primary data file. The other files in the database. Every database has one primary data file. The recommended file name extension for primary data files is .mdf.recommended file name extension for primary data files is .mdf.

Secondary data files Secondary data files Secondary data files comprise all of the data files other than the Secondary data files comprise all of the data files other than the

primary data file. Some databases may not have any secondary data primary data file. Some databases may not have any secondary data files, while others have multiple secondary data files. The files, while others have multiple secondary data files. The recommended file name extension for secondary data files is .ndf.recommended file name extension for secondary data files is .ndf.

Log files Log files Log files hold all of the log information used to recover the database. Log files hold all of the log information used to recover the database.

There must be at least one log file for each database, although there There must be at least one log file for each database, although there can be more than one. The recommended file name extension for log can be more than one. The recommended file name extension for log files is .ldf.files is .ldf.

Page 19: SQL Relational Database Architecture

DatabaseDatabase

Relational Database Architecture (Conti…)Relational Database Architecture (Conti…)How Data Is StoredHow Data Is Stored

Extent (8 contiguous 8-KB pages)

Page (8 KB)

Tables,Indexes

Data

Max row size = 8060 bytes

Data (file).mdf or .ndf

Log (file).Idf

Page 20: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Logical Structure of a DatabaseLogical Structure of a Database Data in SQL Server 2000 is organized into Data in SQL Server 2000 is organized into

database objects that are visible to users when they database objects that are visible to users when they connect to a DB.connect to a DB.

Page 21: SQL Relational Database Architecture

EmpNumEmpNum LastNameLastName FirstNameFirstName ExtensionExtensionCtryCodeCtryCode

10191

10192

10202

Labrie

Labrie

Martin

Angela

Eva

Jose

x19891

x19433

x21467SP

FR

FR

LastModLastMod

\HR\KarlD

\HR\KarlD

\HR\AmyL

Clustered IndexClustered IndexAndersonAndersonBarr...

Database ObjectsDatabase Objects

integer longstring varchar(20) char(6)char(2) longstring

Trigger

EmployeePhoneViewSELECT lastname, firstname, extensionFROM employee

StoredProcedure

UpdatePhonePK FK

Checkx#####

Page 22: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Database Objects in SQL Server 2000Database Objects in SQL Server 2000 Tables:Tables:

A table generally consists of columns and rows of data A table generally consists of columns and rows of data in a format similar to that of a spreadsheet.in a format similar to that of a spreadsheet.

Each row in the table represents a unique records, and Each row in the table represents a unique records, and each column represents a field within the record.each column represents a field within the record.

A data type specifies what type of data can be stored in A data type specifies what type of data can be stored in a column.a column.

Page 23: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Views:Views: Views can restrict the rows or the columns of a table that Views can restrict the rows or the columns of a table that

are visible, or can combine data from multiple tables to are visible, or can combine data from multiple tables to appear like a single table.appear like a single table.

A view can also aggregate columns.A view can also aggregate columns. Indexes:Indexes:

An index is a structure associated with a table or view that An index is a structure associated with a table or view that speeds retrieval of rows from the table or view.speeds retrieval of rows from the table or view.

Table indexes are either clustered or non-clustered.Table indexes are either clustered or non-clustered. Clustering means the data is physically ordered based on Clustering means the data is physically ordered based on

the index key.the index key.

Page 24: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

KeysKeys A key is a column or group of columns that A key is a column or group of columns that

uniquely identifies a row(Primary key), defines the uniquely identifies a row(Primary key), defines the relationship between two tables (foreign key) or is relationship between two tables (foreign key) or is used to build an index.used to build an index.

Page 25: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

User-defined data types:User-defined data types: It is a custom data type, based on a predefined It is a custom data type, based on a predefined

SQL Server 2000 data type.SQL Server 2000 data type. It is used to make a table structure more It is used to make a table structure more

meaningful to programmers and help ensure that meaningful to programmers and help ensure that columns holding similar classes of data have the columns holding similar classes of data have the same base data type.same base data type.

Page 26: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Stored Procedures:Stored Procedures: A stored procedure is a group of T-SQL statements A stored procedure is a group of T-SQL statements

compiled into a single execution plan. compiled into a single execution plan. The procedure is used for performance The procedure is used for performance

optimization and to control access.optimization and to control access. Constraints:Constraints:

It define rules regarding the values allowed in It define rules regarding the values allowed in columns and are the standard mechanism for columns and are the standard mechanism for enforcing data integrity.enforcing data integrity.

Page 27: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Defaults:Defaults: A default specifies what values are used in a A default specifies what values are used in a

column in the event that you do not specify a value column in the event that you do not specify a value for the column when you are inserting a row.for the column when you are inserting a row.

Triggers:Triggers: A trigger is a special class of stored procedure A trigger is a special class of stored procedure

defined to execute automatically when an defined to execute automatically when an UPDATE, INSERT, or DELETE statement is UPDATE, INSERT, or DELETE statement is issued against a table or view.issued against a table or view.

Page 28: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

User-defined functions:User-defined functions: It is a subroutine made up of one ore more T-SQL It is a subroutine made up of one ore more T-SQL

statements used to encapsulate code for reuse. statements used to encapsulate code for reuse. A function can have a maximum of 1024 input A function can have a maximum of 1024 input

parameters.parameters. UDFs can be used in place of views and stored UDFs can be used in place of views and stored

procedures.procedures.

Page 29: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Optimizing Logical Database Design:Optimizing Logical Database Design: The logical design of the database, including the The logical design of the database, including the

tables and the relationships between them, is the core tables and the relationships between them, is the core of an optimized relational database.of an optimized relational database.

Optimizing logical database design begins with a Optimizing logical database design begins with a normalized database design.normalized database design.

Normalization is the process of removing Normalization is the process of removing redundancies from the data.redundancies from the data.

Page 30: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Normalization often involves breaking data from Normalization often involves breaking data from a single file into two or more logical tables in a a single file into two or more logical tables in a relational DB.relational DB.

E.g. you can place customer detail information in E.g. you can place customer detail information in one table and place order information in another one table and place order information in another table, and then relate the two tables based on a table, and then relate the two tables based on a customer number.customer number.

T-SQL queries can then recombine the table data T-SQL queries can then recombine the table data by using relational join operations, when needed.by using relational join operations, when needed.

Page 31: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

By avoiding the need to update the same data By avoiding the need to update the same data in multiple places, normalization improves the in multiple places, normalization improves the efficiency of a client application and reduces efficiency of a client application and reduces the opportunities for introducing errors due to the opportunities for introducing errors due to inconsistent data.inconsistent data.

Page 32: SQL Relational Database Architecture

Relational Database Architecture Relational Database Architecture (Conti…)(Conti…)

Some benefits of normalization include:Some benefits of normalization include: Faster sorting and index creationFaster sorting and index creation A larger number of clustered indexesA larger number of clustered indexes Narrower and more compact indexesNarrower and more compact indexes Fewer indexes per table, which improves the Fewer indexes per table, which improves the

performance of INSER,UPDATE, and DELETE performance of INSER,UPDATE, and DELETE statements.statements.

Fewer null values and less opportunity for Fewer null values and less opportunity for inconsistency, which increase database compactnessinconsistency, which increase database compactness