Intro to tsql unit 5


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 1
  • 45
  • 47
  • Intro to tsql unit 5

    1. 1. Introduction To SQL Unit 5 Modern Business Technology Introduction To TSQL Unit 5 Developed by Michael Hotek
    2. 2. Naming Tables <ul><li>Up to 30 characters </li></ul><ul><li>No blanks </li></ul><ul><li>Underscore is permitted </li></ul><ul><li>Must be unique within the database </li></ul><ul><li>Keep abbreviations to a minimum </li></ul><ul><li>Names should reflect the contents of the table </li></ul>
    3. 3. Naming Columns <ul><li>Up to 30 characters </li></ul><ul><li>No blanks </li></ul><ul><li>Underscore is permitted </li></ul><ul><li>Must be unique within the table </li></ul><ul><li>Keep abbreviations to a minimum </li></ul><ul><li>Names should reflect the contents of the column </li></ul><ul><li>Names should be readily recognizable </li></ul>
    4. 4. Datatype Considerations <ul><li>Determine what type of data will be stored </li></ul><ul><li>Find the range of possible values </li></ul><ul><li>Determine the accuracy of numeric columns </li></ul><ul><li>Efficiency </li></ul><ul><li>Utilize user defined datatypes to enforce consistency </li></ul>
    5. 5. Datatypes <ul><li>Exact Numeric </li></ul><ul><ul><li>Stores data with a specific accuracy </li></ul></ul><ul><li>Approximate numeric </li></ul><ul><ul><li>Stores data with accuracy dependent upon calculations performed </li></ul></ul><ul><li>Money </li></ul><ul><ul><li>Monetary data </li></ul></ul><ul><li>Date and time </li></ul><ul><ul><li>Storage of dates and times </li></ul></ul><ul><li>Character </li></ul><ul><ul><li>Alphanumeric data </li></ul></ul><ul><li>Binary </li></ul><ul><ul><li>Images, byte and bit values </li></ul></ul>
    6. 6. Exact Numeric <ul><li>tinyint </li></ul><ul><ul><li>Whole numbers between 0 and 255 </li></ul></ul><ul><ul><li>1 byte of storage </li></ul></ul><ul><li>smallint </li></ul><ul><ul><li>Whole numbers between -32678 and 32677 </li></ul></ul><ul><ul><li>2 bytes of storage </li></ul></ul><ul><li>numeric(p,s) </li></ul><ul><ul><li>Decimals between -10 38 and 10 38 -1 </li></ul></ul><ul><ul><li>2 to 17 bytes </li></ul></ul><ul><li>decimal(p,s) </li></ul><ul><ul><li>Decimals between -10 38 and 10 38 -1 </li></ul></ul><ul><ul><li>2 to 17 bytes </li></ul></ul><ul><li>s = number of digits to the right of the decimal </li></ul><ul><li>p = total number of digits </li></ul>
    7. 7. Approximate Numeric <ul><li>Float(p) </li></ul><ul><ul><li>Floating point numbers </li></ul></ul><ul><ul><li>4 or 8 bytes of storage </li></ul></ul><ul><li>double precision </li></ul><ul><ul><li>floating point numbers </li></ul></ul><ul><ul><li>8 bytes of storage </li></ul></ul><ul><li>real </li></ul><ul><ul><li>floating point numbers </li></ul></ul><ul><ul><li>4 bytes of storage </li></ul></ul><ul><li>During arithmetic operations, the number of digits to the right of a decimal point will round based upon the values in the calculation </li></ul><ul><li>Float allows you to specific the precision, but not the location of the significant digits in relation to the decimal point </li></ul>
    8. 8. Money <ul><li>money </li></ul><ul><ul><li>-922,337,203,685,477.5808 to 922,337,203,685,477.5807 </li></ul></ul><ul><ul><li>8 bytes of storage </li></ul></ul><ul><li>smallmoney </li></ul><ul><ul><li>-214,748.3648 to 214,748.3647 </li></ul></ul><ul><ul><li>4 bytes of storage </li></ul></ul><ul><li>Accurate up to 4 decimal places, but are rounded to 2 places when displaying </li></ul>
    9. 9. Datetime <ul><li>datetime </li></ul><ul><ul><li>1/1/1753 to 1/31/9999 </li></ul></ul><ul><ul><li>8 bytes of storage </li></ul></ul><ul><li>smalldatetime </li></ul><ul><ul><li>1/1/1900 to 6/6/2079 </li></ul></ul><ul><ul><li>4 bytes of storage </li></ul></ul><ul><li>There is no separate datatype for just times </li></ul><ul><li>Time is stored along with a date with an accuracy of 1/300th of a second </li></ul><ul><li>Due to the limitation on the range of data, a smalldatetime should no longer be used. </li></ul><ul><li>Dates and times can be entered into these columns using a wide variety of date and time formats </li></ul><ul><li>The default display type is determined from the default language for the server </li></ul>
    10. 10. Character <ul><li>char(n) and nchar(n) </li></ul><ul><ul><li>Fixed length alphanumeric data </li></ul></ul><ul><ul><li>n bytes of storage </li></ul></ul><ul><li>varchar(n) and nvarchar(n) </li></ul><ul><ul><li>Variable length alphanumeric data </li></ul></ul><ul><ul><li>actual length of data </li></ul></ul><ul><li>text </li></ul><ul><ul><li>Unbounded alphanumeric data </li></ul></ul><ul><ul><li>16 bytes for an address + multiples of 2K </li></ul></ul><ul><li>Char datatypes provide a small performance benefit over varchar. </li></ul><ul><li>If the data size is predictable or 8 characters or less, use a char </li></ul><ul><li>nchar and nvarchar are used to store multi-byte characters such as Chinese and Japanese </li></ul>
    11. 11. Binary <ul><li>Bit </li></ul><ul><ul><li>0 or 1 </li></ul></ul><ul><ul><li>1 byte of storage </li></ul></ul><ul><li>binary(n) </li></ul><ul><ul><li>up to 255 bytes </li></ul></ul><ul><ul><li>n bytes of storage </li></ul></ul><ul><li>varbinary </li></ul><ul><ul><li>up to 255 bytes </li></ul></ul><ul><ul><li>actual length of data </li></ul></ul><ul><li>image </li></ul><ul><ul><li>up to 2 31 -1 bytes </li></ul></ul><ul><ul><li>16 bytes address + multiples of 2K bytes of storage </li></ul></ul>
    12. 12. Unbound Data <ul><li>The text and image datatypes are used to store large amounts of data </li></ul><ul><li>Text is used for alphanumeric data </li></ul><ul><li>Image is used for binary data </li></ul><ul><li>It is highly recommended that you avoid this two datatypes if at all possible </li></ul><ul><ul><li>They can cause a serious performance problem as well as a space problem. </li></ul></ul><ul><ul><li>Support for them is very limited (specialized functions are required to manipulate them) </li></ul></ul><ul><ul><li>No application tool can handle binary data streaming directly from a database </li></ul></ul>
    13. 13. User Defined Datatypes <ul><li>SQL Server gives you the ability to create your own datatypes </li></ul><ul><ul><li>Enforces consistency among columns </li></ul></ul><ul><ul><li>Gives a single point to bind a common rule or default </li></ul></ul><ul><li>Consists of a name, system datatype, and nullability </li></ul>
    14. 14. Creating UDDTs <ul><li>You add new types with sp_addtype </li></ul><ul><li>exec sp_addtype empid, int not null </li></ul><ul><li>This says to create a new type called empid that is based upon an integer and can not be null </li></ul><ul><li>exec sp_addtype tid, char(6), not null </li></ul><ul><li>This is a new type called tid that can contain up to 6 characters and can not be null </li></ul><ul><li>Use sp_droptype and sp_help type to drop and return information about user datatypes </li></ul>
    15. 15. <ul><li>An identity is a special column property </li></ul><ul><ul><li>Assigned a sequential numeric value for each row that is inserted </li></ul></ul><ul><ul><li>They can be of integer or numeric datatypes, but are only whole numbers </li></ul></ul><ul><ul><li>They can not be null </li></ul></ul><ul><ul><li>There can only be one identity column per table </li></ul></ul><ul><ul><li>Can not be updated </li></ul></ul><ul><ul><li>Data can be explicitly inserted into them using set identity_insert <table> on </li></ul></ul><ul><ul><ul><li>This should only be done in rare instances </li></ul></ul></ul><ul><ul><li>Normally start at 1, but a seed value can be defined for them </li></ul></ul><ul><ul><li>This is normally a sequential number, but you should not count on this </li></ul></ul>Identities
    16. 16. Creating Tables <ul><li>CREATE TABLE [database . [owner] . ]table_name ( {col_name column_properties [constraint [constraint [...constraint]]] | [[ , ] constraint]} [[ , ] {next_col_name | next_constraint}...] ) </li></ul><ul><li>Maximum number of columns per table is 250 </li></ul><ul><li>Maximum row size is 1962 bytes </li></ul><ul><li>To get information about a table use sp_help <table_name> </li></ul>
    17. 17. Create Table Example <ul><li>CREATE TABLE dbo.authors </li></ul><ul><li>(au_id id NOT NULL, </li></ul><ul><li>au_lname char(40) NOT NULL, </li></ul><ul><li>au_fname char(20) NOT NULL, </li></ul><ul><li>phone char(12) NULL, </li></ul><ul><li>address varchar(40) NULL, </li></ul><ul><li>city varchar(20) NULL, </li></ul><ul><li>state char(2) NULL, </li></ul><ul><li>zip char(5) NULL, </li></ul><ul><li>contract bit NOT NULL) </li></ul><ul><li>go </li></ul>
    18. 18. Unit 5 Review <ul><li>Table names can be up to 30 characters long and must be unique within a database </li></ul><ul><li>Column names can be up to 30 characters and must be unique within a table </li></ul><ul><li>Types of data are: exact numeric, approximate numeric, money, date time, character, and binary </li></ul><ul><li>Use unbound datatypes only when strictly necessary </li></ul><ul><li>User defined datatypes can be created from system datatypes using sp_addtype </li></ul><ul><li>An identity is a special property for a column that automatically provides a value for new rows </li></ul><ul><li>You can create and remove tables using the create table and drop table commands </li></ul><ul><li>You can only have 250 columns in a table </li></ul><ul><li>The maximum size of a row is 1962 bytes </li></ul>
    19. 19. Unit 5 Exercises <ul><li>Time allotted for exercises is 30 minutes </li></ul>