Bài giảng Microsoft SQL Server 2012
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Microsoft SQL Server 2012", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Tài liệu đính kèm:
- bai_giang_microsoft_sql_server_2012.pdf
Nội dung text: Bài giảng Microsoft SQL Server 2012
- Microsoft prePress
- Published with the authorization of Microsoft Corporation by: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, California 95472 With Microsoft prePress, you can access just-written content from upcoming books. The chapters come straight from our respected authors, before they’re fully polished and debugged—for critical insights now, when you need them. This document contains one or more portions of a preliminary version of a Microsoft Press title and is provided “as is.” The content may be changed substantially upon final publication. In addition, this document may make reference to pre-released versions of software products that may be changed substantially prior to final commercial release. Microsoft reserves the right to not publish this title or any versions thereof (including future prePress ebooks). This document is provided for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EITHER EXPRESS OR IMPLIED, IN THIS DOCUMENT. Information and views expressed in this document, including URL and other Internet website references may be subject to change without notice. You bear the risk of using it. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Some examples are for illustration only and are fictitious. No real association is intended or inferred. This document does not provide you with any legal rights to any intellectual property in any Microsoft product, service, or other offering. © 2013 Patrick LeBlanc. All rights reserved. Microsoft and the trademarks listed at are trademarks of the Microsoft group of companies. All other marks are property of their respective owners.
- Contents at a glance Part I Getting started with Microsoft SQL Server 2012 Chapter 1 Overview of Microsoft SQL Server 2012 Chapter 2 Installing, configuring, and upgrading Microsoft SQL Server 2012 Chapter 3 Using SQL Server 2012 administration and development tools Part II Designing databases Chapter 4 Designing SQL Server databases Chapter 5 Creating your first table Chapter 6 Building and maintaining indexes Part III Advanced database design topics Chapter 7 Table compression Chapter 8 Table partitioning Chapter 9 Database snapshots Chapter 10 The SELECT statement Part IV Using Transact-SQL (TSQL) Chapter 11 Advanced data retrieval topics Chapter 12 Modifying data Chapter 13 Built-in scalar functions Part V Creating other database objects Chapter 14 Advanced TSQL topics Chapter 15 Views Chapter 16 User-defined functions Chapter 17 Stored procedures Chapter 18 Data manipulation triggers
- Part VI SQL Server replication Chapter 19 Replication Part VII Database maintenance Chapter 20 Backups Chapter 21 Managing and maintaining indexes and statistics Chapter 22 Maintenance plans Part VIII Database management Chapter 23 SQL Server Profiler Chapter 24 Extended events Chapter 25 SQL Server security Chapter 26 Resource Governor Chapter 27 SQL Server Agent Chapter 28 Database mail Chapter 29 Data definition triggers Chapter 30 Dynamic management objects Part IX High-availability solutions Chapter 31 AlwaysOn Chapter 32 Log shipping Note: Chapters included in this file are indicated in black.
- PART I Getting started with Microsoft SQL Server 2012 CHAPTER 1 Sections, body text, and emphasis tags . . . . . . . 3 CHAPTER 2 Code elements . . . . . . . . . . . . . . . . . 7 CHAPTER 3 Using SQL Server 2012 administration and development tools . . . . . . . . . . . . . . . . . . . . . . 21 1
- CHAPTER 1 Overview of Microsoft SQL Server 2012 After completing this chapter, you will be able to ■■ Explain SQL Server components and features and their uses . ■■ Identify SQL Server features vital to your environment . ■■ Define and scope your SQL Server installation topology from a high level. The process of learning a new technology can be daunting and sometimes involves a tremendous amount of time and effort. Each step of the process, from installing and configuring the software to deploying the first project, introduces new challenges. These challenges often grow when the tech- nology includes several components and features, so the first step, especially with a multicomponent technology, is to identify the components your environment requires and gain a good understanding of the functionality of each component . To that end, in this chapter, you will examine the components and features of Microsoft SQL Server 2012 and determine how they fit into your installation. Like most relational database management systems (RDBMSs), SQL Server 2012 includes several components . The product itself, however, is often divided into two distinct categories: business intel- ligence (BI) and the Database Engine . Business Intelligence Business intelligence (BI) refers to data transformed into knowledge that can then be used to make more informed business decisions . For example, a company whose primary purpose is to sell bikes could use its data to identify sales trends and the purchasing patterns of its customers . From that analysis, the company could decide to focus sales efforts on a particular area or region, which in turn could lead to better opportunities and offer the company competitive advantages in its industry . While the BI features of SQL Server 2012 can add highly visible and effective value to business users and data consumers, in this book you'll focus primarily on the features specific to the Database Engine . 3
- Database Engine The Database Engine sits at the core of the SQL Server components . The engine operates as a service on a machine, which is often referred to as an instance of SQL Server . You can run multiple instances of SQL Server on a given server . When you connect to SQL Server, the instance is the target of the connection . Once an application is connected, it sends Transact-SQL (T-SQL) statements to the in- stance . The instance in return sends data back to the client . Within the connection is a security layer that validates access to the data as specified by the database administrators (DBAs). The Database Engine enables you to leverage the full capabilities of all of the other components, such as accessing, storing, and securing the data . The storage component of the Database Engine determines how the data is stored on disk . When designing your databases, you will specify various aspects that will dictate how your tables, indexes, and, in some cases, views are physically organized on your disk subsystem . You will examine the con- cepts of tables, indexes, and views in detail in later chapters . In SQL Server 2012, you can physically distribute data across disks by partitioning it, or dividing the data into distinct, independent parts . Partitioning not only improves query performance, but it also simplifies the process of managing and maintaining your data . With the release of SQL Server 2012, Microsoft increased the number of sup- ported partitions to 15,000 per table . Within the Database Engine itself, the storage engine is the primary component . Surrounding it are several additional components that depend on the engine . These components include the following: ■■ T-SQL programming interface (Microsoft's implementations of the SQL ANSI standard language) ■■ Security subsystem ■■ Replication ■■ SQL Server Agent ■■ High availability and disaster recovery tools ■■ SQL Server Integration Services ■■ SQL Server Management tools The following sections provide a brief explanation of each component . T-SQL Programming Interface What is the value in storing data if you cannot access it? SQL Server provides a rich programming lan- guage that allows you to write simple and complex queries against the underlying storage structures . Using T-SQL, you can write data manipulation queries that enable you to modify and access the data on demand. You can create objects such as views, stored procedures, triggers, and user-defined func- tions that act as a means of surfacing that data. Applications written in programming languages such 4 Part 1 Getting Started with Microsoft SQL Server 2012
- as Visual Basic and C# .NET can send T-SQL queries from applications to the Database Engine. The Database Engine will then resolve the queries and send the results back to the client . In addition, you can write data definition queries to create and modify objects that act as mecha- nisms for surfacing the data. T-SQL also allows you to manage server configurations and security seamlessly . T-SQL is a set-based language, meaning that it performs optimally when interacting with data in sets as opposed to manipulating strings or iterating over rows of data . While T-SQL is capable of these cursor-based operations, these types of operations are less efficient than a properly designed set-based approach. If you find you are using T-SQL to perform cursor-based operations, consider leveraging a common language runtime (CLR) language . Using your favorite compiler (Visual Studio, for example), you can extend the functionality of T-SQL . SQL Server 2012 introduces several new T-SQL programming enhancements, including a simpler form of paging, windowing functions, and error handling. A THROW statement is introduced that provides a way to elegantly handle errors by raising exceptions . You can now create a FileTable that builds on the FileStream technology introduced in SQL Server 2008 . Coupling the FileTable with Full- TextSearch allows you to run complicated queries against massive amounts of text data (such as the complete text of this book) . SQL Server 2012 also introduces several new conversion, string, logical, data, and time functions . Security Subsystem In most organizations, data is the most valuable asset, and keeping that data secure is a major con- cern. Any vulnerability in an organization's security might end up triggering a series of events that could prove catastrophic to the business . This is why SQL Server 2012 consists of a robust security subsystem that allows you to control access via two modes of authentication, SQL and Windows. As an administrator, you are able to configure SQL Server security at multiple levels. UsingT-SQL or SQL Server Management Studio, you can control access to a particular instance of SQL Server, to specific databases, to objects within those databases, and even to columns within a particular table. SQL Server also includes native encryption . For example, if you want to secure employees' So- cial Security numbers, using column level encryption, you could encrypt a single column in a table . SQL Server also includes Transparent Data Encryption (TDE), which allows you to encrypt an entire database without affecting how clients and applications access the data . However, if someone were to breach your network security and obtain a copy of a data file or backup file, the only way that person could access the data is with an encryption key that you set and store . Even with all of these security capabilities, SQL Server provides you with the ability to audit your server and databases proactively. In SQL Server 2012, you can filter audit events before they are writ- ten to the audit log . Chapter 26, “Security,” describes how to plan and deploy your SQL Server security strategy. You will learn specific concepts around creating logins and users, and you will examine how to create a security approach and maintain security accounts . Also in SQL Server 2012, you can create user-defined server roles, which can assist in providing a more secure method of allocating server-level access to server administrators . Microsoft has included Chapter 1 Overview of Microsoft SQL Server 2012 5
- the ability to create users within a database without requiring you to create a server login, known as contained databases . In past versions of SQL Server, prior to granting access at the database level, an administrator was required to create a server login . With the advent of SQL Server 2012, a user can be self-contained within a database . Replication SQL Server replication has been available in most releases of the product . Over time, replication types were introduced to ensure that users could configure replication architectures that satisfied a wide range of scenarios . Using SQL Server replication technology, you can distribute data locally, to differ- ent locations, using File Transfer Protocol (FTP), over the Internet, and to mobile users . Replication can be configured to push data, pull data, and merge data across local area networks (LANs) and wide area networks (WANs). The simplest form of replication, snapshot replication, periodically takes a snapshot of the data and distributes it to servers that are subscribed to the publication . Snapshot replication is typically used to move data at longer intervals, such as daily or nightly. While this method is effective, it is often insuffi- cient in satisfying the high demands of users for near real-time data . If higher throughput is required, users often leverage transactional replication . Instead of distributing snapshots of data, transactional replication continuously sends data changes as they happen to the subscribers . Transactional replica- tion is typically used in a server-to-server topology where one server is the source of the data and the other server is used as a backup copy or for reporting . Both replication types are one-way data movements . But what if you need bidirectional move- ment? For example, assume you have mobile users who work offline. While they are offline, they enter information into a database residing on an instance of SQL Server running on their laptops . What happens when they return to the office and connect to the network? In this scenario, the local in- stance will synchronize with the company’s primary SQL Server database . Merge replication will move transactions between the publisher and subscriber since the last time synchronization occurred . SQL Server professionals debate the use of replication as a high availability (HA) or disaster re- covery (DR) technology . Could it be used for either? There is a possibility; however, replication only moves schema changes and data. To provide an effective HA or DR topology, every aspect of the instance should be included such as security, maintenance, jobs, and so on. Therefore, using replica- tion in either case could pose potential problems in the event of hardware failure or a disaster . See Also Chapter 19, "Replication," discusses the replication in depth. SQL Server Agent SQL Server Agent runs as a separate service on an instance of SQL Server. Each instance of SQL Server has an accompanying SQL Agent service. The primary use of SQL Server Agent is to execute sched- uled tasks, such as rebuilding indexes, backing up databases, loading the data warehouse, and so on . It allows you to schedule the jobs to run at various intervals throughout the day or night. 6 Part 1 Getting Started with Microsoft SQL Server 2012
- To ensure that you are notified in the event of a job failure, SQL Server Agent allows you to config- ure operators and alerts. An operator is simply an individual and an email address. Once you config- ure an operator, you can send notifications or alerts to that person when a job succeeds, completes, or fails . High Availability and Disaster Recovery Tools With growing demands on server availability and uptime, it is vital that your RDBMS include several mechanisms that will ensure the consistency and availability of your data . SQL Server 2012 provides four technologies for high availability: ■■ AlwaysOn Availability Groups In SQL Server 2012, Microsoft introduces AlwaysOn Avail- ability Groups. An Availability Group supports failover for a set of databases and leverages the existing database mirroring technology to maintain secondary replicas of the database on local or remote instances of SQL Server . This technology differs from traditional failover clustering in two ways: • You can configure automatic failover without the use of a Storage Area Network (SAN). • You can configure one or more of the secondary replicas to support read-only operations. Since a SAN is no longer required, you now have the ability to configure HA and DR using one technology . By leveraging the database mirroring capability to move data over distances using TCP/IP, you can have a copy of the database stored in a data center located in a differ- ent geographic area . ■■ Failover clustering SQL Server failover cluster instances provide high availability support at the server level. Prior to building an AlwaysOn SQL Server failover instance, you must create and configure a Windows Server failover cluster. ■■ Database mirroring A predecessor of AlwaysOn, database mirroring provides high availabil- ity at the database level . It maintains two copies of the database on instances of SQL Server running on separate servers . Typically, the servers are hosted in separate geographic locations, not only ensuring HA, but also providing DR. If you want to incorporate automatic failover, you must include a third server (witness) that will change which server is the owner of the da- tabase. Unlike with AlwaysOn, with database mirroring you cannot directly read the second- ary copy of the database . You can, however, create a snapshot of the database for read-only purposes . The snapshot will have a different name, so any clients connecting to it must be aware of the name change . Please note that this feature has been deprecated and replaced by AlwaysOn; therefore, going forward, you should use AlwaysOn instead of database mirroring. ■■ Log shipping This is another technology that provides high availability at the database level, which is ideal for very low-latency networks. The transaction log for a specific database is sent to a secondary server from the primary server and restored. Just as with AlwaysOn and data- base mirroring, you can configure log shipping in a way that allows the secondary database to be read . Chapter 1 Overview of Microsoft SQL Server 2012 7
- Note If you are familiar with SQL Server, you may be wondering why replication does not appear in the preceding list . This is because replication lacks a few key features, such as ho- listic database synchronization (as opposed to object-level movement). SQL Server Integration Services SQL Server Integration Services (SSIS) is a platform that allows you to build high-performance extrac- tion, transformation, and loading (ETL) frameworks for data warehouses . So why is it included in here in a list of Database Engine components? In most cases SSIS is used for ETL; however, it offers a number of tasks and transformations that extend its usage well beyond ETL . For example, if you are new to administering a SQL Server environment, SSIS provides you with the tools needed to perform several administrative tasks, including rebuilding indexes, updating statistics, and backing up databases, which make up the primary list of maintenance items that should be per- formed on any database . Without SSIS, as a new administrator you could spend a lot of time writing T-SQL just to get these activities running on a regular basis. But this is not the extent of the capabili- ties of SSIS for administrators . How often are you asked for an export of data to Microsoft Excel or to move data from one server to another? Using SSIS, you can quickly export or import data from various sources, including Excel, text files, Oracle, and DB2. SQL Server Management Tools SQL Server 2012 includes two graphical user interfaces that enable you to manage, monitor, maintain, and develop in a SQL Server environment. The first isSQL Server Management Studio (SSMS), which allows you to perform just about any action you can think of against an instance of SQL Server. It is an integrated environment where you can access many instances of SQL Server . It consists of a broad set of tools with a rich set of interfaces and script editors that simplify the process of developing and configuring SQL Server instances. In addition to SSMS, SQL Server 2012 introduces SQL Server Data Tools (SSDT) . SSDT is another integrated environment, but it was designed specifically for database developers. You can explore the database and database objects using the SQL Server Object Explorer. So far, some of the most talked-about features of SSDT are the ability to easily create or edit database objects and data, and run queries directly from the interface . Using the visual Table Designer, you can change table schemas for both database projects and online database instances. Summary SQL Server offers a robust set of components and tools to enable you to design an efficient, flexible, and highly available database topology for your organization . Each component either complements or supplements the capabilities and functionality of the others . Throughout the rest of this book, you will discover how the components work independently and together . 8 Part 1 Getting Started with Microsoft SQL Server 2012
- CHAPTER 2 Installing, Configuring, and Upgrading Microsoft SQL Server 2012 After completing this chapter, you will be able to ■■ Understand the differences between the various SQL Server editions . ■■ Select hardware for your SQL Server instance . ■■ Use the setup files to install an instance of SQL Server. Editions of SQL Server 2012 It’s now time to get your hands dirty and start working with SQL Server . However, before you run off and install an instance of SQL Server, you should first become familiar with the different editions of SQL Server that Microsoft offers . The SQL Server 2012 versions are offered in three categories: primary, specialized, and breadth. All editions come in 32-bit and 64-bit flavors, so don’t worry if you don’t have any 64-bit machines available; you can still get started with SQL Server. However, this book will cover the installation and configuration of a 64-bit version of SQL Server. The first category, primary, contains what some consider the three core production editions of SQL Server . With this release of SQL Server, Microsoft has removed the Data Center edition and replaced it with the Business Intelligence edition . Table 2-1 provides a list of each of the primary editions, ac- companied by a brief description . 9
- TABLE 2-1 SQL Server 2012 Primary Editions Edition Description Enterprise This is considered the premium edition of SQL Server . This edition is all-inclusive, meaning that it contains all the features available in every edition . SQL Server Enterprise delivers a comprehensive data center solution that supports a high level of mission-critical workloads, blazing-fast performance, virtualization, and business intelligence (BI) capabilities . Business Intelligence This is a new edition to the SQL Server family . It is focused on delivering all-encompassing BI-focused solutions . The Business Intelligence edition enables organizations to build, de- ploy, and manage highly scalable solutions efficiently and effectively. When accessing data, end users will have a browser-based experience that allows them to slice and dice data in ways that they could previously only imagine . Standard While not as robust as the Enterprise or BI edition, the Standard edition does boast several intriguing capabilities . Most important, it encompasses basic data management and BI ca- pabilities that are more in line with the needs of smaller-scale deployments of SQL Server . If you are looking at running a departmental application, or if you have a smaller organization, this is the version for you . The next category is hard to call a category because it contains only one version, but it is still noteworthy . The specialized category contains the Web edition . This edition is optimally designed for those SQL Server instances that will support Internet-facing workloads and is intended for web hosting service providers . It allows organizations of any size to deploy web-based content such as webpages, applications, sites, and services . The final category, breadth, was designed for specific scenarios and is offered for free or at a very low price . Table 2-2 describes the two editions in this category: Developer and Express . TABLE 2-2 SQL Server 2012 Breadth Editions Edition Description Developer This is identical to the Enterprise edition, except that it is only licensed for development and test sys- tems. You cannot use it for production purposes. Note, however, that you can easily upgrade it to the Enterprise license for production use if you need to . Express The Express version of SQL Server is a great entry-level product . It is perfect for learning and building small data-driven applications . This book covers features of SQL Server that span the entire product line, so it will use the Devel- oper edition . You can download an evaluation copy from . 10 Part 1 Getting Started with Microsoft SQL Server 2012
- Choosing Hardware for SQL Server Choosing the hardware to run your software is often a challenge . With SQL Server 2012, it’s even more challenging because you must consider the disk subsystem along with the typical server specifi- cations, such as CPU and memory, among others . As with any relational database management system (RDBMS), memory is at the top of the re- source list . This book doesn't delve too deeply into a hardware discussion, since the main purpose is to get you started with SQL Server, but note that hardware requirements vary across SQL Server edi- tions. At a minimum, your server should meet the hardware specifications outlined in Table 2-3. TABLE 2-3 SQL Server 2012 Recommended Hardware Specifications Component Requirement Processor Processor type: Intel Pentium IV or AMD Athlon Processor Speed: 2 .0 GHz or faster Memory 4 GB or more Hard disk space Database Engine, data files, and replication: 811 MB SSIS: 591 MB Client components: 1823 MB The requirements provided are specific to an Enterprise, Business Intelligence, or Standard instal- lation . The number of processors, size of your disk subsystem, and amount of memory are primarily dependent on the type of workload, your availability requirements, and I/O requirements . For more information on specific requirements for other editions, please refer to SQL Server 2012 Books Online. An exhaustive list is provided for every edition. Software Prerequisites Once you've chosen your hardware, you must ensure that the proper software is installed before you set up your SQL Server instance . For the sake of brevity, this section will focus on those instances that are included in the primary category of SQL Server editions. Table 2-4 provides a list of the minimum software requirements for those editions . Chapter 2 Installing, Configuring, and Upgrading Microsoft SQL Server 2012 11
- TABLE 2-4 SQL Server 2012 Minimum Software Requirements Software Requirement Operating system Enterprise and BI versions operating system requirements: Windows Server 2008 R2 SP1 64-bit Datacenter Windows Server 2008 R2 SP1 64-bit Enterprise Windows Server 2008 R2 SP1 64-bit Standard Windows Server 2008 R2 SP1 64-bit Web Windows Server 2008 SP2 64-bit Datacenter Windows Server 2008 SP2 64-bit Enterprise Windows Server 2008 SP2 64-bit Standard Windows Server 2008 SP2 64-bit Web Standard and Developer versions operating system requirements: Windows 7 SP1 64-bit Ultimate Windows 7 SP1 64-bit Enterprise Windows 7 SP1 64-bit Professional Windows Server 2008 SP2 64-bit Datacenter Windows Server 2008 SP2 64-bit Foundation Windows Vista SP2 64-bit Ultimate Windows Vista SP2 64-bit Enterprise Windows Vista SP2 64-bit Business .NET Framework .NET 3.5 SP1 Internet software Internet Explorer 7 .0 or later In addition to these requirements, SQL Server setup installs .NET 4.0, SQL Server Native Client, and SQL Server–specific support files. Before Installation Prior to installing SQL Server, ensure that you have selected and configured hardware that will sup- port the version of SQL Server you plan to use. Also, carefully consider the hardware and software requirements for that version . In addition, ensure that all the external needs, such as service accounts and service packs, have been created, configured, or downloaded. SQL Server Instances SQL Server 2012 supports multiple Database Engine instances on the same computer . Typically, the initial install of SQL Server is the default instance, which assumes the name of the computer on which SQL Server is being installed. Any additional installed instances are referred to as named instances . SQL Server 2012 supports side-by-side installations of instances with earlier versions . For example, if a SQL Server 2005 default instance is currently installed, you can install a SQL Server 2012 named in- stance on the same machine . The following is a list of all the SQL Server versions that can be installed side-by-side with SQL Server 2012: 12 Part 1 Getting Started with Microsoft SQL Server 2012
- ■■ SQL Server 2005 (32-bit) ■■ SQL Server 2005 (64-bit) x64 ■■ SQL Server 2008 (32-bit) ■■ SQL Server 2008 (64-bit) x64 ■■ SQL Server 2008 R2 (32-bit) ■■ SQL Server 2008 R2 (64-bit) x64 ■■ Microsoft SQL Server 2012 Release Candidate (RC) 0 (32-bit) ■■ Microsoft SQL Server 2012 RC 0 (64-bit) x64 Service Accounts Each service in SQL Server is a mechanism that is used to manage Windows or SQL authentication for SQL Server operations . During installation, you will be able to select which components to install . As a result, the SQL Server setup will install specific services. Since this book is focused on Database Engine, it will only discuss a few of the possible services: the Database Engine, SQL Server Agent, and SQL Server Integration Services (SSIS) . As a best practice, you should use separate accounts for each SQL Server service. The accounts should be configured with the lowest possible user rights. During the installation, SQL Server will assign default accounts to these services based on the host operating system . If you are running Win- dows 7 or Windows Server 2008 R2, you can use two new types of service accounts: a virtual account or a managed services account (MSA). The primary purpose of both account types is to simplify administration for the database adminis- trator. An MSA is a domain account whose password is automanaged by the domain controller . It can be used to start a Windows service, but not to log on to a computer . Virtual accounts are managed local accounts that are also automanaged . Both accounts can access network resources, but virtual accounts cannot be used with SQL Server failover cluster instances . If other servers and clients need to communicate with these services, you must configure the services to use domain accounts. Note When changing service accounts, always use SQL Server Configuration Manager. Unlike with Windows Services Control Manager, the SQL Server tools will perform additional con- figurations, such as updating the Windows local security store. Chapter 2 Installing, Configuring, and Upgrading Microsoft SQL Server 2012 13
- Collation Sequences During some SQL Server engagements, you will likely encounter many people who just accept the default collation. In most cases, the default is sufficient; however, since it’s responsible for case sensitivity, international characters, case sorting, accenting sensitivity, and rule sorting, you should definitely consider it prior to install. SQL Server allows collation specification at the server, database, and column level. As a best practice, you should use a single collation within your company. Authentication Modes The final consideration is specific to authentication. During installation, you are given two choices of authentication: Windows and SQL Server . If you choose Windows, the SA login, which will be dis- cussed in Chapter 25, "SQL Server Security," will be disabled . Selecting Windows limits access to SQL Server to Windows accounts . If you select mixed mode authentication, you will have the ability to create accounts that are specific to SQL Server. Installing SQL Server This section will show you how to install SQL Server. Note that SQL Server can be installed using several methods, which include unattended, command prompt, configuration file, sysprep, and server core . This section will describe the simplest installation method, which is using the SQL Server 2012 Setup wizard . If you don’t have a licensed copy of SQL Server, you can download an evaluation copy from the Microsoft website . Installing SQL Server from the Setup Wizard This exercise will quickly run through the installation process . 1. Either insert the SQL Server 2012 media into a DVD or CD drive or access it from a local or networked drive . Look in the root folder of the media and double-click setup.exe . If you are using an .iso file, you will need to use a tool to mount it or software such as WINRAR to ex- tract the contents . 2. The SQL Server Installation 2012 Setup wizard will open . Select Installation from the left navi- gation area. On the right, click New SQL Server Stand-Alone Installation or Add Features to an Existing Installation, depending on your needs . 3. The installer will then execute a list of setup support rules . To view a complete list, click the Show Details button . Click OK . 4. If you have a product key, select the radio button labeled Enter the Product Key, and then enter the product key . If you don’t have a product key, click the Specify a Free Edition radio button and select Evaluation from the list of available choices. Then click Next. 14 Part 1 Getting Started with Microsoft SQL Server 2012
- 5. On the next page, check the box labeled "I accept the license terms ". You also have the option of sending additional information to Microsoft about your installation . Make your choice and click Next. Note that the button labeled Next will not be enabled unless you accept the license terms. The installer will then install the necessary setup files. 6. After the setup files are installed, another set of setup support rules will run. Click Next. 7. Now you must select the server role. Select the SQL Server Feature Installation radio button, and click Next. 8. On the Feature Selection page, select the following: Database Engine Services, SQL Server Replication, SQL Server Data Tools, Client Tools Connectivity, Integration Services, Documen- tation Components, Management Tools - Basic, and Management Tools - Complete . The page should resemble Figure 2-1 . This book will focus on these foundation features, but you can in- stall others if you want, as well. Also, if you are installing a second instance of SQL Server 2012, the shared features will already be installed, so the Shared Features options will be grayed out . FIGURE 2-1 SQL Server 2012 Feature Selection page . Chapter 2 Installing, Configuring, and Upgrading Microsoft SQL Server 2012 15
- 9. Toward the bottom of the page are options for specifying the directory where you want to install the features. Accept the defaults and click Next. 10. A few more installation rules will execute. If you have installed all the proper prerequisites, everything should run successfully. Click Next. 11. On the Instance Configuration page, select whether to install a default installation or a named instance . If a default instance is already installed, your only choice will be to install a named instance. The Named Instance text box will display the name you use to connect to the SQL Server—for example, ServerName\InstanceName\. The instance ID is used to identify installa- tion directories and registry keys for an instance of SQL Server. Click Next. 12. The Disk Space Requirements page summarizes how much available space there is and how much is required. Click Next. 13. On the Server Configuration page, you specify the login accounts and startup types for the SQL Server services . If you want other services to communicate with SQL Server and vice versa, you must specify a domain account, an MSA, or a virtual account as the login account for Database Engine. For now, accept the defaults. For the SQL Server Agent service, change the startup type to Automatic. Click the Collation tab next to the Service Accounts tab. You can customize your collation on this page, but for now accept the default collation and click Next. 14. On the Database Engine Configuration page, first select your authentication mode. Select the radio button labeled Mixed Mode (SQL Server Authentication and Windows Authentication). Then specify a password for the SA account. Provide a password of your choice. Click the but- ton labeled Add Current User. On the Data Directories tab, you can change the location where the system databases and user databases are stored. For now, accept the defaults. The final tab allows you to enable FileStream. Leave it disabled for now. Click Next. 15. If you want to report errors about the installation, select the check box on the Error Reporting page, and then click Next. 16. One last rules check is run. If everything passes, click Next. 17. You are now ready to install . Click the button labeled Install . When the installation completes, a page resembling Figure 2-2 will appear . 16 Part 1 Getting Started with Microsoft SQL Server 2012
- FIGURE 2-2 SQL Server 2012 setup summary page . After Installation After your SQL Server installation is complete, there are certain things you should do. The following subsections describe a couple of them . Assigning a TCP/IP Port Number to the SQL Server Database Engine First, you may want to change the SQL Servers default TCP port from 1433 to a different port. This exercise describes how . 1. Open SQL Server Configuration Manager by clicking Start | All Programs | Microsoft SQL Server 2012 | Configuration Tools | SQL Server Configuration Manager. Chapter 2 Installing, Configuring, and Upgrading Microsoft SQL Server 2012 17
- 2. In the left-hand navigation pane, expand SQL Server Network Configuration and click Proto- cols for MSSQLSERVER . If you are changing the port for a nondefault instance, then you will click Protocols for . 3. Right-click TCP/IP in the left section . 4. You can configure each specific IP address, or you can configure the port for all IP addresses. To do so, click the IP Addresses tab, scroll to the bottom to locate IPALL, and change the port number to your desired port . Don’t change the port, as this will require you to include the port number when connecting to this server . 5. Restart the instance of SQL Server that has been changed . Click SQL Server Service in the left navigation pane . 6. Select SQL Server (MSSQLSERVER), right-click, and select Restart . Once this change is made, you are required to specify this port number when you connect to the SQL Server instance . Opening a SQL Server Instance Port Using Windows Firewall If you attempt to connect to SQL Server from another machine now, the connection attempt will time out . To connect to this instance, you must open the port . You can do so using Windows Firewall, as follows: 1. Open Windows Firewall and click Start | Control Panel | Windows Firewall. 2. Toward the top of the page, click Advanced Settings. 3. Select Inbound Rules from the left navigation pane . 4. Click New Rule from the right navigation pane. 5. On the Rule Type page, select the radio button labeled Port, and click Next. 6. Ensure that the radio button labeled TCP is selected and enter 1433 in the text box labeled Specific Local Ports. Click Next. 7. Select the Allow the Connection radio button and click Next. 8. In the text box labeled Name, type a descriptive name for your inbound rule. Click Finish. Now you should be able to connect to this instance of SQL Server using various client tools, which will be discussed later in this book . 18 Part 1 Getting Started with Microsoft SQL Server 2012
- How to Upgrade to SQL Server 2012 Whether you are upgrading an existing server from one version to the next or installing a new version on a new server, you should carefully think through and plan this task . You have two upgrade choices: in-place or side-by-side . Each has advantages and disadvantages . Often your choice will depend on how much downtime your environment can support, the age or state of the existing systems, and funding . During an in-place upgrade, your system will be down for some time . If you are a member of a 24/7 organization, taking the system down may not be an option, and you will be required to do a side-by-side upgrade. Also, if you are looking to replace older or out-of-date machines, then side-by- side is your only option . With that said, purchasing new hardware has a cost, and if you don't have funding, then your only option is an in-place upgrade . In-Place Upgrade Just the thought of upgrading any software may send chills down your spine . Fortunately, the process of upgrading to SQL Server 2012 has been greatly improved over the years . SQL Server 2012 has several supported upgrade paths . Therefore, if you are currently running SQL Server on previous ver- sions, you can quickly upgrade to SQL Server 2012 without upgrading to other versions . SQL Server 2005 with SP4 is the oldest version of SQL Server that has a direct upgrade path. If you are running a version older than this, you will need to upgrade to that version before you can perform an in-place upgrade to SQL Server 2012 . For example, if you are currently running SQL Server 2000, you must upgrade to SQL Server 2005 with SP4 prior to running an in-place upgrade to SQL Server 2012. Since this is an introductory book, how to actually perform the upgrade will not be covered; how- ever, the following preupgrade checklist should assist you prior to an in-place upgrade: ■■ Ensure that your version of SQL Server has a supported upgrade path . ■■ Back up all your databases, including system databases . ■■ Run SQL Server Upgrade Advisor to prepare for the upgrade to SQL Server 2012. ■■ Verify that your hardware and software meet the minimum requirements for SQL Server 2012 . ■■ Stop replication and make sure that the replication log is empty . ■■ Ensure that all the database server logons are stored in the master database . ■■ Estimate the disk space required for the components being upgraded and ensure that suf- ficient disk space is available. When you are ready to upgrade, you will repeat most of the steps from the "Installing SQL Server" section of this chapter. The main differences will involve the configuration. The setup detects older version of SQL Server with support upgrade paths and then guides you through the process . Chapter 2 Installing, Configuring, and Upgrading Microsoft SQL Server 2012 19
- Side-by-Side Upgrade This type of upgrade may not stress you as much as an in-place upgrade, simply because the old serv- er remains in place and can be made available quickly in the event of an installation failure . You will follow the same steps as outlined in the "Installing SQL Server from the Setup Wizard" section of this chapter . Once you've completed the steps, you will need to migrate your security, databases, replica- tion configuration, maintenance plans, and any other custom configurations that have been added to your SQL Server installation . This process gives you the advantage of having a stable rollback plan . In the event of an installation failure or some other type of catastrophe, you can always turn the other server back on and continue operations as normal. Figure 2-3 illustrates a side-by-side migration. While this strategy offers several advantages, it could require that your organization purchase new hardware . In addition, this method may require that you have disk space that accommodates two identical databases . For organizations with very large databases, this could pose a problem . Migrate Security, Databases, Maintenance Plans, etc. Old Server New Server SQL Server 2008 R2 SQL Server 2012 FIGURE 2-3 SQL Server 2012 side-by-side migration . Summary As outlined in this chapter, you can use several techniques and methods to upgrade SQL Server. Regardless of the method you choose, the end goal is typically the same . With any install or upgrade, you should allocate sufficient time to develop an effective strategy and outline the steps necessary for performing the tasks . The success of your plan depends heavily on these two factors . 20 Part 1 Getting Started with Microsoft SQL Server 2012
- CHAPTER 3 Using SQL Server 2012 administration and development tools After completing this chapter, you will be able to ■■ Use SQL Server 2012 Books Online . ■■ Create solutions and projects with SQL Server Management Studio. ■■ Use Object Explorer. ■■ Use SQL Server Data Tools . ■■ Use SQL Server Configuration Manager. Using SQL Server Books Online Over the years, Microsoft SQL Server Books Online (BOL) has been criticized for its lack of content and its inability to effectively explain how to use various SQL Server tools and options . However, as the versions of SQL Server have progressed, so has the documentation . Unfortunately, the perception of BOL remains marred by the many years of criticism and, in some cases, its limited content . While BOL does not and probably will never provide a walk-through for every possible task, it does offer a good foundation and starting point for anyone interested in gaining general knowledge about all of the capabilities of SQL Server . In previous versions of SQL Server BOL, content was installed locally by default . In Microsoft SQL Server 2012, this has changed slightly. When you open BOL for the first time, the Online Help Consent dialog box opens, as shown in Figure 3-1. 21
- FIGURE 3-1 The SQL Server 2012 Online Help Consent dialog box displays the first time you open SQL Server Books Online . You have the option of storing the help content locally or viewing it online . If you decide to view it online, you can always change the setting later . In the next exercise, you’ll install BOL locally . Install Books Online locally 1. Click the Yes button in the Online Help Consent dialog box . Microsoft Help Viewer 1 1. displays . 2. Click the Help Library Manager icon . 3. In the Help Library Manager dialog box, click Install Content from Online. A fetch process begins that provides you with a list of available content . 4. From the list, click the Add button next to Books Online, located under the SQL Server 2012 category . 5. Click the Update button . The install process begins . 6. When the update is complete, click the Finish button . 7. Click Exit . 8. Close Microsoft Help Viewer . 9. Now open SQL Server Books Online by clicking Start | All Programs | Microsoft SQL Server 2012 | Documentation & Community | SQL Server Documentation. 10. In the left navigation section, you should see several SQL Server choices . Take some time to explore the contents of BOL. If you are just getting started with SQL Server, or even if you are seasoned SQL Server veteran, you are bound to find all sorts of information that will provide insight into the full feature set available within SQL Server 2012 . 22 PART I Getting Started with Microsoft SQL Server 2012
- Using SQL Server Management Studio Your ability to efficiently manage and maintain your SQL Server environment has been greatly improved with the introduction of Microsoft SQL Server Management Studio (SSMS) in SQL Server 2005. Administrators can configure other SQL Server components, such as replication, availability groups, Microsoft SQL Server Agent, change data capture (CDC), and many other features that will be discussed later in this book. In addition, you can create databases and database objects, such as tables, views, and stored procedures . Finally, after building a database, you can also manage the data inside the database using SSMS . Get started with SQL Server Management Studio 1. To open SSMS, click Start | All Programs | Microsoft SQL Server 2012 | SQL Server Management Studio . 2. When SSMS opens, the Connect to Server dialog box appears. Accept the defaults for every option except the Server Name drop-down list. Type your server name and click the Connect button . Before you start using SSMS, let’s take a quick tour of the environment . First, you may notice that the SSMS environment is very similar to that of most Microsoft products. At the very top is the main menu, which has several options available . Directly below the main menu is the Standard toolbar, which is loaded by default . If you right-click anywhere on either toolbar, a context menu appears . From this menu, you can select other choices that will add new items to the existing toolbars or add new toolbars to the menu. Below all the menus and to the left of the window is Object Explorer. Object Explorer is a multifunctional window available in SSMS. As previously mentioned, it pro- vides an intuitive interface for navigating and accessing server features and databases . Moreover, you can use Object Explorer to connect to multiple instances of SQL Server, Integration Services, Analysis Services, and Reporting Services instances . Once connected, you have the ability to create databases and database objects, configure other features and components, run performance reports, and perform a number of other functions . When you are connected to an instance of SQL Server, simply right-click to access additional functionality that further demonstrates the true power and flexibil- ity of SSMS . For example, if you right-click the Databases folder, you can create, attach, or restore a database. You may have also noticed that Object Explorer has its own menu. This menu allows you to connect to or disconnect from an instance of SQL Server, refresh the items displayed in the window, and perform many other functions. You’ll get started with Object Explorer in the next exercise. Chapter 3 Using SQL Server 2012 administration and development tools 23
- Use Object Explorer 1. Open SSMS if you have not already done so . When prompted by the Connect to Server dialog box, ensure that Database Engine is selected from the Server Type drop-down list, type your server name in the Server Name drop-down list, and ensure that Windows Authentication is selected in the Authentication drop-down list. 2. If Object Explorer does not open, select Object Explorer from the View menu or press F8. Object Explorer will appear to the left of the SSMS window. 3. Near the top of Object Explorer, you should see the word Connect with a drop-down arrow located directly to the left . Click the drop-down arrow and use the menu that opens to con- nect to other SQL Server components . Since you have installed only a Database Engine, that is the only component that can be connected . 24 PART I Getting Started with Microsoft SQL Server 2012
- 4. You can explore various server objects by expanding any of the folders displayed in Object Explorer. For example, expand the Management folder. You can now view and configure fea- tures such as Data Collection, Database Mail, and Extended Events . 5. Right-click the server name, which is the topmost item in the Object Explorer tree. From the context menu, select Reports | Standard Reports | Server Dashboards. This report provides you with a high-level overview of the server . 6. To view more detailed information, instead of selecting Server Dashboards from the report list, select Activity-All Active Sessions. This report reveals all active open sessions on that server . 7. In the toolbar located above Object Explorer, click the button labeled New Query. A new query window opens in which you can write queries to create objects, configure components, and query database objects. Note The preceding steps provide a quick overview of some of the SSMS function- ality . Throughout this book, you’ll learn more details and additional steps to help you take full advantage of the capabilities of SSMS . While out of the box SSMS is configured to provide a full set of functionality to administrators and developers, it also provides you with the ability to make it your own. If you don’t like Object Explorer on the left, you can move it, or if you don’t like the font of the query editor, you can change it to one of your choice. You have several options available for configuration. Personalize SQL Server Management Studio 1. Open SSMS if it is not already open . 2. Select Tools | Options. 3. In the Options dialog box, select Fonts and Colors . 4. Select Courier New from the Font drop-down list. 5. Select 16 from the Size drop-down list . 6. Click OK . 7. Open a query window and type SELECT @@SEVERNAME . Click the red exclamation point icon in the menu bar to execute the query . 8. Open Object Explorer if it is not already open. Chapter 3 Using SQL Server 2012 administration and development tools 25
- 9. Click the drop-down arrow located to the right of the words Object Explorer . Select Float from the menu . 10. Click and drag Object Explorer onto the left docking option that appears. This docks Object Explorer back in its original position . Explore a little and move it to other docking locations . Find the one that best fits your preference. Using SQL Server Management Studio to create solutions and projects While most of this chapter’s content has been specific to administrators, SSMS does provide func- tionality for developers as well. In other words, you can create project-based solutions that help you organize your development and configuration scripts. Using SSMS, you can create a solution, which is a container of projects. Within SSMS, you can create two types of projects: ■■ SQL Server Scripts ■■ Analysis Services Scripts In the next exercise, you will create a SQL Server Scripts project. Create solutions and projects 1. Open SSMS if it is not already open . 2. From the menu select File | New | Project. 3. The New Project dialog box opens. 4. There are two Installed Templates to select from . Ensure that you select the SQL Server Management Studio Projects template. This choice provides two project types. Select SQL Server Scripts . 5. At the bottom of the screen, in the Name text box, type SBS2012Chp3 . 6. Accept the defaults for the Location and Solution drop-down lists. 7. Type SBS2012 in the Solution Name text box. 8. Click OK . To the right, you will notice a new docked window labeled Solution Explorer . 9. Right-click the Connections folder . 26 PART I Getting Started with Microsoft SQL Server 2012
- 10. Select New Connection. 11. Type your server name in the Server Name drop-down list. 12. Click OK . 13. Right-click the Queries folder . 14. Select New Query. 15. Right-click the newly created query and select Rename . 16. Change the name of the query to Select Server Name . Ensure that you don’t remove .sql . 17. In the query editor, type SELECT @@SERVERNAME . 18. Select File | Save All. Using SQL Server data tools SQL Server 2012 introduces a new development environment for SQL Server database developers called SQL Server Data Tools (SSDT) . Although the primary purpose of this tool is development, it can be used for database deployment and database-level configurations. Using SSDT, you can cre- ate databases and database objects such as tables, views, stored procedures, and triggers. You can also edit data within the tables . In addition, you can execute queries and perform database schema compares . SSDT replaces Business Intelligence Development Studio (BIDS). As a result, not only can you create and deploy databases, but you can also create Analysis Services, Integration Services, and Reporting Services projects. In addition, these projects can be checked into source control solutions such as Team Foundation Server . Use SQL Server Data Tools 1. Click Start | Microsoft SQL Server 2012 | SQL Server Data Tools. 2. If this is your first time opening SSDT, you will be prompted with the following screen. The options available will vary depending on the software installed on your machine . Chapter 3 Using SQL Server 2012 administration and development tools 27
- 3. Select SQL Server Development Settings from the Choose Your Default Environment Settings list box . 4. Click Start Visual Studio . 5. Choose File | New | Project. 6. In the Recent Templates pane located in the left of the New Project dialog box, select SQL Server . 7. Select SQL Server Database Project from the project list. 8. In the Name text box, type AdventureWorks . 9. Accept the default for the Location drop-down list. 10. In the Solution Name text box, type SBSChp3 . 11. Click OK . 12. In Solution Explorer, right-click the AdventureWorks project. Select Import | Database. The Import Database dialog box appears . 28 PART I Getting Started with Microsoft SQL Server 2012
- 13. Click the New Connection button. 14. Type your server name in the Server Name drop-down list. 15. Select AdventureWorks2008R2 from the Select or Enter Database Name drop-down list. 16. Change the selection in the Folder Structure drop-down list to Object Type. 17. Click OK . 18. Accept all the defaults for the remaining items and click the Start button. The database import process begins . 19. Once all the objects have been imported, click the Finish button. 20. In Solution Explorer, expand the Tables folder . 21. Double-click the Address.sql item. 22. In the table designer view, locate AddressLine1 under the Name column. For that column, change the Data Type from nvarchar(60) to nvarchar(65) . 23. Right-click the AdventureWorks project in Solution Explorer and select Deploy from the con- text menu. Now the changes are deployed to the database on the server. Note The changes made in the design view are replicated to the script view . If the changes are made in the script view, they are replicated to the design view . Chapter 3 Using SQL Server 2012 administration and development tools 29
- Using SQL Server Configuration Manager SQL Server Configuration Manager, shown in Figure 3-2, allows you to manage the SQL Server ser- vices that have been installed on your server . FIGURE 3-2 SQL Server Configuration Manager. Using SQL Server Configuration Manager, you can perform the following actions: ■■ Start, stop, and pause a service ■■ Change service accounts ■■ Configure network protocols ■■ Configure advanced properties such as AlwaysOn and Filestream Because these services are centralized, administrators are able to configure and manage services from one location . Changing accounts and account passwords are actions often required or requested . For exam- ple, during installation you may have accepted the defaults for the service account that runs SQL Server, and now you need to change them. As a best practice, you should always use SQL Server Configuration Manager to make the changes because it not only changes the account, but also sets necessary changes to registry permissions so that the account has the proper permissions . 30 PART I Getting Started with Microsoft SQL Server 2012
- Use SQL Server Configuration Manager 1. Open SQL Server Configuration Manager by clicking Start | All Programs | Microsoft SQL Server 2012 | Configuration Tools | SQL Server Configuration Manager. 2. In the left pane, right-click the SQL Server (MSSQLSERVER) . 3. Click Properties in the context menu . 4. In the Properties dialog box, you will notice several tabs . Click each to view the available options . 5. With the Log On tab activated, click the Stop button . 6. Click the Start button . 7. Click OK . 8. Expand the SQL Server Network Configuration item. 9. Select Protocols from MSSQLSERVER . 10. If you want to enable the Named Pipes protocol, right-click and select Enable from the con- text menu . Summary In this chapter, you learned about several administrative and development tools included in Microsoft SQL Server 2012 . Individually, each includes further tools that provide administrators and developers with the ability to create and manage SQL Server instances and objects at different levels. Together, they offer a comprehensive set of tools providing a one-stop shop for the functionality needed to maintain one to many instances of SQL Server . Chapter 3 Using SQL Server 2012 administration and development tools 31
- PART II Designing databases CHAPTER 4 Designing SQL Server databases . . . . . . . . . 35 CHAPTER 5 Creating your first table . . . . . . . . . . . . . 49 CHAPTER 6 Building and maintaining indexes . . . . . . . . . 73 33
- CHAPTER 4 Designing SQL Server databases After completing this chapter, you will be able to ■■ Understand the requirements and functions of each system database . ■■ Understand the SQL Server database structure . ■■ Create a database . ■■ Add and alter filegroups. ■■ Add files to filegroups. ■■ Detach and attach databases . ■■ Understand database recovery models . The database is the container for all objects within Microsoft SQL Server for the relational engine. In this chapter, you will learn about the system databases that store vital information about the SQL Server instance. You will also learn fundamental techniques needed to create user-defined databases, along with methods you can use to control how and where data is stored . The methods include creating databases that consist of multiple filegroups and multiple data files. Finally, you will learn how to move databases from one instance of SQL Server to another, and you will explore database recovery models . Understanding SQL Server System Databases Before you start creating Microsoft SQL Server 2012 databases, you should have a good understand- ing of the system databases that are created by default when you install an instance of SQL Server . Each of the following databases serves a specific purpose and is required to run SQL Server: ■■ master ■■ tempdb ■■ model ■■ msdb ■■ resource ■■ distribution 35
- master database The master database, as its name suggests, is the primary system database . Without it, SQL Server cannot start. The master database contains the most important information about objects within the SQL Server instance, such as the following: ■■ Databases ■■ AlwaysON ■■ Database mirroring ■■ Configurations ■■ Logins ■■ Resource Governor ■■ Endpoints For example, if you want to quickly obtain a list of all the databases on an instance of SQL Server, you can execute the following query: //The following code returns a list of all databases on an instance of SQL Server Select * from sys.master_files This query returns a list of databases and also additional configuration options that have been specified for each database. This approach is faster than usingMicrosoft SQL Server Management Studio (SSMS), where you view this information one database at a time . tempdb database The tempdb database is a global playground for temporary objects created by the internal processes that run SQL Server and temporary objects that are created by users or applications. These temporary objects included temporary tables and stored procedures, table variables, global temporary tables, and cursors. In addition to temporary objects, tempdb stores row versions for read-committed or snapshot isolation transactions, online index operations, and AFTER triggers. One important thing to note about tempdb is that it is re-created every time SQL Server is restarted. Although you can create objects in tempdb, you should never use it as a database where persisted information is stored. model database The model database is exactly what its name implies: a model for all databases that are created on an instance of SQL Server . In other words, it’s used as a template each time you create a database . For example, if you want a particular table to exist in every database created on an instance of SQL Server, you will create that table in the model database. As a result, each time a database is created, it will include that table . 36 PART II Designing Databases
- Note If the model database does not exist or is offline, tempdb cannot be created . This is because, as mentioned previously, it is re-created each time SQL Server is restarted . Since each database uses model as a template, and tempdb is no exception, it must exist to re- create tempdb at startup . msdb database The s serves primarily as the back-end database for Microsoft SQL Server Agent. Whenever you create and/or schedule a SQL Server Agent job, the metadata for that job is stored in this database. In addi- tion to SQL Server Agent data, msdb stores information for the following components: ■■ Service brokers ■■ Alerts ■■ Log shipping ■■ SSIS packages ■■ Utility control point (UCP) ■■ Database mail ■■ Maintenance plans resource database The resource database is a hidden, read-only database that is usually not discussed very often . The resource database's primary purpose is to improve the upgrade process from one version of SQL Server to the next. All system objects for an instance of SQL Server are stored within the resource database . This database cannot be backed up or restored . You should not attempt to change or move this database unless Microsoft Customer Support directs you to do so . distribution database The final system database is thedistribution database . This database exists only when you have con- figured this instance as a distributor for replication. Prior to configuring replication, you must perform this configuration. All metadata and history for the various types of replication are stored within this database . See Also For more information on replication, see Chapter 19, "Replication." Chapter 4 Designing SQL Server databases 37
- View system databases 1. Open SQL Server Management Studio (SSMS) and connect to a server . 2. Object Explorer should be open. If it is not, press F8 to open it. 3. In Object Explorer, expand Databases. 4. You will see a folder labeled System Databases . Expand it . Understanding the SQL Server database structure As mentioned previously, databases are the primary data storage objects within SQL Server. The database creation process, while very simple, always requires careful thought relating to the struc- ture . Databases can be created using many different technologies and techniques . In this chapter, you will focus on using T-SQL and SSMS. By default, every SQL Server database consists of two files (see Figure 4-1): ■■ The data file contains data and database objects such as tables, views, and stored procedures. ■■ The log file contains information that assists in the recoverability of transactions in the database . SBSChp4DB Data File (C:\SQLData\SBSChp4DB.mdf) Log File (C:\SQLData\SBSChp4DB_log.ldf) FIGURE 4-1 The SQL Server database structure consists of at least a single data file and a single log file. 38 PART II Designing Databases
- Creating a database There are two types of data files: primary and secondary. When a database is initially created, the primary data file is created. By default, it contains all the startup information for the database. As user-defined objects are created, they may also be stored in the primary data file. However, you may implement certain architectural strategies to improve the performance, scalability, and maintainabil- ity of your database. These strategies are discussed in the upcoming “Adding Files and Filegroups” section . Prior to running the script, create two folders on the root of your C drive: SQLData and SQLLog . Create your first database with SSMS 1. Open SSMS . 2. Open Object Explorer, if it is not already opened. 3. Click the arrow next to your server . 4. Right-click the Databases folder . 5. In the context menu, select New Database. 6. The New Database dialog box opens. Ensure that General is selected in the Select a Page sec- tion on the left . 7. In the Database Name text box, type SBSChp4SSMS . 8. In the Database Files section, locate the Path column. On the first row under the Path column, click the ellipsis button. Browse to C:\SQLData. 9. On the same row, under the File Name column, type SBSChp4SSMS . 10. On the second row, under the Path column, click the ellipsis button. Browse to C:\SQLLog. 11. On the same row, under the File Name column, type SBSChp4SSMS_log . Chapter 4 Designing SQL Server databases 39
- 12. Click OK . 13. In the left section labeled Select a Page, select Filegroup . Create your first database with T-SQL 1. Open the query editor in SSMS . 2. In the query editor, enter the following T-SQL code: Use this script to create a database using T-SQL USE master; CREATE DATABASE SBSChp4TSQL ON PRIMARY (NAME='SBSChp4TSQL1', FILENAME = 'C:\SQLDATA\SBSTSQL1.mdf', SIZE=10MB, MAXSIZE=20, FILEGROWTH=10%) LOG ON (NAME='SBSChp4TSQL_log', FILENAME = 'C:\SQLLog\SBSTSQL_log.ldf', SIZE=10MB, MAXSIZE=200, FILEGROWTH=20%); Understanding arguments In the previous script, several arguments are used so that the database is placed in a specific directory and it grows at a certain rate . SQL Server provides a long list of arguments that can further extend how a database is created and where it resides . The previous script uses the following commonly used arguments: 40 PART II Designing Databases
- ■■ database_name is the name of the database, which must be unique to any of the databases that exist at the time of creation . ■■ ON specifies the filegroup and begins the section where the data file is defined. ■■ LOG ON begins the section where the log is defined. ■■ Name is the logical file name used by SQL Server when referencing the file. As withdatabase_ name, it must be unique . ■■ FileName is the operating system path and file name, including the file extension. ■■ Size specifies the initial size of the file in megabytes (MB) by default. Kilobytes (KB), gigabytes (GB), and terabytes (TB) can also be specified. ■■ Maxsize specifies the maximum size to which the file can grow (shown in megabytes by default) . ■■ Filegrowth specifies the growth increment of the file. It is also shown in megabytes by default, but it can be specified as a percentage. Note This is not an exhaustive list of available database creation options. As you work more and more with SQL Server, you may discover a need for the other available options, which you can find in SQL Server Books Online. Adding files and filegroups Instead of placing user-defined objects in the primary data file, you have the option of adding a secondary data file to your database. These files types are usually distinguished by the file extension: primary files are usually suffixed with.mdf, while secondary files are suffixed with .ndf. Neither is a requirement; however, it is a best practice to use these extensions. The secondary data files are often used to spread data across disk subsystems or to add more disk space to a database in the event that the other data files have reached maximum capacity. In addition to adding multiple files to a database, another best practice is to group the files using filegroups. When a database is created, the primary filegroup containing the primary data file is cre- ated by default. Additional filegroups are then created to ease database administration and typically to group data files together (see Figure 4-2). Chapter 4 Designing SQL Server databases 41
- SBSChp4DB Filegroup Primary Data File (C:\SQLData\SBSChp4DB.mdf) Secondary Filegroup Secondary Data File (C:\SQLData\SBSChp4DB1.ndf) Data File (C:\SQLData\SBSChp4DB2.ndf) Log File (C:\SQLData\SBSChp4DB_log.ldf) FIGURE 4-2 Database files and filegroups. In the image are two filegroups: ■■ The primary filegroup contains the primary data file. ■■ The secondary filegroup contains two secondary data files. Add files and filegroups using SSMS 1. Open SSMS and connect to a SQL server instance . 2. Expand the Databases folder . 3. Right-click the SBSChp4SSMS database and select Properties. 4. Select Filegroups from the Select a Page section of the Database Properties dialog box . 5. Click the Add button under the Rows section. 6. In the newly created row, under the Name column, type SBSSSMSGroup1 . 7. In the second row, under the Default column, check the box . 42 PART II Designing Databases
- 8. In the Select a Page section, select Files, and then maximize the window . 9. Click Add. 10. In the newly created row, under the Logical Name column, enter SBSChp4SSMS1 . 11. In the Filegroup column, select SBSSSMSGroup1 . 12. In the Path column, click the ellipsis button. Browse to C:\SQLData. 13. In the File Name column, enter SBSChp4SSMS1.ndf . 14. Click OK . Add files and filegroups using T-SQL 1. Open the query editor in SSMS . 2. In the query editor, enter the following T-SQL code: Use this code to add a file and filegroup to a database USE master; ALTER DATABASE SBSChp4TSQL ADD FILEGROUP SBSTSQLGroup1; Chapter 4 Designing SQL Server databases 43
- ALTER DATABASE SBSChp4TSQL ADD File ( NAME='SBSChp4TSQL2', FILENAME = 'C:\SQLDATA\SBSTSQL2.ndf', SIZE=10MB, MAXSIZE=20, FILEGROWTH=10% ) TO FILEGROUP SBSTSQLGroup1; Detaching and attaching SQL Server databases Now that you’ve created your database, what happens if you need to move it to another instance of SQL Server? For example, assume that you want to redistribute the free space on a server or decom- mission a server, which would require you to detach a database from one instance of SQL Server and then attach the database to a new instance of SQL Server . To accomplish this, you can use either T-SQL or SSMS . There are currently two ways to attach a database to and one way to detach a database from an instance of SQL Server . To attach a database, you use sp_attach or CREATE DATABASE specifying the FOR ATTACH argument. Please note that the sp_attach system stored procedure has been deprecated and will be removed from future versions of SQL Server. As a result, it is recommended that you use only the CREATE DATABASE option when attaching databases. Detach a SQL Server database using SSMS 1. Open SSMS . 2. Open Object Explorer, if it is not already open. 3. Expand the server node . 4. Expand the Databases folder . 5. Right-click the SBSChp4SSMS database. 6. Select Tasks | Detach. 7. In the Detach Database dialog box, check the boxes in the Drop Connections and Update Statistics columns . 44 PART II Designing Databases
- 8. Click OK . Now that the database is detached, you can copy the files to the new storage location and attach the database to a new instance of SQL Server . Detach a SQL Server database using T-SQL 1. Open SSMS, and then open a new query window . 2. Enter and execute the following script: USE Master; EXEC sp_detach_db @dbname = 'SBSChp4TSQL'; Attach a SQL Server database using SSMS 1. Open SSMS . 2. Open Object Explorer, if it is not already open. 3. Expand the server node . 4. Right-click the Databases folder . Chapter 4 Designing SQL Server databases 45
- 5. Click Attach. 6. Click the Add button. 7. In the Locate Database Files dialog box, expand the folder labeled C . 8. Locate and expand the SQLData folder, and then select the SBSChp4SSMS.mdf file. 9. Click OK . 10. Click OK . Attach a SQL Server database using T-SQL 1. Open SSMS, and then open a new query window . 2. Enter and execute the following script: USE master; CREATE DATABASE SBSChp4TSQL ON (FILENAME = 'C:\SQLData\SBSTSQL1.mdf'), (FILENAME = 'C:\SQLData\SBSTSQL2.ndf'), (FILENAME = 'C:\SQLLog\SBSTSQL_Log.ldf') FOR ATTACH; 46 PART II Designing Databases
- Understanding database recovery models A SQL Server database can be set to one of three recovery models: ■■ Simple ■■ Full ■■ Bulk-logged The model determines how precisely a database may be restored . Simple model The simple model does not allow for transaction log backups. As a result, you cannot restore a database to a point in time . Your database is vulnerable to data loss when using this model . That said, using this model does ease the task of administration because SQL Server will reclaim space automati- cally from the transaction log . Full model With the full model, data loss is minimal when the transaction log is backed up on a regular basis . Every transaction is fully logged to the transaction log, and the transaction log will continue to grow until it is backed up . While this model does add administrative overhead, your data is protected from data loss . Bulk-logged model When you use the bulk-logged model, bulk operations are minimally logged, which reduces the size of the transaction log. Note that this does not eliminate the need to back up the transaction log. Unlike in the full recovery model, in the bulk-logged model you can restore only to the end of any backup; you cannot restore to some point in time . Summary You can create SQL Server databases using several different tools . In this chapter, you learned about two methods to create databases, but you are also able to use other tools such as SQL Server Data Tools, Windows PowerShell, and the C# and VB .NET programming languages. Each tool offers certain advantages and disadvantages, therefore you should take some time to explore all options available to ensure you use the tool that best fits your development needs. Chapter 4 Designing SQL Server databases 47
- CHAPTER 5 Creating your first table After completing this chapter, you will be able to ■■ Develop a naming standard . ■■ Understand schemas . ■■ Understand the different SQL Server data types . ■■ Understand column properties . ■■ Create and alter tables . ■■ Understand computed columns . ■■ Add constraints to a table. ■■ Understand the FileTable feature . ■■ Create a database diagram . Just as the database is the primary container of all objects on an instance of Microsoft SQL Server, the table is the primary container of all data on a SQL Server instance . Tables are the foundation of all objects, and without them a database is useless. The power in any application is the data that it accepts and stores . Without a relational database management system (RDBMS) to store and main- tain that data, the application would likely not exist . While this book's primary focus is SQL Server, it should be noted that databases come in many shapes and forms . For example, the most widely used database is a Microsoft Excel spreadsheet . Many people extract data or request data from an RDBMS and import that data into Excel . Once the data is in Excel, the end user may create a series of spreadsheets and workbooks that together pro- vide a very robust reporting tool containing answers to many organizational questions . The downside of this approach is that those Excel spreadsheets and workbooks become data silos that are typically stored on users’ machines . If the spreadsheets and workbooks are not secured and backed up regularly, the information stored in them is vulnerable to a failure or catastrophe . In addition, the process to populate those spreadsheets and workbooks is often manual, and only one person understands how it works . Finally, by storing data in Excel, users are not able to realize the RDBMS benefits of multiuser concurrency and data integrity, which are the foundation of most data- base management systems . 49
- The previously described downsides alone provide sufficient justification for using an RDBMS. Whether you are working with SQL Server or a similar system, most RDBMSs offer a way to centrally maintain and monitor access and availability to the data . Moreover, they provide governance on how the data is structured, organized, and delivered . These three key components are not typically available in something like an Excel spreadsheet . Using a robust RDBMS such as SQL Server provides administrators and developers with the ability to ensure that data is stored in a central location, and they can enforce naming standards and additional control that almost guarantee consistent and cred- ible data across the organization . In this chapter, you will first learn the importance of implementing and enforcing a naming standard . From there, you will be introduced to the various data types that are supported by SQL Server. Then you will create your first table using Microsoft SQL Server Management Studio (SSMS) and Transact-SQL (T-SQL) . Finally, you will use the same methods to add constraints and keys to your tables . Developing a naming standard The first step in any database design project is to develop a naming standard that will be used during the design process. While naming standard development is definitely not a requirement, continuing without some standard could yield an unorganized database that may present challenges to develop- ers when accessing the data . Inconsistent naming conventions often inhibit the development process indirectly . For a developer who is writing T-SQL to modify or retrieve data, naming standards provide clear paths to constructing T-SQL statements . For example, assume that you are designing a database that will store human resources data . You are asked to create a structure that houses information about individual employees, such as their name, address, phone number, and department. Assume that you have designed the database shown in Figure 5-1 . Employee EmployeeDept tblDepartments EmployeeID DeptID FK1 EmpID FK1 DepartmentID Addresses AddressIdentification FK1 EmployeeID FIGURE 5-1 This simple database schema does not have naming conventions . 50 PART II Designing Databases
- The database schema in Figure 5-1 shows four tables. Notice that each table uses a different nam- ing convention . The name of the table that will store address information is plural, and the name of the table that will store department information is prefixed with tbl . There are other inconsistencies, but you should get the picture . If you were a developer new to this database, writing T-SQL against this database could pose a challenge . Since the table names vary, a developer would have to spend a significant amount of time becoming familiar with the database prior to writing queries. You may have also noticed the inconsistencies in the column names, which further complicate working with this database . Enforcing governance with regard to naming objects within a database makes the database easier to work with . The following are some best practices: ■■ General standards • Do not use spaces within any object or column name. • Underscore characters are acceptable, but be aware that they can present some challenges with visualization tools . • Use PascalCase, which means capitalizing the first letter of each word that is used to name an object or column. • Do not use reserved keywords . Plural table and column names are acceptable, but singular is preferred in this book . This is completely a matter of preference . ■■ Table naming standards • Names should reflect the contents of the table. • Names must be unique to the database and the schema. ■■ Column naming standards • Names should be unique to each table. • Names should reflect the business use. • Select the appropriate data type, as discussed later in this chapter . Note Naming conventions for other objects are discussed when appropriate in context throughout this book . Once this governance is put into place, the updated schema for the earlier sample database resem- bles Figure 5-2 . Chapter 5 Creating your first table 51
- Employee EmployeeDeptartment EmployeeID DepartmentID FK1 EmployeeID Address Departments AddressID FK1 DepartmentID FK1 EmployeeID FIGURE 5-2 This database schema has naming conventions . All the tables now have a common naming standard. Every new word begins with an uppercase letter, and the names are spelled completely. The main thing to notice is that each table name reflects the contents of the table . Understanding schemas While a database is the primary container of all objects, schemas offer another level of containment and organization within a database. Using a schema, a user can group objects of similar scope or ownership together . By default, the database owner (dbo) schema is automatically created within a database. Any object that is created is added to this schema. You can change this behavior in a couple of ways, as you will learn later in this book . Consider the schema shown in Figure 5-2 . You could create a schema containing information specific to the human resources department. However, if you extend the database to include sales information for each employee, you can place the new objects in a Sales schema. Create a database schema using SSMS 1. Open SSMS and connect to a SQL Server instance . 2. Expand the Databases folder . 3. Expand the SBSChp4SSMS database. 4. Expand the Security folder . 5. Right-click the Schema folder and select New Schema from the context menu. 6. In the Schema – New dialog box, type Sales in the Schema Name text box and dbo in the Schema Owner text box . 52 PART II Designing Databases
- 7. Click OK . Create a database schema using T-SQL 1. Open the query editor in SSMS . 2. In the query editor, enter and execute the following T-SQL code: Use this code to create a SQL Server database with a single data and log file USE SBSChp4TSQL; GO CREATE SCHEMA Sales; GO CREATE SCHEMA HumanResources; GO Note As a best practice, try to create all schemas prior to creating tables. However, if that is not possible, you can always move a table or any other object from one to another using the ALTER SCHEMA TRANSFER statement. Chapter 5 Creating your first table 53
- A final thing to mention about schemas is that you can grant users permissions to schemas. In Chapter 25, "Security," you’ll look in depth at several security aspects of SQL Server, including schemas . Understanding SQL Server data types SQL Server contains four distinct data type categories, as shown in Figure 5-3. Numeric Date and Time Strings Other FIGURE 5-3 SQL Server contains four data type categories . Each of the four categories contains subcategories. All columns within a table, declared variables, and parameters must have a corresponding data type. A data type simply specifies what type of data can be placed into the object (column, variable, parameter, and so on). Database integrity depends heavily on appropriately scoped data types; therefore, you should not always depend or rely on an application to enforce data type usage . Numeric data types The numeric data type has two subcategories: exact and approximate. Exact data types fit within a finite range of numbers. Table 5-1 lists and defines each exact numeric data type. TABLE 5-1 Exact Numeric Data Types Data Type Range Storage bigint –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 8 bytes int –2,147,483,648 to 2,147,483,647 4 bytes smallint –32,768 to 32,767 2 bytes tinyint 0 to 255 1 byte money –922,337,203,685,477.5808 to 922,337,203,685,477.5807 8 bytes smallmoney –214,748.3648 to 214,748.3647 4 bytes If you need a column in a table that only stores values between 1 and 10, you should use a tinyint . In addition to the data types in Table 5-1, the exact numeric category includes two more data types: decimal and numeric . They are slightly different from the others in that they allow decimal 54 PART II Designing Databases
- places, which are restricted by two values: precision and scale . Essentially, they are very similar in what and how they store data . Precision is the total number of digits that can be stored on both sides of the decimal place. This value can only be between 1 and 38. Scale is the number of digits that can be stored to the right of the decimal place and is only specified when precision is provided. This value will be between 0 and the specified precision. Therefore, if you wanted to store a four-digit number with only two digits to the right of the decimal place, you would use decimal(4,2) . Table 5-2 lists preci- sion ranges and their corresponding storage requirements . TABLE 5-2 Precision Ranges and Storage Requirements Precision Storage 1–9 5 bytes 10–19 9 bytes 20–28 13 bytes 29–38 17 bytes The approximate subcategory is similar to the decimal and numeric data types in that one accepts a precision value, which is float . The other does not accept a precision value; instead, it can store up to seven digits, which includes digits on both sides of the decimal . For example, if you attempt to store the number 1234.5678 in a real data type, the value rounds up to 1234.568. However, if you want to maintain the precision of that value, you can store it in a float(25) . The main difference between the decimal and float data types is that you have a more precise level of storage with decimal than float. Table 5-3 lists precision ranges and their storage requirements for approximate numeric data types . TABLE 5-3 Approximate Precision Ranges and Storage Requirements nvalue Precision Storage 1–24 7 digits 4 bytes 25–53 15 digits 8 bytes String data types The string data type contains three subcategories: character, Unicode, and binary . Each contains three specific data types. The data types are similar in that each subcategory contains a fixed-length data type, a variable-length data type, and a data type that has been deprecated . Note n defines the string length that can be stored. For variable-length data types, max can be specified for n, which indicates that the maximum storage size is 2 GB . Chapter 5 Creating your first table 55
- The character string subcategory will store non-Unicode data . The three types are as follows: ■■ char(n) Fixed-length string data type with a string length between 1 and 8,000 . ■■ varchar(n) Variable-length string data type that can store up to 2 GB of data . ■■ text Deprecated data type . Replace it with a varchar(max) . The Unicode string subcategory will store both Unicode and non-Unicode data . The three types are as follows: ■■ nchar(n) Fixed-length string data type with a string length between 1 and 4,000. ■■ nvarchar(n) Variable-length string data type that can store up to 2 GB of data . ■■ ntext Deprecated data type . Replace it with nvarchar(max) . The binary string subcategory will store binary data . The three types are as follows: ■■ binary(n) Fixed-length binary data type with a string length between 1 and 8,000 . ■■ varbinary(n) Variable-length binary data type with a string length up to 2 GB . ■■ image Deprecated data type . Replace with varbinary(max) . As a best practice, you should use the fixed-length char( , nchar, binary) data types across all sub- categories when the values being stored are a consistent size . When the values are not consistent, you should use the variable-length data types (varchar, nvarchar, varbinary) . Date and time data types Date and time data types are used widely in SQL Server databases . They offer the convenience of storing the date and time in various ways . There are six date and time data types . ■■ time(n) This data type stores the time of day without time-zone awareness based on a 24-hour clock. time accepts one argument, which is fractional seconds precision . You can only provide values between 0 and 7. As the number increases, so does the fractional precision. If you specify a data type of time(2), you can store a value similar to 11:51:04:24. Changing 2 to 3 increases the precision to three numbers, similar to 11:51:04:245. ■■ date This data type stores a date value between 01-01-01 and 12-31-9999. ■■ smalldatetime This data type stores a date and time value . The value of the date is between 1/1/1900 and 6/6/2079. The time precision is down to seconds. A value of 4/1/2012 11:15:04 can be stored using this data type . ■■ datetime This data type is similar to smalldatetime, but it offers a larger date range and a higher level of precision with regard to time . It offers the same date range as the date param- eter, 01-01-01 to 12-31-9999, and it has a more precise value of time. A value of 4/1/2012 11:15:04:888 can be stored using this data type. 56 PART II Designing Databases
- ■■ datetime2(n) This data type is similar to datetime, but it offers extended flexibility of time. Unlike with datetime, you can control the fractional second precision with a value . You can only provide values between 0 and 7 . If you specify a data type of datetime2(2), you can store a value similar to 4/1/2012 11:51:04:24. Changing 2 to 3 increases the precision to three num- bers, similar to 4/1/2012 11:51:04:24. ■■ datetimeoffset This data type includes all the capabilities of datetime2, and it also has time- zone awareness . This makes it unique among the date and time data types . Using this data type, you can store the time-zone offset along with the date and time. A value of 4/1/2012 03:10:24 -06:00 can be stored using this data type. Other data types In addition to the data types covered in the preceding sections, SQL Server includes several other data types. Table 5-4 lists each additional data type with a brief description. TABLE 5-4 Other SQL Server 2012 Data Types Data Type Description cursor A temporary copy of data that will be used for recursive or iterative processes. Of all the data types, this is the only one that cannot be included as part of a table . rowversion(timestamp) This data type automatically generates an 8-byte value similar to 0x0000000000000001 . rowversion replaces the timestamp data type, which has been deprecated . This data type is typically used to detect changes in data . hierarchyid This is a positional data type . It represents a position in a hierarchy . hierarchyid is used to organize data such as a bill of materials and organizational charts . sql_variant This is the chameleon of data types . sql_variant can assume the identity of just about any data type in the list of SQL Server data types . Prior to performing any types of op- erations on it, you must convert it to the respective data type . For example, if you want perform addition, you must cast this data type to an int or some other numeric data type that supports that operation . xml You can store actual XML data using this data type . geospatial SQL Server supports two geospatial data types: GEOGRAPHY and GEOMETRY. GEOGRAPHY represents data in a round-earth coordinate system. GEOMETRY is a flat or planar data type in which you can store points, lines, and other geometric figures. This data type allows you to store common unstructured data such as documents and filestream images. SQL Server has been coupled with the NTFS file system, allowing the storage of varbinary(max) on the file system. Since the data types in Table 5-4 are typically used for advanced operations, details regarding how to use them are beyond the scope of this book . If you feel the need to delve deeper into these data types, you can search SQL Server Books Online for some great examples . Chapter 5 Creating your first table 57
- Understanding column properties You're almost ready to create your first table. Before doing so, however, you must understand that a table contains one or more columns, which make up the rows of a table . Each column stores very specific information. You can configure certain properties for a given column based on the selected data type, which is a property itself . The most common property is Allow Nulls. This simply means that you can insert a row into the table without supplying a value. For example, say you have a table that contains FirstName, MiddleName, and LastName. Every person does not have a middle name; therefore, that value should be optional . When designing your table, consider the business logic behind the value when deciding nullability . Note NULL is a special value in the database world. It does not mean empty; rather, it rep- resents the absence of a value and is different from an empty string . The second most common property is Is Identity . It is second because it is only available for most numeric data types . When you set this value for a column, SQL Server automatically generates a num- ber as each row is inserted. You can customize or configure the starting point and how the number will increment using the properties that are available. You will learn how to configure the identity value later in this chapter . SQL Server 2012 introduces a new autonumber-generating mechanism called Sequence, which is a schema-bound object that generates a sequence of numeric values based on certain options speci- fied during its creation. Chapter 12, “Modifying Data,” discusses this topic at length. Creating tables Admittedly, creating tables with SSMS is much easier than with T-SQL. The biggest disadvantage to using SSMS, though, is not having very portable code . Once T-SQL is written, it can be saved and executed against the same instance or another instance of SQL Server without your having to re- create the script, but this is not the case with SSMS . If you use the table designer to create a table, you are required to perform the same steps on another instance of SQL Server if you want to re-create the table. Nevertheless, it is worth knowing and understanding the steps. You should learn how to create the table using T-SQL not only because most things on SQL Server are accomplished using T-SQL, but also because it allows for easy portability . Now it is time to create a table of your own. Create a table named Addresses using the information provided in Table 5-5 . 58 PART II Designing Databases
- TABLE 5-5 Address Table Requirements Name Data Type Length Allow Nulls Identity AddressID int NA No Yes (start at 1 increment by 1) StreetAddress varchar 125 No NA StreetAddress2 varchar 75 Yes NA City varchar 100 No NA State char 2 No NA EmployeeID int NA No NA Create a table using SSMS 1. With SSMS open, expand the Databases folder . 2. Expand the SBSChp4SSMS database. 3. Expand the Security folder . 4. Right-click the Schemas folder . 5. Select New Schema from the menu. 6. In the Schema – New dialog box, type HumanResources in the Schema Name text box. 7. Type dbo in the Schema Owner text box . 8. Click OK . 9. Right-click the Tables folder . The table designer opens . 10. Select New Table from the menu. Chapter 5 Creating your first table 59