Date post: | 05-Jan-2016 |
Category: |
Documents |
Upload: | avice-mckenzie |
View: | 215 times |
Download: | 0 times |
Data TypesData TypesLesson 4
Skills MatrixSkills Matrix
TableTable
• A table stores your data. • Tables are relational in that they are
organized as rows and columns (a matrix).
• Each intersection of a row and column provides a storage location.
• Each storage field must be defined with a data type.
Data TypeData Type
• Specifies the kind of information that can be stored in a column, parameter, or variable.
• System-supplied data types are provided by SQL Server.
• Alias data types (formerly known as user-defined data types) can be created by any database user.
Built-in Data TypesBuilt-in Data Types
• Each field, or column, in a table has a specific data type, which causes the RDBMS to restrict the type of data that can be inserted.
SQL Server Built-in Data TypesSQL Server Built-in Data Types• biging• binary• bit• char• date• datetime• datetime2• datetimeoffset• decimal• float
• geography• geometry• hierarchyid• image• int• money• nchar• ntext• numeric• nvarchar
SQL Server Built-in Data TypesSQL Server Built-in Data Types• nvarchar(max)• real• smalldatetime• smallint• smallmoney• sql_variant• text• time• timestamp• tinyint
• uniqueidentifier• varbinary• varbinary (max)• varchar• varchar(max)• vardecimal• xml
Data Type PrecedenceData Type Precedence
• Data types have a certain order, or precedence.
• This means when combining data types, implicit data conversion occurs, and the data type with the highest rank will be given priority.
Data Type PrecedenceData Type Precedence1. User-defined data
types (highest)2. Sql_Variant3. Xml4. DateTimeOffset5. DateTime26. DateTime7. SmallDateTime8. Date9. Time10.Float
11.Real12.Decimal13.Money14.SmallMoney15.BigInt16.Int17.SmallInt18.TinyInt19.Bit20.NText
Data Type PrecedenceData Type Precedence21.Text22.Image23.Timestamp24.Uniqueidentifier25.Nvarchar/
Nvarchar(Max)26.Nchar27.Varchar/Varchar(Max)28.Char29.VarBinary/
VarBinary(Max)30.Binary (lowest)
Formatting and Converting DataFormatting and Converting Data
• You’ll often need to convert and modify data.
• SQL Server offers an extensive set of functions you can use to perform conversions and formatting.
• You can categorize these functions into groups, as delineated in the following sections.
Introducing Alias Data TypesIntroducing Alias Data Types• Sometimes it’s useful to create your own
data types in order to streamline your business environment and use a common method for referring to specific data.
• You gain a major benefit when using managed code, CLR (common language runtime), because it also gives you the ability to create a complex data type and define its methods and properties.
• You define an alias data type in SQL Server by using either the CREATE TYPE or the sp_addtype syntax.
Creating CLR User-Defined TypesCreating CLR User-Defined Types• Working with managed code data types or
CLR alias types adds an additional dimension to how you work with SQL Server. – A user-defined type can be a CLR class
that has reference types and value types.• Alias types contain multiple elements and
are useful in environments to indicate geospatial data, to indicate date and time functionality, or even to perform data encryption or object-oriented storage.
Creating CLR User-Defined TypesCreating CLR User-Defined Types
• Catalog the assembly (CREATE ASSEMBLY).
• Create the data type by using a CREATE TYPE statement, in which you reference the assembly.
Data RowData Row
• A data row consists of a row header (4 bytes) followed by a data portion.
• The data portion can contain:– Fixed-length data– Null block– Variable block– Variable-length data
PagePage
• SQL Server enters fixed-length data into a slot on a page before variable-length data.
• An empty fixed-length data entry takes as much space as a populated entry.
• A table with only fixed-length columns always stores the same number of rows on a page.
Estimating Table Storage Estimating Table Storage RequirementsRequirements
• Tables provide templates specifying data storage. All data stored in a table must adhere to a data type.
Estimating Table Storage Estimating Table Storage RequirementsRequirements• You can follow a specific process to
estimate the space required by a table:– 1. Calculate the space used by a
single row of the table.– 2. Calculate the number of rows that
fit on one page.– 3. Estimate the number of rows the
table will hold.– 4. Calculate the total number of
pages that might be required to hold these rows.
Data Types and SizesData Types and Sizes
Calculating Rows Per PageCalculating Rows Per Page
• Once you have a number indicating the total bytes used per row, you can easily calculate the number of rows that fit on a single page. Because every page equals 8 KB in size and has a header, about 8,096 bytes are free for storing data. You can calculate the total number of rows per page as follows:8096 ÷ (RowSize + 2)
Estimating Table Storage Estimating Table Storage RequirementsRequirements
• Estimating the Number of Rows for the Table
• Calculating the Number of Pages Needed
• Estimating Index Storage Requirements
SummarySummary
• You learned that tables consist of columns and rows (entities and instances) and that each of the fields (the intersections) in the table has a specific data type that restricts the form of data it can hold and return.
SummarySummary
• Then you learned you can create your own data types both with Transact-SQL and with several procedural languages. – Transact-SQL data types use system
data types with all the required parameters presupplied and labeled in a more obvious way; for example, zip instead of char(5).
SummarySummary• You learned that each of these data types
has a storage requirement. – Based on your choices your database
can be larger than optimum to meet your business needs.
– Consider sitting down with subject matter experts and plan data requirements prior to creating database objects.
SummarySummary– Consider using the techniques espoused
by Business Process Reengineering, Joint Application Design, or Service-Oriented Architecture techniques to help you.
– You need to decide what tables will contain and make the tables as specific and efficient as possible.
SummarySummary• Consider using automated tools such as
ER/Studio (Embarcadaro Systems) or ERWIN (Computer Associates) to help you with such tasks as affinity analysis (the grouping of related items appropriate to a single table).
Summary for Certification ExaminationSummary for Certification Examination• Know your data types. • Be familiar with the built-in data types and
know when to use each one. • For example, it might be obvious to you
when you should use varchar instead of float, but it might not be as obvious when you need to use smallmoney versus money.
• Designing databases considers trade-offs and balances. The certification examination poses questions requiring the best solution.
Summary for Certification ExaminationSummary for Certification Examination
• Know the basics of how to retrieve and format data into information.
• In conjunction with the SELECT statement, know the difference between CAST and CONVERT.
• Both can do most functions. CONVERT adds style formatting capabilities.