You are previewing Beginning SQL Server for Developers, Fourth Edition.
O'Reilly logo
Beginning SQL Server for Developers, Fourth Edition

Book Description

Beginning SQL Server for Developers is the perfect book for developers new to SQL Server and planning to create and deploy applications against Microsoft’s market-leading database system for the Windows platform. Now in its fourth edition, the book is enhanced to cover the very latest developments in SQL Server, including the in-memory features that are introduced in SQL Server 2014.

Within the book, there are plenty of examples of tasks that developers routinely perform. You’ll learn to create tables and indexes, and be introduced to best practices for securing your valuable data. You’ll learn design tradeoffs and find out how to make sound decisions resulting in scalable databases and maintainable code.

SQL Server 2014 introduces in-memory tables and stored procedures. It's now possible to accelerate applications by creating tables (and their indexes) that reside entirely in memory, and never on disk. These new, in-memory structures differ from caching mechanisms of the past, and make possible the extraordinarily swift execution of certain types of queries such as are used in business intelligence applications. Beginning SQL Server for Developers helps you realize the promises of this new feature set while avoiding pitfalls that can occur when mixing in-memory tables and code with traditional, disk-based tables and code.

Beginning SQL Server for Developers takes you through the entire database development process, from installing the software to creating a database to writing the code to connect to that database and move data in and out. By the end of the book, you’ll be able to design and create solid and reliable database solutions using SQL Server.

  • Takes you through the entire database application development lifecycle
  • Includes brand new coverage of the in-memory features
  • Introduces the freely-available Express Edition
  • Table of Contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Author
    8. About the Technical Reviewer
    9. Acknowledgments
    10. Introduction
    11. Chapter 1: Overview and Installation
      1. Why Should I Use SQL Server?
      2. SQL Server Editions
      3. The Example
      4. Installation
        1. Install
        2. Setup Role
        3. Choosing the Features to Install
        4. Naming the Instance
        5. Service Accounts
        6. Authentication Mode
        7. Defining the Data Directories
        8. FILESTREAM Options
        9. Creating the Reporting Services Database
        10. Distributed Replay Options
      5. Security
        1. Services Accounts
        2. Looking at the Authentication Mode
        3. The sa Login
      6. Summary
    12. Chapter 2: SQL Server Management Studio
      1. A Quick Overview of SSMS
      2. Query Editor
      3. sqlcmd
      4. Summary
    13. Chapter 3: Database Design and Creation
      1. Defining a Database
      2. Prebuilt Databases Within SQL Server
        1. master
        2. tempdb
        3. model
        4. msdb
        5. AdventureWorks/AdventureWorksDW
      3. Choosing the Database System Type
        1. OLTP
        2. BI/OLAP
        3. Example System Choice
      4. Gathering the Data
      5. Determining the Information to Store in the Database
        1. Financial Products
        2. Customers
        3. Customer Addresses
        4. Shares
        5. Transactions
      6. External and Ignored Information
      7. Building Relationships
        1. Using Keys
        2. Creating Relationships
        3. More on Foreign Keys
      8. Normalization
        1. Assign Each Entity a Unique Identifier
        2. Store Only Information That Directly Relates to That Entity
        3. Avoid Repeating Values or Columns
        4. Normal Forms
        5. Denormalization
      9. Creating the Sample Database
        1. Creating a Database in SQL Server Management Studio
        2. Dropping the Database in SQL Server Management Studio
        3. Creating a Database in a Query Pane
      10. Summary
    14. Chapter 4: Security and Compliance
      1. Logins
      2. Server Logins and Database Users
      3. Roles
        1. Fixed Server Roles
        2. Database Roles
        3. Application Roles
      4. Schemas
      5. Before You Can Proceed with Your Solution
      6. Declarative Management Framework
      7. Encryption
      8. Summary
    15. Chapter 5: Defining Tables
      1. What Is a Table?
      2. SQL Server Data Types
        1. Table Data Types
        2. Program Data Types
      3. Columns As More Than Simple Data Repositories
        1. Default Values
        2. Generating IDENTITY Values
        3. Uniqueidentifiers for an Identity
      4. Sequences Instead of IDENTITY
        1. The Use of NULL Values
      5. Creating a Table in SQL Server Management Studio
      6. Creating a Table Through Query Editor
      7. Creating a Table: Using SQLCMD
      8. The ALTER TABLE Statement
      9. Defining the Remaining Tables
      10. Setting a Primary Key
      11. Creating a Relationship
        1. Check Existing Data on Creation
        2. Enforce Foreign Key Constraints
        3. Choosing Delete and Update Rules
      12. Building a Relationship via T-SQL
      13. Relationships: Database or Application
      14. Partitioning
      15. Summary
    16. Chapter 6: Creating Indexes and Database Diagramming
      1. What Is an Index?
        1. Types of Indexes
        2. Uniqueness
      2. Determining What Makes a Good Index
        1. Using Low-Maintenance Columns
        2. Using Primary and Foreign Keys
        3. Finding Specific Records
        4. Using Covering Indexes
        5. Looking for a Range of Information
        6. Keeping the Data in Order
      3. Determining What Makes a Bad Index
        1. Using Unsuitable Columns
        2. Choosing Unsuitable Data
        3. Including Too Many Columns
        4. When There Are Too Few Records in the Table
      4. Reviewing Your Indexes for Performance
      5. Creating an Index
        1. Creating an Index with the Table Designer
        2. Indexes and Statistics
        3. The CREATE INDEX Syntax
        4. Creating an Index in Query Editor
      6. Dropping an Index
      7. Altering an Index
      8. Included Columns
      9. When an Index Does Not Exist
      10. Diagramming the Database
        1. Database Diagramming Basics
        2. The SQL Server Database Diagram Designer
        3. The Default Database Diagram
        4. The Database Diagram Toolbar
        5. Modifying Your Database Using Database Diagram Designer
      11. Summary
    17. Chapter 7: In-Memory Tables
      1. Cost-Based Processing
      2. What Is an In-Memory Table?
        1. Differences to Disk-Based Tables
      3. In-Memory Indexes and the Hash Index
        1. Hash Buckets
        2. General Index Points
      4. Operating System and Hardware Requirements
      5. Database Requirements Prior to Table Creation
      6. Creating an In-memory Table Using SQL
      7. Migrating to an In-Memory Table
      8. Summary
    18. Chapter 8: Database Backups and Recovery
      1. Backup Strategies
        1. Recovery Models
        2. Types of Backups
        3. Backup Strategy Decisions
      2. Transaction Logs
      3. When Problems May Occur
      4. In-Memory Table Backup and Restore
      5. Backups: Taking a Database Offline
      6. Backing Up the Data
        1. Backup Considerations
        2. Performing a Manual SQL Server Backup
        3. Backing Up the Database Using T-SQL
        4. Transaction Log Backup Using T-SQL
      7. Restoring a Database
        1. Restoring Using SQL Server Management Studio
        2. Restoring Using T-SQL
        3. Restoring to a Point in Time
      8. Detaching and Attaching a Database
        1. Detaching and Attaching Using SQL Server Management Studio
        2. Detaching and Attaching Using T-SQL
      9. Producing SQL Script for the Database
      10. Summary
    19. Chapter 9: Database Maintenance
      1. Creating a Database Maintenance Plan
      2. Executing the Plan
      3. Setting Up Database Mail
      4. Modifying a Maintenance Plan
      5. Summary
    20. Chapter 10: Data Insertion, Deletion, and Transactions—Disk-Based
      1. Inserting Data
        1. The T-SQL INSERT Statement Syntax
        2. INSERT SQL Statement
        3. DBCC CHECKIDENT
        4. Column Constraints
        5. Inserting Several Records at Once
      2. Transactions
        1. Transaction Basics
        2. Transaction Commands
        3. Locking Data
      3. Deleting Data
        1. DELETE Syntax
        2. Before Demonstrating the DELETE Statement
        3. Using the DELETE Statement
        4. Truncating a Table
        5. Dropping a Table
      4. Summary
    21. Chapter 11: Selecting and Updating Data from Disk-Based Tables
      1. Retrieving Data
        1. Using SQL Server Management Studio to Retrieve Data
        2. Using the SELECT Statement to Retrieve Data
      2. Limiting a Search: The Use of WHERE
        1. TOP (n)
        2. TOP (n) PERCENT
        3. SET ROWCOUNT n
      3. String Functions
      4. Order! Order!
      5. The LIKE Operator
      6. Creating Data: SELECT INTO
      7. Updating Data
        1. The UPDATE Statement
        2. Updating Data within Query Editor
        3. Updating Data: Using Transactions
        4. Nested Transactions
      8. Using More Than One Table
      9. Summary
    22. Chapter 12: Working with In-Memory Tables
      1. INSERT, UPDATE, DELETE, and SELECT Statements
      2. Locking and Isolation Levels
      3. MERGE Statement
      4. In-Memory Table Unsupported Functionality
      5. Summary
    23. Chapter 13: Building a View
      1. Why a View?
        1. Using Views for Security
        2. Encrypting View Definitions
      2. Views and In-memory Tables
      3. Creating a View: SQL Server Management Studio
      4. Creating a View by Using a View
      5. Creating a View Using T-SQL Syntax
        1. The CREATE VIEW Syntax
        2. Creating a View with CREATE VIEW and SELECT
        3. Binding Columns in a View with SCHEMABINDING
      6. Indexing a View
      7. Summary
    24. Chapter 14: Stored Procedures, Functions, and Security
      1. What Is a Stored Procedure?
      2. Creating a Stored Procedure
        1. CREATE PROCEDURE Syntax
        2. Returning a Set of Rows
        3. Creating a Stored Procedure: Management Studio
        4. Different Methods of Execution
      3. Returning Results
        1. Using RETURN
        2. Returning Multiple Result Sets
      4. Controlling the Flow
        1. IF . . . ELSE
        2. BEGIN . . . END
        3. WHILE . . . BREAK Statement
        4. CASE Statement
      5. Bringing It All Together
      6. Security
        1. Security Gains
        2. Granting and Revoking Permissions
      7. User-Defined Functions
        1. Scalar Functions
        2. Table-Valued Functions
        3. Considerations When Building Functions
      8. Summary
    25. Chapter 15: Natively Compiled Stored Procedures
      1. What Is a Natively Compiled Stored Procedure?
      2. CREATE PROCEDURE and Syntax
      3. Summary
    26. Chapter 16: Essentials for Effective Coding
      1. Variables
      2. Temporary Tables
      3. Aggregations
        1. COUNT/COUNT_BIG
        2. SUM
        3. MAX/MIN
        4. AVG
      4. Grouping Data
      5. HAVING
      6. Distinct Values
      7. Functions
        1. Date and Time
        2. String
        3. System Functions
      8. RAISERROR
      9. Error Handling
        1. @@ERROR
        2. TRY...CATCH and THROW
      10. Summary
    27. Chapter 17: Advanced T-SQL and Debugging
      1. Creating a SEQUENCE
      2. Subqueries
        1. Using Subqueries
        2. IN
        3. EXISTS
        4. Tidying Up the Loose End
      3. The APPLY Operator
        1. CROSS APPLY
        2. OUTER APPLY
      4. Common Table Expressions
        1. Using Common Table Expressions
        2. Recursive CTE
      5. Pivoting Data
        1. PIVOT
        2. UNPIVOT
      6. Table Variables
      7. Ranking Functions
        1. ROW_NUMBER
        2. RANK
        3. DENSE_RANK
        4. NTILE
      8. Cursors
      9. Debugging Your Code
        1. Debugging Windows
        2. Debugging Options
        3. Debugging Pop-Up Window
      10. Summary
    28. Chapter 18: Triggers
      1. What Is a Trigger?
      2. The DML Trigger
        1. CREATE TRIGGER Syntax for DML Triggers
        2. Why Not Use a Constraint?
        3. Deleted and Inserted Logical Tables
        4. Creating a DML FOR/AFTER Trigger
        5. Checking Specific Columns
      3. DDL Triggers
        1. DDL Database-Level Events
        2. Dropping a DDL Trigger
        3. EVENTDATA()
      4. Summary
    29. Chapter 19: Connecting via Code
      1. General Application Security
      2. Connecting with Excel or Excel Visual Basic for Applications (VBA)
        1. Using Excel with the Data Ribbon
        2. Using Excel with VBA
      3. Visual Studio
        1. Terminology
        2. VB.NET
        3. C#
      4. Java
      5. Summary
    30. Index