1 / 17

Module 4: Creating Data Types and Tables

Learn about creating data types and tables in SQL Server, including guidelines for specifying data types, organizing data in rows, and generating scripts for database objects.

Download Presentation

Module 4: Creating Data Types and Tables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 4: Creating Data Types and Tables

  2. Overview • Creating Data Types • Creating Tables • Generating Column Values • Generating Scripts

  3. Creating Data Types • System-supplied Data Types • Creating and Dropping User-defined Data Types • Guidelines for Specifying Data Types

  4. System-supplied Data Types • Numeric • Integer • Exact numeric • Approximate numeric • Monetary • Date and Time • Character and Unicode Character • Binary • Other

  5. EXEC sp_addtype city, 'nvarchar(15)', NULL EXEC sp_addtype region, 'nvarchar(15)', NULL EXEC sp_addtype country, 'nvarchar(15)', NULL Creating and Dropping User-defined Data Types Creating Dropping EXEC sp_droptype city

  6. Guidelines for Specifying Data Types • If Column Length Varies, Use a Variable Data Type • Use tinyint Appropriately • For Numeric Data Types, Commonly Use decimal • If Storage Is Greater Than 8000 Bytes, Use text or image • Use money for Currency • Do Not Use float or real as Primary Keys

  7. Creating Tables • How SQL Server Organizes Data in Rows • How SQL Server Organizes text, ntext, and image Data • Creating and Dropping a Table • Adding and Dropping a Column

  8. Data Header Fixed Data NB VB Variable Data 4 bytes Null Block Variable Block How SQL Server Organizes Data in Rows

  9. How SQL Server Organizes text, ntext, and image Data Data row Text Pointer Root Structure Intermediate Node Intermediate Node block 1 block 2 block 1 block 2

  10. Column name Data type NULL or NOT NULL CREATE TABLE dbo.Categories (CategoryID CategoryName Description Picture int IDENTITY(1,1)nvarchar(15) ntext image NOT NULL,NOT NULL, NULL, NULL) Creating and Dropping a Table • Creating a Table • Column Collation • Specifying NULL or NOT NULL • Computed Columns • Dropping a Table

  11. Adding and Dropping a Column ALTER TABLE CategoriesNew ADD Commission money null ADD Customer_name Sales_amount Sales_date Customer ID Commission DROP ALTER TABLE CategoriesNew DROP COLUMN Sales_date

  12. Generating Column Values • Using the Identity Property • Using the NEWID Function and the uniqueidentifier Data Type

  13. Using the Identity Property • Requirements for Using the Identity Property • Only one identity column is allowed per table • Use with integer, numeric, and decimal data types • Retrieving Information About the Identity Property • Use IDENT_SEED and IDENT_INCR for definition information • Use @@identity to determine most recent value • Managing the Identity Property

  14. CREATE TABLE Customer (CustID uniqueidentifier NOT NULL DEFAULT NEWID(), CustName char(30) NOT NULL) Using the NEWID Function and the uniqueidentifier Data Type • These Features Are Used Together • Ensure Globally Unique Values • Use with the DEFAULT Constraint

  15. Generating Scripts • Generate Schema as a Transact-SQL Script • Maintain backup script • Create or update a database development script • Create a test or development environment • Train new employees • What to Generate • Entire database into single script file • Table-only schema • Table and index schema

  16. Specify Appropriate Data Types and Data Type Sizes Always Specify Column Characteristics in CREATE TABLE Generate Scripts to Recreate Database and Database Objects Recommended Practices

  17. Review • Creating Data Types • Creating Tables • Generating Column Values • Generating Scripts

More Related