Beginning SQL server for developers, 4th edition

670 63 0
Beginning SQL server for developers, 4th edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them www.it-ebooks.info Contents at a Glance About the Author��������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewer������������������������������������������������������������������������������������������� xxi Acknowledgments����������������������������������������������������������������������������������������������������������� xxiii Introduction���������������������������������������������������������������������������������������������������������������������� xxv ■■Chapter 1: Overview and Installation��������������������������������������������������������������������������������1 ■■Chapter 2: SQL Server Management Studio��������������������������������������������������������������������25 ■■Chapter 3: Database Design and Creation�����������������������������������������������������������������������43 ■■Chapter 4: Security and Compliance�������������������������������������������������������������������������������93 ■■Chapter 5: Defining Tables���������������������������������������������������������������������������������������������127 ■■Chapter 6: Creating Indexes and Database Diagramming���������������������������������������������167 ■■Chapter 7: In-Memory Tables����������������������������������������������������������������������������������������197 ■■Chapter 8: Database Backups and Recovery�����������������������������������������������������������������211 ■■Chapter 9: Database Maintenance���������������������������������������������������������������������������������265 ■■Chapter 10: Data Insertion, Deletion, and Transactions—Disk-Based��������������������������303 ■■Chapter 11: Selecting and Updating Data from Disk-Based Tables�������������������������������335 ■■Chapter 12: Working with In-Memory Tables����������������������������������������������������������������379 ■■Chapter 13: Building a View������������������������������������������������������������������������������������������395 ■■Chapter 14: Stored Procedures, Functions, and Security����������������������������������������������421 v www.it-ebooks.info ■ Contents at a Glance ■■Chapter 15: Natively Compiled Stored Procedures��������������������������������������������������������463 ■■Chapter 16: Essentials for Effective Coding������������������������������������������������������������������469 ■■Chapter 17: Advanced T-SQL and Debugging����������������������������������������������������������������521 ■■Chapter 18: Triggers������������������������������������������������������������������������������������������������������567 ■■Chapter 19: Connecting via Code����������������������������������������������������������������������������������593 Index���������������������������������������������������������������������������������������������������������������������������������641 vi www.it-ebooks.info Introduction Beginning SQL Server for Developers is for those people who see themselves as becoming either developers, database administrators, or a mixture of both, but have yet to tread that path with SQL Server This edition of my book is for readers who wish to learn and develop on either the free version of SQL Server, SQL Server Express, or with either the trial or licensed versions of SQL Server Whether you have no knowledge of databases, or have knowledge of desktop databases such as MS Access, or even come from a server-based background such as Oracle, DB2, Sybase, then this book will provide you with the insight to get up and running with SQL Server Right from the start, your basic knowledge will be expanded, and you will soon be moving from being a beginner through to a competent and professional developer It is the aim of this book to cater to a wide range of developers, from those who prefer to use the graphical interface for as much work as possible, to those who want to become more adept at using SQL Server’s programming language, T-SQL Where practical, each method of using SQL Server is demonstrated, explained, and expanded so that you can evaluate what works best in your situation You will also find a chapter on how to work with Excel, NET and Java, and incorporate them with T-SQL into your programming There are plenty of examples within the book of every action, along with details about the security of your data You will learn the best way to complete a task and even how to make the correct decision when there are two or more choices that can be made And let us not forget that SQL Server can work with in-memory tables and natively compiled stored procedures Once you reach the end of this book, you will be able to design and create solid and reliable database solutions competently and proficiently xxv www.it-ebooks.info Chapter Overview and Installation Welcome to Beginning SQL Server for Developers This book has been written for those who are interested in learning how to create solutions with Microsoft SQL Server, but have no prior knowledge of SQL Server You may well have had exposure to other database management systems (DBMSs), such as MySQL, Oracle, or Microsoft Access, but SQL Server uses different interfaces and has a different way of working compared to much of the competition The aim of this book is to bring you quickly up to a level at which you are developing competently with SQL Server This book is specifically dedicated to beginners and to those who at this stage wish to use only SQL Server You may find this book useful for understanding the basics of other database management systems in the marketplace, especially when working with T-SQL within this book Many DBMSs use an ANSI-standard SQL, so moving from SQL Server to Oracle, Sybase, and so on, will be a great deal easier after reading this book This chapter answers the following questions: • Why should I use SQL Server? • How I know whether my hardware meets the requirements? • Can I just confirm that I have the right operating system? • What can I with SQL Server? We will also look at installing your chosen edition and cover the following topics: • Installing SQL Server on a Windows platform • Options not installed by default • Where to install SQL Server physically • Multiple installations on one computer • How SQL Server runs on a machine • How security is implemented • Logon IDs for SQL Server, especially the sa (system administrator) logon Why Should I Use SQL Server? The following discussion is from my point of view, and although it no doubt differs from that of others, the basis of the discussion holds true SQL Server faces competition from other database management systems, not only from other Microsoft products such as Microsoft Access, but also from competitors such as Oracle, Sybase, DB2, and Informix, to name a few www.it-ebooks.info Chapter ■ Overview and Installation You can find Microsoft Access on a large number of PCs The fact that it is packaged with some editions of Office and has been around for a number of years in different versions of Office has helped make this DBMS ubiquitous; in fact, a great number of people actually use the software Unfortunately, it does have its limitations when it comes to scalability, speed, and flexibility, but for many small, in-house systems, these areas of concern are not an issue since such systems not require major DBMS functionality To this end, Microsoft Access can be the correct solution Now you come to the serious competition: Oracle and Sybase Oracle is seen as perhaps the market leader in the DBMS community, and it has an extremely large user base There is no denying it is a great product to work with, if somewhat more complex to install and administer than SQL Server; it fits well with large companies that require large solutions It also forms the backbone of some major software packages There are many parts to Oracle, which make it a powerful tool, including scalability and performance It also provides flexibility in that you can add on tools as you need them, making Oracle more accommodating in that area than SQL Server For example, SQL Server forces you to install the NET Framework on your server regardless of whether you use the new NET functionality However, Oracle isn’t as user-friendly from a developer’s point of view in areas like its ad hoc SQL Query tool and its XML and web technology tools It is also less straightforward in how to build up a complete database solution Other drawbacks include its cost and the complexity involved in installing and running it effectively However, you will find that it is used extensively by web search engines even though SQL Server could and does work just as effectively SQL Server has always been a one-purchase solution, such that (providing you buy the correct version or license) tools that allow you to analyze your data or copy data from one data source such as Excel into SQL Server will all be “in the box.” With Oracle, on the other hand, you have to purchase more options for every additional feature you want Then there is Sybase It is very much like SQL Server with similar SQL commands and statements (this is the code used to work with the database and its data) although not as feature-rich There is a GUI for Sybase although it is separate from the DBMS installation and a separate product You will find that many Sybase developers use command-line commands or third-party tools Sybase is also mainly found on Unix/Linux although there is a Windows version It is very fast and very robust, and it is rebooted only once, or maybe twice, a year Being hosted on Unix, Linux, or Solaris also helps with the reduction in reboots because you don’t have the monthly Windows patching However, Sybase isn’t as command-and feature-rich as SQL Server SQL Server has a more extensive programming language and functionality that is more powerful than Sybase Each DBMS has its own SQL syntax although they all will have the same basic SQL syntax, known as the ANSI-92 standard This means that the syntax for retrieving data and such is the same from one DBMS to another However, each DBMS has its own special syntax to maintain it—trying to use a feature from this SQL syntax in one DBMS may not work, or may work differently, in another SQL Server for me is the best choice in the DBMS marketplace There are versions of SQL Server that can be installed and be small enough for a handful of users and versions large enough for the largest corporations, and there is even a version for Cloud computing There is also a specific Development Edition, which gives almost the full functionality of the most expensive version, the Enterprise Edition, but for a tiny fraction of the price It has the ability to scale up and deal with terabytes of data without many concerns As you will see, it is easy to install; it comes as one complete package for most of its functionality, with a simple install to be performed for the remaining areas if required Now that you know the reasons behind choosing SQL Server, you need to know which versions of SQL Server are available for purchase, what market each version is aimed at, and which version will be best for you, including which version can run on your machine SQL Server Editions Before buying this book, you will have perhaps found the Microsoft SQL Server home page, taken a look at the different editions of SQL Server, and noticed that there are several choices to make This book focuses on the Development Edition as this is the edition to work with prior to moving into industry There are, however, many developers who will use the Express Edition because it is a free version that is ideal for smaller developments and, www.it-ebooks.info Chapter ■ Overview and Installation where required, the change or non-existence of functionality is detailed The differences between the two editions that this book will cover are the in-memory held data and options concerning backing up and restoring data Other editions, where relevant, may be mentioned in passing The editions you have are as follows: • Enterprise: the most powerful edition of SQL Server often used by corporations that will be running SQL Server on at least one powerful server; fully functional • Developer: the same functionality as Enterprise but not licensed for production • Business Intelligence (BI): tools and feature set orientated to building and browsing business intelligence based data; has a feature set that is reduced or not available where the feature is designed for transactional based data • Standard: reduced functionality and memory allowed to be used is limited to 128GB; ideal for corporations that don’t work with very large data sets due to the reduction useful functionality • Web: ideal for web \-based corporations that work with smaller online-based data sets; also useful for ISPs to offer to clients to build their web sites off • Azure: SQL Server is provided as a service in the cloud where you are supplied with a preinstalled SQL Server instance and you buy compute and memory • Express: ideal for small companies and for those wishing to develop small database applications such as students, charity organizations, and small work-at-home developers This edition is free to license If you are planning to read this book with an Express Edition of SQL Server, either the Express Edition with Tools or Express Edition with Advanced Services is required The Example In order to demonstrate SQL Server fully, we will develop a system for a financial company that will have features such as banking, purchasing shares, and regular buying, including a unit trust savings plan This is an application that could fit into a large organization or, with very minor modifications, could be used by a single person to record their own banking and financial transactions The book builds on this idea and develops the example, demonstrating how to take an idea and formulate it into a design with the correct architecture It should be said, though, that the example will be the bare minimum to make it run, as I don’t want to detract from SQL Server and why you have bought the book ■■Note It would be possible to use the Microsoft standard example that can be downloaded; however, this detracts from many areas that, as a developer, you need to be aware of and be confident dealing with Building an example from the very beginning is the only way to learn SQL Server If you are coming from another database, you may find that you can skip parts of the example building I also use a tool from Red Gate Software to generate some random test data You not need this tool for the book as the random data are available for download from the Apress web site and my own web site But before you can get to this point, you need to install SQL Server www.it-ebooks.info Chapter ■ Overview and Installation Installation This chapter will guide you through the installation process of the Developer Edition, although virtually all that you see will be in every edition Reporting Services is only available within the Advanced Services Edition of the Express Edition, which is not covered within the book Security is also different in the Express and Web Editions, as will be noted later Microsoft offers a 180-day trial version at www.microsoft.com/sql, which you can use to follow along with the examples in this book if you don’t already have SQL Server and are not ready to purchase However, the Developer Edition is very cheap to purchase a license to use compared to other editions, and so, by selecting this version, it is not cost-prohibitive to continue past the trial period This book will cover many of the options and combinations of features that can be included within an installation A number of different tools are supplied with SQL Server to be included with the installation You will look at these tools so that a basic understanding of what they are will allow you to decide which to install Installation covers a great many different areas: • Security issues • Different types of installation—whether this is the first installation and instance of SQL Server or a subsequent instance, for development, test, or production • Custom installations • Installing only some of the products available Most of these options will be covered so that by the end of the chapter, you can feel confident and knowledgeable enough to complete any subsequent installations that suit your needs As mentioned in the Editions section of the chapter, this book uses the Developer Edition because it is most likely the edition you will use as a developer since it doesn’t have all the operating system requirements of the Enterprise Edition I will denote in the installation when a feature is not available in other editions Insert the CD or download and extract SQL Server from the web site for the Microsoft SQL Server edition of your choice or mount an ISO image as a virtual drive on your computer The installation is based on a local computer with only a C: hard drive I will also mention installation options concerning servers and network storage where appropriate Install Ensure that you have logged on to the machine with administrative rights so that you are allowed to create files and folders on your machine, which is obviously required for installation to be successful If you are using a CD-ROM and the installation process does not automatically start, open up Windows Explorer and double-click setup.exe, found at the root level of the CD-ROM If you are not using a CD-ROM, double-click the installer executable that you downloaded This may expand the downloaded single file to a folder with the setup.exe within it ■■Tip  For installation on multiple computers, it is possible to create an automatic build script to ensure all servers are built to the same company standard I not cover this topic, but it is an option to be aware of You are now presented with the installation screen for Microsoft NET Framework if it is not already installed .NET is a framework that Microsoft created that allows programs written in VB NET, C#, and other programming languages to have a common compile set for computers SQL Server uses NET for some of its own internal work, but also, as a developer, you can write NET code in any of Microsoft’s NET languages and include this within SQL Server databases With SQL Server 2008 and later, there is also the ability to query the database using NET and LINQ rather than native T-SQL, although for clarity the code would be converted to T-SQL www.it-ebooks.info Chapter ■ Overview and Installation You are then presented with the SQL Server Installation Center This screen, shown in Figure 1-1, deals with all setup processes, including new installations, upgrades from previous versions of SQL Server, and many other options for maintaining SQL Server installations Figure 1-1.  Beginning the install with the Installation Center When you click Installation on the left-hand menu, the options within the main menu alter and you can now select the New SQL Server stand-alone installation or add features to an existing installation item (at the top of the Installation Center); then SQL Server installation starts The first few screens may be different depending on the edition you are installing The first screen asks for an edition and product key If you have a product key, such as that for the Developers Edition, you would enter it now Whatever your selection you wish to use here, click Next You will then be asked to accept the license terms Then click Next You then come to the system configuration check known as the Global Rules Check, as you see in Figure 1-2 This is my setup support rules screen, but your screen may slightly differ depending on operating systems and service packs It will only be shown if you have action points; otherwise, the SQL Server installation process will continue automatically Its main function is to check that the PC meets the hardware and software requirements, that there are no outstanding reboots, and that you are logged in as an administrator www.it-ebooks.info Contents About the Author��������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewer������������������������������������������������������������������������������������������� xxi Acknowledgments����������������������������������������������������������������������������������������������������������� xxiii Introduction���������������������������������������������������������������������������������������������������������������������� xxv ■■Chapter 1: Overview and Installation��������������������������������������������������������������������������������1 Why Should I Use SQL Server?������������������������������������������������������������������������������������������������������1 SQL Server Editions�����������������������������������������������������������������������������������������������������������������������2 The Example����������������������������������������������������������������������������������������������������������������������������������3 Installation�������������������������������������������������������������������������������������������������������������������������������������4 Install��������������������������������������������������������������������������������������������������������������������������������������������������������������������� Setup Role������������������������������������������������������������������������������������������������������������������������������������������������������������� Choosing the Features to Install���������������������������������������������������������������������������������������������������������������������������� Naming the Instance������������������������������������������������������������������������������������������������������������������������������������������� 11 Service Accounts������������������������������������������������������������������������������������������������������������������������������������������������� 12 Authentication Mode������������������������������������������������������������������������������������������������������������������������������������������� 13 Defining the Data Directories������������������������������������������������������������������������������������������������������������������������������ 14 FILESTREAM Options������������������������������������������������������������������������������������������������������������������������������������������� 15 Creating the Reporting Services Database���������������������������������������������������������������������������������������������������������� 15 Distributed Replay Options���������������������������������������������������������������������������������������������������������������������������������� 16 Security���������������������������������������������������������������������������������������������������������������������������������������18 Services Accounts����������������������������������������������������������������������������������������������������������������������������������������������� 18 Looking at the Authentication Mode�������������������������������������������������������������������������������������������������������������������� 19 The sa Login�������������������������������������������������������������������������������������������������������������������������������������������������������� 23 Summary�������������������������������������������������������������������������������������������������������������������������������������24 vii www.it-ebooks.info ■ Contents ■■Chapter 2: SQL Server Management Studio��������������������������������������������������������������������25 A Quick Overview of SSMS����������������������������������������������������������������������������������������������������������25 Query Editor��������������������������������������������������������������������������������������������������������������������������������38 sqlcmd�����������������������������������������������������������������������������������������������������������������������������������������40 Summary�������������������������������������������������������������������������������������������������������������������������������������41 ■■Chapter 3: Database Design and Creation�����������������������������������������������������������������������43 Defining a Database��������������������������������������������������������������������������������������������������������������������44 Prebuilt Databases Within SQL Server����������������������������������������������������������������������������������������45 master����������������������������������������������������������������������������������������������������������������������������������������������������������������� 46 tempdb���������������������������������������������������������������������������������������������������������������������������������������������������������������� 46 model������������������������������������������������������������������������������������������������������������������������������������������������������������������� 47 msdb�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 48 AdventureWorks/AdventureWorksDW����������������������������������������������������������������������������������������������������������������� 48 Choosing the Database System Type������������������������������������������������������������������������������������������48 OLTP��������������������������������������������������������������������������������������������������������������������������������������������������������������������� 48 BI/OLAP���������������������������������������������������������������������������������������������������������������������������������������������������������������� 49 Example System Choice�������������������������������������������������������������������������������������������������������������������������������������� 50 Gathering the Data����������������������������������������������������������������������������������������������������������������������50 Determining the Information to Store in the Database����������������������������������������������������������������52 Financial Products����������������������������������������������������������������������������������������������������������������������������������������������� 53 Customers����������������������������������������������������������������������������������������������������������������������������������������������������������� 53 Customer Addresses�������������������������������������������������������������������������������������������������������������������������������������������� 53 Shares����������������������������������������������������������������������������������������������������������������������������������������������������������������� 54 Transactions�������������������������������������������������������������������������������������������������������������������������������������������������������� 54 External and Ignored Information������������������������������������������������������������������������������������������������54 Building Relationships�����������������������������������������������������������������������������������������������������������������54 Using Keys����������������������������������������������������������������������������������������������������������������������������������������������������������� 55 Creating Relationships����������������������������������������������������������������������������������������������������������������������������������������� 56 More on Foreign Keys������������������������������������������������������������������������������������������������������������������������������������������ 59 viii www.it-ebooks.info ■ Contents Normalization������������������������������������������������������������������������������������������������������������������������������61 Assign Each Entity a Unique Identifier����������������������������������������������������������������������������������������������������������������� 62 Store Only Information That Directly Relates to That Entity��������������������������������������������������������������������������������� 62 Avoid Repeating Values or Columns�������������������������������������������������������������������������������������������������������������������� 62 Normal Forms������������������������������������������������������������������������������������������������������������������������������������������������������ 62 Denormalization�������������������������������������������������������������������������������������������������������������������������������������������������� 64 Creating the Sample Database����������������������������������������������������������������������������������������������������65 Creating a Database in SQL Server Management Studio������������������������������������������������������������������������������������ 65 Dropping the Database in SQL Server Management Studio�������������������������������������������������������������������������������� 84 Creating a Database in a Query Pane������������������������������������������������������������������������������������������������������������������ 88 Summary�������������������������������������������������������������������������������������������������������������������������������������91 ■■Chapter 4: Security and Compliance�������������������������������������������������������������������������������93 Logins������������������������������������������������������������������������������������������������������������������������������������������93 Server Logins and Database Users�������������������������������������������������������������������������������������������106 Roles�����������������������������������������������������������������������������������������������������������������������������������������106 Fixed Server Roles��������������������������������������������������������������������������������������������������������������������������������������������� 106 Database Roles�������������������������������������������������������������������������������������������������������������������������������������������������� 107 Application Roles����������������������������������������������������������������������������������������������������������������������������������������������� 108 Schemas�����������������������������������������������������������������������������������������������������������������������������������111 Before You Can Proceed with Your Solution������������������������������������������������������������������������������113 Declarative Management Framework���������������������������������������������������������������������������������������117 Encryption���������������������������������������������������������������������������������������������������������������������������������124 Summary�����������������������������������������������������������������������������������������������������������������������������������125 ■■Chapter 5: Defining Tables���������������������������������������������������������������������������������������������127 What Is a Table?������������������������������������������������������������������������������������������������������������������������127 SQL Server Data Types��������������������������������������������������������������������������������������������������������������129 Table Data Types������������������������������������������������������������������������������������������������������������������������������������������������ 130 Program Data Types������������������������������������������������������������������������������������������������������������������������������������������� 135 ix www.it-ebooks.info ■ Contents Columns As More Than Simple Data Repositories��������������������������������������������������������������������135 Default Values���������������������������������������������������������������������������������������������������������������������������������������������������� 135 Generating IDENTITY Values������������������������������������������������������������������������������������������������������������������������������ 136 Uniqueidentifiers for an Identity������������������������������������������������������������������������������������������������������������������������ 136 Sequences Instead of IDENTITY������������������������������������������������������������������������������������������������137 The Use of NULL Values������������������������������������������������������������������������������������������������������������������������������������� 137 Creating a Table in SQL Server Management Studio�����������������������������������������������������������������138 Creating a Table Through Query Editor��������������������������������������������������������������������������������������147 Creating a Table: Using SQLCMD�����������������������������������������������������������������������������������������������149 The ALTER TABLE Statement�����������������������������������������������������������������������������������������������������151 Defining the Remaining Tables��������������������������������������������������������������������������������������������������153 Setting a Primary Key����������������������������������������������������������������������������������������������������������������154 Creating a Relationship�������������������������������������������������������������������������������������������������������������155 Check Existing Data on Creation����������������������������������������������������������������������������������������������������������������������� 161 Enforce Foreign Key Constraints����������������������������������������������������������������������������������������������������������������������� 161 Choosing Delete and Update Rules�������������������������������������������������������������������������������������������������������������������� 161 Building a Relationship via T-SQL����������������������������������������������������������������������������������������������162 Relationships: Database or Application�������������������������������������������������������������������������������������164 Partitioning��������������������������������������������������������������������������������������������������������������������������������164 Summary�����������������������������������������������������������������������������������������������������������������������������������166 ■■Chapter 6: Creating Indexes and Database Diagramming���������������������������������������������167 What Is an Index?����������������������������������������������������������������������������������������������������������������������167 Types of Indexes������������������������������������������������������������������������������������������������������������������������������������������������ 168 Uniqueness�������������������������������������������������������������������������������������������������������������������������������������������������������� 170 Determining What Makes a Good Index������������������������������������������������������������������������������������170 Using Low-Maintenance Columns��������������������������������������������������������������������������������������������������������������������� 171 Using Primary and Foreign Keys����������������������������������������������������������������������������������������������������������������������� 171 Finding Specific Records����������������������������������������������������������������������������������������������������������������������������������� 172 Using Covering Indexes������������������������������������������������������������������������������������������������������������������������������������� 172 x www.it-ebooks.info ■ Contents Looking for a Range of Information������������������������������������������������������������������������������������������������������������������� 172 Keeping the Data in Order��������������������������������������������������������������������������������������������������������������������������������� 172 Determining What Makes a Bad Index��������������������������������������������������������������������������������������173 Using Unsuitable Columns��������������������������������������������������������������������������������������������������������������������������������� 173 Choosing Unsuitable Data���������������������������������������������������������������������������������������������������������������������������������� 173 Including Too Many Columns����������������������������������������������������������������������������������������������������������������������������� 174 When There Are Too Few Records in the Table�������������������������������������������������������������������������������������������������� 174 Reviewing Your Indexes for Performance���������������������������������������������������������������������������������174 Creating an Index����������������������������������������������������������������������������������������������������������������������175 Creating an Index with the Table Designer�������������������������������������������������������������������������������������������������������� 175 Indexes and Statistics��������������������������������������������������������������������������������������������������������������������������������������� 179 The CREATE INDEX Syntax��������������������������������������������������������������������������������������������������������������������������������� 180 Creating an Index in Query Editor���������������������������������������������������������������������������������������������������������������������� 182 Dropping an Index���������������������������������������������������������������������������������������������������������������������185 Altering an Index�����������������������������������������������������������������������������������������������������������������������186 Included Columns����������������������������������������������������������������������������������������������������������������������187 When an Index Does Not Exist���������������������������������������������������������������������������������������������������188 Diagramming the Database�������������������������������������������������������������������������������������������������������189 Database Diagramming Basics�������������������������������������������������������������������������������������������������������������������������� 189 The SQL Server Database Diagram Designer���������������������������������������������������������������������������������������������������� 190 The Default Database Diagram�������������������������������������������������������������������������������������������������������������������������� 190 The Database Diagram Toolbar�������������������������������������������������������������������������������������������������������������������������� 193 Modifying Your Database Using Database Diagram Designer��������������������������������������������������������������������������� 195 Summary�����������������������������������������������������������������������������������������������������������������������������������196 ■■Chapter 7: In-Memory Tables����������������������������������������������������������������������������������������197 Cost-Based Processing�������������������������������������������������������������������������������������������������������������197 What Is an In-Memory Table?����������������������������������������������������������������������������������������������������199 Differences to Disk-Based Tables���������������������������������������������������������������������������������������������������������������������� 199 xi www.it-ebooks.info ■ Contents In-Memory Indexes and the Hash Index������������������������������������������������������������������������������������200 Hash Buckets����������������������������������������������������������������������������������������������������������������������������������������������������� 201 General Index Points������������������������������������������������������������������������������������������������������������������������������������������ 202 Operating System and Hardware Requirements�����������������������������������������������������������������������202 Database Requirements Prior to Table Creation������������������������������������������������������������������������202 Creating an In-memory Table Using SQL�����������������������������������������������������������������������������������203 Migrating to an In-Memory Table����������������������������������������������������������������������������������������������204 Summary�����������������������������������������������������������������������������������������������������������������������������������210 ■■Chapter 8: Database Backups and Recovery�����������������������������������������������������������������211 Backup Strategies���������������������������������������������������������������������������������������������������������������������212 Recovery Models����������������������������������������������������������������������������������������������������������������������������������������������� 212 Types of Backups����������������������������������������������������������������������������������������������������������������������������������������������� 212 Backup Strategy Decisions�������������������������������������������������������������������������������������������������������������������������������� 213 Transaction Logs�����������������������������������������������������������������������������������������������������������������������214 When Problems May Occur�������������������������������������������������������������������������������������������������������215 In-Memory Table Backup and Restore��������������������������������������������������������������������������������������216 Backups: Taking a Database Offline������������������������������������������������������������������������������������������216 Backing Up the Data������������������������������������������������������������������������������������������������������������������218 Backup Considerations�������������������������������������������������������������������������������������������������������������������������������������� 218 Performing a Manual SQL Server Backup��������������������������������������������������������������������������������������������������������� 219 Backing Up the Database Using T-SQL�������������������������������������������������������������������������������������������������������������� 224 Transaction Log Backup Using T-SQL���������������������������������������������������������������������������������������������������������������� 232 Restoring a Database����������������������������������������������������������������������������������������������������������������235 Restoring Using SQL Server Management Studio��������������������������������������������������������������������������������������������� 236 Restoring Using T-SQL��������������������������������������������������������������������������������������������������������������������������������������� 241 Restoring to a Point in Time������������������������������������������������������������������������������������������������������������������������������� 244 xii www.it-ebooks.info ■ Contents Detaching and Attaching a Database����������������������������������������������������������������������������������������249 Detaching and Attaching Using SQL Server Management Studio���������������������������������������������������������������������� 250 Detaching and Attaching Using T-SQL��������������������������������������������������������������������������������������������������������������� 254 Producing SQL Script for the Database�������������������������������������������������������������������������������������258 Summary�����������������������������������������������������������������������������������������������������������������������������������263 ■■Chapter 9: Database Maintenance���������������������������������������������������������������������������������265 Creating a Database Maintenance Plan������������������������������������������������������������������������������������265 Executing the Plan���������������������������������������������������������������������������������������������������������������������283 Setting Up Database Mail����������������������������������������������������������������������������������������������������������288 Modifying a Maintenance Plan��������������������������������������������������������������������������������������������������298 Summary�����������������������������������������������������������������������������������������������������������������������������������301 ■■Chapter 10: Data Insertion, Deletion, and Transactions—Disk-Based��������������������������303 Inserting Data����������������������������������������������������������������������������������������������������������������������������304 The T-SQL INSERT Statement Syntax���������������������������������������������������������������������������������������������������������������� 304 INSERT SQL Statement�������������������������������������������������������������������������������������������������������������������������������������� 305 DBCC CHECKIDENT�������������������������������������������������������������������������������������������������������������������������������������������� 315 Column Constraints������������������������������������������������������������������������������������������������������������������������������������������� 316 Inserting Several Records at Once�������������������������������������������������������������������������������������������������������������������� 323 Transactions������������������������������������������������������������������������������������������������������������������������������324 Transaction Basics��������������������������������������������������������������������������������������������������������������������������������������������� 325 Transaction Commands������������������������������������������������������������������������������������������������������������������������������������� 325 Locking Data������������������������������������������������������������������������������������������������������������������������������������������������������ 326 Deleting Data�����������������������������������������������������������������������������������������������������������������������������327 DELETE Syntax��������������������������������������������������������������������������������������������������������������������������������������������������� 327 Before Demonstrating the DELETE Statement��������������������������������������������������������������������������������������������������� 328 Using the DELETE Statement����������������������������������������������������������������������������������������������������������������������������� 328 Truncating a Table���������������������������������������������������������������������������������������������������������������������������������������������� 333 Dropping a Table������������������������������������������������������������������������������������������������������������������������������������������������ 334 Summary�����������������������������������������������������������������������������������������������������������������������������������334 xiii www.it-ebooks.info ■ Contents ■■Chapter 11: Selecting and Updating Data from Disk-Based Tables�������������������������������335 Retrieving Data��������������������������������������������������������������������������������������������������������������������������336 Using SQL Server Management Studio to Retrieve Data����������������������������������������������������������������������������������� 336 Using the SELECT Statement to Retrieve Data�������������������������������������������������������������������������������������������������� 338 Limiting a Search: The Use of WHERE���������������������������������������������������������������������������������������345 TOP (n)��������������������������������������������������������������������������������������������������������������������������������������������������������������� 348 TOP (n) PERCENT����������������������������������������������������������������������������������������������������������������������������������������������� 349 SET ROWCOUNT n���������������������������������������������������������������������������������������������������������������������������������������������� 350 String Functions������������������������������������������������������������������������������������������������������������������������352 Order! Order!�����������������������������������������������������������������������������������������������������������������������������354 The LIKE Operator����������������������������������������������������������������������������������������������������������������������357 Creating Data: SELECT INTO������������������������������������������������������������������������������������������������������360 Updating Data����������������������������������������������������������������������������������������������������������������������������361 The UPDATE Statement�������������������������������������������������������������������������������������������������������������������������������������� 362 Updating Data within Query Editor�������������������������������������������������������������������������������������������������������������������� 363 Updating Data: Using Transactions�������������������������������������������������������������������������������������������������������������������� 367 Nested Transactions������������������������������������������������������������������������������������������������������������������������������������������ 368 Using More Than One Table�������������������������������������������������������������������������������������������������������370 Summary�����������������������������������������������������������������������������������������������������������������������������������377 ■■Chapter 12: Working with In-Memory Tables����������������������������������������������������������������379 INSERT, UPDATE, DELETE, and SELECT Statements�������������������������������������������������������������������379 Locking and Isolation Levels�����������������������������������������������������������������������������������������������������380 MERGE Statement���������������������������������������������������������������������������������������������������������������������390 In-Memory Table Unsupported Functionality�����������������������������������������������������������������������������393 Summary�����������������������������������������������������������������������������������������������������������������������������������393 ■■Chapter 13: Building a View������������������������������������������������������������������������������������������395 Why a View?������������������������������������������������������������������������������������������������������������������������������396 Using Views for Security������������������������������������������������������������������������������������������������������������������������������������ 396 Encrypting View Definitions������������������������������������������������������������������������������������������������������������������������������� 397 xiv www.it-ebooks.info ■ Contents Views and In-memory Tables����������������������������������������������������������������������������������������������������397 Creating a View: SQL Server Management Studio��������������������������������������������������������������������398 Creating a View by Using a View�����������������������������������������������������������������������������������������������406 Creating a View Using T-SQL Syntax�����������������������������������������������������������������������������������������410 The CREATE VIEW Syntax����������������������������������������������������������������������������������������������������������������������������������� 410 Creating a View with CREATE VIEW and SELECT����������������������������������������������������������������������������������������������� 412 Binding Columns in a View with SCHEMABINDING�������������������������������������������������������������������������������������������� 414 Indexing a View�������������������������������������������������������������������������������������������������������������������������416 Summary�����������������������������������������������������������������������������������������������������������������������������������418 ■■Chapter 14: Stored Procedures, Functions, and Security����������������������������������������������421 What Is a Stored Procedure?�����������������������������������������������������������������������������������������������������422 Creating a Stored Procedure�����������������������������������������������������������������������������������������������������423 CREATE PROCEDURE Syntax������������������������������������������������������������������������������������������������������������������������������ 423 Returning a Set of Rows������������������������������������������������������������������������������������������������������������������������������������ 425 Creating a Stored Procedure: Management Studio������������������������������������������������������������������������������������������� 425 Different Methods of Execution������������������������������������������������������������������������������������������������������������������������� 431 Returning Results����������������������������������������������������������������������������������������������������������������������431 Using RETURN���������������������������������������������������������������������������������������������������������������������������������������������������� 431 Returning Multiple Result Sets�������������������������������������������������������������������������������������������������������������������������� 436 Controlling the Flow������������������������������������������������������������������������������������������������������������������437 IF . . . ELSE����������������������������������������������������������������������������������������������������������������������������������������������������������� 437 BEGIN . . . END����������������������������������������������������������������������������������������������������������������������������������������������������� 438 WHILE . . . BREAK Statement������������������������������������������������������������������������������������������������������������������������������� 439 CASE Statement������������������������������������������������������������������������������������������������������������������������������������������������ 441 Bringing It All Together��������������������������������������������������������������������������������������������������������������443 Security�������������������������������������������������������������������������������������������������������������������������������������445 Security Gains���������������������������������������������������������������������������������������������������������������������������������������������������� 445 Granting and Revoking Permissions������������������������������������������������������������������������������������������������������������������ 448 xv www.it-ebooks.info ■ Contents User-Defined Functions�������������������������������������������������������������������������������������������������������������457 Scalar Functions������������������������������������������������������������������������������������������������������������������������������������������������ 458 Table-Valued Functions������������������������������������������������������������������������������������������������������������������������������������� 458 Considerations When Building Functions���������������������������������������������������������������������������������������������������������� 459 Summary�����������������������������������������������������������������������������������������������������������������������������������462 ■■Chapter 15: Natively Compiled Stored Procedures��������������������������������������������������������463 What Is a Natively Compiled Stored Procedure?�����������������������������������������������������������������������463 CREATE PROCEDURE and Syntax�����������������������������������������������������������������������������������������������464 Summary�����������������������������������������������������������������������������������������������������������������������������������468 ■■Chapter 16: Essentials for Effective Coding������������������������������������������������������������������469 Variables������������������������������������������������������������������������������������������������������������������������������������469 Temporary Tables����������������������������������������������������������������������������������������������������������������������472 Aggregations�����������������������������������������������������������������������������������������������������������������������������474 COUNT/COUNT_BIG�������������������������������������������������������������������������������������������������������������������������������������������� 474 SUM������������������������������������������������������������������������������������������������������������������������������������������������������������������� 476 MAX/MIN������������������������������������������������������������������������������������������������������������������������������������������������������������ 476 AVG�������������������������������������������������������������������������������������������������������������������������������������������������������������������� 477 Grouping Data���������������������������������������������������������������������������������������������������������������������������478 HAVING��������������������������������������������������������������������������������������������������������������������������������������480 Distinct Values���������������������������������������������������������������������������������������������������������������������������481 Functions�����������������������������������������������������������������������������������������������������������������������������������482 Date and Time���������������������������������������������������������������������������������������������������������������������������������������������������� 483 String����������������������������������������������������������������������������������������������������������������������������������������������������������������� 492 System Functions���������������������������������������������������������������������������������������������������������������������������������������������� 499 RAISERROR��������������������������������������������������������������������������������������������������������������������������������509 Error Handling���������������������������������������������������������������������������������������������������������������������������512 @@ERROR��������������������������������������������������������������������������������������������������������������������������������������������������������� 512 TRY CATCH and THROW������������������������������������������������������������������������������������������������������������������������������������ 514 Summary�����������������������������������������������������������������������������������������������������������������������������������520 xvi www.it-ebooks.info ■ Contents ■■Chapter 17: Advanced T-SQL and Debugging����������������������������������������������������������������521 Creating a SEQUENCE����������������������������������������������������������������������������������������������������������������521 Subqueries��������������������������������������������������������������������������������������������������������������������������������526 Using Subqueries����������������������������������������������������������������������������������������������������������������������������������������������� 526 IN����������������������������������������������������������������������������������������������������������������������������������������������������������������������� 528 EXISTS��������������������������������������������������������������������������������������������������������������������������������������������������������������� 529 Tidying Up the Loose End���������������������������������������������������������������������������������������������������������������������������������� 530 The APPLY Operator�������������������������������������������������������������������������������������������������������������������531 CROSS APPLY����������������������������������������������������������������������������������������������������������������������������������������������������� 532 OUTER APPLY����������������������������������������������������������������������������������������������������������������������������������������������������� 533 Common Table Expressions�������������������������������������������������������������������������������������������������������534 Using Common Table Expressions��������������������������������������������������������������������������������������������������������������������� 534 Recursive CTE���������������������������������������������������������������������������������������������������������������������������������������������������� 536 Pivoting Data�����������������������������������������������������������������������������������������������������������������������������538 PIVOT����������������������������������������������������������������������������������������������������������������������������������������������������������������� 538 UNPIVOT������������������������������������������������������������������������������������������������������������������������������������������������������������� 539 Table Variables��������������������������������������������������������������������������������������������������������������������������541 Ranking Functions���������������������������������������������������������������������������������������������������������������������543 ROW_NUMBER��������������������������������������������������������������������������������������������������������������������������������������������������� 544 RANK������������������������������������������������������������������������������������������������������������������������������������������������������������������ 546 DENSE_RANK����������������������������������������������������������������������������������������������������������������������������������������������������� 547 NTILE����������������������������������������������������������������������������������������������������������������������������������������������������������������� 548 Cursors��������������������������������������������������������������������������������������������������������������������������������������549 Debugging Your Code����������������������������������������������������������������������������������������������������������������556 Debugging Windows������������������������������������������������������������������������������������������������������������������������������������������ 557 Debugging Options�������������������������������������������������������������������������������������������������������������������������������������������� 558 Debugging Pop-Up Window������������������������������������������������������������������������������������������������������������������������������� 559 Summary�����������������������������������������������������������������������������������������������������������������������������������566 xvii www.it-ebooks.info ■ Contents ■■Chapter 18: Triggers������������������������������������������������������������������������������������������������������567 What Is a Trigger?����������������������������������������������������������������������������������������������������������������������567 The DML Trigger������������������������������������������������������������������������������������������������������������������������568 CREATE TRIGGER Syntax for DML Triggers�������������������������������������������������������������������������������������������������������� 569 Why Not Use a Constraint?�������������������������������������������������������������������������������������������������������������������������������� 570 Deleted and Inserted Logical Tables������������������������������������������������������������������������������������������������������������������ 571 Creating a DML FOR/AFTER Trigger������������������������������������������������������������������������������������������������������������������� 572 Checking Specific Columns������������������������������������������������������������������������������������������������������������������������������� 577 DDL Triggers������������������������������������������������������������������������������������������������������������������������������585 DDL Database-Level Events������������������������������������������������������������������������������������������������������������������������������� 586 Dropping a DDL Trigger�������������������������������������������������������������������������������������������������������������������������������������� 587 EVENTDATA( )����������������������������������������������������������������������������������������������������������������������������������������������������� 588 Summary�����������������������������������������������������������������������������������������������������������������������������������591 ■■Chapter 19: Connecting via Code����������������������������������������������������������������������������������593 General Application Security�����������������������������������������������������������������������������������������������������594 Connecting with Excel or Excel Visual Basic for Applications (VBA)������������������������������������������595 Using Excel with the Data Ribbon���������������������������������������������������������������������������������������������������������������������� 595 Using Excel with VBA����������������������������������������������������������������������������������������������������������������������������������������� 606 Visual Studio�����������������������������������������������������������������������������������������������������������������������������612 Terminology������������������������������������������������������������������������������������������������������������������������������������������������������� 614 VB.NET��������������������������������������������������������������������������������������������������������������������������������������������������������������� 614 C#���������������������������������������������������������������������������������������������������������������������������������������������������������������������� 622 Java�������������������������������������������������������������������������������������������������������������������������������������������629 Summary�����������������������������������������������������������������������������������������������������������������������������������640 Index���������������������������������������������������������������������������������������������������������������������������������641 xviii www.it-ebooks.info About the Author Robin Dewson has been hooked on programming ever since he bought his first computer in 1980, a Sinclair ZX80 His first major application of his own was a Visual FoxPro program that could be used to run a Fantasy League system It was at this point he met up with a great help in his PC development life, Jon Silver at Step One Technologies, where in return for training he helped Jon with some other Visual FoxPro applications From there, realizing that the marketplace for Visual FoxPro was limited, he decided to learn Visual Basic and SQL Server Starting out with SQL Server 6.5, Robin soon moved to SQL Server and Visual Basic 5, and became involved in developing several applications for clients both in the UK and in the United States From there, he has moved through each version of SQL Server through to the current one Robin now specializes in databases and C# in both client and server side coding Robin can be contacted at robin@fat-belly.com or at www.fat-belly.com xix www.it-ebooks.info About the Technical Reviewer Rodney Landrum went to school to be a poet and a writer And then he graduated, so that dream was crushed He followed another path, which was to become a professional in the fun filled world of Information Technology He has worked as a systems engineer, UNIX and network admin, data analyst, client services director, and finally as a database administrator The old hankering to put words on paper, while paper still existed, got the best of him, and in 2000, he began writing technical articles, some creative and humorous, some quite the opposite In 2010 he wrote The SQL Server Tacklebox, a title his editor disdained, but a book closest to the true creative potential he sought; he wanted to a full book without a single screen shot He promises his next book will be fiction or a collection of poetry, but that has yet to transpire xxi www.it-ebooks.info Acknowledgments In every book there are so many people to thank either for helping with the book, or helping me in my role: Here, much like an Oscar-winning speech (without the tears), are my expressions of thanks to all those who helped: To Andrew and all at Host-It.co.uk, my ISP who give excellent support with the websites I run Some great friends including Jane Harper and Mark Smith, Mark and Trudy Mather and the Harper family for making rugby on a Saturday so enjoyable Many work colleagues including Anthony “Jock” Jawad, Aubrey Lomas, Harry Toland, Jason Oates, Steve Nicklin, James McVey, Andy Bennett, Richard Barclay, Ian Phillips, Guy Batey, Darren Skinner, the great Noel Morgala, Syed Yasir Shah, Yao Wu, and the rest of the team at Nomura; and Ian Howell and Nick Sharp for some great evenings out, Robert McMillan (Toad), a great mate from college with whom I got back in touch after many years—each in their own way have helped me over the months and years Also thanks to Sams Species Reptile Sanctuary samsspecies.yolasite.com, especially to Sally Shapter for being a brilliant person with reptiles and their rescue and then safe keeping, Northampton Reptile Centre, especially Sam Shapter, Lois, and Ant Hook and Gary Rolfe for their time with my dragons and chameleon Also thanks to “Guinness” John Mason and Richard Lowther for their help, input, and time with the www.rolling-maul.com and www.hospital-pass.com websites Of course all the people at Apress and my long time friend Jonathan Gennick, and the others who have helped like Jill Balzano, a great person to keep me motivated and say the right things And the book would be nothing without the brilliantly talented technical editor Rodney Landrum Then, of course, there are my mum and late dad who put up with not watching television while I commandeered it for my personal computer in 1980 and onward I owe them so much and love them to bits Without them I wouldn’t be doing what I am doing today and I might be stuck in a job I didn’t enjoy as much as I this —Robin Dewson xxiii www.it-ebooks.info ... Beginning SQL Server for Developers This book has been written for those who are interested in learning how to create solutions with Microsoft SQL Server, but have no prior knowledge of SQL Server. .. SQL Server This edition of my book is for readers who wish to learn and develop on either the free version of SQL Server, SQL Server Express, or with either the trial or licensed versions of SQL. .. of SQL Server are available for purchase, what market each version is aimed at, and which version will be best for you, including which version can run on your machine SQL Server Editions Before

Ngày đăng: 13/03/2019, 10:34

Từ khóa liên quan

Mục lục

  • Contents at a Glance

  • Contents

  • About the Author

  • About the Technical Reviewer

  • Acknowledgments

  • Introduction

  • Chapter 1: Overview and Installation

    • Why Should I Use SQL Server?

    • SQL Server Editions

    • The Example

    • Installation

      • Install

      • Setup Role

      • Choosing the Features to Install

      • Naming the Instance

      • Service Accounts

      • Authentication Mode

      • Defining the Data Directories

      • FILESTREAM Options

      • Creating the Reporting Services Database

      • Distributed Replay Options

      • Security

        • Services Accounts

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan