Module 2: Creating Data Types and Tables
Overview Creating Data Types Creating Tables Creating Partitioned Tables
Lesson 1: Creating Data Types What Are System-Supplied Data Types? Using System-Supplied Data Types What Are Alias Data Types? Practice: Creating Data Types
What Are System-Supplied Data Types? Category Data types Numeric Integer int, bigint, smallint, tinyint Exact decimal, numeric Approximate float, real Monetary money, smallmoney Date and time datetime, smalldatetime Character Non-Unicode char, varchar, varchar(max), text Unicode nchar, nvarchar, nvarchar(max), ntext Binary binary, varbinary, varbinary(max) Image image Global identifier uniqueidentifier XML xml Special bit, cursor, timestamp, sysname, table, sql_variant
Using System-Supplied Data Types Exact and approximate numeric data types Exact – decimal, numeric Approximate – float, real Character data types Fixed – nchar Variable – nvarchar Date and time data Dates and times – datetime, smalldatetime Row version – timestamp Large data values Large object type – text, image max specifier – varchar(max), nvarchar(max), varbinary(max)
What Are Alias Data Types? Based on system-supplied types Used for common data elements with a specific format Created by using the CREATE TYPE statement CREATE TYPE dbo.StateCode FROM char(2) NULL
Practice: Creating Data Types In this practice, you will: Create a data type by using SQL Server Management Studio Create a data type by using Transact-SQL
Lesson 2: Creating Tables How SQL Server Organizes Data in Rows How SQL Server Organizes Large Data Values Considerations for Creating Tables Generating Transact-SQL Scripts Practice: Creating Tables
How SQL Server Organizes Data in Rows Header Fixed Data NB VB Variable Data 4 Bytes Null Block Variable Block 2 Bytes 10111001 … 2 Bytes 2 Bytes … Number of columns More column bits as required Number of variable-length columns Additional pointer per variable column Nullability bit per column (first 8 columns) Pointer to end of first variable column data
How SQL Server Organizes Large Data Values Large value types out of row option ON Data row nvarchar(max) [ < 8K] nvarchar(max) [ > 8K] Large value types out of row option OFF nvarchar(max) [ < 8K] nvarchar(max) [ > 8K] Data row
Considerations for Creating Tables Column collation Column nullability Special column types Computed columns Identity columns timestamp columns uniqueidentifier columns CREATE TABLE Sales.CustomerOrders (OrderID int IDENTITY NOT NULL, OrderDate datetime NOT NULL, CustomerID int NOT NULL, Notes nvarchar(200) NULL)
Generating Transact-SQL Scripts Object Explorer Connect to a SQL Server Database Engine instance Expand Object Explorer and locate the object Right-click and then click Script <object type> as 1 2 3 Generate Scripts Wizard Right-click a database, point to Tasks, and then click Generate Scripts Follow the steps in the wizard 1 2
Practice: Creating Tables In this practice, you will: Create a table by using SQL Server Management Studio Create a table by using Transact-SQL Generate a Transact-SQL script
Lesson 3: Creating Partitioned Tables What Are Partitioned Tables? What Are Partition Functions? What Is a Partition Scheme? Demonstration: Creating a Partitioned Table What Operations Can Be Performed on Partitioned Data?
What Are Partitioned Tables? Data is partitioned horizontally by range < 2003 2003 -- 2004 2004 -- 2005 > 2005 Sales.Orders
What Are Partition Functions? Partition functions define partition boundaries Boundary values can be assigned to LEFT or RIGHT < 2003 2003 -- 2004 2004 -- 2005 > 2005 CREATE PARTITION FUNCTION pf_OrderDate (datetime) AS RANGE RIGHT FOR VALUES ('01/01/2003', '01/01/2004', 01/01/2005')
What Is a Partition Scheme? A partition scheme assigns partitions to filegroups The “next” filegroup can also be defined < 2003 2003 -- 2004 2004 -- 2005 > 2005 CREATE PARTITION SCHEME ps_OrderDate AS PARTITION pf_OrderDate TO (fg1, fg2, fg3, fg4, fg5)
Demonstration: Creating a Partitioned Table In this demonstration, you will see how to: Create a partition function Create a partition scheme Create a partitioned table View partitioned table behavior
What Operations Can Be Performed on Partitioned Data? SWITCH: Swap a populated table or partition with an empty table or partition MERGE: Combine two adjacent partitions into a single partition SPLIT: Insert a boundary in an existing partition to create a new partition SWITCH SPLIT < 2003 2003 -- 2004 2004 -- 2005 2005 -- 2006 > 2006 MERGE
Lab: Creating Data Types and Tables Exercise 1: Creating Data Types Exercise 2: Creating Tables Exercise 3: Creating Partitioned Tables