Module 2 Working with Data Types. Module Overview Using Data Types Working with Character Data Converting Data Types Working with Specialized Data Types.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Module 6 Implementing Table Structures in SQL Server ®2008 R2.
Module 4: Creating Data Types and Tables. Overview Creating Data Types Creating Tables Generating Column Values Generating Scripts.
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
1 Nassau Community CollegeProf. Vincent Costa Acknowledgements: Introduction to Database Management, All Rights ReservedIntroduction to Database Management.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Copyright ©2014 Pearson Education, Inc. Chapter 6 Physical Design Chapter6.1.
Database Fundamentals
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
Module 17 Storing XML Data in SQL Server® 2008 R2.
Working with Data Types February 7, 2015 John Deardurff Website:
Basis Data Terapan Yoannita. SQL Server Data Types Character strings: Data typeDescriptionStorage char(n)Fixed-length character string. Maximum 8,000.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Using New Data Types in 2008 Andrew Couch UK Access User Group ASC associates
Winter 2006Keller Ullman Cushing8–1 Turning in Assignments Please turn in hard copy (use only in the direst of circumstances). I am not your secretary.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
CSC 2720 Building Web Applications Database and SQL.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Module 3 Designing a Physical Database Model. Module Overview Selecting Data Types Designing Database Tables Designing Data Integrity.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
Module 3: Creating Data Types and Tables. Overview Working with Data Types Working with Tables Generating Column Values Generating Scripts.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
Data Types Lesson 4. Skills Matrix Table A table stores your data. Tables are relational in that they are organized as rows and columns (a matrix). Each.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Session 11 Creating Tables and Using Data Types. RDBMS and Data Management/Session 11/2 of 40 Session Objectives Define the data types and list the categories.
Sql DDL queries CS 260 Database Systems.
IMS 4212: Data Modeling—Attributes and Domains 1 Dr. Lawrence West, Management Dept., University of Central Florida Attributes and Domains.
Best Practices in SQL Programming. Do not use irrelevant datatype  VARCHAR instead of DATETIME  CHAR(N) instead of VARCHAR(N)  etc.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Session 1 Module 1: Introduction to Data Integrity
SQL server Section 2&3. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
What is your Character Data Type? March 5, 2016 John Deardurff Website:
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Standards and Conventions
Querying with Transact-SQL
Creating Database Objects
Introduction To Oracle
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Module 2: Creating Data Types and Tables
Chapter 2 Elementary Programming
Data Definition and Data Types
Attributes and Domains
Ouch! Our Data Type Choices Did THAT?
SQL Implementation & Administration
14 T-SQL Functions You May Not Know
Proper DataType Usage = Guaranteed Better Performance and Accuracy
What is your Character Data Type?
Working with Data Types
Numerical Data Types.
Database systems Lecture 2 – Data Types
Defining a Database Schema
PT2520 Unit 5: Physical Design
Data Types Do Matter Start local instance of SQL Start ZoomIt
14 T-SQL Functions You May Not Know
Attributes and Domains
Creating Database Objects
Working with SQL Server 2016 Data Types
Presentation transcript:

Module 2 Working with Data Types

Module Overview Using Data Types Working with Character Data Converting Data Types Working with Specialized Data Types

Lesson 1: Using Data Types Introducing Data Types Exact Numeric Data Types Working with IDENTITY Approximate Numeric Data Types Date and Time Data Types Unique Identifiers NULL or NOT NULL Columns Demonstration 1A: Working with Numeric Data Types

Introducing Data Types Data types determine what can be stored  Constrains the type of data that an object can hold  Provides limits on the range of values Data types apply to database columns, variables, expressions, and parameters Critical to choose appropriate data type  Assists with query optimization  Provides a level of self-documentation Three basic sets of data types  System data types  Alias data types  User-defined data types

Exact Numeric Data Types Data TypeNotes tinyint8 bits (0 to 255) smallint16 bit integer ( to 32767) int32 bit integer ( to ) bigint64 bit integer (-2^63 to 2^63 – 1) decimalfixed precision and scale (-10^38+1 – 10^38-1) numericfunctionally equivalent to decimal smallmoneyfixed scale of 4 decimal places in 16 bits – avoid moneyfixed scale of 4 decimal places in 64 bits - avoid bitvalues of 1, 0, or NULL Wide variety of numeric types are supported Vary in range, precision, and accuracy

Working with IDENTITY CREATE TABLE Sales.Opportunity ( OpportunityID int NOT NULL IDENTITY(1,1), Requirements nvarchar(50) NOT NULL, ReceivedDate date NOT NULL, LikelyClosingDate date NULL, SalespersonID int NULL, Rating int NOT NULL ); CREATE TABLE Sales.Opportunity ( OpportunityID int NOT NULL IDENTITY(1,1), Requirements nvarchar(50) NOT NULL, ReceivedDate date NOT NULL, LikelyClosingDate date NULL, SalespersonID int NULL, Rating int NOT NULL ); Property of a column Specify a seed and an increment Default seed and increment are both 1 SCOPE_IDENTITY(),

Approximate Numeric Data Types Data TypeNotes floatapproximate value with scale of up to 53 digits realISO standard floating point with fixed storage of 32 bits Two approximate numeric types are supported float is from float(1) to float(53) float defaults to float(53) real is fixed 4 byte storage float and real not regularly used in business applications as they are not precise

Date and Time Data Types Data TypeNotes date to with accuracy of 1 day datetime2same date storage as date data type plus time storage as per time data type datetime to with accuracy of 3.33 milliseconds datetimeoffsetas per datetime2 plus timezone offset of -14:00 to +14:00 smalldatetime to with 1 minute accuracy time00:00: to 23:59: with 100 nanosecond accuracy Rich set of options is available for storing date and time data Need to be very careful of string literal formats of each Large set of functions available for processing these data types

Unique Identifiers uniqueidentifier data type is typically used for storing GUID values GUID is globally unique identifier Storage is essentially a 128-bit integer but standard integer arithmetic is not supported =, <>,, = are supported along with NULL and NOT NULL checking IDENTITY cannot be used New values from NEWID() function Common error is to store these as strings

NULL or NOT NULL Columns CREATE TABLE Sales.Opportunity ( OpportunityID int NOT NULL, Requirements nvarchar(50) NOT NULL, ReceivedDate date NOT NULL, LikelyClosingDate date NULL, SalespersonID int NULL, Rating int NOT NULL ); CREATE TABLE Sales.Opportunity ( OpportunityID int NOT NULL, Requirements nvarchar(50) NOT NULL, ReceivedDate date NOT NULL, LikelyClosingDate date NULL, SalespersonID int NULL, Rating int NOT NULL ); Can determine whether a value must be provided Can be defined on columns and parameters Cannot be defined on variables Often inappropriately defined

Demonstration 1A: Working with Numeric Data Types In this demonstration you will see: Work with IDENTITY values Work with NULL Insert GUIDs into a table

Lesson 2: Working with Character Data Understanding Unicode Character Data Types Understanding Collations Demonstration 2A: Working with Character Data

Understanding Unicode Is a worldwide character-encoding standard Simplifies software localization Improves multilingual character processing Is implemented in SQL Server as double-byte for Unicode types Requires N prefix on constants Uses LEN() to return number of characters, DATALENGTH() to return the number of bytes nvarchar(10); = N'Hello'; = N' 你好 '; = N' こんにちは '; nvarchar(10); = N'Hello'; = N' 你好 '; = N' こんにちは ';

Character Data Types Data TypeNotes charFixed length single-byte character data ncharFixed length Unicode character data varcharVariable length single-byte character data nvarcharVariable length Unicode character data varchar(max)Large value single-byte character data nvarchar(max)Large value Unicode character data textVariable length single-byte character data (deprecated do not use for new work) ntextVariable length Unicode data (deprecated do not use for new work) Fixed length, variable length, and large character data types Single byte and double byte (Unicode) data types

Understanding Collations Collations in SQL Server control:  Code page that is used to store non-Unicode data  Rules that govern how SQL Server sorts and compares values for non-Unicode types SQL Server supports a large number of collations, including case-sensitivity options Collation settings can be determined at the instance, database, and column levels Comparisons between data stored in different collations require specifying the collation to use for the comparison SELECT * FROM Production.Product WHERE Name LIKE N'%ball%' COLLATE SQL_Latin1_General_Cp1_CS_AS; SELECT * FROM Production.Product WHERE Name LIKE N'%ball%' COLLATE SQL_Latin1_General_Cp1_CS_AS;

Demonstration 2A: Working with Character Data In this demonstration you will see: How to work with Unicode and non Unicode data How to work with collations

Lesson 3: Converting Data Types Using CAST Using CONVERT Implicit Data Conversion Common Conversion Issues Demonstration 3A: Common Conversion Issues

Using CAST Converts an expression of one data type to another in SQL Server CAST is based on SQL standards SELECT 'The list price is ' + CAST(ListPrice AS varchar(12)) AS ListPriceMessage FROM Production.Product WHERE ListPrice BETWEEN AND ; SELECT CAST(SYSDATETIME() AS nvarchar(30)); SELECT 'The list price is ' + CAST(ListPrice AS varchar(12)) AS ListPriceMessage FROM Production.Product WHERE ListPrice BETWEEN AND ; SELECT CAST(SYSDATETIME() AS nvarchar(30));

Using CONVERT Converts an expression of one data type to another in SQL Server Optionally allows providing a style SQL Server specific extension to the SQL language SELECT 'The list price is ' + CONVERT(varchar(12),ListPrice) AS ListPriceMessage FROM Production.Product WHERE ListPrice BETWEEN AND ; SELECT CONVERT(varchar(8),SYSDATETIME(),112); SELECT CONVERT(char(8), 0x4E616d65, 0) AS 'Style 0, binary to character'; SELECT 'The list price is ' + CONVERT(varchar(12),ListPrice) AS ListPriceMessage FROM Production.Product WHERE ListPrice BETWEEN AND ; SELECT CONVERT(varchar(8),SYSDATETIME(),112); SELECT CONVERT(char(8), 0x4E616d65, 0) AS 'Style 0, binary to character';

Implicit Data Conversion When data isn't explicitly converted between types, implicit data conversion is attempted and is based on data type precedence. Not all data types can be implicitly converted to all other data types decimal(18,2) = ; int = 50000; xml; = ' '; decimal(18,2) = ; int = 50000; xml; = ' ';

Common Conversion Issues Many common issues arise during data type conversions  Inappropriate values for the target data type  Value is out of range for the target data type  Value is truncated while being converted (sometimes silently)  Value is rounded while being converted (sometimes silently)  Value is changed while being converted (sometimes silently)  Assumptions are made about internal storage formats for data types  Some datetime conversions are non-deterministic and depend on language settings  Some parsing issues are hard to understand

Demonstration 3A: Common Conversion Issues In the following demonstration you will see: How to convert date data types explicitly How language settings can affect date conversions How data can be truncated during data type conversion Issues that can arise with implicit conversion

Lesson 4: Working with Specialized Data Types timestamp and rowversion Alias Data Types Other Data Types Demonstration 4A: rowversion Data Type

timestamp and rowversion rowversion assists in creating systems based on optimistic concurrency  Automatically changes value whenever a row is modified  Replaces timestamp data type  New value is always larger than the previous value and is unique within the database

CREATE TYPE ProductNumber FROM nvarchar(20) NOT NULL; GO CREATE TABLE Production.ProductConversion (ProductConversionID int IDENTITY(1,1), FromProduct ProductNumber, ToProduct ProductNumber ); CREATE TYPE ProductNumber FROM nvarchar(20) NOT NULL; GO CREATE TABLE Production.ProductConversion (ProductConversionID int IDENTITY(1,1), FromProduct ProductNumber, ToProduct ProductNumber ); Alias Data Types CREATE TYPE can be used to create alias types Alias types are subtypes of existing system data types Alias types can include the details of nullability Often used to maintain consistency across data type usage in an application

Other Data Types Data TypeNotes binaryIs a fixed length binary data varbinaryIs a variable length binary data varbinary(max)Is a long variable length binary data imageIs a long variable length binary data – deprecated – use varbinary(max) instead hierarchyidRepresents a position in a tree hierarchy sql_variantStores values of various other data types xmlIs XML data stored in an internal format cursorIs used for variables that need to hold a reference to a cursor tableHolds an entire resultset geometry, geography Are used to hold spatial data

Demonstration 4A: rowversion Data Type In this demonstration you will see: How to use the rowversion data type

Lab 2: Working with Data Types Exercise 1: Choosing Appropriate Data Types Exercise 2: Writing Queries With Data Type Conversions Challenge Exercise 3: Designing and Creating Alias Data Types (Only if time permits) Logon information Estimated time: 45 minutes

Lab Scenario A new developer has sought your assistance in deciding which data types to use for three new tables she is designing. She presents you with a list of organizational data requirements for each table. You need to decide on appropriate data types for each item. You need to export some data from your existing system but while being exported, some of the columns need to be converted to alternate data types. If you have time, there is another issue that your manager would like you to address. She is concerned about a lack of consistency in the use of data types across the organization. At present, she is concerned about addresses and phone numbers. You need to review the existing data types being used in the MarketDev database for this and create new data types that can be used in applications, to avoid this inconsistency.

Lab Review What data type should I use to store the number of seconds since midnight? Which of the following columns are likely to be nullable: YTD_Sales, DateOfBirth?

Module Review and Takeaways Review Questions Best Practices