SQL Server 2014 Development Essentials
4.5/5
()
About this ebook
Related to SQL Server 2014 Development Essentials
Related ebooks
Microsoft SQL Server 2012 Integration Services: An Expert Cookbook Rating: 5 out of 5 stars5/5SQL Server: Tips and Tricks - 2 Rating: 4 out of 5 stars4/5SQL Server 2016 Developer's Guide Rating: 0 out of 5 stars0 ratingsOracle Advanced PL/SQL Developer Professional Guide Rating: 4 out of 5 stars4/5Advanced Oracle PL/SQL Developer's Guide - Second Edition Rating: 5 out of 5 stars5/5Securing SQL Server: Protecting Your Database from Attackers Rating: 0 out of 5 stars0 ratingsSQL Server: Tips and Tricks - 1 Rating: 5 out of 5 stars5/5Oracle SQL Developer Rating: 0 out of 5 stars0 ratingsCreating your MySQL Database: Practical Design Tips and Techniques Rating: 3 out of 5 stars3/5The SQL Workshop: Learn to create, manipulate and secure data and manage relational databases with SQL Rating: 0 out of 5 stars0 ratingsMicrosoft SQL Server 2014 Business Intelligence Development Beginner’s Guide Rating: 0 out of 5 stars0 ratingsIntroduction to Oracle Database Administration Rating: 5 out of 5 stars5/5Learning PostgreSQL Rating: 1 out of 5 stars1/5Microsoft SQL Server 2012 Administration: Real-World Skills for MCSA Certification and Beyond (Exams 70-461, 70-462, and 70-463) Rating: 0 out of 5 stars0 ratingsWhat Is Sql ?: Fundamentals of Sql,T-Sql,Pl/Sql and Datawarehousing. Rating: 0 out of 5 stars0 ratingsSQL Clearly Explained Rating: 5 out of 5 stars5/5Oracle SQL Developer 2.1 Rating: 0 out of 5 stars0 ratingsSQL Tutorial For Beginners Rating: 0 out of 5 stars0 ratingsSQL Interview Questions: A complete question bank to crack your ANN SQL interview with real-time examples Rating: 0 out of 5 stars0 ratingsBasic DBA Query v.1: Oracle Database Rating: 5 out of 5 stars5/5Database Design and SQL for DB2 Rating: 5 out of 5 stars5/5Expert Cube Development with Microsoft SQL Server 2008 Analysis Services Rating: 5 out of 5 stars5/5PostgreSQL 9.0 High Performance Rating: 4 out of 5 stars4/5MySQL for Python Rating: 5 out of 5 stars5/5AWS Certified Database Study Guide: Specialty (DBS-C01) Exam Rating: 0 out of 5 stars0 ratingsMastering MariaDB Rating: 0 out of 5 stars0 ratingsRelational Database Systems Rating: 0 out of 5 stars0 ratingsSQL Server MVP Deep Dives Rating: 0 out of 5 stars0 ratingsDB2 9 for z/OS Database Administration: Certification Study Guide Rating: 0 out of 5 stars0 ratings
Programming For You
Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Mastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Secrets of the JavaScript Ninja Rating: 4 out of 5 stars4/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsRaspberry Pi: Beginner's Guide - Learn Raspberry Pi in Easy Steps Rating: 2 out of 5 stars2/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5C# Programming from Zero to Proficiency (Introduction): C# from Zero to Proficiency, #0 Rating: 0 out of 5 stars0 ratingsPython QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratings
Reviews for SQL Server 2014 Development Essentials
2 ratings0 reviews
Book preview
SQL Server 2014 Development Essentials - Basit A. Masood-Al-Farooq
Table of Contents
SQL Server 2014 Development Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Instant updates on new Packt books
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Microsoft SQL Server Database Design Principles
Database design
The requirement collection and analysis phase
The conceptual design phase
The logical design phase
The physical design phase
The implementation and loading phase
The testing and evaluation phase
The database design life cycle recap
Table design
Tables
Entities
Attributes
Relationships
A one-to-one relationship
A one-to-many relationship
A many-to-many relationship
Data integrity
The basics of data normalization
The normal forms
The first normal form (1NF)
The second normal form (2NF)
The third normal form (3NF)
Denormalization
The SQL Server database architecture
Pages
Extents
The transaction log file architecture
The operation and workings of a transaction log
Filegroups
The importance of choosing the appropriate data type
SQL Server 2014 system data types
Alias data types
Creating and dropping alias data types with SSMS 2014
Creating and dropping alias data types using the Transact-SQL DDL statement
Creating an alias data type using CREATE TYPE
Dropping an alias data type using DROP TYPE
CLR user-defined types
Summary
2. Understanding DDL and DCL Statements in SQL Server
Understanding the DDL, DCL, and DML language elements
Data Definition Language (DDL) statements
Data Manipulation Language (DML) statements
Data Control Language (DCL) statements
Understanding the purpose of SQL Server 2014 system databases
SQL Server 2014 system databases
The master database
The model database
The msdb database
The tempdb database
The resource database
The distribution database
An overview of database recovery models
The simple recovery model
The bulk-logged recovery model
Full recovery
Creating and modifying databases
Create, modify, and drop databases with T-SQL DDL statements
Creating a database with T-SQL DDL statements
Example 1 – creating a database based on a model database
Example 2 – creating a database that explicitly specifies the database data and the transaction log file's filespecs properties
Example 3 – creating a database on multiple filegroups
Modifying a database with T-SQL DDL statements
Example – adding a secondary data file to an existing database
Dropping a database with T-SQL DDL statements
Create, modify, and drop databases with SSMS 2014
Creating a database with SSMS 2014
Modifying a database with SSMS 2014
Dropping a database with SSMS 2014
Creating and managing database schemas
Managing schemas using T-SQL DDL statements
Managing schemas using SSMS 2014
Creating and managing tables
Creating and modifying tables
Creating and modifying tables with T-SQL DDL statements
Creating a table with T-SQL DDL statements
Modifying a table with T-SQL DDL statements
Dropping a table with T-SQL DDL statements
Creating and modifying tables with SSMS 2014
Creating a table with SSMS 2014
Modifying a table with SSMS 2014
Deleting a table with SSMS 2014
Grant, deny, and revoke permissions to securables
Grant, deny, and revoke permissions to securables with T-SQL DCL statements
Granting permissions to securables with T-SQL DCL statements
Denying permissions to securables with T-SQL DCL statements
Revoking permissions to securables with T-SQL DCL statements
Managing permissions using SSMS 2014
Summary
3. Data Retrieval Using Transact-SQL Statements
Understanding Transact-SQL SELECT, FROM, and WHERE clauses
The SELECT statement
The FROM clause
The WHERE clause
Using T-SQL functions in the query
Aggregate functions
Configuration functions
Cursor functions
Date and time functions
Mathematical functions
Metadata functions
Rowset functions
Security functions
String functions
System statistical functions
Multiple table queries using UNION, EXCEPT, INTERSECT, and JOINs
The UNION operator
The EXCEPT operator
The INTERSECT operator
The JOIN operator
Using INNER JOIN
Using outer joins
Using LEFT OUTER JOIN
Using RIGHT OUTER JOIN
Using FULL OUTER JOIN
Using CROSS JOIN
Using self joins
Subqueries
Examples of subqueries
Common Table Expressions
Organizing and grouping data
The ORDER BY clause
The GROUP BY clause
The HAVING clause
The TOP clause
The DISTINCT clause
Pivoting and unpivoting data
Using the Transact-SQL analytic window functions
Ranking functions
PERCENT RANK
CUME_DIST
PERCENTILE_CONT and PERCENTILE_DISC
LEAD and LAG
FIRST_VALUE and LAST_VALUE
Summary
4. Data Modification with SQL Server Transact-SQL Statements
Inserting data into SQL Server database tables
The INSERT examples
Example 1 – insert a single row into a SQL Server database table
Example 2 – INSERT with the SELECT statement
Example 3 – INSERT with the EXEC statement
Example 4 – explicitly inserting data into the IDENTITY column
Updating data in SQL Server database tables
The UPDATE statement examples
Example 1 – updating a single row
Example 2 – updating multiple rows
Deleting data from SQL Server database tables
The DELETE statement examples
Example 1 – deleting a single row
Example 2 – deleting all rows
Using the MERGE statement
The MERGE statement examples
The TRUNCATE TABLE statement
The SELECT INTO statement
Summary
5. Understanding Advanced Database Programming Objects and Error Handling
Creating and using variables
Creating a local variable
Creating the cursor variable
Creating the table variable
Control-of-flow keywords
BEGIN…END keywords
The IF…ELSE expression
A CASE statement
WHILE, BREAK, and CONTINUE statements
RETURN, GOTO, and WAITFOR statements
Creating and using views
Creating views with Transact-SQL and SSMS 2014
Creating, altering, and dropping views with Transact-SQL DDL statements
The CREATE VIEW statement
The ALTER VIEW statement
The DROP VIEW statement
Creating, altering, and dropping views with SSMS 2014
Creating views with SSMS 2014
Altering and dropping views with SSMS 2014
Indexed views
Indexed view example
Creating and using stored procedures
Creating a stored procedure
Modifying a stored procedure
Dropping a stored procedure
Viewing stored procedures
Executing stored procedures
Creating and using user-defined functions
Creating user-defined functions
Creating a user-defined scalar function
Using a user-defined scalar function
Creating a user-defined table-valued function
Inline table-valued function example
Multistatement table-valued function example
Modifying user-defined functions
Using a user-defined table-valued function
Dropping user-defined functions
Viewing user-defined functions
Creating and using triggers
Nested triggers
Recursive triggers
DML triggers
Inserted and deleted logical tables
Creating DML triggers
Modifying a DML trigger
Dropping a DML trigger
Data Definition Language (DDL) triggers
The EVENTDATA function
Creating a DDL trigger
Modifying a DDL trigger
Dropping a DDL trigger
Disabling and enabling triggers
Viewing triggers
Handling Transact-SQL errors
An example of TRY...CATCH
An example of TRY...CATCH with THROW
An example of TRY...CATCH with RAISERROR
Summary
6. Performance Basics
Components of SQL Server Database Engine
The SQL Server Relational Engine architecture
Parsing and binding
Query optimization
Query execution and plan caching
Query plan aging
The improved design in SQL Server 2014 for the cardinality estimation
Optimizing SQL Server for ad hoc workloads
Manually clearing the plan cache
The SQL Server 2014 in-memory OLTP engine
The limitations of memory-optimized tables
Indexes
The cost associated with indexes
How SQL Server uses indexes
Access without an index
Access with an index
The structure of indexes
Index types
Clustered indexes
When should you have a clustered index on a table?
Nonclustered indexes
Single-column indexes
Composite indexes
Covering indexes
Unique indexes
Spatial indexes
Partitioned indexes
Filtered indexes
Full-text indexes
XML indexes
Memory-optimized indexes
Columnstore indexes
The architecture of columnstore indexes
Creating and managing columnstore indexes
Guidelines for designing and optimizing indexes
Avoid overindexing tables
Create a clustered index before creating nonclustered indexes when using clustered indexes
Index columns used in foreign keys
Index columns frequently used in joins
Use composite indexes and covering indexes to give the query optimizer greater flexibility
Limit key columns to columns with a high level of selectability
Pad indexes and specify the fill factor to reduce page splits
Rebuild indexes based on the fragmentation level
Query optimization statistics
Database-wide statistics options in SQL Server to automatically create and update statistics
Manually create and update statistics
Determine the date when the statistics were last updated
Using the DBCC SHOW_STATISTICS command
Using the sys.stats catalog view with the STATS_DATE() function
The fundamentals of transactions
Transaction modes
Implementing transactions
BEGIN TRANSACTION
COMMIT TRANSACTION
ROLLBACK TRANSACTION
SAVE TRANSACTION
An overview of locking
Basic locks
Optimistic and pessimistic locking
Transaction isolation
SQL Server 2014 tools for monitoring and troubleshooting SQL Server performance
Activity Monitor
The SQLServer:Locks performance object
Dynamic Management Views
SQL Server Profiler
The sp_who and sp_who2 system stored procedures
SQL Server Extended Events
Summary
Index
SQL Server 2014 Development Essentials
SQL Server 2014 Development Essentials
Copyright © 2014 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2014
Production reference: 1180714
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78217-255-0
www.packtpub.com
Cover image by Angela Robledo (<angel.spo@gmail.com>)
Credits
Author
Basit A. Masood-Al-Farooq
Reviewers
Basavaraj Biradar
Brenner Grudka Lira
David Loo
Richard Louie
José (Cheo) Redondo
Acquisition Editor
Neha Nagwekar
Content Development Editor
Neil Alexander
Technical Editor
Pankaj Kadam
Copy Editors
Insiya Morbiwala
Sayanee Mukherjee
Aditya Nair
Project Coordinator
Sageer Parkar
Proofreaders
Simran Bhogal
Ameesha Green
Indexers
Rekha Nair
Tejal Soni
Graphics
Ronak Dhruv
Production Coordinators
Kyle Albuquerque
Saiprasad Kadam
Conidon Miranda
Cover Work
Kyle Albuquerque
About the Author
Basit A. Masood-Al-Farooq is an internationally known Lead SQL DBA, trainer, and technical author with twelve years' experience of the Microsoft technology stack. He is an accomplished development and production SQL Server DBA with a proven record of delivering major projects on time and within budget. He is an expert at evaluating the clients' needs against the capabilities of the SQL Server product set, with the objective of minimizing costs and maximizing functions by making innovative use of advance capabilities. Basit has authored numerous SQL Server technical articles on various SQL Server topics for different SQL Server community sites, which include SQLMag.com, MSSQLTips.com, SQLServerCentral.com, SSWUG.org, SQL-Server-Performance.com, and SearchSQLServer.com.
He has also developed and implemented many successful database infrastructures, data warehouses, and business intelligence projects. He holds a Master's degree in Computer Science from London Metropolitan University and industry-standard certifications from Microsoft, Sun, Cisco, Brainbench, ProSoft, and APM, which include MCITP Database Administrator 2008, MCITP Database Administrator 2005, MCDBA SQL Server 2000 and MCTS .NET Framework 2.0 Web Applications. He also has a good understanding of ITIL principles.
He can be reached via Twitter (@BasitAali), his blog (http://basitaalishan.com), or via LinkedIn (http://uk.linkedin.com/in/basitfarooq).
He was a technical reviewer for SQL Server 2012 Reporting Services Blueprints, Marlon Ribunal and Mickey Stuewe, Packt Publishing and Reporting with Microsoft SQL Server 2012, James Serra and Bill Anton, Packt Publishing.
Acknowledgments
First and foremost, I would like to praise and thank Allah SWT, the compassionate, the almighty, the most merciful, who has granted me countless blessings, knowledge, and opportunities. Without the will of Allah SWT, none of this would be possible.
I would like to thank my parents for getting me started on my journey, giving me the opportunity for a great education, allowing me to realize my own potential, and giving me the freedom to choose my career path. Thanks Dad (Masood Ahmad Nisar) and Mom (Saeeda Perveen); you both have always supported me and encouraged me in everything I have ever done. You both have worked extremely hard all your life to give me the life I wanted.
I would like to thank my caring, loving, and supportive wife, Aniqa, for all the encouragement and support. Despite the long days, sleepless nights, and long and exhausting marathons of writing, a few words of love and encouragement from you always successfully wipe away all my fatigue. Thank you for supporting me, coping with my hectic work schedule, and taking care of our kids and household activities, and giving me time so that I can pursue and concentrate on this book-writing project. I would also like to thank my two sons, Saifaan and Rayyan, and would like to dedicate this book to them because they always make me smile and understood on those weekend mornings when I was writing this book instead of playing with them. I hope someday you both will read my book and understand why I spent so much time in front of my laptop.
I would also like to thank Packt Publishing for giving me the opportunity to write this book. Last but not least, I would like to thank Neha Nagwekar, the Acquisition Editor; Neil Alexander, the Content Development Editor; Pankaj Kadam, the Technical Editor; and all the technical reviewers, for their help, advice, and constructive comments, because without their feedback this book would not have been possible.
About the Reviewers
Bas avaraj Biradar holds a Master's degree in Computer Applications with gold medals from Gulbarga University, India. Besides these, he has a Microsoft Certified Professional certification in SQL Server 2000. Basavaraj has a rich experience of more than 13 years in designing and developing databases for complex, large, online systems. Currently, Basavaraj is working as Senior Technical Lead in a major IT security company.
Basavaraj writes blogs regularly at http://sqlhints.com on SQL Server technologies and his articles are quite popular in the industry. Basavaraj speaks about SQL Server in Microsoft User Group meetings and in his company as well. You may contact Basavaraj through his e-mail ID,
Brenner Grudka Lira has been a data analyst and DBA at Neurotech since 2012. He has a Bachelor's degree in Computer Science and a postgraduate degree in Project Management, both from the Catholic University of Pernambuco in Recife, Brazil.
He also has experience in building and modeling data warehouses and has knowledge in SQL Server and MySQL database management. Today, he is dedicated to the study of project management and database tuning.
He has reviewed Microsoft SQL Server 2012 Integration Services: An Expert Cookbook, Reza Rad and Pedro Perfeito; Oracle BI Publisher 11g: A Practical Guide to Enterprise Reporting, Daniela Bozdoc; and Getting Started with SQL Server 2014 Administration, Gethyn Ellis, all by Packt Publishing.
David Loo is a senior software professional with over 25 years' experience in both software development and people management. He is respected for his ability to focus teams on service excellence and for designing and implementing practical process improvements and solutions. He is always on the lookout for ways to contribute his knowledge and experience of software development, team-building, and development best practices.
He has reviewed Getting Started with SQL Server 2012 Cube Development, Simon Lidberg and Getting Started with SQL Server 2014 Administration, Gethyn Ellis, both by Packt Publishing.
Richard Louie is a Senior Business Intelligence Developer with over 20 years' experience in software development and project management. He has extensive hands-on experience in Oracle and Microsoft SQL for ETL, SSIS, SSRS, SSAS, and VB.Net. Richard is a graduate of the University of California, Irvine in Information and Computer Science, and is ASQ Green Belt Certified.
He has reviewed Getting Started