navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

SQL Server 2008 Essential Training
Mark Todd

SQL Server 2008 Essential Training

with Simon Allardice

 


In SQL Server 2008 Essential Training, Simon Allardice explores all the major features of SQL Server 2008 R2, beginning with core concepts: installing, planning, and building a first database. Explore how Transact-SQL is used to retrieve, update, and insert information, and gain insight into how to effectively administer databases. The course also covers features outside SQL Server's database engine, including technologies that have grown up around it: SQL Server Reporting Services and Integration Services. Exercise files are included with the course.
Topics include:
  • Using T-SQL (Transact-SQL)
  • Managing databases with SQL Server Management Studio
  • Understanding database normalization
  • Using SELECT statements
  • Building indexes
  • Monitoring database size and integrity
  • Backing up and restoring databases
  • Creating functions and stored procedures
  • Managing database permissions
  • Creating and formatting reports
  • Adding charts to reports
  • Creating and executing a simple SSIS package

show more

author
Simon Allardice
subject
Business, Developer, Servers, Databases
software
SQL Server 2008
level
Beginner
duration
6h 54m
released
Dec 15, 2010

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hi! I am Simon Allardice and welcome to SQL Server 2008 Essential Training.
00:09In the next few hours, we'll cover the core features found in SQL Server 2008 R2,
00:14the latest version of Microsoft's flagship database management system.
00:20In this course we'll begin by choosing the right edition of SQL Server from the
00:24many available options.
00:26We'll see how to install it properly and explore the different tools and
00:30applications for working with SQL Server.
00:33We'll move on to designing and creating databases and tables and see how to
00:38use Transact-SQL, the language of SQL Server, to retrieve, insert, and manipulate data.
00:45We'll perform core database administration tasks, like working with indexing
00:50to improve performance, see how to secure your data, and how to work with
00:54Backup and Restore.
00:56We'll even have an introduction to some of the more advanced features of this
00:59product, things like SQL Server reporting services and integration services.
01:04If you've been working with SQL Server already, you may soon find that you've
01:08only scratched the surface of what it can really do, and if you're just
01:11beginning to use SQL Server, this is the perfect place to start.
01:15So welcome to SQL Server 2008 Essential Training.
Collapse this transcript
Using the exercise files
00:01If you are a premium member of lynda.com, you can download some exercise
00:05files for this course.
00:06Now I've got these saved to my Desktop here and if I open this up, you'll see
00:10that a few of the chapters in this course have their own folder.
00:13I could drill down into that folder, find the number of a movie, and find any
00:18file that's associated with it.
00:19Now, not all movies have files and in fact most of what I've provided here in the
00:24download is simply a copy of what you will end up writing if you just follow
00:29along with the course.
00:30But in a few movies in this course, you'll see an overlay appear that might
00:34point you to a chapter, say Chapter 8, and a movie, say number 05, with a starter file.
00:40That's just designed to save you a little time.
00:43If you don't have access to the exercise files at that moment, you will be able
00:46to follow along just fine.
00:48You just may have to type a couple of lines of code.
00:50Now we're also going to be using some sample Microsoft databases, which aren't
00:55part of this download.
00:57But I'll show you how to get to those and install them just a little later.
Collapse this transcript
1. Getting Started
SQL Server core concepts
00:00You'll often hear SQL Server 2008 referred to as a database, but it isn't.
00:06SQL Server 2008 is not a database.
00:08It's a database management system and there is a big difference.
00:12More specifically it's a relational database management system or RDBMS.
00:19Okay, you might be concerned that we're 15 seconds into this course and I'm
00:22already starting to hit you with abbreviations.
00:25But as you almost certainly know, SQL Server is not your only choice for
00:28managing databases. You have Oracle, DB2, MySQL, Access. There are dozens of RDBMS products.
00:37So if we're going to understand what SQL Server is, we have two questions.
00:42One, why do we need an RDBMS in the first place?
00:45And two, if we're going to have one, since there are many available, why would
00:51we want this one, SQL Server, and what's special about this one?
00:55So why have one at all?
00:57Well the easy answer, you have some data.
00:59You, the company that you work for, the clients that you have, or even the
01:03business that you're wanting to build.
01:05This information could be about customers, products, employees, orders.
01:10It could be information about hits on your website.
01:13So it could be text, but it could also be documents.
01:17It could be images, audio, or video.
01:19Now if you just had a small amount of data, you might not need a
01:23database management system.
01:25There is nothing really wrong with just using a bunch of text files, or
01:29spreadsheets, and storing your stuff in there.
01:33But this is not a very robust way to do it, because once starts off as a small
01:39amount of data has a tendency to turn into a large amount of data and there is your problem.
01:44What happens when two people try to edit this file at the same time?
01:49How about 200 people at the same time?
01:52What stops people from going into this and adding invalid or missing data?
01:58If this spreadsheet solution is nice and speedy when there are 20 or 250 lines in it,
02:03what happens when there are two million lines in it?
02:07So often the first pinpoint is having a lot of data.
02:12Now what a lot of data means could differ from place-to-place.
02:14It could be 500 piece of information, 20,000, a million, 100 million pieces of
02:20information that you've got to manage, and to ask questions of this, to query it
02:26in a variety of ways.
02:27So we take it and we put this in a database.
02:32The RDBMS, your management system, manages this database.
02:36It controls what gets accessed to that data.
02:39If you want to get to the data, you go through the database management system.
02:45But of course, it's not just necessary to have a database because you've got a lot of data.
02:49The other key is that whatever is in this database is important.
02:53You can't lose that.
02:54This is critical to your business.
02:56But many companies, particularl, in the web world your, data is your business.
03:01Think of Amazon.com, it's a database.
03:04eBay, Craigslist, Google, even lynda. com, the business is the information.
03:09Sure, on these sites there are some great programming and a nice front-end
03:13that could be a website or an iPhone application but it's all about getting to this data.
03:19But it would be wrong to think about a database management system
03:24just giving you a place to dump your data.
03:27That's the easy part.
03:29The true power of a database management system lies in the other stuff that
03:33it gives you, the invisible stuff, the things you can't see. The security on
03:38this data, the enforced integrity of it, the ability to get to it really fast,
03:43and get to it reliably.
03:45This has to be robust.
03:47It needs to withstand lots of people at the same time, and even to correctly
03:52survive hardware issues without corrupting your data.
03:54Now of course, most database management systems promise this, so why SQL Server?
04:00Well there are a lot of good reasons to use SQL Server.
04:03It's been around for 20 years.
04:04It's a very mature product with an immense amount of features.
04:08Now of course the choice may have been made already.
04:12This may be the chosen database system of the business that you worked for or
04:16the clients you have.
04:17Not surprisingly, it's very common in a Microsoft-oriented shop because the
04:22integration is great with other Microsoft products, things like SharePoint,
04:26ASP.NET for web development, Silverlight, all the Office products
04:31particularly like Excel.
04:32Those are great with SQL Server.
04:34Now if you are a web developer, another very common reason is that many web
04:39hosting companies will allow you to rent and create a SQL Server database and
04:45often inexpensively, when it's still quite rare to find other enterprise-level
04:50databases like Oracle or DB2 on offer.
04:53Now SQL Server also comes with many add-ons for advanced features.
04:59There are parts of SQL Server that deal with reporting, with business
05:03intelligence and data analysis, integration for making SQL Server work with
05:07other database management systems and other systems.
05:11It's not just a place to dump some data.
05:14Now in many larger organizations, it's not even an either/or.
05:18You may have many databases managed by different RDBMSs.
05:23You've got SQL Server and Oracle, SQL Server and MySQL, DB2, PostgreSQL.
05:27Now as I mentioned earlier, you often hear the term "database" being used to
05:33describe the software product you're using such as Oracle or SQL Server.
05:37My database is Oracle.
05:38My database is SQL Server.
05:39Well that can be a little confusing when you're first learning this.
05:42Again, SQL Server is a database management system.
05:46We use it to create and manage different databases.
05:50One SQL Server can manage many different databases inside it.
05:55In the diagram, you actually often see these databases represented as cylinders.
05:59But a great thing about it is these cylinders are significantly transferable.
06:05Once you know one, it's easy to work with the others because all these
06:08SQL-oriented databases, and there are dozens, kind of work the same way.
06:13They have the same core idea at heart.
06:15Your data is stored in these different databases.
06:19The databases themselves are built of one or more tables.
06:22Tables consist of columns and rows.
06:25Now we're going to go into the construction of databases a little later.
06:29I'm expecting you probably have some exposure to these concepts.
06:33If you've got some database experience with a desktop database application like
06:38Microsoft Access or FileMaker, that's great knowledge.
06:41But one key difference is that those are single self-contained applications.
06:47When you open up Access, it's what you use to create the database, to enter data
06:51into it, to manage it, to create reports on it, even to install it on the
06:55desktop of your end user.
06:57SQL Server is not like that.
07:01It's better to think of SQL Server as an ecosystem.
07:05It's not one program, but it's a collection of different components to choose from.
07:09The core database engine is first installed, typically on a dedicated server or
07:14even a dedicated group of servers.
07:17And then there are a variety of different services and tools that you can
07:22choose or you can ignore if you don't need them.
07:25Major optional components of SQL Server include reporting services.
07:29This is for creating and distributing reports based on your data.
07:33Analysis services for really deep data analysis and business intelligence.
07:37There is integration services talking between SQL Server and external sources
07:43of data, other applications, other databases.
07:46Now on top of that, you have a collection of different applications that you can
07:50install, or not, for working with this stuff.
07:54Those include SQL Server Management Studio.
07:56This is the big one for creating and managing your databases.
08:00There is Configuration Manager to say what parts of your DBMS are turned on and
08:05who can talk to them.
08:06There is Business Intelligence Development Studio for creating complex analysis
08:11and integration projects.
08:13Even have things like Visual Studio if you're a developer.
08:15Now if you wanted to be a SQL Server DBA, a database administrator, you would be
08:21expected to know all of these and more besides.
08:24Now one thing that often surprises people coming from desktop database
08:29applications is that SQL Server does not provide any kind of application
08:34targeted at the end user.
08:36There is no SQL Server data entry program.
08:39The idea is that you will build that yourself.
08:42You will provide a website, or a desktop application, or a mobile application
08:46that's going to be specific to the database you create.
08:50Now we're not going to focus on application building in this course.
08:53We are going to talk about these components.
08:56The core of the database itself and the things that you use to create and
09:02manage your databases.
Collapse this transcript
SQL Server editions
00:00In this course, we're going to work with Microsoft SQL Server 2008 R2.
00:06This is the latest version of SQL Server, released in April 2010.
00:11R2 added several features on top of the first release of SQL Server 2008, although
00:17the purposes of an Essential Training course like this,
00:20the R2 part really doesn't matter.
00:23In fact, most of the core things we're going to talk about are very similar to
00:27even earlier versions like SQL Server 2005.
00:31But SQL Server 2008 R2 is the latest version.
00:35It's what we're going to use.
00:37Now although, SQL Server 2008 R2 is the version that we're using,
00:42the capabilities of it will differ depending on the edition of SQL Server 2008 R2
00:48that you have, because this product has been around for 20 years.
00:53The same way that Microsoft Office is now available in different editions,
00:56SQL Server has fragmented into about ten editions with different feature sets for
01:02different audiences.
01:03So there is a Compact edition for mobile devices.
01:06There is a Web edition specific for making web sites.
01:10There is an Express Edition, which is free.
01:12There is the Azure edition for hosting your databases in the cloud, all the way
01:16up to super high-end versions for data centers and data warehouses that can
01:20handle hundreds of terabytes of data.
01:24Now rather than go through some kind of matrix of the different editions
01:27feature-by-feature, which I can assure you is not that helpful right now,
01:32I suggest that you just forget about all the different editions, because we're
01:35interested in one of them.
01:37SQL Server 2008 R2 Standard edition.
01:42This is the classic edition of SQL Server.
01:46So in this course, I'm not going to talk about the super-advanced,
01:49super-expensive edition for data centers, nor am I going to talk explicitly
01:53about the cut-down version of SQL Server intended for being embedded on a mobile device.
01:58We're working with the Standard edition.
02:01The core features of SQL Server found in the core product.
02:04If you have a different edition, that's okay, because everything you'd
02:08learn from the Standard edition can be taken to the other editions later on if you need.
02:13Now while that is the focus of this course, there is one more edition worth mentioning.
02:17SQL Server 2008 R2 Express edition.
02:21This Express edition is free to download and free to use, so you can install and
02:26run it on your own desktop or your laptop.
02:29It's great for developing small applications and websites, for experimenting, and
02:33just for learning how to work with SQL Server and database development.
02:37so we'll see that one too.
02:38Of course, there are going to be differences between the Standard and Express editions.
02:43As you might expect, the free version does not have as many features as the full version,
02:47but Express is surprisingly full-featured and it's not going to be
02:51until later in the course that we'll see where Express leaves off.
02:54I'll be very clear where that happens and where the differences are.
02:58So SQL Server 2008 R2 Standard edition is the one we'll concentrate on in this course,
03:05and it's the one we need to understand.
Collapse this transcript
Applications included with SQL Server
00:00When you're new to SQL Server you'd be forgiven for thinking that there is one
00:04program, one application that manages all of it.
00:08But if you could look at the desktop or the laptop of a typical SQL Server
00:11database administrator, you'd see a bunch of installed Microsoft programs that
00:16they would consider to be just part of their SQL Server 2008 toolkit.
00:21In fact, these that I'm looking at right now are the programs that you'd see
00:25on your Start menu after a typical SQL Server 2008 R2 Standard edition installation.
00:33Although the SQL Server database engine itself might be installed on a separate
00:38machine in your back office or data center or even in the cloud, these programs
00:43are what you'd want to have on your desktop or laptop.
00:46They are what you're going to use to connect to the SQL Server machine.
00:49Now you could ask which one is the most important and of course I'd say that
00:53they're all important.
00:55But for this course we're initially interested in three of the available tools
01:00and really in one of them more than anything.
01:03Here is the main one, SQL Server Management Studio.
01:07This is the program we'll spend most of our time in.
01:10We'll use it to connect to SQL Server, even to different SQL Server machines.
01:15We'll use it to explore what's on the server.
01:18We'll create databases with it.
01:20We'll work with security with it.
01:21We can edit the data in those databases.
01:24We can write SQL to administer our database.
01:27We'll spend a lot of time in SQL Server Management Studio.
01:31Now at some point, you're also going to need to use SQL Server Configuration Manager.
01:36Now this is actually a small program.
01:39It's small but it's essential.
01:41This manages which parts of SQL Server are running.
01:44What other machines can talk to SQL Server and what network protocols are allowed.
01:49It lets you start and stop the different pieces of your SQL Server machine.
01:54Now after SQL Server has actually been installed and configured for the first time,
01:58you probably won't need to use this all that often, but you will run
02:02into it now and again.
02:04You should also have access to SQL Server Books Online.
02:09This is the help system.
02:11Now wait, wait before you dismiss this.
02:13Yes, we've all encountered help systems that aren't worth the name, where you
02:17just end up going out to the web to find the assistance that you're looking for.
02:21But SQL Server Books Online is one of the best technical help systems you'll
02:27ever come across and it's often underestimated by beginners.
02:30It's well thought out. It's huge.
02:33It covers everything from installation and setup to using and creating your databases.
02:39It's both a complete reference of every little piece of SQL Server and it's full
02:45of tutorials as well.
02:46You'll find that when you need to look things up, you'll often go to Books Online first,
02:50instead of out to the web.
02:52Now you can find a web-based version of SQL Server Books Online but I'm a big
02:58fan of having it installed on my desktop as it's easier and faster to use it,
03:03to navigate and search through it, and as you get more advanced you'll use even more
03:08of these available tools.
03:09Now there are several tools that a database administrator might use that we
03:13won't be using in this course.
03:15Things like the Database Engine Tuning Advisor or the Analysis Services
03:20Deployment Wizard, as they are beyond the scope of this course and we don't need
03:24to know them to get moving with SQL Server.
03:26But the ones we do need to worry about right now are SQL Server Management Studio,
03:31SQL Server Configuration Manager, and SQL Server Books Online.
03:35So how do we get these if we don't have them already?
03:38If you're going to be installing SQL Server yourself, you can just choose
03:42to install all of these tools during the full install and we'll see that a little later on.
03:48If you already have a database engine installed somewhere else and you just need
03:53the tools to talk to that and work with it, you can run through a regular SQL
03:58Server install but you have an option to just install the tools.
04:03Now to get to this you will still need the regular SQL Server Install Media,
04:08the DVD or the ISO file.
04:11If you have an MSDN or TechNet subscription, you can download it from there.
04:15If you're working with the free SQL Server Express editions you'll find your
04:20download options at www.microsoft. com/express/database and the default
04:28downloader that you can get here installs the database and the management tools.
04:33It's a cut down version of SQL Server Management Studio called SQL Server
04:38Management Studio Express.
04:39You want to take care with this because you will find that SQL Server Express is
04:44available in a variety of editions ,some which are just the database only, some
04:49are just the management tools, some are both of them together and some with
04:53advanced services, which includes reporting services.
04:56We'll see this when we get to the install.
04:58SQL Server Express edition doesn't include Books Online but you can
05:03download that separately and in this case you would go to
05:06microsoft.com/downloads and just search for it.
05:11I'd be careful here that I would search for "books online 2008 r2," because
05:19whenever you're downloading anything from Microsoft do pay close attention to
05:23the version numbers. Because there are so many versions of SQL Server that still
05:27need to be supported,
05:29it's very easy to end up downloading a SQL Server 2005 program.
05:33So again everything I'm being careful to look up is not just 2008 but 2008 r2.
05:41But with all that in mind we can go ahead and get ready for the install.
Collapse this transcript
2. Installing SQL Server 2008 R2
Preparing for installation
00:00If we're going to learn SQL Server then we need to have both SQL Server and all
00:05the necessary tools to talk to it and administer it.
00:08Now I'm going to imagine that you fall into one of the three categories here.
00:13First is the idea that you're going completely from scratch.
00:16You have nothing installed at all.
00:18You have no existing SQL Server, anything.
00:21Now way at the other end is the idea that you have everything already
00:25installed successfully.
00:27You not only have a SQL Server 2008 R2 database server to talk to, you also have
00:32all the client tools installed and ready to go. SQL Server Management Studio,
00:37SQL Server Configuration Manager, Books Online, all of that stuff.
00:41If so, you can actually just go skip ahead to the next section.
00:45If you're someone in-between, say you know that you have a SQL Server database
00:50you can get to but you have no management application yet, well, I'm going
00:54to talk about going completely from scratch and you can just pick and choose
00:58the bits that you need.
00:59Now to be very specific about this, what we're going to do here is go through the
01:03process of getting a local SQL Server installed, up and running for development,
01:08for testing, and for just learning SQL Server.
01:11I'm not going to tell you how to set up your production server.
01:14That would be a very different discussion involving lots of white boarding about
01:19budgets and service level agreements and expected usage numbers.
01:24So what do you need to go ahead and install it? While the Standard and the
01:27Express versions of SQL Server 2008 R2 will install on most reasonably
01:33up-to-date Windows operating systems,
01:34it could be Windows 7, Windows Vista with Service Pack 2, even Windows XP with
01:41the most recent service packs.
01:43In a production environment of course, it's typically installed on a server
01:47operating system like Server 2003 or 2008.
01:51During this course I'm going to install it on a 64-bit machine running Windows 7,
01:56but again for this course whatever you can install it on will work.
02:00So, how to install this thing?
02:01Well, if you don't already have a DVD or some other install media you'll need
02:06to find the installer.
02:08If you have a valid MSDN or TechNet subscription you can log on and download it,
02:13though it is a 4 GB download.
02:15So it might take a while.
02:16You'll find that there are 32 and 64-bit installers for both of the Standard and
02:21the Express editions.
02:23For this course you won't see a difference choosing between those, but if
02:27you're installing on a 64-bit operating system you might as well get the 64-bit installer.
02:32If you're not, get the 32-bit installer.
02:35Now if you are planning to install the free Express edition rather than the
02:39Standard edition, you don't even MSDN or TechNet account.
02:43As mentioned before the most direct way to get to this is to go to
02:47www.microsoft.com/express/ downloads, as the choices are clearest here.
02:55You will find on this page you will be suggested to grab the 32-bit or 64-bit
03:01version of this free edition of SQL Server 2008.
03:06You do have some other installation options and you'll find there is actually
03:09four versions you can download, one that is the Database Engine only.
03:14One that is being Management Studio Express only.
03:17One is both the database with the management tools and the biggest download is
03:22the SQL Server 2008 R2 Express edition database with Advanced Services.
03:29This is the one that I suggest that you get, because it is the biggest.
03:33It includes the most features.
03:35So grab whichever installation media you need and once you have that we're ready
03:40for the next step, which will be creating our service accounts.
Collapse this transcript
Creating service accounts
00:01Next up is an optional step, but it's one I want to spend a couple of minutes on.
00:05You see once SQL Server is installed, it won't just be running as one piece.
00:10As I mentioned before, it will really be running in the background as several
00:14different components and the recommended best practice is that each of these
00:18components has its own unique identity that it runs under an account that was
00:23created just for that piece.
00:26Now it's certainly arguable that you don't need to go to all this trouble
00:29when setting up a simple development SQL Server, that you can just give them
00:33all the same account.
00:34But I want to show this process.
00:36So I'm going to go ahead and set up five accounts.
00:40One for the core database engine, one for the part of SQL Server called the SQL
00:45Server Agent, and that handles a lot of background jobs and processes to keep
00:50everything running, and three for the big junk pieces, Reporting Services,
00:55Analysis Services and Integration Services.
00:59While there are a couple of other components of SQL Server that could have
01:03their own accounts, like full text search and the SQL browser, I'm going to use
01:08one of the built-in low-privileged accounts on this machine when it comes to the installed.
01:13So I am on Windows 7.
01:15I'm going to go to the entry for My Computer, right-click it, and select Manage.
01:22This might be a little different if you're on another operating system. I'm going
01:26to expand my Local Users and Groups.
01:28Now this machine that I'm on is not connected to a domain.
01:32So I'm just creating local users.
01:34I need to create five accounts here.
01:37I'll right-click, saying New User, and make the first one.
01:40I will call this one SQLEngine.
01:43We give it a password, whatever you want there.
01:46This is not a real user, so they can't change the password of the next logon.
01:51So I'm going to uncheck that and select that for our purposes Password never
01:55expires is OK, and click Create.
02:00I'm just going to repeat the process for the other four accounts.
02:04Now it doesn't matter what the names are.
02:07It's just useful if when you're later looking at these you can recognize why a
02:12particular account was set up in the first place.
02:15So I now have my five accounts ready to go.
02:17You'll see that during the install I will have a chance to plug these into the right places.
02:23I don't need to make any security changes to these accounts as the SQL Server
02:27install will grant any necessary privileges during that install procedure.
Collapse this transcript
Installing SQL Server
00:00So I am going to go ahead and get started installing SQL Server 2008 R2 Standard edition.
00:07I have the installation media for a regular standard install here. I'm going to
00:12launch the setup application.
00:15Now I would often tell you to go and grab a cup of coffee while doing this but
00:19there'll be plenty of time for that.
00:21Installing SQL Server is a long install procedure and there are many steps to it.
00:26It's not something to try and do in a hurry.
00:29So let me tell you a little bit about what to expect here.
00:32If you have a setup that is magically identical to mine in every single way,
00:38then you will have a similar experience to me.
00:41But if your machine or your environment differs in the slightest, you will have
00:46a different experience installing SQL Server.
00:47So if you have a different operating system or a different set of applications
00:52installed, you might expect to see a different message now and that's okay.
00:56It's just part of the deal when installing SQL Server.
00:59In fact, the first thing that opens here is not some kind of
01:03Next, Next, Next install.
01:04It's called the Installation Center and it's on the first page, which is
01:08Planning, and there are whole bunch of different things.
01:12Documentation for requirements and Setup Documentation and Upgrade Advisors.
01:17If you've got old versions, Upgrade Documentation.
01:20What I'm going to do here because I'm on a fairly fresh machine is just run the
01:24System Configuration Checker and it's going to go through a whole bunch of rules
01:29just to give me some idea whether I can go ahead and install this.
01:33So just take your time, run this, pay extra attention to any messages you get
01:37during that installation.
01:39You may have to install some prerequisite software if it's not on your machine already.
01:48So this runs through a whole bunch of rules.
01:50It says it's Passed: 15 and Failed: 0.
01:52I can click for the details and I can see the type of things it's checking.
01:56Things like whether the computer has been restarted recently, does the operating
02:00system work, are there any problems with incompatible SQL Server versions?
02:05Everything looks good, so I'll click OK.
02:07That didn't do anything except run a few checks.
02:10We'll still have a few more checks to go on as we go forward.
02:13I am going to select the Installation section here on the left-hand side and
02:18over there I'll say I want to do a new installation.
02:22There obviously are different ideas and different options if I'm installing on
02:26top of say SQL 2000 or SQL 2005.
02:28But it's a new one for me and before it does its thing it's going to make me
02:35go through a whole bunch of different questions about how this is going to be installed.
02:41It first runs its own set of rules.
02:43I can click Show details.
02:45Everything looks good. Click OK.
02:47Then it gives me the opportunity to enter in my product key.
02:54Also here you could actually choose to install an evaluation or trial
02:58edition without a product key or even select the Express edition or Express
03:03with Advanced Services.
03:04I am just going to go ahead and do a regular install, so I'll click Next.
03:07I will accept the license terms and usually that would be about it, but we still
03:12have a long way to go.
03:14What SQL Server it will do now is install support files.
03:18This is not at doing the proper install.
03:20It takes longer than most installs you would do but this is just some supporting
03:25stuff in the background.
03:26So I am going to click the Install button to go ahead with that and that will
03:29take a couple of minutes.
03:35When it's done it will appear to disappear for a second but it should come back
03:38in just a moment with the results of those support files.
03:45What I can see here is most things have been passed. I do have a warning here
03:49besides Windows Firewall and if I click that it will tell me that it's detected
03:54that the Windows Firewall is enabled.
03:56And that will be a problem if I was expecting someone else to be able to connect
04:00to the SQL Server on this machine, if I am shutting them down with my firewall.
04:04It's okay because this is a development machine so I am going to say that's not
04:07a problem and go ahead and click Next.
04:10Next up, we have a choice . Are we installing SQL Server by selecting the
04:15different features, the different component pieces of it, or do we want to
04:19install all features with defaults?
04:21The second option here would be the easy button but I'm not going to do that.
04:25I am going to step through the feature level installation. Even though I am
04:29going to install everything I want to show that part to you.
04:33So click Next into the different features and this is where we see that yes
04:37indeed, SQL Server is installed as our whole bunch of different pieces.
04:42The database engine itself and the different parts of that such as
04:45Replication and Search.
04:47Analysis Services, Reporting Services, then a lot of different tools like Books Online
04:52the Business Intelligence Development Studio, the Management Tools.
04:56In fact I am just going to go ahead and click the button to say Select All.
05:00I am not going to touch any of the paths that this will be installed on.
05:05Just accept the defaults there and click Next.
05:12It runs one more in a whole bunch of checks that it's running through and
05:16everything looks okay. I'll click Next.
05:19What it's asking me now is what's the name of the Instance. And what does this mean?
05:24Well, you can actually install multiple instances of SQL Server and each
05:29instance can contain its own set of databases.
05:34It's not something you have to do but you can have up to 50 instances of SQL
05:38Server on one machine.
05:40If you do, you have to give each of those 50 or 5 or 10 a different name.
05:46But if we're installing it from scratch we can say I don't need to name this
05:50instance because I'm not intending to put any others on.
05:53So I'm just going to call it the default instance.
05:55That means later on if I want to access this instance I don't really have to
05:59care about what it's called because it's the default instance on this machine.
06:03I can see there are no existing instances here.
06:07So I will go ahead and click Next.
06:11It gives me a summary of the amount of space it's going to take up,
06:14not something I really care about if I've got plenty of disk space available and I'm
06:19not expecting this database to grow very large. I'll click Next.
06:24What we have now is the place that I can plug in those service accounts that I
06:29created a little earlier, giving different identities to each part of SQL Server
06:35that will be running.
06:36So I'm going to go ahead and type in the account names that I had set up
06:39earlier, the first one being for SQL Server Agent and give it the password
06:44that it knows about.
06:47Then the database engine, which for me was SQLEngine, and go ahead and enter the
06:52remaining service accounts.
06:54I am going to leave the last two just setup as using the built-in local service account.
06:59This is a fairly low privilege account, which is fine for those.
07:02The tab behind it is something called Collation.
07:05I am not going to touch that.
07:06Collation is the idea of how does SQL Server do things like case sensitivity and
07:12accent sensitivity behind the scenes.
07:14If you are ordering a bunch of last names of people, does it care whether it's
07:19an uppercase A or a lowercase a?
07:21Does it care if it's an a or an a with an accent over the top of it?
07:26I'm going to accept the default, which is just for this case a SQL
07:30Latin1-General case-insensitive, accent-sensitive, and just not touch it.
07:35But if you have to handle multiple languages, you may have to do
07:38something slightly different.
07:40So I'll go ahead and click Next and it asks me how the security is going to be
07:46set up on this instance of the database, and it's saying the authentication mode
07:51is Windows authentication or Mixed Mode.
07:55Windows is the default and it means that everybody who's going to get access to
07:59these databases has to have a Windows account and it's by that that we will specify
08:04what they're allowed to do.
08:06Mixed Mode means there could be either a Windows account or you could actually
08:10set them up their own username and password within SQL Server.
08:14It wouldn't be a Windows account.
08:16Now the default and the recommendation for Microsoft these days is stick with
08:20Windows authentication mode because it's a bit more secure.
08:23But in some cases, say when you're building web applications you may be working
08:27with Mixed Mode and you get to set up different kinds of accounts.
08:30I'll leave it at the default, and the only thing I'm going to do on this screen
08:34is down here where it asks me to specify the SQL Server administrators.
08:38I am going to say Add Current User.
08:40Yes, I want to be added as an administrator on this box.
08:44I am going to leave the other two tabs of Data Directories and FILESTREAM, which
08:48name both where these files are going to be installed on the machine.
08:52FILESTREAM is a new feature in SQL 2008 that allows you to store large files,
08:58even bigger than two gigabytes, on the file system and still manage them through the database.
09:03I'm not going to touch this right now. It's little bit beyond the scope of this class.
09:07So with Windows authentication mode set and me listed as the user I can go ahead.
09:13Just to let you know, you can actually change the authentication mode later.
09:16So it's not a disaster if you have the wrong one.
09:19Next up, it's asking me to specify the administrators for Analysis Services.
09:25This isn't something we've talked about a lot yet, but what I'm going to do is
09:28just go ahead and click that Add Current User button again and name myself as
09:33having unrestricted access there and just go ahead and click Next.
09:37In this screen, we're being asked to set up Reporting Services, this part of SQL
09:42Server that can handle reporting.
09:46There are three options here.
09:48Installing the native mode default configuration, getting it set up from scratch
09:52to install with SharePoint, and then installing it but not configuring it.
09:56I am again going to accept the default one, which is the first option, and click Next.
10:02I am going to choose not to send any error reports to my corporate report
10:08server and click Next.
10:10It runs through a few more checks.
10:12I am going to say Show details.
10:14Everything looks okay.
10:17Click Next and it gives me a summary. It's going to install all these
10:22different features.
10:23These are the names it's going to use.
10:25These are the accounts that it's going to use. As you can see,
10:27there is a whole lot of stuff going on here. We're ready to go.
10:32I'm going to go ahead and click Install and this will take several minutes.
10:37So I'm just going to go ahead and let it do its thing.
10:44So later on, perhaps much later depending on the speed of your machine, but
10:49certainly after many minutes you should hopefully get to this screen, that your
10:53SQL Server 2008 R2 installation completed successfully.
10:58It will give you a log file if you feel like you need to take a look at it.
11:01Obviously if there are any alternate messages, you get anything else, you need to
11:05go and take a look at that too.
11:07But hopefully that should do it.
11:10Going back to the Installation Center we can also run a search for product updates.
11:14I am not going to do that right now but it's certainly something that in the
11:17long process we should do.
11:19So we now have SQL Server 2008 pretty much up and running.
11:22If I look at my All Programs I should find a whole section under SQL Server 2008 R2
11:28with the Analysis Services Tools, Configuration Tools, Documentation,
11:34Integration Services and Performance, and we're going to go through quite a few
11:38of these as we go through the course.
11:40But we're ready to go.
Collapse this transcript
Post-installation checks
00:01So it'll be useful to prove to ourselves that we're actually have a database to deal with.
00:05So, how do we do that?
00:06Well, opening up our SQL Server programs there is a couple of things we can look at.
00:10If I'm really just interested in the different parts of SQL Server and how they're
00:14running, I could go to my Configuration Tools section and open up SQL Server
00:19Configuration Manager.
00:21Now what this will do is allow me to see those different components, which ones
00:26are running, which ones are not.
00:28In these SQL Server Services I'll see that I have the actual SQL Server engine
00:33running under the identity of SQLEngine.
00:36We've got an Analysis Services as running.
00:38The SQL Server Browser is not and that is by default it doesn't
00:42automatically start.
00:43Integration Services is running.
00:45The Agent is not running.
00:47It is stopped, because it's a manual start.
00:50The Full-text Filter Daemon is running and so is Reporting Services.
00:54This would be the screen where you could do things like configure whether they
00:59start up automatically or manually, start them or stop them, change the identity
01:05that they're running under.
01:06For us, we don't really need to touch any of that right now.
01:09The one other thing that you're likely to do is in the SQL Server Network
01:13Configuration, and you could have two entries for that depending on if you're 64 or 32 bit,
01:17it will tell you the protocols that were running.
01:20How can the other machines talk to us right now?
01:23By default we're going to have TCP/IP as Enabled.
01:27That's the default way people will connect to this machine.
01:30If I double-click it, it will tell me a few entries like is it enabled yes or no
01:34and what IP address is this on.
01:37By default the TCP port for SQL Server is 1433.
01:42That can be changed, but that's just what I've expect to see right now.
01:46But of course we're not looking at anything.
01:48It seems to say to us that we've got a lot of background services running, but
01:51what do we actually have.
01:52Well, to see that I'm going to go to the SQL Server management tool, which will
01:58be in Management Studio.
02:00So, opening up SQL Server Management Studio.
02:02What it's going to ask me to do is say, well, which database engine do you
02:06want to connect to?
02:07Because we could use this one program to connect to a database installed on
02:12our local machine, to one in our back office, even one that's out there over the
02:17web and in the cloud.
02:18Well, I just want to connect to the local machine.
02:20So I can accept the default server name, type in the name of the actual machine
02:24that it says, or even just put a single dot here and say Connect.
02:30What that means is yes, we're actually connected to this machine right now and
02:34I can expand over here my Databases section.
02:38That seems to say that I've got some System Databases and if I expand that I've
02:42got master and model and msdb and tempdb.
02:46We'll talk about those in a minute.
02:47Couple of other databases that are being used by the Reporting Services part,
02:52but that's about it.
02:52If you're expecting to find some sample data, well, you're not really going to,
02:58because SQL Server doesn't install any template databases or any samples for you to look at.
03:03But you can get that and we'll see how to get some data to look at in just a minute.
Collapse this transcript
Installing sample databases
00:00Although SQL Server installs a lot of pieces during that whole long-winded
00:05install procedure, sample databases are not one of them, but we can get them.
00:10I'm going to open up a browser and go to the address
00:13sqlserversamples.codeplex.com.
00:18Codeplex.com is a web site where a Microsoft put a whole bunch of extensions and
00:23downloadable options you can find for SQL Server and other things.
00:28And on this page, you will find product code samples for SQL Server 2008,
00:31but the things that I'm looking for are the AdventureWorks 2008R2 sample databases.
00:38If you've been working with Microsoft technologies for a few years, you might be
00:42familiar with the classic sample databases that Microsoft have had like
00:47Northwind and Pubs.
00:49AdventureWorks is the company that they came up with a few years ago and I'm
00:53going to go and click the link that says they can be downloaded from here.
00:58They're a lot bigger than the old Northwind and Pubs sample databases ever were.
01:05And if I agree to this, I'm going to download these AdventureWorks sample databases.
01:12And once it's downloaded, I'm going to go ahead and run this.
01:15Of course, we don't have to install these databases, but it will give us
01:18something to play around with when we are writing bits of SQL later on.
01:29It will unzip a bunch of files and then run just a little installer that will
01:33allow you to go ahead and basically import those.
01:37It's not quite the right word, but it'll do for the time being, import them into
01:42our default installation of SQL Server.
01:45You'll notice that there is a few markers here, that there are certain things
01:49that say manually deploy via BIDS after install.
01:53These are for the more advanced OLAP databases for analytical processing being
01:58use by Analysis Services.
02:00I'm not really going to attach these in this course.
02:03What I'm really just interested in are the AdventureWorks OLTP and perhaps the
02:08data warehouse ones.
02:10I'm going to go ahead and install.
02:14And after several minutes you should get to the finish screen.
02:18I'm just going to hit Finish and we should be done.
02:20Of course I want to prove this, so I'm going to close down my browser and go
02:24back and open up SQL Server Management Studio, connect to the local machine, and
02:33expand Databases and see what we can find, and there we go.
02:36We've got AdventureWorks, AdventureWorks data warehouse, and the light versions of it.
02:41And I could even start to expand these and now I'll see inside AdventureWorks.
02:45I have tables, which I can expand, which will have data about employees and
02:50people and productions and bill of materials.
02:52This is all sample information of course.
02:57I could select the HumanResources.Employee table.
03:00We'll talk much more about what this is in the moment.
03:03I'm going to right-click it and save Select Top 1000 Rows and I'll finally
03:08see that we have indeed some data that's being stored in our SQL Server
03:13database that certainly seems like we could start to manipulate it and play around with it.
03:18So even though we're going to get on to creating our own databases, having the
03:23AdventureWorks databases in here in SQL Server will certainly make it useful
03:27when it comes to talking about examples of retrieving and updating information.
03:33Now you'll notice that this particular SQL Server, what we call the instance
03:38of SQL Server and this is the default instance, itself is containing multiple databases.
03:45In this case we have AdventureWorks is a database that itself contains dozens of tables.
03:52AdventureWorksDW is another one.
03:54AdventureWoksDW2008R2, AdventureWorksLT, ReportServer, ReportServerTempDB, and
04:01we have the system databases.
04:03And it's an important distinction to make, because it's very common that people
04:06talk about SQL Server as if it's a database.
04:10They say that very generic term.
04:12And of course SQL Server is not a database.
04:15SQL Server is your database server that itself can contain dozens of databases.
04:21You might only be interested in making one database for yourself, but you could have many.
04:26So there is very much a distinction between the databases you have and the
04:31instance of SQL Server that holds those databases.
04:34And because these databases are independent of each other, if you wanted to,
04:40you could go through and delete them or do what's called detaching them, meaning we
04:45just don't care about them anymore.
04:47I'm not going to do that right now, because I do want to work with them later
04:50but you certainly could do that later on.
Collapse this transcript
3. Tools and System Databases
Introduction to SQL Server Management Studio
00:01Let's take a look at SQL Server Management Studio.
00:03This will be our main toolbox for working with a SQL Server, and since I'm going
00:08to find it under my SQL Server 2008 R2 programs, I'm going to right-click and
00:14just pin this to my Start menu.
00:16It makes it easier to get to.
00:17Now this tool is not intended for the end user.
00:22It is an administration tool.
00:24It will help us create and change databases, diagram them, report on them.
00:29There is a lot of stuff it can do.
00:30Now there is no real point in opening SQL Server Management Studio without
00:35connecting to a SQL Server instance.
00:38So that's the first thing you'll be asked to do.
00:41And again, you can connect this to different SQL Servers as long as you know
00:45where they are and what credentials to use to get access to them.
00:48I am connecting to the local machine here so I am just going to use a dot
00:52for the server name.
00:53I had installed it using Windows Authentication and I had named myself as the
00:58administrator of this instance.
01:00So once I connect, I'm in and I can do anything.
01:04Now the most obvious section and the most useful initially is this area
01:09over here on the left.
01:10It's called the Object Explorer, but the name is not really important.
01:14This is the section that lets you drill down into different parts of the SQL
01:19Server instance you're connected to. Not just the databases and the data inside
01:24them but administrative stuff too.
01:26What are the users and the user identities that SQL Server knows about?
01:31What roles does the database understand?
01:34How for example would you say that a user called Bob should be allowed to edit
01:40the Orders database but not allowed to edit the HR database?
01:43That stuff is all defined in here.
01:45Now when you're clicking around here, what you can actually do is see a little
01:50more information by selecting the Object Explorer Details pane from the View
01:55window and when I have say my databases selected, I don't just see the names of them
01:59but I can see their owner and their collation if I find that useful.
02:04But more than anything you will find that right-clicking is your friend in SQL
02:09Server Management Studio.
02:11You can select pretty much any of these levels.
02:14Right-click an object in them and you'll see a collection of
02:18context-sensitive options.
02:20If you, for example, select a database like the AdventureWorks database, you can
02:23drill down into Tasks and see that you can take it offline or copy the database
02:30or import data or back it up or restore it.
02:34One of the common things you are likely to do with databases, you'll be curious
02:38of how much space they take up.
02:40So you'll find that right-clicking on the database allows you to go to Reports
02:44where you have a whole bunch of standard reports that says Disk Usage, how much
02:49disk usage is this database taking up.
02:51Drill back down into Reports and I'll find things like Disk Usage by Top Tables,
02:57generating lots of really useful information for that.
03:01On the other hand, if you start to drill down inside the databases themselves
03:05and actually start to drill down into the tables, the things that hold your
03:09information, the right-click choices here will be different.
03:13You'll have things like select the top 1000 rows or even edit the top 200 rows.
03:20You can even right-click the Tables and say Design, allowing you to change the
03:25table and change what it actually means to be this data and we'll see all of
03:29this a little later on.
03:30I do encourage you to explore around the Object Explorer for a few minutes if
03:35you haven't already.
03:36Now of course as with any advanced development environment, take care when you're doing this.
03:43You are working directly with the database instance and if you decide to tell
03:49SQL Server Management Studio to delete a database for example or change a table,
03:54it will say "Well, you're the boss" and it will do it.
03:58One example of the place to take care might be, for example, Database Diagrams.
04:03If you start working with some of these sample databases you'll see a Database
04:07Diagrams section and this is very tempting to play around with.
04:13Typically the first time you would click them you may be prompted that the
04:16database doesn't have everything in place to use this.
04:19Do you want to create that?
04:20I'm going to say yes I do.
04:22Let me close the couple of reports I have opened.
04:24What that then allows me to do is make a new database diagram based on the
04:30actual defined pieces of that database.
04:32In this case, I'm doing it on the database called AdventureWorksLT and I see
04:37that I have a whole bunch of tables here and I can just start clicking Add to
04:43add them to my diagram.
04:44Maybe I'll err on the side of just adding everything right now.
04:49If I close that, I see that I've got this rather large diagram going on here.
04:55In fact, up at the top of the screen I can shrink that down.
04:58Let's say it's 50%.
04:59It's still a little big so I can drag a few things around if I so desire.
05:05But what I'm trying to show is the point that this is really-really good stuff
05:11and the ability to diagram your database reading from the actual structure of it
05:15is a great feature but again, be careful.
05:17It's very easy to say look at a diagram like this and realize that you don't
05:22need to show everything, you'll right- click one of these options and you'll
05:26see that there is the option Remove from Diagram, right next to Delete Tables from Database.
05:32And the end result of selecting these things would be very different.
05:37So again, take care.
05:39Some great features, but be careful with them.
05:41Of course, everything that we are actually doing with this SQL Server Management
05:47Studio interface is generating commands that talk to the SQL Server instance and
05:54if you are someone who lives and breathes command line operations, you can
05:58actually open up something called SQLCMD, just typing in SQL command, and that
06:04will open a command prompt to allowing to talk to the SQL Server instance.
06:08I could do a couple simple things here.
06:10I am going to say use AdventureWorks... And all I'm doing is typing a couple of
06:29simple commands to shift the SQL Server Instance to look at the AdventureWorks
06:34database and then show me the top 10 first names from one of the tables.
06:40Now I'm not intending to spend any significant time in the command line prompt
06:45here in this course.
06:46We're going to use the graphical interface you can always take what you get
06:50there and put it in the command line later.
06:53However, we still are going to write a fair amount of code ourselves. We are
06:57just going to do it in SQL Server Management Studio, which is a much more
07:00pleasant way to do that.
07:02It does also have some helpers for us.
07:04If I go to the View menu and select something called the Template Explorer, I am
07:09going to see this section appear over here on the right.
07:12This is hundreds of pieces of boilerplate code we can use for the most common
07:17tasks in SQL Server.
07:19Everything from creating a database for example, which starts to script it out there,
07:25we just go and fill in the blanks, all the way to advanced database needs
07:30such as creating partition schemes, which can be a bit more complex.
07:36Now if the idea of writing a lot of this code fills you with dread, we will see
07:41that quite a bit of our creation can also be done using the graphical user
07:45interface, but you will always be required to kind of come back and write a few
07:51commands that will be executed by SQL Server Management Studio.
07:55We will of course be using a lot more pieces of this application as and when we
07:59need them, but with these core ideas to get started we can move on and start
08:04exploring one more tool.
Collapse this transcript
Introduction to SQL Server Books Online
00:00When you are new with the SQL Server, I can assure you that it's a really good
00:05idea to have an easy way to get to SQL Server Books Online.
00:10In a default installation you'll find under the Documentation and Tutorials
00:15section of SQL Server 2008 R2.
00:16I am actually going to right-click this and pin this to the Start menu as well
00:22just so I have access to both Management Studio and Books Online very easily.
00:27Opening it up, you'll find that it's really split, at least initially, into a
00:32couple of different sections.
00:34It starts off with this core section talking about the categories of SQL Server
00:39but really, it split over here on the left into the Contents section, allowing
00:43you to drill down into the different pieces, and a very useful index.
00:48You'll probably spend a lot of time looking things up in the index because it is
00:52huge and links you to a whole bunch of different stuff.
00:55Again, you will find that this does not just have a reference to it.
00:59The index is a great reference but inside Books Online, you'll find whole areas
01:05like SQL Server Tutorials, tutorials on using Management Studio, or the SQLCMD
01:10utility, or the Database Tuning Advisor.
01:13So as you start to look through those, you will be likely to find tutorials
01:18in just about everything and you can see then at the bottom of most of the
01:23pages, you even have the ability to send feedback to Microsoft if you
01:26disagree with something.
01:30As you start to drill down into the different parts of it, so for example,
01:35selecting the Database Engine section within Contents, you'll see that you can
01:40browse a lot of this material by life cycle.
01:42Are you really more interested in getting started, are you interested in
01:46security, are you interested in deployment or development?
01:51Not only that but you can drill down into a lot of this content by browsing
01:55it by the particular role. If you're a developer or an information worker,
02:00you're interested in a different kind of information than your typical SQL
02:04Server administrator.
02:06Books Online is huge.
02:08Initially, you'll find it rather overwhelming till your comfortable with making
02:12a way around it but it's a very, very useful application to have.
02:15It will be your go-to source of information most of the time within SQL Server.
02:21When you find parts of it that you think are really, really useful, say for
02:27example, you have drill down into the Information Worker and you found the
02:31section about Accessing and Changing Database Data, you can decide to make this
02:36page a favorite up here in the toolbar.
02:39You have an Add to Help Favorites link.
02:42You click that, and what you'll find is that the third tab along the
02:47bottom on the left-hand side now allows you to switch from the general contents
02:52to the index to your particular bookmarks, your little Help favorites.
02:56That's all I really want to explain to get a started with Books Online.
02:59just get used to keeping this open in the background and referencing it
03:04whenever you can.
Collapse this transcript
SQL Server system databases
00:00Now although when you install SQL Server, you don't get any of the sample
00:04databases like AdventureWorks,
00:06what you will see, if you're looking through SQL Server Management Studio, is
00:10you will see at least four system databases.
00:12Four is pretty typical for a right- out-of-the-box standard installation.
00:17You have got master, model, msdb, and tempdb and I'm often ask how important
00:23are these and are you supposed to do anything with them, are you supposed to change them?
00:28Well in this particular course, we're unlikely to touch any of them at all.
00:32You are just going to leave them alone but to give you the quick runthrough of what they mean,
00:37yes, they are important, they are vital.
00:39The master database, which even sounds important straight off the bat, is the
00:44one database that says what exists in this instance of SQL Server.
00:49If the master database wasn't available, the instance doesn't even start. It won't run.
00:55This says what else is available.
00:58The model database can be changed but you're unlikely to at this level of content.
01:04What model does is it describes the template for a new database.
01:09So later on in this course, when we make a new database, it will actually be based on model.
01:16So say at some point in the future, you had a very specific database
01:20creation process. You might change model so that every new database was
01:25based on your changed design.
01:26msdb is used internally and it's used for queuing and to put some jobs to have
01:33things run in the background such as database mail and a little part of SQL
01:38Server called Service Broker.
01:40We won't really be touching either of these in this course.
01:43The only other one we have here is tempdb.
01:46Again, you might look at the name and think it looks like it's something
01:49temporary and it is.
01:51This is really a scratchpad for SQL Server.
01:53tempdb is used when SQL Server is doing large amounts of calculation and sorting
01:59and it's reordering things.
02:01It uses that as kind of scrap paper to write things on.
02:05If you were to shut down and restart SQL Server, you'd find a tempdb would be
02:09deleted and re-created.
02:12So that's the basic idea of what these four databases are for.
02:15In this course, we won't be touching them, but if you're curious about them,
02:18they are vital.
02:19just leave them alone.
Collapse this transcript
4. Creating Databases
Planning your database
00:00It's very useful to have these sample databases to play around and experiment with
00:05but let's face it.
00:06The main reason we want to have a database server is so that we can have our own database.
00:12But it would be a bad idea to start by just jumping into SQL Server Management
00:16Studio and going crazy with the right-click mouse button.
00:19That is how bad databases get started.
00:21So in the next few minutes what I am going to do is talk about the basics of how
00:26to plan or model your database before we start to build it.
00:30You see while in other areas of development, things like programming and web
00:35development, I'm a really big fan of the iterative incremental design idea
00:40that you build apps quickly, you get them out, you revise them, you add new
00:44features repeatedly over weeks or even days, but that's not what I want to do with the database.
00:51You see building a database is like getting tattooed.
00:54You'll really want it to be correct the first time you do it. Changes are
00:58possible but they are painful.
01:01The great thing is though while version of SQL Server 2008 R2 might be new,
01:06relational databases aren't.
01:09They've been around since the '70s and that's a good thing, as the methods from
01:13modeling a database have been battle- tested over four decades, and to make a
01:18good one, you just follow pretty much a 40-year old process.
01:22It doesn't really even matter what database management system you're using.
01:25Now while you can use a diagramming piece of software like Visio, all you really
01:31need to model a database, at least initially, is pencil and paper and be prepared
01:36to think deeply about a few questions.
01:39A database modeling is not a place to express your inner creativity and find
01:45wild and crazy new ways of doing things.
01:47If you want to get wild and crazy, do it in your user interface, in your
01:50application, but in your database, I am afraid you want to be patient,
01:55methodical, step-by-step.
01:58The first thing we need to do is ask ourselves a few questions.
02:02Number 1, what's it for?
02:05What is the point of this database?
02:07Be careful of the first answer that comes to mind.
02:10Sure, in most cases, you're building a database to support an application,
02:14whether that's Desktop, or mobile, or a web application, but say you're building
02:19an online bookstore.
02:21Well that's way too easy to say that, the database stores necessary product
02:25and order information.
02:27Yes, that might be true but you should be asking what are the goals of the
02:33store, or the website, or the application. Because wherever you wanted to go over
02:38the next year or five should affect what you build right now.
02:42Even having an elevator picture or a mission statement in mind will help you
02:46build a better database.
02:48If the idea behind your online store was "We help customers find books, discover
02:53what others thought about them, purchase and track their orders, contribute
02:57their own reviews and opinions, learn about other products they might like based
03:00on people with similar reading habits," you'll build a very different database
03:05from that description than you would from the first one.
03:09The second question.
03:10What do you have already?
03:12Do you have an existing database, even in something like Access or FileMaker?
03:16If so, well what's wrong with it? Because you don't want to just make the
03:21assumption that you're going to import all your existing data straight into this new system.
03:26There is almost certainly problems with everything you have.
03:29Take the opportunity to fix any problems before you just re-create those same
03:34problems in SQL Server.
03:36Do you have an existing process that this database will replace or help, even a manual process?
03:41If so, get all your physical assets together, printouts, order sheets, filing
03:45cabinets, and people, of course, because understanding the data that you
03:50already have is essential before you can answer the next question, which is
03:55what tables do you need?
03:57Each database you make will consist of one or more tables and these tables are
04:01the basic building blocks of a database.
04:03When you create a separate tables for each entity, that is each object or each
04:09thing that needs to be represented in your system, some of your tables might
04:14represent things that exist in the real world like a customer, or product, or an
04:18employee but others can be more abstract. A blog entry, a comment, an
04:24appointment, or event.
04:26Each of these tables will contain multiple rows of information, whether that's
04:32one employee, or a dozen, or a million.
04:35This is one of those places where you could take a look for inspiration at some
04:40of the databases that are provided as samples.
04:44We can actually expand say AdventureWorks LT, the light version, and look
04:48at their tables here.
04:49They've got SalesLT.Customer, Product, ProductCategory, ProductDescription,
04:54SalesOrderDetail, SalesOrderHeader.
04:55Now if you notice these are the table names and they are prefixed here with SalesLT.
05:01That is something specific to SQL Server here.
05:04It's what's called a schema and it's a way of grouping your tables together into
05:09larger containers if you will.
05:11We are not going to do an awful lot with schemas but to give you some idea
05:15of where that goes,
05:16if we look at one of the larger sample databases, the tables in here are grouped
05:22into several tables under the Person schema, such as Person.Address,
05:26Person.Contact, Person.ContactType.
05:29We have got Production schemas and HumanResources.
05:33So the easy perspective to take is
05:35it's just a way of grouping some of your stuff together. But we're defining
05:39fairly simple tables to begin with and after figuring out what tables need to exist,
05:45you then zoom into each individual table to specify exactly what data
05:52should be stored for each entity.
05:55So in this case for an employee and here you need to be as specific as possible,
06:01defining separate columns for each individual piece of information.
06:06As a rule, you're going to go as granular as possible, not just a name but first
06:11name and last name or even title and suffix if needed.
06:15As you are defining your columns, you're going to define what is the data
06:19type for each column. What is it?
06:21Is it text data? Or is it numeric?
06:23Is it a date or time?
06:25Or even binary data or XML?
06:27You are going to define how big they should be.
06:30If it's a text column for example, will it represent a few characters of a name?
06:35Or will represent the contents of a thousand-page manuscript?
06:38Your database management system wants to know so it can be efficient about storing it.
06:43Is this column required or is it optional?
06:46Are there maximum and minimum values for it?
06:49Should it match a pattern like an email address, or phone number, or a credit card number?
06:53You see flexibility is usually our friend but it's not what you're looking
06:58for in your database.
06:59If you want to store an e-ail address, you want to know it will always be an
07:04email address, not sometimes an e mail address, and sometimes a date, and
07:08sometimes an inspiring quots, and sometimes an MP3 file.
07:12Defining your columns as exactly as possible means that SQL Server will enforce
07:17rules on those columns, your data will stay valid, and you won't end up with a
07:22database full of garbage.
07:24Next up, you need to define what keys you have and that really is how do you get
07:29to a particular row.
07:31Each row in each table should have something called a primary key and it's
07:37something that uniquely identifies that individual row.
07:41If we have an employee ID, it should take us to only one employee.
07:46If we have a customer ID, it takes us to only one customer.
07:50So we define which column it is that contains our primary key.
07:55Now sometimes the key is already naturally in the data but often you'll need to
07:59generate a unique key and the database can help you with that.
08:03Then you'll define what relationships you need.
08:07You are splitting your database up into tables but many of these tables will
08:11need to know about each other.
08:13If you're creating an order, for example, that order would be typically
08:17connected to a particular customer and it will represent the purchase of one or more products.
08:24We never want to duplicate data in our database.
08:26So we don't want to copy customer information into order or copy product
08:30information into order.
08:32We instead describe relationships between our order table, our customer,
08:36and product tables.
08:37We shortly see how to define what those relationships are.
08:40They can be one to many, many to many, one to one, or none at all.
08:45You can then use these relationships to answer all sorts of questions.
08:49How many orders has a customer had, how many products were in a particular order,
08:53or going the whole other way, find a particular product and find all the
08:57customers you ever ordered it by going through the order table.
09:01After you've planned out of these tables, these columns, these keys, and
09:06relationships, you can technically go ahead and build them in SQL Server,
09:10start adding some data, and see if it exposes any issues with your first design,
09:15and it typically will.
09:16You'll realize something needs to be stored differently or split out into its own tables.
09:21There is something called database normalization,
09:24a set of guidelines and rules you can go through that will expose issues with
09:29your database, and they are super important but I'm going to talk about those
09:33after we've seen how to apply some of this in SQL Server.
Collapse this transcript
Creating a SQL Server database
00:00To create a database, we are really defining two different things.
00:04The idea of what our database is, our tables, our columns, our keys, and our
00:10relationships, but to do that, we first need to create the other piece.
00:14the more boring mundane piece.
00:16See the real deal is that all of our tables and all of the data in those tables
00:21that we want to store will end up being stored on files on the hard drive.
00:27So we need to say what those files are.
00:29We need to say where is this database going to be created.
00:32So I have opened up SQL Server Management Studio.
00:35I'm going to right-click my Databases folder and say New Database, and a new
00:40window will appear that's going to ask me first off to give this thing a name.
00:45I just have to name this database.
00:46So I'm going call it TwoTreesTest.
00:50I am pretending this is a test database for the Two Trees organization.
00:54I am expecting to end up deleting this.
00:57So I'm not really too bothered about how it's created.
01:00I can see that down below it,
01:02it says this will have two database files, something called TwoTreesTest
01:07and TwoTreesTest_log.
01:09This is the default that you will find that everything you end up putting in the
01:13database will be stored in a primary data file and a transaction log file.
01:18I am going to widen this window just a little wider so we can read it.
01:23It will even show you over here what the path to these files will be.
01:29The interesting stuff, however, is this part, the Initial Size and the
01:34Autogrowth, and probably between the two the Autogrowth is the big deal.
01:39Because if we are storing potentially thousands or even millions of rows of data
01:44in our tables and dozens of tables in our database, well, they are all going in
01:49this one file and it could grow pretty large.
01:51We have this Autogrowth section here and if I click the ellipsis button, it
01:55will tell me what the behavior will be.
01:58We have Autogrowth enabled.
02:00What's going to happen is the file will start off at 2 MB and if it gets more
02:05data than that, it's going to start growing by 1 MB at a time.
02:09Now, if you knew you were inserting a lot of information, you might say, well
02:14I want it to grow by 16 MB or 32 MB at a time.
02:18We've also got this idea here of Unrestricted File Growth.
02:22Autogrowth can be changed.
02:23You can see the second one here says this is allowed Autogrowth but it will
02:27grow, instead of MB,
02:29it will by 10% at a time.
02:31So, I am just going to click OK.
02:33I am not going to change either of that except the defaults unless you know differently.
02:38You can change your Autogrowth factors later on.
02:41If I go over to the left-hand side of this new database window, we do have a few options.
02:46I am not going to change anything here but here's where you could change to a
02:50different collation.
02:51Again, the collation is the idea of case sensitivity and accent sensitivity that
02:56the database management system will use when it's sorting, or ordering, or
03:01grouping stuff together.
03:02And the third part here is what's called Filegroups.
03:06In this particular course, I'm not really going to get into Filegroups.
03:10You can just ignore them.
03:11As you are creating more complex databases, you will find that these are ways of
03:16abstracting the physical files that make up your database so you don't have to
03:20deal with the actual individual files on the hard drive.
03:23We are just using one.
03:25That's all I'm going to do with this particular course.
03:28I am going to click OK.
03:31And we now should have a database.
03:34There we go, TwoTreesTest.
03:36I could right-click that and say Properties and you'll find a whole bunch of
03:41other information that you can now start to manipulate and play with.
03:45Again if you're new to SQL Server, I wouldn't suggest that you do, but you
03:49can see there are lot of the options that we had to answer can be changed in
03:53the future as we go on.
03:55I am going to just click OK there, because after all, although we have a
03:59database, a database that is devoid of tables isn't much use to anyone.
04:04So we'll go ahead and do that next.
Collapse this transcript
Creating tables
00:00To create new tables, we need to drill into the database.
00:04We want to add those tables too, then expand the Tables folder and just
00:09right-click it and select New Table.
00:13Now you would expect to first say what the table is called but in fact your
00:17cursor is blinking away now at a column name.
00:20That's because it's giving you this temporary title for the table here, Table_1.
00:24We will get the chance to change that in just a minute but I'm going to describe
00:30a few columns because apparently that's what I have to do here.
00:33Let's say what I wanted to do was create a table to hold employee information.
00:38So I have got things like employee first name, employee last name, date joined,
00:43birth date, phone, that kind of thing.
00:46So I'm going to just create a few simple columns here.
00:48The first would be FirstName.
00:50Again I'm splitting this up into first name and last name rather than just
00:53having a whole name.
00:54Then when I hit Tab, it gives me a drop-down of Data Type.
00:59This is, again, the type of data that this is going to hold.
01:03If I click the drop-down box, we have a whole bunch here.
01:06We have got geography and geometry, is it decimal, is it date time, is it a bit?
01:11We will talk about quite a lot of these different data types but what I am
01:14going to change it to is nvarchar(50), and what this means is a variable length
01:22text field, variable length character field that can store up to 50 characters, all Unicode data.
01:28I am going to talk more about those data types in a later movie but this
01:33will get us started.
01:35The next column is do we allow nulls or not.
01:38Can this field be blank?
01:39A lot of this is going to be done to your particular business process but let's
01:43say that I imagine everybody in my company has a first name.
01:47I am going to say no, I don't allow nulls.
01:50Same with LastName and I will also make that nvarchar(50).
01:57Now I will just go through and do a couple of different things.
02:00Title can be nvarchar(50).
02:02HireDate will be a date.
02:08In fact, what you will is there are multiple date types. There's datetime, datetime2,
02:14but date is fine for me.
02:16BirthDate, that I will also have be a date.
02:20Phone, that's going to be an interesting one because we'll see how that
02:24one could be stored.
02:26We might want to allow people to put in dashes, for example, or even
02:31letters, who knows.
02:33So I am going to change that to nvarchar.
02:36It certainly won't be 50 characters long but I'll give it say 20 or 25. 20 will do.
02:43And let's perhaps say we have an employment status field.
02:46I am going to call it Status and this might just mean they're employed or
02:50they're not employed.
02:51It's just a yes or no.
02:52Sure, you could store that as the text word Yes or the text word No in a
02:58character field but what I am going to do instead is have a field that is a bit
03:03and this basically is a little flag, the equivalent of a checkbox.
03:07It's either true or false.
03:08And I would say that that has to be required.
03:12So let me imagine for right now that this might be it.
03:16So this is all I'm going to have for my employee table.
03:20I can assure you it's really not complete yet but let's pretend that it is.
03:24What I am going to do is hit Ctrl+S to save this.
03:27That's going to now tell me finally, enter a name for the table and I'm going
03:32to call this Employee.
03:34Now there is a bit of a tedious debate over whether to use plural or singular
03:39names for your table.
03:40Should this be an employees table that contains multiple rows each representing
03:45an employee or is it employee singular?
03:49I've worked in places where there's been a standard one way or the other.
03:53I really don't care.
03:54I just think you need to be consistent.
03:56If, for example, I'll save this as a singular table name and if you look at
04:01say what Microsoft is doing with AdventureWorks, you can see that this appears to be singular.
04:07It's Product, Location, Culture, Address, Contact, rather than contacts,
04:12products and so on.
04:14So I'll just take my lead from them at this particular moment.
04:18And the question is so what, what can we do?
04:21Well, now that we've got at least one table created and I can see that in my Two
04:26Trees Test database.
04:27It's showing up as dbo.Employee and this again is using the dbo as the default
04:33schema, the default container, if you will, for our tables.
04:37I'm just going to leave most of our tables as that, which is perfectly
04:40acceptable for our case.
04:42If you get into more complex database development, such as something on the size
04:47of the AdventureWorks one, you may want to start grouping your tables together
04:51into different schemas.
04:52Schema don't really mean anything other than a way to group and understand
04:57objects in your database and potentially to give them different owners.
05:02We are not really going to use them in this course.
05:05So you'll see a lot of dbo for our tables.
05:08One thing I could do at this point is open on my Database Diagrams panel.
05:12It's going to prompt me that the database does not have one or more of the
05:16support objects required to use database diagramming and do you want to create them.
05:20Well, yes, I do.
05:21And the question is what does that mean?
05:23Well, right now nothing.
05:24I have got the support files to create them.
05:26I haven't made one yet.
05:28If I click this, and then right-click, say New Database Diagram, it's going to
05:33throw open the diagram and then pop up this prompt that says "Well, I'm going to
05:36give you the option to add your existing tables to the diagram."
05:40Of course, I only have one table so I might as well add that. I am going to click Close.
05:46Then we can see that we are actually looking at a diagram of our employee table
05:49with all our different columns listed in it.
05:52Now one thing you can do if you are so inclined is you can actually use this
05:56database diagram technique to create new tables.
05:59I could right-click anywhere in the blank area and actually say that I want
06:03to make a new table.
06:05It's not going to ask me to give a name for this table.
06:08I will call it Product.
06:10And in this, I can directly starts typing and I will zoom back into 100%, column
06:16names such as a ProductID. Let's call that the nchar data type of 10 is fine.
06:26ProductName, nvarhcar(50). Well, let's say we have long products. Maybe this
06:32will be 200 and so on.
06:34Now do bear in mind, you are not just creating a pretty diagram here.
06:41I can see that I have got this little asterisk after the name of product and
06:44what that really means is its detecting there is a change made to this table
06:49definition that has not been saved.
06:52But if I were to save it, I am just hitting Ctrl+S, I am saving the diagram here
06:59and that's kind of a little deceptive sometimes because as I'm saving that,
07:03it's actually telling me, "Well the following tables will be saved to your database,
07:06" do you want to continue? Yes I do.
07:09That has created not only a diagram but whatever I did on the diagram really did
07:15affect the database.
07:16So I now have dbo.Product.
07:19I could continue to edit it over here in the diagram or I could right-click
07:24over here and say Design, which would allow me to jump into the standard view of editing.
07:31As you'll see below, anytime I have one of these columns selected, I do have a
07:35whole host of more properties that I can change about it.
07:39And we will visit some of these a little later on.
07:41But that's the general process of how we start to add tables to our database and
07:47how we start to manipulate columns in those tables.
Collapse this transcript
Data types in SQL Server
00:00When you're defining your columns in SQL Server Management Studio, you will find
00:05that the data type for each column presents you with a rather large set of
00:09choices of things that you could possibly use here and it can be a little
00:13intimidating if you are new to SQL Server or you're not quite sure exactly
00:17which one you should be picking.
00:19So let's explore what some of those options are, at least what the most common
00:23ones that you want to know about.
00:26The data types are split really in two categories.
00:29Is it character or text data? Is it a number?
00:32Is it a date?
00:33What actually is this?
00:35And then we can get a little bit more specific about each of those pieces.
00:39Let's start off with character or text data.
00:42Well, even when you are looking at this, you might be a little annoyed by the
00:46fact that you have six character data types: text, ntext, char, nchar,
00:51varchar, and nvarchar.
00:53And you might think, what, I am supposed to make six decisions for every single
00:57bit of text that I have?
00:58Well, actually it's a little simpler than that because straight off the bat we
01:02can lose two of these.
01:04Even though text and ntext will show up in that drop-down data type box,
01:09you should not use them.
01:10These are old school and they're officially deprecated in this version of SQL Server,
01:16meaning they'll actually be removed from a future version.
01:19They are only there for backwards compatibility.
01:21So we're left with four and we can choose between these four simply by
01:26asking two questions.
01:28One, is the data we are going to store Unicode or non-Unicode?
01:33And two, is it a fixed length or a variable length?
01:37If we need Unicode data, that's if you support or ever expect that you might
01:43possibly support international language character sets, you need to pick Unicode.
01:49And those are the two character data types with the letter n at the front of
01:54them, nchar and nvarchar.
01:56If you know that your text is only ever going to be plain old Western
02:00European character set, pick non- Unicode which is char and varchar without
02:05the n at the front.
02:06The impact is that if you pick Unicode, because Unicode can hold a much bigger
02:12character set, many, many different international languages, that takes up 2
02:17bytes per character in SQL Server.
02:19If it's not Unicode, it's a char or varchar,
02:21it's 1 byte per character.
02:24Now officially, SQL Server 2008 R2 has something called Unicode compressions.
02:28So if you are really worried about the space, you can look into that.
02:31Now, I'm just a fan of using Unicode by default all the time.
02:35So I'll pretty much always pick the one with the n in front.
02:39But the next question that I have to ask is, is this fixed length or is
02:43it variable length?
02:45And that's simply allows you to choose.
02:46If it's fixed length, it's nchar or char.
02:49If it's variable length, it's the one with the var inside it.
02:53And of course, that's going to be up to you.
02:55What is it that you're storing?
02:57If it's product ID, for example, you might know that it's always going to be
03:0110 characters long.
03:03If it's the contents of a blog entry, well, that's going to be a bit more variable.
03:08So you just answer these two questions, Unicode or non-Unicode, fixed length or
03:12variable length, and that will take you to the character data type that you need to pick.
03:17Once you've chosen that, you need to ask a couple of other questions.
03:21See if it's a fixed length character data like nchar or char, well, you need to
03:26then predict the amount of space that's going to take.
03:29If it's nchar(10), meaning it's a length of 10, that will take up to 20 bytes
03:35and even if you only fill 3 characters of it, it will pad it with spaces.
03:40If it's just char of 10, it will be 10 bytes.
03:43SQL Server likes to know the size of it so it can make its own optimization
03:48about how much space this is going to take up in the system.
03:52But even if you're picking variable length character data, you need to know the
03:55impact this is going to have.
03:56If you specify an nvarchar with a length of 10 characters, well, the length of it is flexible.
04:03It depends.
04:04The maximum of this will be 20 bytes but if you're only storing one or two
04:08characters, it should only take up the space for one or two characters.
04:12Well, one of the questions might be how long can this get?
04:15Well, if you're working with Unicode, the maximum length you can have of either
04:20an nchar and nvarchar is 4000 and that's 4000 characters.
04:26Again, because it will take, by default, 2 bytes per character. That's up to 8000 bytes here.
04:32Now because 8000 is allowed, if you are just working with the non-Unicode
04:36version, your maximum is 8000.
04:388000 for a varchar or a char is the maximum specified length.
04:44Well, you might ask what happens if it's bigger than that?
04:47Or if it ever could be more than 8000 bytes
04:51out of 4000 Unicode characters or 8000 regular ASCII characters?
04:56Then you have one other choice.
04:58You just use the word max or varcharmax.
05:02That allows you up to 2 GB per row per column.
05:08So you can store some pretty large stuff in here.
05:11You just can't specify a length any larger than 4000 for Unicode and 8000 for non-Unicode.
05:16Once you get beyond that, it's max.
05:19Next up, we have numeric data types.
05:22Let's start with the exact numbers.
05:24We have the classic int or integers, storing whole numbers from roughly -2
05:30billion to +2 billion.
05:32Nothing after the decimal point; they are always whole numbers.
05:35If you don't need that much space, you can have what's called a smallint, which
05:39is roughly -32 thousand to +32 thousand.
05:43You've got a tinyint, which is 0 to 255.
05:47If you know that you have a very small range.
05:49But what if you want more than 2 billion?
05:51Well, you do have bigint as well, which be +/-2^63 so that should hold pretty
05:57much whatever you can come up with.
05:59We also have money and smallmoney.
06:02These are exact numbers even though they have got numbers after the decimal point.
06:07It's a fixed length.
06:09Both the money and the smallmoney data types have four digits after the decimal point.
06:15Money can hold up to, as you can see here, that's a fairly large amount, whereas
06:19small money is +/-214,748 and then four digits after the decimal point.
06:27Formerly with your exact numbers, you also have decimal which is a fixed
06:32precision and scale.
06:33I will explain what that means in a second.
06:35Now you'll also see something called numeric.
06:37Decimal and numeric are exactly the same.
06:40Again, the two terms are here for kind of backwards compatibility.
06:44I pick decimal all the time.
06:46It doesn't really matter.
06:47Just pick one and stick with it.
06:48What precision and scale means is as you define two numbers. The p represents the precision,
06:54the total amount of digits being stored in this column.
06:59And ,s for the scale is the amount of digits after the decimal point.
07:04So for example, creating decimal with 9,2 means there is nine digits in total
07:11and two after the decimal point.
07:13So the maximum value would be seven 9s before the point and two 9s afterwards.
07:18So that's decimal and numeric.
07:21You do have approximate or floating point numbers.
07:24You have got float where you say exactly what the floating point should be and
07:29then you have something called real.
07:31I would suggest that if you're getting into the floating point numbers and you
07:34need to be aware of those, we will look them up on Books Online for more
07:38specific information.
07:39I will show that in just a second.
07:41You do have several date and time data types.
07:44We have one that just holds a date.
07:47We have one that just holds a time.
07:49There is one that's called datetime.
07:52That's been around for quite a while. It holds not only the date but the time as well.
07:56Although SQL Server has something called datetime2, and that is the one
08:01that's recommended.
08:02Even if it looks kind of an ugly data type, that's the one you should use if you
08:06need to store both dates and times because it to be more accurate, down to about
08:10100 nanoseconds, and it doesn't take as much space as datetime does.
08:15If you're working, however, with international time zones and you need to store
08:19your time offset from Universal Time Code, there is also the datetimeoffset,
08:25which takes that into account.
08:27So you could look at that one and if you need on the other side a bit less
08:30precision, you can also look at smalldatetime.
08:34But I'd say the main ones here should be reasonably obvious, which is you just
08:38need a date, use date.
08:39You just need a time, use time.
08:41You need both, use datetime2.
08:44As you start to work with your data types, you will realize that they are not
08:47trying to give you several possible options for the same need.
08:51They really all do have pretty specific needs to them.
08:53There are several other data types.
08:56We have binary and varbinary.
08:59As you might guess from looking the char and varchar, this really means a
09:03fixed length and a variable length binary field for storing things like file attachments.
09:08That can grow as big as 2 GB, which you will find is the fairly magic number for
09:13large column sizes in SQL Server.
09:15There is bit which is basically a field that can be true or false.
09:20You have got XML. You can store raw XML data, even though it is text, and you
09:25can think surely I could just put it in a text field. You could, but the XML
09:29column has the ability for SQL Server to understand the XML and even parse the XML if needed.
09:35There is a unique identifier, which would create a 16 digit unique ID, what's
09:40often referred to as a GUID, a Globally Unique Identifier, and you can even
09:45define your own data types, though we are not going to do this in this course.
09:50There is a couple of other rare ones that rounded up.
09:53If you start looking through the drop-down box, you will see things
09:55like geometry and geography for ellipses and paths and even for
10:00latitude and longitude.
10:02And there is a few other deprecated ones like timestamp and image not to be used.
10:07Here is the real deal. Until this all becomes second nature, what you want to be
10:12doing is making sure that you're using SQL Server Books Online and use that as
10:18your little quick reference for these.
10:20Let me show you the easiest way to start working with that.
10:23If I open up my SQL Server Books Online, what I'm going to do is I want to find
10:29my data type definitions.
10:30Well, I can just type in the word in my index here.
10:33So I'm looking in my index, the second tab, and I am saying data types.
10:37Now I select data types and I have all sorts of things.
10:40It says data types here and blanks and currency and all sorts of stuff but
10:45unfortunately I am picking the wrong ones, because SQL Server Books Online is
10:49showing me a bit too much information because I got data types in PowerPivot for
10:53Excel and data types in CLR integration and data types ODBC.
10:58What I'm going to do is filter this down a little bit. Tather than have the
11:03unfiltered showing me every possibility,
11:06I'm going to say to say give me the SQL Server 2008 Database Engine and let me
11:10try that data types again.
11:13Well we have a few still here.
11:14Data types CLR, data types ODBC, data types OLE DB.
11:19Data types SQL Server is what I'm looking for and if you are thinking, well,
11:23how do I know for sure?
11:24Well, unless, you know that you're going for OLE DB or SMO or SQL Server
11:28application, this one is a pretty good bet.
11:31And if I highlight that, I can see that it starts talking about the related data types.
11:36So I will come down in this page.
11:37Let me delete that a little bit.
11:40We've got the data type categories, we have got the bigints, and bits,
11:43and decimals, and ints.
11:44I could jump into any of these pieces such as say smallmoney and it will tell me
11:50exactly what that range is for smallmoney.
11:54So this looks like a pretty useful page to have for my data types as I am
11:58getting used to them.
11:59And in fact what I'm going to do is go up here onto my menu bar and just click
12:04Add to Help Favorites, hit that button, and what that means is that from now on
12:10if I want to just go ahead and remind myself of the data types, I jump to my
12:13Help Favorites tab in Books Online.
12:16I hit that and it will allow me to drop through all the different data types.
12:20The other ones, the binaries, the Unicode, character, date and time, all of it.
Collapse this transcript
Defining keys
00:00One thing you should do on all of your tables, at least as a general rule, is
00:04defined what's called a primary key. This is a way that's going to identify
00:09how do we get to an individual role. One value that would always take us to
00:14just one row in that table.
00:16And your primary key can be any of the columns that you actually have defined if
00:22that makes sense. Sometimes it does, sometimes it doesn't.
00:26If I look at my Product Table, for example I have ProductID, ProductName,
00:31UnitPrice and UnitsInStock, and let's say that my business process was such
00:36that ProductIDs were decided around a conference table and manually entered into the database.
00:42Well I could say then ProductID should be my primary key.
00:47One ProductID should always identify a row in this table in this database.
00:52Well I have it open in Design. I can right- click the column and just say Set primary key.
00:58Notice what happens to the Allow Nulls column when I do this.
01:02It's going to turn it off because when you think about it, it doesn't make sense.
01:05You can't really have a unique primary key if you're allowing that value to
01:10not be entered at all.
01:12So I'm going to hit that and say cool, I'm done.
01:14A ProductID will be unique and it will be manually entered in for each product.
01:20I'm going to now try and save my changes and the problem that I run into, it will
01:25say "Saving changes is not permitted."
01:27The changes that I've made require the following table to be dropped and re-created.
01:32Now this is one of the problems here, one of the issues. It's why I say that you want
01:37to figure your database tables out before you build them, because you don't want
01:42to have to go back in and edit them multiple times.
01:45Many of the changes that you might think are just a small change, according to
01:49the database are fairly big ones, and in fact SQL Server Management Studio is not
01:54letting me save that change. The change it cares about was not actually the
01:58primary key. It was me saying that Nulls were allowed and now they're not.
02:03Even though I have no data stored in this table yet, it's still considering that
02:09a really significant change so it's stopping me from doing it.
02:12Well I can tell SQL Server Management Studio to lighten up a little bit. I am
02:17going to go to my Tools > Options and find my Designers section where I have an
02:25option here that says Prevent saving changes that require table re-creation.
02:29 I am going to uncheck that, so I am saying yes, I can save a substantial change
02:34that requires the table to be re-created.
02:36You want to be careful with that one because obviously un-checking that allows
02:41you to make some fairly significant changes that you might not want to.
02:45So I'm going to now try that, save again, and we are good. W now have a primary
02:50key defined on that column.
02:52Well what about the other table?
02:55We have dbo.Employee here.
02:56I am going to right-click and select Design.
02:59I can scan the columns here and see that there is no primary key because you do
03:03get it just showing up there if you need to. And I can look at that and think,
03:07well, is the one that is obvious? And there isn't.
03:12You could make a case that says well, perhaps first name and last name together
03:16could be a primary key, and in fact you can do something like that.
03:20It's what's called a compound key, but still in a big company there is no
03:24guarantee that you wouldn't get two people with the same first and last name.
03:28So here would be a good idea for just creating one. I am going to add an EmployeeID,
03:33and this one I'm going to say is just going to be an integer.
03:39It's just going to be a number.
03:41If we say that every employee has to have one, I'm going to uncheck this and say
03:45no, don't allow Nulls. Don't allow that missing value there.
03:50And I could just right-click and name this as the primary key and that would work.
03:54However, it does mean that right now I would have to manually type this in or
03:59have an application that creates it.
04:01What a nice thing would be is can we get SQL Server to help us generate that
04:07EmployeeID every time a new employee is added.
04:10And not surprisingly, and I know where you think I'm going with this, you can.
04:14If I keep this column highlighted so EmployeeID is highlighted and then down in
04:19the Properties for the column we have a section called Identity Specification.
04:24This is a little switch that we can flip and it says Is Identity from No to Yes.
04:30And then what it changes and it says you've got an Identity Seed and Identity
04:34Increment, which is basically what number do we start with and how much do we go up each time.
04:40So the default is we start at number 1 and add 1 to it every time around, but
04:44I can decide to start it any number. Let's start at 500 and go up by 3 each time around.
04:52I'm going to now just save these changes and we now have a primary key defined there.
04:56Well it'd be nice to prove it. I'll go ahead and do that. I'm going to
05:01right-click the Employee table and say Edit the Top 200 Rows. That switches me
05:06into this editing view.
05:07I don't have any data yet, so let's put in our first piece.
05:11So the FirstName I'll say that will be Joe, LastName as West.
05:15You'll notice that every time I am entering something in I get the exclamation mark
05:19and it's telling me that the cell has changed but the change has not been
05:22committed to the database. The original data was NULL.
05:25I'll just continue on. That sounds all right.
05:27I'll see the Title is a Manager, and the HireDate was the first of February in 2005.
05:34BirthDate was the first of January of 1970.
05:38Phone number was just a text value. I could put pretty much anything in here.
05:44The Status was the bit data type and that really means just expecting a Boolean
05:50value, either true or false.
05:52A lot of developers wonder should they put a 1 or a 0. Well you can just write
05:56the word true in there.
05:58You might think well what happens if I didn't, if I just typed in the number 99?
06:03Well as soon as I try and tab off it, it's going to say, "I'm sorry this change
06:07value is not recognized as valid." You'd get this on any of these fields.
06:11The string was not recognized as a valid Boolean.
06:14Okay, that makes sense.
06:15I'll click OK and I'll type true, which should be a valid Boolean, and it allow me to tab off.
06:23Now you notice here I'm now on EmployeeID.
06:27This is the one that should be our identity and should be automatically created.
06:32So I am not going to type anything in here.
06:34I am just going to tab, which would make me go to the next row.
06:37And when I do that we take off that first row, and by leaving the row all these
06:42changes should now be committed to the database, and if I move to the right a
06:45little bit I can see that I am actually Employee 500, that it's been entered
06:50correctly, and if I would to enter in more information, it will be 503, 506, 509,
06:53512 because we said that the Identity Increment was a factor of 3 for that.
07:04And the last thing I'm going to actually do here is change this EmployeeID and I
07:09want to show it first and I can just do that by rearranging my column.
07:13I am going to grab it and just drag it up to the top and save my changes.
07:17Just going to close these existing windows down right now and reopen the
07:22Employee table and I'll just say select the Top 1000 Rows.
07:26And what'll happen is it's bringing back that information where EmployeeID 500,
07:31all the data that I just entered in, and it does actually show me the command
07:35that is really being executed right now, which is just a Select statement.
07:39We'll get into those a lot later.
07:42And you'll notice that I do have a Keys folder and I can expand it and it
07:46can show me PK_Employee, which means that the primary key for employee has been defined.
07:53Now there are different kinds of keys that you can create. You can also create
07:56what are called secondary keys, anytime you know you're going to be using a
07:59particular field to try and access a part of the database, but we'll talk
08:04about those a little later on. Primary keys are what we need to start dealing with first.
Collapse this transcript
Creating default values
00:00When you're designing databases, it's a good idea to apply as many rules or
00:05constraints to your data as possible.
00:08And one of the classic ways to do this is by setting up default values for your columns.
00:12The idea of this of course is simple.
00:14We have a table such as an OrderItem table made of product IDs and quantities and unit prices.
00:21Whereas a lot of this information is very different from row to row, Quantity
00:26for example might typically be the number 1. Occasionally it might be 2, but
00:31nine times out of ten or nineteen times out of twenty or ninety-nine out of a
00:35hundred it will be the number 1.
00:36So we will make this the default value for that column, so it doesn't always
00:41have to be entered, either by the application or by the user.
00:45I'm going to go into my OrderItem table in SQL Server Management Studio and select Design.
00:51Applying a default value is very simple.
00:54Find the column that you're looking for, in this case Quantity, and then
00:57down here in the Column Property section, you'll find a section that says Default Value.
01:02We hit the number 1.
01:04That's counted as a change to the database definition.
01:08So you'll see that we have the asterisk up here.
01:10I am going to hit Save to save those changes to the table.
01:14Close that down and now let's test it.
01:16I am going to right-click and say Edit Top 200 Rows and I am going to enter in a new order item.
01:22I am, of course, just going to do this manually.
01:23Now, right now I have OrderItemID added as an identity column for a primary key,
01:31which means it will generate that too.
01:32Hopefully, the number 19 or the number 20.
01:34I am going to put in ProductID-123.
01:38I'm going to Tab to skip over the Quantity and I am going to say the
01:42UnitPrice is 54.98.
01:43Then I am going to tab off on to next row.
01:47Now you will quite often see this message where the row itself kind of shows up
01:52in a pale color and the exclamation mark, if I tooltip over it, it will say,
01:57"This row was successfully committed to the database.
02:00However, there was an issue with retrieving the data back after the commit,
02:04because the displayed data is read-only."
02:07But it almost certainly worked.
02:09So I'm going to just come out of this and go back in and just select the Top
02:151000 Rows again, just to double check that.
02:18I can see that yes, at position 19 we have ProductID 123 with a default
02:23value of 1 at 54.98.
02:25Now bear in mind though that the only real point of working with the default value
02:31is it will have an effect when a row is being added to the table.
02:36If I was editing rows and I just decided, for example, to leave a blank value,
02:42it's not going to dump in a new default value for a row that already exists.
02:46So I will hit Escape there.
02:48The only place where default values can have an effect after the fact is when
02:53we're adding a new column.
02:55Let's say going back into OrderItem. I am going top hit Design and I am going to
03:00add a new column here that I will call DateCreated.
03:04I want this to always have a date or indeed a datetime.
03:07I will use datetime2 and uncheck Allow Nulls.
03:11I always want a value in this column of when that row was created.
03:16When I make that change, if I'm trying to save my changes back, I'm going to get
03:20this Post-Save Notification pop-up.
03:23It was unable to modify the table.
03:25It gives me a long-winded explanation that really comes down to
03:30you can't create a column and say it's not allowed to have null values and then
03:35add it to lots of existing rows with null values.
03:37Well, one of the ways we can use the default value here is I'm going to say the
03:43default value is getdate.
03:46This is the default value of DateCreated.
03:49If I now save this, what will happen is it's going to use that rule for any new row.
03:56Because we've also used it for pushing a change to this table, it's going to use
04:01that value for the old rows as well.
04:03Just to close down my existing information, I will then right-click and reopen it up.
04:09And I can actually see that the DateCreated has been filled out for all those old rows.
04:15From this point forward if I add a new row, ProductID 456, I will skip over
04:20Quantity because that should give me a default value.
04:22I will do a UnitPrice of 98 and I will skip over the DateCreated.
04:28If we then close that down and just reopen it, I'm going to see that I have
04:34both the default value of 1 and the DateCreated of just a moment ago.
Collapse this transcript
Creating check constraints
00:00Another way that we can increase the reliability, the integrity of our data, is
00:05to apply what are called check constraints.
00:07A check constraint is a simple idea.
00:09If we have some data such as Quantity where we can look at that and understand
00:14this really unacceptable range for this to be in,
00:17even though this column is actually being defined as an int, we don't need all
00:21that an int can give us.
00:22Because an integer can support from roughly -2 billion to plus 2 billion,
00:27it is quite unlikely that someone will order -2 billion quantities of a unit.
00:33So let's say I want to enforce some rules on this column that says Quantity must
00:37be greater than 0 and less than 51 say.
00:40Well, I do this by applying a check constraint to the table.
00:45I am going to right-click anywhere in the Designer area.
00:47It really doesn't matter. And I have a section called Check Constraints.
00:51Right now it's saying there are no check constraints on this table.
00:54I am going to add one.
00:55It's typically called CK_ the name of the table.
00:59What we need to type in is an expression.
01:02In this case, it's quite simple. Quantity > 0 AND Quantity < 51. Close.
01:17We now have a check constraint defined on the table.
01:19It is considered a table change.
01:20So I see the asterisk.
01:22I have got to save that. Close this down.
01:26Now, when I go into that table and I say change one of the existing rows, 50 is
01:33accepted as a change and I can click off that row.
01:3651 will actually throw an error here.
01:40The update statement conflicted with the check constraint.
01:44I also can't put a 0 Quantity and I can't put a negative Quantity.
01:52Check constraints can easily be removed.
01:54I need to escape out of here before it tries to apply those changes.
01:58I could go back into the Design view, again right-clicking anywhere in the
02:02Designer, because the check constraints are defined for the table.
02:05They're not defined specifically on a column.
02:08It's just this expression.
02:09You could have one check constraint that has multiple columns in it.
02:13Quantity above a certain amount if ProductID in a particular range.
02:17But if we wanted to, we could just come in here and hit the Delete button to
02:21remove the check constraint from the table, but I'm going to keep it.
Collapse this transcript
Creating unique constraints
00:00Another kind of constraint you can work with is what's called a unique constraint.
00:04Now in fact, we already have one of these on most of our tables.
00:08If you have a primary key, you have a unique constraint. That primary key
00:12cannot be duplicated.
00:14In this case, there can only be one ProductID with 1008 in it.
00:20But occasionally, you will want to define another column as being a unique
00:25constraint, that the value cannot occur more than once in the entirety of the
00:31column in that table.
00:32Now right now none of these columns should be made unique.
00:36Certainly not UnitsInStock and certainly not Color. That repeats quite a lot.
00:41We might have ProductName made unique, but not the way that we work with it.
00:44For example, I have a Road-650 58 in Black and I have a Road-650 58 product in
00:52Red and a Road-650 58 product in Green.
00:55I can't make ProductName unique.
00:57What would be interesting, however, is if I could force uniqueness on a
01:01combination of the two.
01:02What that allows me to do is make sure that they aren't accidentally two rows
01:07that represent the same product color combination, that the database would not
01:11allow me to enter that.
01:13Everything we do in our database design should all be about enforcing valid data.
01:18So how do we do this?
01:19Well, not surprisingly, it's going to be in the Design view of the product.
01:23I'm going to right-click that table and jump into Design.
01:26Now you won't find any on the unique options in your Column Properties.
01:31What you have to do is right-click in the blank designer area and while we had
01:36things like Check Constraints there, you don't see a unique constraint.
01:39The area that we are interested in is this one, Indexes/Keys.
01:44If I select that, we should find we already have one.
01:47Here's my index for the primary key.
01:49It's saying this is the ProductID.
01:52It is unique, yes, and it is the primary key.
01:55We don't want to mess with that.
01:56What I want to do is add another one.
01:59It's not a primary key.
02:00It's what's called a unique key.
02:02I am going to click Add and we get this IX_Product, because this dialog box that
02:08I'm looking at is how we add new indexes to our table.
02:11How we add new ways of looking things up quicker when we have large amounts of data.
02:15Now, we are going to be talking about indexes a little later on.
02:18So don't worry too much about all the options here like Create As Clustered
02:22and all that stuff.
02:23I actually want to change this.
02:25I click the Add button and I want to say no, this is not an index.
02:28This is a unique key.
02:30It changes the Is Unique to Yes.
02:32We do have to pick the columns that we are interested in.
02:35It's defaulted to ProductID, but ProductID is already unique.
02:39I am going to click the little ellipsis button.
02:41I am going to say what I actually want is for my column to be ProductName.
02:46And because it's going to create its own little set of product names to make
02:52sure that there won't be a duplicate one, we can choose whether that
02:55Ascending or Descending.
02:56It doesn't matter here.
02:57However, I don't want it to be just ProductName.
02:59That wouldn't work.
03:00In fact, I couldn't enforce that rule on my existing table.
03:03It's going to be ProductName and I am going to click beneath it and say Color. That's fine.
03:08Color Ascending, ProductName Ascending. I click OK.
03:11It's now telling me that the combination is both of them, ProductName and Color.
03:16I can ignore the rest of the stuff and just click Close.
03:20This still is considered an unsafe change to my table.
03:23So I'm going to save the change now.
03:24I am going to go back into editing that and I'm going to add a new product.
03:32Let's say in this case it's going to be Road-660 60 in Black and there's 3
03:40UnitsInStock and that works just fine.
03:43Now let's say we add Road-650 58 in Green.
03:50Now this one already exists.
03:52So when I tab off, I should get a problem.
03:54"I'm sorry, there was a Violation of UNIQUE KEY constraint, can't insert duplicate key.
03:58The statement has been terminated."
04:01Again, if you are a developer, a lot of the times developers think, "Well,
04:04surely, I could just take care of that in my application.
04:07I'll make a check beforehand."
04:09But the great thing about doing it here is we are saving those rules as business
04:13rules in the database.
04:14We are making sure that we'll never get that duplicate data entered in rather
04:19than just trusting an application or a user to make that check for us.
04:23I won't be able to save that.
04:25So I can either add it as a new color, Blue, which would allow me to save that row,
04:31or I could have deleted that row.
Collapse this transcript
Introduction to relationships and foreign keys
00:00A vital part of designing any database is the ability to create relationships
00:05between your tables. Let's go through an example.
00:08We have a simple product table, dbo.Product, here.
00:12It has its own primary key, the ProductID, which is automatically generating a
00:18unique number for each product as it's entered into the database.
00:21And one of the benefits of having that primary key is we can use it elsewhere.
00:26So we have a different table, this time called OrderItem, helping us
00:30construct our orders.
00:32Itself it has its own primary key with its own automatically incremented value,
00:37but what's a bit more interesting is it has another column here of ProductID.
00:43Now ProductID in OrderItem should reference the ProductID in the Product table,
00:49but whereas in the Product table it has to be unique, in OrderItem it doesn't.
00:53We could have for example the ProductID 1001 three times in OrderItems, or four
01:00times, or a dozen, all referencing the same product.
01:03We could have 1010 pointing to our 1010 product. We could have 1002.
01:10This is the relationship that we're talking about between our tables.
01:15In the Product table, ProductID is referred to as the primary key. It is unique.
01:20In the OrderItem table, ProductID is referred to as a foreign key.
01:25It's a key to some other table, allowing us to join our information together.
01:30The benefit of having this relationship described is we can go either way we can
01:34go from the OrderItem and go and get product details, or we can go from the
01:39product and find how many order items have been created for that product.
01:43And when you have a relationship like this it's what's defined as a
01:47one-to-many relationship.
01:50For every one product we can have many order items.
01:53Now you'll end up creating dozens or potentially hundreds of relationships when
01:59you design your databases.
02:00If you're using SQL Server Management Studio to show a database diagram you'll
02:05actually see that relationship defined between the tables.
02:09And as you create more-and-more complex databases, you'll find that you're going
02:12to have relationships between most of them.
02:15What I'm looking at here is just a zoomed out diagram of the AdventureWorks
02:18database and that's not even a particularly large database.
02:21It's very, very common to have relationships between your tables.
02:26And there really are two main kinds of relationships that we care about.
02:29The first one, the most common, what we just explored, the idea of a
02:33one-to-many relationship.
02:35So one customer many orders, one category has many products in it, one
02:41department has many employees.
02:43Now bare in mind when you describe a one- to-many relationship you don't actually
02:48have to have many. Any one customer might only place one order.
02:53Might even place zero orders, but one customer can have many orders,
02:58one category can have many products, one department can have many employees.
03:03But the important point is here that at least according to your own business
03:07rules the opposite of these statements is not true, that in your business an
03:12order has one customer.
03:14You don't say an order can have many customers. It can't.
03:17An employee has one department.
03:19A product has one category.
03:21So you can view this either from the top- down or the bottom-up, whatever makes sense.
03:25Now as this is the most common thing that you're going to see when you're
03:29looking at a diagram in SQL Server Management Studio, you'll see the relationship
03:33defined like this with this icon. The key represents the one and the infinity
03:38sign represents the many.
03:40So when you're looking at this diagram it doesn't matter whether the tables are
03:43shown on top or underneath. What's important is where the key is.
03:48The next kind of relationship we can have is called a many-to-many relationship.
03:53It's not as common as one-to- many, but it's still quite common.
03:56Now you often have to think about this one, because these business situations
04:01can often feel like one-to-many.
04:02At first, let's say we have an author table with a list of a few authors inside it
04:10and their names, and some kind of key and identity for them, and we also
04:14have a title table with the list of book titles.
04:18Well at first glance what we might say is okay, this is a one-to-many
04:22relationship. We could take the author number 74, Jordan Winters, and say that we
04:27want to represent that author has written both book one, DB Design, and book 3, SQL Server.
04:34And we can say that Fred Summers, author number 75, wrote book 2 on SharePoint.
04:39And if this was a classic one-to-many relationship, one author, one or more
04:43titles, we could add a new column to the title table. This would be an author ID column.
04:49It would be a foreign key to the author table.
04:52But here is the issue. What happens if in a day, or a week, or a month we say
04:58we'll actually the SQL Server book was written by two authors?
05:03Well the way that we have it right now, we have that author column can only store one,
05:07storing a foreign key to author ID 74.
05:10And this is what we mean by a many-to- many relationship. One author can have
05:15many titles, one title can have many authors.
05:18Now the way some people try and model this is they'll add a new column to the
05:23title table. They'll put in author ID 2 and they'll have that be 76 and
05:28pointed to John Marr.
05:29However, adding new columns to your tables, and particularly these kind. what are
05:35called repeating groups or repeating columns. is a really, really bad idea and
05:40it's a definite no-no in database design.
05:42So we'll get rid of that idea. Well some other people think, "Well I'm going to
05:46cheat a little bit and I'll just do something quick and dirty and I'll just
05:50slide in little set of comma separated values there."
05:53So that the author column can point to both 74 and 76, but that's just a cheat
05:59and like adding a new column this is not suggested either.
06:04In fact we're going to solve this problem by getting rid of the author column entirely.
06:10So we go back to two completely detached tables and what we do to fix this is
06:15we add another table.
06:18We add what's often referred to as a junction or a linking table.
06:21Now the only reason for this table to exist is to join author and title together.
06:27So in fact the name of this title by convention would be authortitle.
06:30It could also be titleauthor.
06:32It doesn't really matter which way round it goes, because what we're going to do
06:36is setup two one-to-many relationships.
06:39In fact, you cannot, in SQL Server or in any other relational database,
06:44you cannot express a many-to-many relationship directly. You can only do two
06:50one-to-many relationships.
06:51So what we'll do is we'll define a one-to- many relationship from the author to authortitle.
06:57So one author with ID 74 can exist in the AuthorID foreign key column in
07:03authortitle twice, or three times, or five times, or a dozen.
07:07Using that we can go from author to authortitle, find a TitleID ,and map it up
07:13to the title table.
07:14And we can also go the other way.
07:17We can take the identity of a title like number 3, take it back to the
07:21authortitle table to two rows there so one-to-many relationship back the way,
07:25grab the AuthorID, and follow that back up to the author table.
07:30So we are expressing now a many-to-many relationship.
07:34And if you go looking in say some of the sampled database, whether it's the
07:38larger ones like AdventureWorks or even the small ones like AdventureWorksLT,
07:43anytime you see a table name that seems to join the name of two other tables--
07:48So we have ProductModel, ProductDescription, and then
07:52ProductModelProductDescription.
07:53You can make the assumption that the one with a long name is simply there to
07:57join ProductModel and ProductDescription together.
08:00In fact, if you want to confirm that what we could do is make a new database diagram.
08:06I'm going to add just those three tables that I suspect that from,
08:10ProductDescription, I'll hold down Ctrl and select ProductModel and
08:13ProductModelProductDescription, click Add, close this,
08:18and what I can actually see here is my many-to-many being rendered out.
08:23We go from ProductModel is the one, to the many, a ProductModelProductDescription.
08:27ProductDescription is the one to the many.
08:30The only reason for the existence of this table is to join the other two together.
08:35In a large database you're going to end up with a lot of one-to-many
08:39relationships between your tables and a few of those will really be used to
08:44create a many-to-many relationship.
08:46Officially, there is also a one-to- one relationship that is possible, but
08:50it's not common at all.
08:52If you think about it, if one row in one table is pointing to one row and only
08:57one row in another table, well you might as well just combine them so it's just
09:01one row in both places.
09:03Although also bear in mind that if those are the official three kinds of
09:06relationships, one-to-many very common, many-to-many quite common, and
09:11one-to-one not common at all, you also have what some people consider a fourth
09:16kind of relationship.
09:17Zoom into any large database diagram and you'll find things like this, a table
09:22just existing without any.
09:24Some people consider "none" to be a relationship.
09:27If you get obsessive about what these relationships are, and again the geek
09:31trivia term is the cardinality is what we call something that describes the
09:35relationship between tables.
09:37Some people say none is an official relationship.
09:39I don't think so, but you'll certainly see that a lot.
09:42You don't have to connect your tables to other parts of your database.
09:46So the key question is going to be, how do we do this?
09:49And we'll see that next.
Collapse this transcript
Creating relationships in SQL Server Management Studio
00:00I am going to go ahead and describe a one-to- many relationship between two of my own tables.
00:06So I have two very-very simple tables here.
00:08I have a product table, which has a few products in it right now.
00:12It's got a ProductID as the unique key, the primary key for this.
00:17That's automatically incrementing, ranging from roughly 1000 through 1013 right now, and
00:23I also have an OrderItem table.
00:25This itself has its own primary key, OrderItemID, that I don't really care
00:29about, but it's got a place for a ProductID and this is going to become the foreign key.
00:35I'd like to be able to create multiple order items for each product so that
00:40ProductID 1001 can be used several times if I deem it necessary.
00:46That means our many-to-one relationship is one product, many order items.
00:52So how do I make it?
00:53Well I am going to close down these tables.
00:55There are a couple of different ways I can do this.
00:58One way would be to go to the Database Diagrams area, add a new satabase
01:02diagram, grab the two tables that I'm interested in, OrderItem and Product, and I
01:08could actually drag and drop between them.
01:13Grabbing hold of the little column to the left, grabbing ProductID and
01:16dropping it on here.
01:17However, I am not going to step through that way, because we haven't really been
01:21doing too much with relationships and I'd rather show you a slightly more manual
01:25way to do it, just editing the tables directly.
01:28I'm going to go into the OrderItem table into Design view.
01:32So I am in the many table here.
01:35I am going to right-click on the blank area and come down where I have an option
01:39for Relationships and it's going to tell me there are no foreign key
01:44relationships right now.
01:45So I'll click Add to make one.
01:48Now it doesn't actually know what this relationship is.
01:51So it's actually telling me here that the Tables And Columns Specification property
01:55needs to be filled in before the new relationship will be accepted.
01:58That's this expandable drop down here, where right now it's saying "Well I don't
02:03know who the foreign key and the primary key tables are so please tell me" and
02:08there is a little ellipses button that you need to click. We open that up.
02:11What it's going to say is here's the drop-down for the primary key table.
02:15Who is really in charge, where is the one, and the one is going to be in the Product table.
02:22I select that and then click in the column below and I'll select ProductID.
02:27Foreign key table here, it says it's OrderItem. That's correct but it's not
02:30OrderItemID that I want to link up.
02:33Very commonly we are going to be linking up two columns with the same name.
02:37They don't have to have the same name, but they do have to have the same kind of data.
02:41You couldn't for example link an integer on one table to a date on another table.
02:48You'll notice that by tweaking this,
02:50it's actually saying up here as far as it's concerned
02:52it's foreign key _OrderItem_Product, just giving us a hint that there is two
02:57tables involved, and it's the OrderItem table and the Product table.
03:00So I am going to click OK and I'm going to click Close.
03:03Now this is still considered an unsaved change.
03:06So I am going to try and save it.
03:08I'll hit the Save OrderItem and it will say, "The following tables will be saved.
03:12Do you want to continue?"
03:13Kind of prompting me that there are really two changes here. Yes, I do.
03:17Unfortunately, it's giving me an error.
03:20Error was encountered during the save process.
03:22The ALTER TABLE statement conflicted with the foreign key constraint.
03:26So what's going on here?
03:28Well if I open back up this Product table and the OrderItem table, what's going
03:35to happen if I spent my time as I could scan through this Product ID and figure
03:40out that perhaps a couple of them just aren't right.
03:42I have a ProductID down here for 500.
03:45Well there is no product with ProductID of 500, and if we are trying to enforce
03:50that foreign key, that relationship, the database is saying no, we can't do it.
03:55We can't enforce it for product that doesn't exist.
03:58So let me say that perhaps this was intended to be 1005 and I've got another
04:03one here for 1012, because we don't have a 1012 product so that wouldn't work either.
04:09So you might have to do a little bit of tidying up of your data before you can
04:14enforce any new relationships.
04:16I am going to go back to the table design where it should still have our change saved.
04:21We've got the little asterisk marking that there is a dirty change here.
04:24So I am going to try and save it again. Save.
04:25Do I want to do this?
04:28Yes I do and well that seemed to work.
04:32So let's go ahead and see what impact this would have.
04:36If I go into my OrderItem now and actually start to change one of these, perhaps
04:42I'll change the one that was 500 and I'll change it back to 500.
04:46Well, it's not going to let me do that.
04:48This is the benefit of having that relationship, that foreign key, described.
04:52We now have the idea of integrity, which often referred to as
04:56referential integrity here.
04:57When you describe that relationship it means something.
05:01If you describe the ProductID in this table as the foreign key to another table,
05:05then you better have a legitimate value here.
05:08Now the impact of this does mean that in a lot of cases your applications have
05:12to be very careful in what order they do things.
05:15If for example, you wanted to add a new product and add a new order item,
05:19you better do them in that order, because the product needs to exist first.
05:24Once we add a new product, this one being added automatically with 1014,
05:33we could then change the value immediately to 1014 in another row
05:38that references that.
05:39We just can't at this point change it to 1050.
05:44And bearing in mind, if you want to create a many-to-many relationship, that's not
05:48created directly. You do two one to many, you do two of these.
05:53Follow that process twice with the in-between table.
05:56What if I wanted to take a look at this relationship, perhaps just confirm that
06:01it worked. And let me hit the Escape key to get out of my changes here, because
06:05it won't allow me to save that.
06:06I am going to make a new database diagram.
06:08I am going to add those two tables and what should happen is that closing that,
06:14it should show us the relationship. The key to the infinity symbol, the Product
06:19to OrderItem as the one to the many.
06:22We can have the option here to even use the diagram to delete the
06:25relationship from the database. Not something that I really want to do but
06:28something that I could do.
06:30I do get the option to tweak this.
06:31I am going to right-click one of the tables.
06:33I'll right-click OrderItem and jump into Relationships because the one thing
06:37that I did want to show you, bringing up this relationship is this little dropdown here.
06:43The INSERT and UPDATE Specification.
06:45You've got something called a Delete Rule and an Update Rule and what that means
06:49is if I delete something from the Product table and there are references to it
06:56in OrderItem table, will it let me do that?
06:58Now by default there should be No Action.
07:01No, that's a problem.
07:02But you can set it to do what's called cascading deletes.
07:06That means if I went into the Product table and I deleted the product with the
07:10ID 1005 that it would cascade down into the OrderItems and delete every order
07:16item with that foreign key of 1005.
07:20Cascading is not something that is typically recommended. You can have it turned on
07:25but I'm not going to.
07:26Just to go back and prove that I can see that in OrderItem, not only can't I
07:32change ProductID to something that doesn't exist -- so I'll hit Escape and change back --
07:37if I pick one of the ProductIDs that I am using, 1001 for example and go over
07:42there and try and delete the product row,
07:46it's going to say, "Are you sure? You are about to delete one rows."
07:48Yes I am going to try that.
07:49"Oh! I'm sorry you can't do it." Again, keeping the referential integrity of
07:54the database together.
07:56So we won't be allowed to do that.
07:58If I wanted to delete that product, I would first have to go and find every
08:03order item that referred to it, either change this ID or delete that row, and then
08:08and only then would it allow me to go back and delete the row of the product.
Collapse this transcript
Database normalization
00:00Database normalization is the process of taking your database design through a
00:04set of rules called normal forms.
00:07So that it conforms to relational database standards and you really want to
00:11do this, so that your database will contain a minimum of duplicate data or
00:16redundant data.
00:17It'll contain data that's easy to get to define to edit and maintain, and that
00:23you can perform operations even difficult ones on your database without creating
00:28garbage inside, without invalidating the state of it.
00:31It should be carried out for every database you design, and it's really not that hard,
00:36 even though yes, when you first start reading about database
00:39normalization, you're likely to run into phrases like "your database won't be
00:44in third normal form until every non- prime attribute of R is non-transitively
00:48dependent (i.e. directly dependent) on every candidate key of R," but you don't
00:52have to get into all this language.
00:54You just have to understand these were a set of rules developed about 40 years
00:59ago by E. F. Codd, the father of databases, and we step through them basically
01:05one, two, three, first normal form, second normal form, third normal form.
01:10So what's first normal form?
01:13Well, it starts off with stuff we've been doing already.
01:16Your data needs to have a unique key.
01:18It should always have a unique key.
01:20There are a few very rare situations in which you don't have a unique and
01:24primary key, but we're going to have one for all our databases.
01:28Really, the key for first normal form is that each of your columns, each of
01:32your fields, should contain one value and just one value and there should be no
01:38repeating groups.
01:40Okay, what does this mean with actually our tables?
01:43Well, let's say, for example, I begin developing a customer table.
01:47I've got a customer ID, so that's good. We've started off for first normal form.
01:51I've got the name of the customer and the city they're based in.
01:54Then what I decide to do is say that all our customers have a representative,
01:58the person we talk to.
02:00So add another column to the table.
02:02This would be the customer contact.
02:04Who do we speak to at ACME Corp?
02:06Who do we speak to at Two Trees or Acacia?
02:08The issue is what happens when one of these companies starts to grow a little bit,
02:13and we find out that we've got more than one contact.
02:15Well, there is a couple of ways you could deal with it.
02:19You could just start stuffing extra data into that one column.
02:24So we could just start putting commas or any other delimiter and putting
02:28multiple values in the one Contact column.
02:31Well this is a no-no.
02:33This is not in first normal form if you do this because first normal form
02:38demands that every column,
02:40every field, contains one and only one value.
02:43If you decide to show multiple values in like this, you'll find it harder to
02:47search, you'll find it harder to sort, you'll find it harder to maintain.
02:51Well, what some people do is they rip it out that way, go back to the original one,
02:55and then they start adding more columns, Contact, Contact 2, Contact 3.
03:01This is what's called a repeating group and there should be no repeating groups.
03:05The classic sign of a repeating group is fields with the same name and
03:09different numbers.
03:11We don't want either of these things.
03:13So what do we do?
03:14What we do is what we do for a lot of the normalization steps.
03:18We rip the Contact data out and create our own customerContact table.
03:23This then has relationships.
03:25We go to many-to-one relationship between customer and customerContact, where
03:29we can go from customer 1, find the contact, go from customer 2, find the
03:32contact, go from customer 3 and find the three contacts, and this would get it
03:37in first normal form. That's step one, because to go onto second normal form,
03:43well, first you have to be in first normal form.
03:45You don't pick and choose.
03:46You go through this one, two, three.
03:49Second normal form has the rather puzzling phrase that "any non-key field should
03:54be dependent on the entire primary key, " and that's about as simple as it can
03:58get phrased.
03:59Now what does this actually mean?
04:01Well, for most of what we've done in this course, this isn't an issue for us.
04:06We're already in second normal form.
04:08Let me show you a table that currently is in first normal form, but not
04:13in second normal form.
04:14I have an events table here that has an ID of a course and a Date and
04:18a CourseTitle.
04:19Now what's actually happening is this table has been defined so that it's using
04:25two columns as the key to it.
04:28This is what's referred to as a compound primary key.
04:31Instead of just one ID column, which I can't use the ID here, because as you see
04:35SQL101 appears multiple times, but I can combine the ID with the Date and in a
04:42lot of cases this makes sense.
04:44The issue is if you do this and use a compound key, you need to look at the
04:50other columns in this table.
04:52So I have got CourseTitle as Intro to SQL.
04:55Seats, five seats available.
04:57It's in room 14 and a lot of this information is unique to this one entry
05:02and that's fine.
05:03But second normal form demands that all my non-key columns, things aren't keys,
05:09CourseTitle, Seats and Room, they have to be dependent on the entire
05:13primary key.
05:14Now that is the case for Seats and Room.
05:17Those are unique values based on the fact that we're running this course on a
05:21particular date in a particular room with a certain number of seats available.
05:24But CourseTitle I could get from just the course ID part of the key.
05:31This might sound a bit ivory tower, but here's the impact.
05:34What happens if somebody reaches into this table and they change that course ID,
05:40because accidentally it was SQL101?
05:43It's now changed to ASP101.
05:43Well, now I've got the wrong title for a piece of data.
05:48That's because my data is not in second normal form, and if I now look at this row,
05:52ASP101, Intro to SQL, well, which one is right?
05:55Is it the wrong ID or the wrong title?
05:57I don't know.
05:58So how do we fix this?
06:00Well, once again we're going to rip out the CourseTitle.
06:04We're going to create a separate courses table where we can map the ID from the
06:10events table to the ID in the courses table and always have one specific value
06:16for one specific ID, and everything in the events table is all based on the
06:21whole key, in this case both ID and Date.
06:24Again, if you're not using compound keys, it's not really a concern.
06:27You can just step ahead, go right through second normal form and into third
06:32normal form. About as plain English as I can describe this one is that no non-key fields,
06:38and things that are not part of the primary key, none of them are
06:42dependent on another non-key field.
06:45This is in a way similar to second normal form.
06:47It's still saying, can I figure out any of the fields I have from other
06:52fields that I have?
06:53So for example, I'm looking at an updated version of the events table.
06:57This is in both first normal form and second normal form, but it's not in
07:02third normal form.
07:03It's not in first normal form.
07:05I have got my key.
07:07I don't have any repeating groups and I don't have any repeating values within a column.
07:12It's actually in second normal form because I have decided to change it to
07:16have one column primary key, which is EventID, but it's not in third normal form. Why?
07:22Well, what I can do is scan through my non-key fields, which for me is
07:26everything other than EventID.
07:27SQL101 is occurring on the 2nd of April.
07:33There are apparently five seats available. That's being held in room 14.
07:36There is a capacity of 18.
07:38These values, the date, the availability, the room, could be different from row
07:44to row so they're fine.
07:45The issue is with the Capacity column.
07:49If we are looking at the room, so Room 14 has 18 seats and Room 11 has 24 seats
07:54and Room 8 has 12 seats, well that means one non-key field that we have,
07:59Capacity, is dependent on another non-key field, Room.
08:03If we can figure out Capacity from Room, we don't need to store in the same table.
08:09We need to, you've guessed it, split this out into its own table.
08:12So we need to pull out Capacity from this table and just keep Room.
08:17That's as long as Room can always tell us the capacity if we have, say, a Room table.
08:22Another example of third normal form would be this, which is quite common.
08:26You'll often see it's an orderItems table, which has an ID and a ProductID and a
08:31UnitPrice and a Quantity and a Total, but if I look here the Total is based on
08:36Quantity times UnitPrice.
08:38Quantity and UnitPrice are both non-key fields.
08:41We can figure out what total is from the other fields that we have.
08:45So we rip it out.
08:47Don't store information that's easily ascertained from other non-key fields.
08:51Now, you can actually in SQL Server create what's called a computed,
08:55a calculated field that's not really stored in the database.
08:58So if you wanted this actual behavior to make an easy way to scan the Total
09:03particularly when you've got complex quantities, you can do that and I'll show
09:06you that a little later. But don't store it because there is nothing that would
09:10stop me from storing UnitPrice for 100, Quantity of 3, Total 75,000.
09:16It doesn't have to make sense and we want our data to make sense.
09:19Now in fact third normal form is quite an odd one, because you will actually
09:23find that a lot of tables out there are not in third normal form.
09:28A classic example is any table that's full of address information.
09:33If I look at a table like this and I see that I've got PostalCode being stored
09:38as the last column here, well, I can figure out what the City, the State and the
09:43name of the state are from the PostalCode.
09:45That means I have non-key fields that are dependent on another non-key field.
09:50You've probably had situations yourself where if you're talking to someone on
09:54the phone and filling in an address, they don't actually ask for the city and state.
09:57They just say, "Can I get the postal code?" because that's all they really
10:00need is the postal code.
10:02If I get the postal code, I can figure out the rest of it.
10:05Now AddressLine1 is not dependent on the PostalCode.
10:10So AddressLine1 isn't a problem.
10:12It's City, State and the name of the state.
10:15We could rip out that information and put it in a separate table.
10:19Now, a lot of the time we don't do that just because it makes it easier and
10:22quicker to scan through, say, address tables, and in fact the process of taking
10:27all the way to third normal form, ripping the stuff out and then deciding to put
10:31it back in, is what's called denormalization.
10:34But make no mistake.
10:36If you decide to store the City and the State and the name of the state
10:40information in your address table, you are storing redundant data.
10:44There is no real reason why you should store the ZIP code 91502 and the city
10:49Burbank and the state of CA, and the full name of California a thousand times,
10:53when you could get it all from having a zip code database with a city
10:58and a state in it.
10:59So you might denormalize to make things a bit more efficient, but do it knowingly.
11:04And those really are the three steps that we would go through.
11:08You can take normalization even further into what are called voice card and
11:13fourth and fifth normal forms, but that's really not very typical and I've very,
11:17very rarely run across that.
11:19We want to take our database designs through the first normal form, about our
11:25primary key and on non-repeating fields.
11:27Our second normal form, making sure our data is based on the whole key, and third
11:33normal form, that all of our data is based on the whole key, or if you prefer,
11:37the quicken mnemonic is that your data should always be based on the key, the
11:42whole key and nothing, but the key. So help me God.
Collapse this transcript
Creating computed columns
00:00There is a column that you can add to your tables that comes in handy from
00:05time-to-time and really it's not a real column at all. What it is, is a
00:09calculated or computed column.
00:11So let's say for example we're working with this OrderItem table, and I've got
00:16multiple entries here.
00:17I've only got 20 or so but we could have 50,000 of them that deal with
00:21ProductIDs, Quantities and UnitPrice.
00:24Or you might find it useful to have kind of a rolling total of each order item going,
00:30particularly if you've got a lot of separate quantities.
00:33So instead of having to load this data into an application to then perform a
00:37calculation, it just might be nice if each order item entry had its own little subtotal.
00:43So we can do that of course. We could just add a regular column. I'll go into
00:48that table into Design view and I'll add a new column called Total. I'll make
00:53it a money column, save my changes, I've allowed Null so won't complain about
00:59that right now, and then I go back in and try and edit directly in SQL Server Management Studio.
01:05One order item, which is 1 of 12.32 would be 12.32, 1 of 234 would be 234, and so on.
01:14The issue is there is nothing that's actually stopping me here from typing in
01:1950,000, and now there is no relationship between the total and the unit price
01:24and quantity, and there really should be.
01:27And it's for this reason that in general database design, not just in SQL Server
01:32but in overall database design, you should never be creating a column that
01:37stores a separate value that can be calculated based on other columns.
01:42So we don't want to do it this way.
01:43However, we can still get the result that we want.
01:45I am going to go back into Design, come down into the Total of money, and what
01:50I'm going to do is come down into the Column Properties and this is the part
01:55that I'm interested in, Computed Column Specification.
01:59But I am telling it, don't store this data separately.
02:02Just figure it out every time I ask for one of these rows. And it gives a place
02:06to type in a formula, which for us is very simple.
02:09It's simply Quantity * UnitPrice.
02:17Is Persisted mean is saved No. Just leave that at the default and what that
02:20actually means is I'll save this, and there I was talking about making
02:24changes to two tables simply because I have a relationship between this one
02:28and the Product table.
02:30But when I now go into OrderItem and click Edit the Top 200 Rows I can see that
02:35total is automatically doing this totaling of the Quantity * UnitPrice.
02:40So I've got 3 of 74.12, 222.36.
02:46It shows up in this kind of a lighter brown color as does the primary key, giving
02:51us the hint that we're not supposed to directly enter in anything. In fact if we do
02:56it will be ignored.
02:57So a computed column result is actually recalculated when we retrieve, when we
03:02ask for that data back, but it can be very useful or at least very convenient
03:07for the data in your tables.
Collapse this transcript
5. Importing Existing Data
Using the SQL Server Import and Export Wizard
00:00It's a very common need that when creating a database we need to import some
00:04data that exists somewhere else.
00:07That could be in an Excel file, a CSV file, an Access database, even another
00:12instance of SQL Server or a previous version of it, or we can do all of these
00:16things using the SQL Server Import/Export Wizard.
00:20Under my installed programs for SQL Server 2008 R2, I actually have this link,
00:25Import and Export Data 32-bit and 64-bit, I have two versions.
00:30Now the reason for that is that while my SQL Server instance is installed as
00:3564-bit, some of the data sources that I might want to import from or export to aren't
00:4264-bit so I need to talk to them using the 32-bit version, and basically if one
00:46of them doesn't work try the other.
00:48So I've opened the wizard here.
00:50It just gives me a greeting message that it will copy between databases,
00:54spreadsheets, text files, and so on.
00:56I'm going to click Next because I need to give it two pieces of information:
01:00where am I copying from and where I'm copying to.
01:03Now when I click this top level drop- down box I can see I have a bunch of
01:08selections here. I've got the ability to talk to Oracle, to talk to Microsoft
01:12Excel, to talk to Access SQL Server, and depending which one I pick, I may have
01:18to provide other really simple information such as selecting from Excel, give it
01:22the file path, and say which version it is.
01:25If how many other hand I'd selected to import from Oracle, we have all the .NET
01:30Framework Data Provider source and I have to provide things like the password
01:34and the user ID and the data source information.
01:37Essentially once this is declared then we just double it up about and say where
01:41it's actually going to.
01:42It will then step us through the process.
01:45I'm going to show a couple of examples of that in just a minute.
01:48Now one thing to show is though even that we could copy from SQL Server itself
01:53using this method, it's not the suggested way to do it. In fact if I click the
01:57Back button to that first greeting page, it does tell you or remind you to move
02:02or copy databases and their objects from one SQL Server instance to another,
02:06cancel this wizard and use the Copy Database Wizard instead, I'll show you where
02:10that is in just a minute.
02:12And this is the last thing before I get started. I would say that this same thing
02:17is available through SQL Server Management Studio.
02:20So if you are in the middle of designing your databases, say my TwoTreesTest, and
02:25I know that I want to import some information here, if I right-click my database,
02:30come down to Tasks, which shows me a whole bunch of different things.
02:34Take it offline, shrink it, back it up, restore it. But down here I have Import
02:39Data and Export Data.
02:40Well, not surprisingly clicking those is just going to open up exactly the same
02:46wizard that will allow us to step through that process.
02:50Again we're getting that same message that to use the Copy Database Wizard if
02:54we want to copy SQL Server databases and you won't find that under any of the tools options.
02:59It is again under one of the tasks.
03:02Instead of selecting the task to import and export, I'd clicked Copy Database.
03:08That would open up the Copy Database Wizard.
03:10I'm not going to use that one right now. In fact, I'm basically going to shut
03:14that down and show you one more thing here.
03:18And this is just the phrase that it uses on the welcome page of the wizard.
03:23This wizard helps you to create simple packages. Now what does this mean?
03:28Well we're really using a part of SQL Server called SQL Server Integration
03:33Services, which can get much more involved than just a simple import, and if
03:38you're looking at setting I'm not just a one-off import but a scheduled,
03:41repeatable, automated import that happens twice-a-week or even twice-a-day,
03:46you're going to be looking at using SQL Server Integration Services.
03:50And I'll be covering that towards the end of this course.
03:52But before we get to that let me walk through a few examples of just using this Import/Export Wizard.
Collapse this transcript
Importing Excel files into SQL Server
00:00So I'm going to go ahead and take a straightforward Excel spreadsheet and
00:05import that into SQL Server.
00:07This is the spreadsheet that I have at the moment.
00:09It's a list of names, addresses, and zips and email, just some
00:13contact information.
00:15I can see that the first row does seem to contain the column names for this, so
00:19that might be useful in just a moment.
00:21But that's what I want. I just want it in my database, because it's way too easy
00:25to mess it up when it's an Excel.
00:27So I'm going to open up the SQL Server 2008 R2, Import and Export Data.
00:34I'll open up the 32-bit one, so it should be all I would need here.
00:37I'm going to click Next.
00:39Select that my data source is Microsoft Excel.
00:43It's going to ask me for the file path, which is just out here on my desktop.
00:48And it's going to ask me for the Excel version and I think it's 97-2003.
00:51If there is a problem with that it will probably tell me in just a moment.
00:55And I'll leave this checked.
00:57Yes the first row has column names. I'll click next.
00:59It's asking me now where is it going to.
01:02Well, this really means the current SQL Server, SQL Server Native Client 10.0.
01:06But I could be copying to anywhere.
01:09Again, all these management tools don't have to be running on the local machine
01:14of a SQL Server instance, so it's just asking for the server name.
01:17Well, I'm just going to put in the period here. I could type in the name of my
01:22machine as well. That would work.
01:24If I click the drop-down box it will try and see if there's any more that it
01:27knows about and there won't be right now. So that's just my local machine.
01:32It doesn't matter which one I use.
01:33I'm going to say yes, I'm talking to it Using Windows Authentication.
01:37And because I've named that if I click the drop-down box now I should see a list
01:41all of the databases there, and yes I do.
01:44I'm going to select my TwoTreesTest custom database that I have.
01:49I would even have the option to make a new database at this time.
01:52Now I'm going to do that. I'm going to click Next and it's going to ask
01:56what we are copying.
01:57Now this is a little bit over-complex for what I'm doing right now, because it's
02:00saying are we copying data from one or more tables or views?
02:05And this is a bit more useful when say you're coping from an Access database and
02:09you only want to copy certain pieces of it.
02:12I'm going to leave the default selected and click Next and it's going to tell
02:15me this is the stuff that it knows about.
02:18Apparently, it's got a source of data called exceldata$.
02:22And it will create something called dbo.exceldata$.
02:26Not quite what I'm after, but I can always change that later.
02:29The interesting button here is going to be this one down at the bottom, Edit Mappings.
02:34That's going to tell me what it's going to try and do with the columns in the
02:39Excel data source and what it's going to map it to.
02:42Now right now, they're named quite well.
02:44We've got the FirstName, LastName, Address, City, State, and so on.
02:47I'll keep those names and it will even tell me here what it's going to do with them,
02:52what data types it's going to use when it creates this table.
02:56And mostly they're listed as nvarchar, so variable-length Unicode fields that
03:00are nullable and is this generic size of 255.
03:05That's not quite what I was looking for and this one definitely looks wrong.
03:09Yes my Zip is numeric, but it's certainly not a float.
03:13That's an estimate based on the data that existed in that spreadsheet.
03:18So I could change that.
03:19And again, depending on what I'm doing, whether the postal codes I know
03:23might have letters in them or they might have dashes, even if I'm using US postal codes.
03:28So I could change that to an nvarchar as well.
03:32But I see that what it did there was actually change it to max, which is
03:36not what I really want.
03:37It doesn't need 2 gigabytes of space to go and in fact, 20 would probably do it just fine.
03:42You will occasionally find that it does this, where
03:44it doesn't accept something that you're typing into.
03:47And there is a couple of different ways as you could do it.
03:49So for example, I've changed the data type and suddenly seemed to take my 20.
03:52So I'm going to now change it back. It just seems to be a little of strange
03:56behavior I've seen a couple of times.
03:58And it's up to you whether you want to change them to be Nullable or not.
04:03I'm going to leave this as is. Click OK.
04:06Then I'm going to click Preview, which just shows me, yes this is the data that
04:11it's going to try and fetch from Excel.
04:14It all seems to be mapped correctly. I'll click OK.
04:17Click Next.
04:18It's giving me a little prompt here that it's kind of letting me know that it
04:22thinks that zip was a double, and I'm wanting to store in an nvarchar.
04:26So is there a conversion process going on? And yes there is.
04:30Well, what if there's an error, what do I want to do?
04:33Do I want to do something special, do I want to fail the whole thing or just
04:36ignore it and move on? If the data was going to be truncated,
04:40so if say I decided to move it into a varchar with a maximum length of two digits,
04:46we'd have a lot of truncation going on.
04:48I'm just going to use the Global Settings, which are these ones down here.
04:52Any error makes it fail and any truncation makes it fail.
04:56I'll click Next, and I'll tell it yes, Run immediately.
05:00The other option would be to save this as an SSIS Package, which is SQL Server
05:05Integration Services.
05:07Again, we'll talk about more of that later.
05:09But just to let you know, even a simple import that you're doing is using
05:12integration services.
05:13I'm going to say Run immediately. Click Next.
05:16It gives me a summary and click Finish.
05:19It runs through a whole bunch of stuff. We even get a message here that 101
05:23rows were transferred.
05:25I'm going to close this, go back into SQL Server Management Studio, connect to
05:30the database, and not surprisingly we should find now in our tables this
05:36rather ugly looking dbo.exceldata$, but I could open it up and say Select Top 1000 Rows.
05:43And we're bringing back all our content.
05:46I could right-click the table right now, and just rename it to something more specific.
05:50Let's imagine these were coming from customers, so I called it the Customer table.
05:55And of course, I can even right-click and hit Design, jump into this, play
06:01around with the data types, even add a primary key, perhaps even generate one.
06:06This is the basic procedure of how you would import from an Excel spreadsheet.
Collapse this transcript
Importing CSV files into SQL Server
00:00If the data that you want to import is in comma separated values or indeed in
00:05anything that's just a regular text delimited file--
00:08in fact here I've got to some very straightforward values, very similar to the
00:12last time, but they are separated with the vertical bar.
00:15I can also bring this into SQL Server although the way of doing it does have
00:19quite a few different options than doing it with Excel.
00:23So I'm going to go ahead and open up my Import and Export Data Wizard.
00:28I'm going to use the 32-bit one.
00:30The source I'm going to pick here is Flat File Source.
00:34And when I select that, not surprisingly it's going to ask me to browse to that file,
00:39which is currently saved as a CSV on my desktop.
00:42And once I do that we have a very different screen for determining what that data is.
00:48If I click on the Columns section on the left, it will take a look and think yes,
00:52this column delimiter seems to be a vertical bar, and if you select that, this is
00:56what the date is going to look like and it looks pretty good.
00:59Although it does seem to say that the first name, last name, address, and
01:03city are values. Now it's not the case. I actually did one of those to be the column names.
01:08If I look back on my general section I'll see that I do have a checkbox.
01:13The column names are in the first data row.
01:15So we click back to the Columns, and yes we seem to have that now sorted correctly.
01:19Interestingly, if I go to the Advanced section, it will tell me the way it's
01:24interpreting these values.
01:26So it's breaking down all the different columns and saying FirstName it thinks is
01:30a string that's 50, and LastName as a string that's 50, and Address as a string that's 50.
01:35And in fact you'll see this a lot.
01:36It's pretty much going to assume that everything as a string of 50, even a zip,
01:42and email, and phone, and that's okay, but it might lead us into issues.
01:47For example, I'm pretty sure I have some e-mail addresses that are longer
01:50than 50 characters.
01:52There is a button-down here called Suggest Types.
01:55Now what that will do is look through some of this semi-mapped data and make
02:01a prediction that if something is purely numeric, well maybe it should be an integer.
02:06I'm not a big fan of using Suggest Types, because I tend to find that it gets a bit too pessimistic.
02:12So for example, if I were to click Suggest Types here, it might go through
02:16all the last names
02:17and if the largest one that it finds is 12 characters, it's going to assume that
02:21the widest this column needs to be is 12 characters.
02:25That may be true, but that may lead you into issues later on.
02:28So I'm going to leave that alone.
02:31If I click Next, we then say where are we copying this data to.
02:34And I'll get a chance to map it again in just a minute, but I'm copying it to
02:38my current SQL Server database, which I'm going to represent with just the dot, the period.
02:43I'm using Windows Authentication and that should give me the drop-down list
02:47where I can select my new custom database. Click Next.
02:52It's telling me it's going to import a table.
02:55That's importing from the CSV data and it's going to create a table called csvdata.
03:01I could choose to change that if I wanted, but let me just leave that right now.
03:05Once again, I'm going to click Edit Mappings.
03:08Now we'll see here that everything is now being listed as a varchar with
03:11a maximum size of 50. That may work.
03:15That may not.
03:16In fact I'm pretty sure that certain things like my address should be at least a
03:22hundred and that my e-mail should be at least a hundred as well.
03:27I'm going to click OK, click Next, tell it that yes I want to run immediately.
03:32click Finish and go.
03:34And we see that we've got an error here, we've got some messages popping-up
03:37hat's saying, unfortunately the output column Email failed because
03:44truncation occurred.
03:45And the truncation row disposition on the output column specifies failure on
03:50truncation. What does this mean?
03:53Again it's to do with how we're specifying this copy, this import, to work.
03:58I'm going to click Back quite a bit here and go back to where it thinks these
04:05mappings occur, and in fact the problem is probably on Email. Wll that's were
04:09it said the problem was.
04:11Right now it does think that it only needs 50 characters to do its mapping.
04:14I'm going to say no, you need a hundred characters or truncation will occur.
04:19I'm going to click Next and go ahead keeping the same data.
04:24What I've got an issue with now is with Mappings.
04:27So we'll take a quick scan of those, now it did say address was different, but
04:30it didn't complain about address so I'll just leave that.
04:33Click Finish and try this again, and that looks a lot better.
04:36We now have a warning here. The total number of rows processed is 101.
04:41There is a partial row at the end of the file.
04:44That's quite common message to get. You can check that later, but we do seem to
04:48have a hundred rows transferred.
04:50So I'm going to close that, open up Management Studio, and just double-check that
04:54everything looks okay.
04:59And it does seem to have brought everything in.
05:01We do have the columns named correctly.
05:04We do have a hundred rows.
05:06It does not seem to have created anything in that last one, so we're looking good.
05:10Once again of course we could change the definitions of that.
05:13But if you know that you are immediately needing to go in and change this
05:16information, you should try and do a lot of it as you are mapping it before you
05:20actually create the table during the import.
05:22But that's the process of bringing in a CSV file.
Collapse this transcript
Importing Access databases into SQL Server
00:00If the requirement you have is to take an Access database and get back into SQL Server,
00:05either all of it or part of it, you have a couple of ways of doing that.
00:09Not surprisingly one of them is using the Import and Export Wizard.
00:14But it's possibly a little different from what you're expecting.
00:18If I pick the 32-bit one, I am going to click the Next button and then when I
00:22select my data source, I could just easily carry on and select Microsoft Access.
00:27It asks me to browse the file.
00:29It's looking for an Access database.
00:31But I'm browsing to my desktop and it doesn't show up.
00:35That's because it's going to be looking for the old school pre-Access 2007
00:39Access files, .mdb files which are not going to show up.
00:44If I try and bypass that by just selecting all files and then I will select this
00:48classic Northwind Access database.
00:51It's in the .accdb format. I could try that.
00:55I'll click Next and it's not going to work at all.
00:59That's because this original data source is old school Microsoft Access.
01:04It's not going to work.
01:06Well, what can I do?
01:08Well, not a lot immediately, because what you need to have installed-- and I'm
01:14just going to open up an Internet Explorer.
01:16If you are importing from Access 2007 or Access 2010, before you do this,
01:23you will need to go out to microsoft.com/downloads and download some
01:29connectivity components.
01:31In fact, that's what I am going to search for here is the phrase "data
01:35connectivity components."
01:40You'll see a couple of different choices here.
01:42If you have the 2007 version, you can get the Office 2007 System Driver:
01:47Data Connectivity Components. You want it to come from Access 2007.
01:50If you're coming from Access 2010, get the Microsoft Access Database Engine 2010
01:57Redistributable, which comes in two versions, either the 32-bit or the 64-bit.
02:03Now I've installed the 64-bit.
02:05What does that mean?
02:07Well, it means right now when I open up my 64-bit Import/Export Wizard,
02:14what I'm going to find on this is a data source of Microsoft Office 12.0 Access Database Engine.
02:22Now this, formally speaking is what's called an OLE DB Data Provider, which is a
02:27little bit of a tedious phrase.
02:29In fact, when you try and open it, it's not going to just say "Well, point to the accede."
02:36Really that's what it's after. It just doesn't make it obvious.
02:39So it says "Enter the data source and/or location of the data."
02:43Location is blanked out.
02:45But I actually can enter the data source of this.
02:47In fact, if I just right-click it to find the address of it here,
02:51it's right here on my desktop.
02:53I am going to copy that path and paste that in and \Northwind.accdb.
03:02I am going to click the Test Connection button and it says test
03:05connection succeeded.
03:07Once they do that, it's going to allow me to click on Next through, to select
03:11then where I'm going to put it.
03:13In this case, I might decide to create a new database or I could put it in
03:19the old Two Trees Test.
03:21I got the same screen I got with an Excel import that I can select from one or
03:26more tables or views.
03:27And if I do that, it will give me the screen here where I can choose just
03:32individual pieces of this Access database or the entire thing, with each one then
03:38breaking down into the mappings that it's going to do.
03:41Now, if you're looking in this with the idea that you are going to check
03:46everything and bring in the entire Access database, you can do it this way.
03:51I just wouldn't advise it.
03:53I'll show you the way that you do it after that.
03:56So if I just said I'm going to bring in one piece of information, which is the
04:00Products table, just unchecking that, select Products, click Next.
04:06It just gives us the same idea that it's going to create a SQL Server
04:10Integration Services package and it's copied across 45 rows.
04:14So that's just one thing.
04:16If, on the other hand, what you are wanting to do is take the entire Access database,
04:20this is the way I would advise it.
04:22In 2007 or 2010, open up Access and get into your system however it is you need to do it.
04:30You want to close down any object you have open and simply select from your
04:35Database Tools, where you have this upsizing wizard, a way of migrating some or
04:41all of your database into SQL Server.
04:43So this way you are doing a push and what it is going to step you through is the whole process.
04:48Would you like to use an existing database or create a new one?
04:50I am going to say create a new one.
04:53It's asking the same kind of information that we have to give when we are
04:56connecting to the SQL Server ourselves.
04:58What machine is it?
04:59Well we are using the phrase "local," which is fine. A dot would work as well.
05:03It's going to ask, do I have a login ID and password? Well, no, I don't.
05:06I'm logged in right now as the administrator.
05:09So I am going to tell it to use the current connection, the trusted connection.
05:12It's going to then say if you want to make a new database, what you want to call it?
05:16We will say NorthwindSQL. I will click Next.
05:18Which tables do you want?
05:20Let's just say all of them. Click two arrows, then Next.
05:24Then also asking, what do I want to upsize?
05:28Do I want to copy across my indexes or my validation rules?
05:32Or my default values and table relationships.
05:35In fact, I am going to take all of them.
05:37I am going to leave the other options in the default. Click Next.
05:41It's going to give me the option to kind of separate the data parts from
05:44the Access front end.
05:46I'm not going to do any of them.
05:47I just want to take all the data and move it up into SQL Server.
05:51So I will click Next.
05:52It says it has all the information it needs. Then click Finish.
05:55This may take a few minutes depending on the size of your database.
06:00But when it finally comes back, it will even give you this Upsizing Wizard Report
06:04and tell you the details of what it's actually taken and uploaded.
06:10Information about how it's mapped the different fields across here, so you can
06:14actually page through that.
06:16Certainly, something that you will often have to do is a little bit of
06:19revisiting the table design once you get in into SQL Server.
06:22So don't imagine that everything is going to work perfectly.
06:26I didn't get any errors on this because it was a fairly vanilla database of the
06:31basic Northwind sample.
06:32But what I would like to do is certainly go and check and see if it's in
06:36SQL Server correctly. And I expect so.
06:39The database was called NorthwindSQL.
06:42That looks like it's there.
06:44I can even start using the regular database diagramming tools once I've provisioned them.
06:49I can say, for example, create a new database diagram.
06:52And I'm going to add just several tables on the top of it.
06:56The reason I am doing this is just to show that you can tell the database design
07:01has mapped across correctly and even as we start to look at them, things like
07:05the relationships have started to be copied across as well into parts of this.
07:10Of course, you're going to take some time and verify that everything seems to work.
07:15But it's looking pretty good right now.
07:19So your real decision most of the time is when you're bringing in information
07:22from Access, are you just bringing in a table here or there?
07:26In which case, using the Import/Export Wizard is just fine.
07:29If you're trying to migrate the entire database, I do it more of a push from the
07:33Microsoft Access side.
Collapse this transcript
6. Retrieving Data with SQL
Introduction to Transact-SQL
00:00Structured Query Language or SQL is the common language that lies at the heart
00:05of not only Microsoft SQL Server, but every RDBMS that you're likely to use.
00:11Now, you've probably noticed the different pronunciation of this.
00:14Some people say "Sequel." Others say "S-Q-L."
00:17I tend to say S-Q-L when I talk about the original language itself
00:20and "Sequel" when it's combined as an SQL Server, T-SQL or MySQL.
00:26SQL the language has been around since the 70s.
00:29It's actually one of the few languages that I was writing 25 years ago and
00:33I'm still writing now.
00:35A lot of programming languages have come and gone, become fashionable and
00:39unfashionable, but SQL has stuck around and I still expect it to stay for a long time to come.
00:44Now SQL is a pretty small language and the key to learning it is to understand
00:50that it's a little different from other programming languages you might have come
00:53across, things like C, Java, or Python.
00:57SQL is what's called a declarative language whereas those others are what are
01:01referred to as procedural or imperative languages.
01:04Okay, this is jargon, yes.
01:07What it means with a declarative language is that you use SQL to describe
01:12what you want and you let the database management system handle how that's actually done.
01:17You don't have to manually layout the algorithm, the different steps of the
01:21procedure, as you would do in other programming languages.
01:25So let's say we have got a thousand different products and I want to know which
01:29of these products have a price of less than a hundred dollars.
01:32Well, in a procedural or imperative language like C or Java, I would describe
01:37the steps to do this.
01:39I would write some code that would loop through all the individual products one
01:43by one, and every time I went through the loop, I will have to ask the question
01:46again, is this less than a hundred?
01:48If so, do one thing.
01:50If not, do another thing.
01:52Even in pseudocode it looks complex.
01:54I would be writing a loop, I would be writing conditions, I would be
01:58writing return statements.
02:00That's a procedural language, but in SQL you describe what you want.
02:05In English, you would say I want all products less than $100.
02:08That written in SQL is this: SELECT * FROM products WHERE price < hundred.
02:16No conditions, no loops, no return statements. Just the description.
02:21The database management system will look at your data,
02:24it will figure it out, and it will take this and return what's called the
02:29result set, whether this is one product or five hundred products or even none,
02:34based on this query.
02:36So SQL can be used to retrieve or read your data and ask questions of it.
02:41But it can also be used to create data, to update it, and to delete it.
02:46This Create, Read, Update and Delete is often referred to with a
02:50wonderful acronym of CRUD.
02:52But SQL can be used actually to create the databases themselves as well.
02:57Now, this module is about something called T-SQL.
03:00That's because most database management systems have their own implementation of
03:06the core SQL language.
03:08If you work with Oracle, you're using something called PL/SQL.
03:10We're working with Microsoft SQL Server and Microsoft's flavor of SQL they call
03:17Transact-SQL or T-SQL for short.
03:20It's still SQL at heart, but there are a few simple things added to it that will
03:24come in handy later on.
03:26SQL is a language you can learn in a few hours and be using it for decades.
03:32Get rid of all the fancy GUI interfaces, the query builders and the qizards and
03:36you can still do anything you need to do to a database as long as you have a
03:40command line prompt and know a little SQL.
Collapse this transcript
Using SELECT statements
00:00Let's begin by going through some simple SQL to read or retrieve
00:04information from our database.
00:06I am going to open up SQL Server Management Studio and connect to my instance here.
00:12After it's open, the button that I am going to click is this one, the New Query
00:15button on the toolbar.
00:17A query, well, that's the Q in SQL.
00:20It's Structured Query Language.
00:22We write queries and this one will use the most common piece of SQL, the SELECT statement.
00:29We are selecting or choosing information from one of the tables in one of our databases.
00:35So I am going to write this phrase to begin with SELECT *, using the asterisk, From.
00:40I want to get everything from somewhere.
00:43Well, there's the question. Which database?
00:46Which table?
00:48Because in SQL Server Management Studio I can see that I have got quite a few
00:51databases, even the system databases.
00:54And I could be writing a select statement that goes against any of them.
00:57So I am first going to have to say which database I'm coming from.
01:00So I'm going to say I want to use AdventureWorksLT.
01:03So I will type A and I immediately get this IntelliSense, this auto complete
01:08that pops up which is great.
01:09I am going to come down with my cursor key to AdventureWorksLT and that's the
01:13one that I am going to use, the LT version, then do a dot, and then instead of
01:17getting directly to the tables, I am getting to the schemas and roles here.
01:21I will explain that in just a second.
01:22But for the moment, I will select SalesLT., come down to Customer.
01:29This is actually a complete SQL query.
01:32I don't need to finish it with a semicolon or anything like that.
01:36I can actually go ahead and run or execute this either by clicking this button
01:40that says Execute or I could hit the F5 key on my keyboard.
01:44It would do the same thing.
01:46Down here it says Query executed successfully.
01:48It took 847 rows of data out of the database and it's now showing them to me.
01:54It is just a select, so you're not going to find these are editable.
01:57We are just reading this information.
01:59By convention, you'll see that the SQL keywords such as Select and Insert and
02:05Update and From, and we will see quite a few of them, are written in uppercase.
02:10It actually doesn't matter.
02:11SQL Server Management Studio and even SQL Server is not going to care.
02:15I could just as easily write select in all lowercase and it would work as well.
02:20But by convention of many, many years you tend to see SQL statements written
02:25with the keywords in uppercase.
02:27If we were going to write a lot of SQL statements, it would get a little tedious.
02:31You'd have to write the name of the database again and again and again, because
02:35typically you are going against the same database all the time.
02:38But I do have to say which database I'm interested in.
02:41There's a couple of ways to do it.
02:43I can put it in the From part to say explicitly what table and what scheme and what database.
02:49Another way that I could do it if I was going to write many SQL statements is
02:54before the first one, I just have the phrase Use AdventureWorksLT.
03:00This would do exactly the same thing.
03:02If I hit Execute again, I will see the same results.
03:05Another way that I could do it, if you notice that this Use AdventureWorksLT,
03:10we've also got it popping up here.
03:12This is the equivalent.
03:13It's actually selecting the database that we are interested in right now.
03:18So, I can also get rid of that and just make sure that the correct database is
03:22showing up in SQL Server Management Studio.
03:26Execute and there we go.
03:28It's a common problem for beginners that you're writing a piece of SQL and
03:32you think it's correct, but without you noticing it, the wrong database is highlighted.
03:38Say master is the very common default database and when you execute, it says
03:43"I have no idea what this SalesLT.Customer is."
03:48So I will make sure I'm on the right one.
03:49Now I am using the SELECT * here.
03:54SELECT * just means select everything.
03:57Give me every column in this particular table.
04:01While that's nice and convenient when you're in SQL Server Management Studio,
04:04you probably don't want to end up doing that.
04:07Because when you're writing say an application, of course the database may change.
04:11So you want to be very specific about the columns you are going to retrieve.
04:14All I can see that I have things like FirstName, MiddleName, LastName,
04:18SalesPerson, his EmailAddress is here.
04:21So I can instead of using the asterisks, I can say I would like FirstName.
04:27And you notice I have IntelliSense there. Comma, LastName, comma, EmailAddress From SalesLT Customer.
04:36I can break this up onto another line as well.
04:39This will work just fine.
04:40If I hit Execute now, then I get a filtered amount of data.
04:44This is what you want to do as a general rule unless you are writing really
04:47casual SQL statements.
04:49Be specific about the columns that you're interested in.
04:52Now, if we were writing this SQL because, for example, we were going to be
04:56printing this information on a report, we might even want to combine the
05:01FirstName and LastName.
05:02Even though they are stored separately in the database, which is a really good way
05:06of filtering and sorting things, combining them there is a way we are going
05:10to write them out in a report or an email.
05:13Well, we can combine them on the fly within SQL.
05:16I can actually instead of using the comma here, I could use the plus sign to
05:21concatenate or join these two together.
05:23Now, if I just use the plus sign, we are going to have a problem.
05:26If I hit Execute, what I am going to see is we are combining or concatenating
05:32these two together into this and it says, "Well, there's no column name."
05:36It is no FirstName or LastName.
05:37It's kind of something in-between.
05:39Not only that, but we have the FirstName and LastName squished up against each other.
05:45Well, we'll fix that in two ways.
05:47One is I am going to say that rather than combine FirstName and LastName
05:51directly together, I'm going to combine them with a space in the middle.
05:55Now, when we are doing a space or a string, we use the single quotes to separate it.
06:01So I am doing single quote, space, single quote. Add FirstName, then a space, add LastName.
06:06We will execute that and that looks a little better in the values that we are
06:11getting back, but it still says No column name.
06:14It probably be useful to have a kind of madeup or constructed column name here.
06:19The way we do it is this.
06:21We are saying I want to select FirstName and space and LastName AS FullName.
06:27There isn't a FullName column in that table.
06:30So not a problem there.
06:31But we can make it seem like there is.
06:33So I execute that and now it seems like we are retrieving the FullName column
06:38and the EmailAddress.
06:40So what we've seen very quickly is just by using a few little key phrases,
06:44we can start to get very flexible about the information that we're retrieving.
06:48Now, right now I'm bringing back 847 rows. That might not be what I want.
06:53We are going to go ahead and start adding on to this simple SELECT
06:57statement where we are saying select the data that we want from the table
07:03that we are interested in.
07:04But we need to move on and make sure that we are only retrieving the data in the
07:08right circumstance or in the right order or when certain conditions are true.
07:13So we are going to build on that next.
Collapse this transcript
Changing the default database
00:00When you first begin working with SQL Server Management Studio, a lot of
00:04people do write queries.
00:05That's how they're getting to grips with the database and with SQL.
00:09Some folks can get a little frustrated by the fact that when you reopen
00:12Management Studio and click New Query, you will find this drop-down list of the
00:18default database often shifts back to whatever it was.
00:23Meaning that you have to remember to change it or to write the word USE
00:28AdventureWorksLT for example, or to include the name of the database in your SQL statement.
00:33And that can be a little annoying.
00:35Now what a lot of people try and do is they say, okay, if it keeps coming up
00:39with master or a different database, they go and look in say Tools > Options
00:44and they try and find some setting in SQL Server Management Studio where this is controlled.
00:50And they won't find one.
00:51So sometimes they give up and say, well, it's always going to be master or it's
00:55always going to be a particular database.
00:57Well, really, this setting has not a lot to do with SQL Server Management Studio
01:03and more to do with how you are logged on to this database instance, because
01:08every user has their own identity in SQL Server.
01:12If I expand the Security folder, the one right of the top here,
01:15I am going to expand Logins and find myself listed here.
01:19This is all the information about me as a user logged on to SQL Server.
01:23I right-click and select Properties.
01:26What I will find is that I have a default database.
01:30And what's happening is Management Studio is just reading my settings.
01:34What I need to do is change this to, for example, AdventureWorksLT, click OK,
01:41and now if I close down Management Studio and a little later or the next day I
01:47reopen it, I connect to that instance, and I say I want to make a new query,
01:51it's going to read my data, say what is your default database, and that's what
01:56will show up in this drop-down list.
01:58So, you might find that convenient from time to time.
02:01Though do bear in mind that every different user has a different default
02:05database and if somebody else has a different one from you, you might not
02:09necessarily just be able to copy and paste them SQL in an email and have it work.
02:14But this makes it a bit more convenient than remembering to type the name of the
02:18database all the time in your SQL.
Collapse this transcript
Creating conditions in SQL
00:01So we have this very simple SQL query, selecting these three columns from the
00:06SalesLT.Customer table in the AdventureWorksLT database.
00:11If I execute that, it's going to bring back the entire contents of that
00:14table, which is 847 rows.
00:17But I can either see by scanning through it or down here in the status bar, 847 rows.
00:23But of course, if I don't want all those rows available, if I want to filter
00:27that information, it's a very common thing.
00:30So, the way that I do it is by using the WHERE keyword.
00:35I only want to bring back that data where people have a certain last name or
00:41have a certain email address or their customer ID is in a particular range.
00:43Now it all depends on what kind of data that you're filtering on.
00:47So, for example, if I wanted to bring back information where the LastName of the
00:51person was equal to Vargas,
00:54well, if it's text I need to write it in quotes.
00:58Another single quote again here.
01:00If I execute that, I get in this case four rows coming back.
01:04It looks like my table may even have some duplicate data that I will need to
01:08look at a little later.
01:09You'll find that this equality is case insensitive.
01:13That is based on the collation all the database.
01:15So whether it's lowercase v or uppercase V, it doesn't matter.
01:18But I do need the quotes.
01:20After all if I was missing the quotes, the SQL Server database is trying to
01:25imagine that this is probably a column.
01:26I try and execute it.
01:28It says I have no idea what this is meant to be.
01:31So if it's text, it's in the single quotes.
01:34If however it is a number,
01:36so for example this table has a CustomerID, which is numeric,
01:41it's just an integer,
01:42I could say CustomerID = 600.
01:45I don't want the quotes here and I am now retrieving that piece of data.
01:49Now you notice that I can put a WHERE clause here based on a column that I'm not
01:56retrieving in my SELECT part of the statement, which is perfectly acceptable.
02:01I can either be bringing back CustomerID or not. it doesn't matter.
02:04I can still filter on it.
02:05Now just as we have the equal sign, we could always say WHERE CustomerID > 600 and execute that.
02:14466 rows returned.
02:16Well, surprisingly again, we have the less than sign, less than and equal to, all the usual suspects.
02:25We can also describe a range.
02:27I could say WHERE CustomerID >600 and it's >800 and the way that I would
02:33write is using the keyword AND actually written out here. AND CustomerID < 800. Execute that.
02:43We get 59 rows returned.
02:46We also have a slightly more readable way of doing it where I could actually say
02:50WHERE CustomerID between 600 and 800.
02:57However, this one is considered an inclusive statement, meaning that if I was
03:02actually retrieving the CustomerID as part of my statement, just to take a look
03:07it at, we can see that when it says between 600 and 800.
03:11It's like a greater than or equal to.
03:13So we are retrieving 600 there rather than a greater than sign, which would skip the 600.
03:21You can also do this with textual or character fields.
03:33Although in this case because we have no one exactly matching A and C, we are
03:37just bringing back all the B LastNames.
03:41If you wanted to check for multiple values where you're interested in several
03:45different LastNames,
03:46for example, we could of course say where a LastName = Smith or LastName =
03:51Bright or a LastName = Bremer.
03:54Just like having the AND we can have an OR statement.
03:56But we can also use the keyword IN to describe a range.
04:09Because I'm checking for a text value, I do need to surround each independent
04:15value with the single quotes and just separate them by commas.
04:20Now getting a little bit more flexible with this, let's say for example that we
04:24have been retrieving some information including the CompanyName.
04:29And when we are scanning things, we are finding several companies with very
04:33similar names, like Metro for example.
04:38Well, what I could ask is to bring back this information where the CompanyName
04:45begins with the word Metro.
04:47The way that I do this is I actually use the keyword LIKE.
04:54I type in the text that I want to find and for whatever else it could be,
04:57the wildcard is the percent sign.
05:00In a lot of languages or other ways of doing this, this might be represented
05:04with an asterisk, but in SQL it's the percent sign and we use LIKE.
05:08So this should bring me back anything where the company name begins with
05:12Metro, anything else.
05:14We click execute, and oops!
05:16Unfortunately, what I've got to be careful with this is I had Metro
05:19actually highlighted.
05:20That means it was just trying to execute that word.
05:23I do need to make sure that I haven't got any particular piece of text selected there.
05:26And there we go. We are bringing back 14 rows with several different places, Metro Cycle Shop,
05:32Metropolitan Bicycle Supply, Metro Manufacturing.
05:38Again, like any text operation this is case insensitive.
05:41So it doesn't matter whether it's an uppercase M or lowercase m. You can also
05:46put the % sign at the beginning of the word, and in this case this would do
05:51a wildcard search for anything in the company name column that had etro
05:57anywhere in that string.
05:59I execute that and we are also getting back Petroleum Products Distributors.
06:05If however, you're just looking for a single letter, you can represent that with
06:10the underscore in your LIKE statement, which in this case brings back the same
06:16information, but is a slightly more specific way to do it.
06:20Do you bear in mind that if you're doing lots of wildcard searches, this can
06:24be very inefficient, particularly on large databases if it has to scan the
06:30tables and go through every column row by row figuring out if these letters
06:35occur anywhere there.
06:36Now depending on the data that you are retrieving, sometimes you will get a
06:41value and sometimes you won't.
06:43If I retrieve the MiddleName, for example, I can scan the data returned and see
06:47that several of them have NULL being returned.
06:51This is also slightly highlighted to indicate it's not word null.
06:55It is a NULL value.
06:56There's nothing here.
06:57Well, what if I wanted to only retrieve those rows where that value was NULL?
07:03Well, I might be tempted to say WHERE MiddleName = NULL and we try and execute
07:14that and nothing comes back.
07:16Because that's not the word that we use when we are checking for the existence of NULL.
07:21What we want is the word IS where MiddleName IS NULL, not = NULL, because
07:27a quality is a value.
07:28This is the lack of a value.
07:29So, now I'm retrieving the 343 rows where MiddleName is NULL.
07:36Conversely, if I'm just interested in the fact where it's not NULL, well,
07:41we just say IS NOT NULL and execute that.
07:47And now, we only have the rows where the MiddleName is not NULL and has a value.
07:54You can of course get a little deeper than this on your conditions and we'll
07:58see how to connect values to other tables and what's going on in other parts of the database.
08:03But with what we've just seen, that'll get you writing most of the conditions in
08:08at least your basic SQL statements.
Collapse this transcript
Sorting your output
00:00So let's take the idea of filtering our data a bit further.
00:04In this very simple SQL statement I am retrieving all the product information
00:10from the Product table here, and I have got a lot of different things in
00:13different colors, with names and Product IDs and list prices.
00:17What I would like to do is go buy the cheapest to the most expensive.
00:22Well it's yet another SQL clause and we'll put it right at the end of the SQL
00:27statement, and I'll just put in Order By.
00:30Well, order by what?
00:31Well, in this case I'd like to Order By ListPrice.
00:35I hit F5 to execute that and we are going with the ordered ListPrice.
00:41By default it will be ascending. If we want to make it descending we just
00:45type-in the word DESC afterwards, hit Execute and now we have by
00:50ListPrice descending.
00:52You can if you want to pick multiple columns to order on, so I could order for
00:57example by Color, ListPrice, and what this would do is order by color ascending
01:04and then ListPrice descending within it. So if I execute that we have first off
01:09all the Null colors because that will come before any values and they are
01:13ListPrice descending, and then black and then blue and multi and red and so on.
01:19You can of course combine this with the WHERE clause to filter these even more.
01:25So if I wanted to just ask for Color = 'Red', well I now don't really need to
01:30order by color, but just have the red products ordered by ListPrice descending
01:36and we get 38 rows instead of nearly 300.
01:40Now a lot of the times when you start to order your data you might even not be
01:46interested in the full amount. So say in this case when I am ordering by
01:51ListPrice descending I might not be interested in the fact that there is 295 of them.
01:56But I might be interested in the most expensive 10 or the most expensive 20.
02:01What we can do if we only want to have say ten results is I can put right after
02:06the SELECT TOP 10. Now I'll split this up onto a separate line.
02:11It doesn't matter if you kept it altogether but this might be a little easier to read.
02:15So select the top 10 ProductID, Name, Color, and ListPrice from the Product table,
02:20order by ListPrice descending. It will do the ORDER BY first and then just return
02:24the top ten values, so in this case we are getting ten rows back.
02:28Quite similarly you can also go, let's say if we wanted the top quarter products,
02:33we could say TOP 25 PERCENT, hit F5, and in this case we are getting about 74 rows
02:40and seeing as the amount of total rows is nearly 300,
02:44that looks about right.
02:46And very simply one of the ways we could actually get the first most expensive
02:51list price we could go and use the MAX function, but in here I am just going to
02:56say SELECT TOP 1. We'll find the most expensive product.
03:00Now it is possible here that there could be multiple ones with that exact same [00:03:05. Even though we are ordering by ListPrice descending it's really
03:09going to be a bit of the luck of the draw which one we get if we are limiting
03:12it to only one result, and it's more about how that data will be internally
03:17stored in the table.
03:18But it can certainly be very useful. And that's how you start to sort your results.
Collapse this transcript
Using aggregate functions
00:01Next up, we have a few more keywords to work with an SQL and these would be
00:05grouped under the idea of something called aggregate functions, which is a
00:08fairly unpleasant name for a very useful piece.
00:11Now we've seen already how we can do a simple select statement like this, SELECT
00:16* FROM SalesLT.Product, and this returns everything.
00:20All our columns, all our rows and in fact, I can see it's 295 rows either
00:24by scrolling down to the bottom of the results or just reading it here in the status bar.
00:29But what if that was the piece of information I wanted?
00:32What if I just wanted to know the answer was 295?
00:36We have 295 products.
00:38I don't care about the rest of the data.
00:40Well, I can do that by using a piece of SQL called COUNT.
00:45And what I'm going to do is give it a little argument here, a little bit
00:50of information that says I want the count of everything. SELECT COUNT(*)
00:53FROM SalesLT.Product.
00:54Execute and it basically returns just that value, just 295.
01:00I can see that I don't have a column name for it.
01:02If it was important for me to have a column name, I could perhaps return it as
01:08TotalProducts and execute it that way, but that might be necessary if I was
01:13sending this SQL back to a developer. But this is all I need, SELECT COUNT(*).
01:18Now occasionally I'm asked, is it really important that I have the star, for example?
01:25Because if all I was doing earlier was bringing back ProductID From
01:30SalesLT.Product, that's 295 as well.
01:33So couldn't I just say SELECT COUNT of ProductID, instead of COUNT(*), and you can in fact.
01:41If I do that I'll get the same result, 295.
01:44You just need to be careful here because star will always give you the count of everything.
01:52If you pick another column, so if I was to pick Color, which is a column in the
01:58Product Table, and execute that, well, in this case I get 245 instead of 295
02:05because what it's doing is it's ignoring any null values that might occur.
02:10So as a general rule if you want a count of everything, it's SELECT COUNT(*).
02:15You can of course also use WHERE clause, as if you want to restrict the results
02:19to where products have a color of red. I could just type that in and execute it.
02:24I still got the count and in this case it's 38.
02:26Now sometimes however the count isn't what you want.
02:30If you're looking at some of the data that you have, in this case, I'm going
02:33to back to selecting everything, I can see that I have a lot of products and
02:37they're all split up with different list prices and standard costs and weight and so on.
02:42But what if I wanted to find out what the maximum ListPrice was?
02:48Well, I could do an ORDER BY with the top one or I could do something like this. use the word MAX.
02:54Now in this case I wouldn't say MAX(*) because it's not the max of everything.
02:59I want the maximum of the column ListPrice, hit Execute on that, and it tells me
03:05the most expensive thing was 3578.27.
03:10Well, if we have MAX, it's a pretty good guess that we're also going to have MIN
03:14and we do, 2.29, and we also have AVG. Give us the average price and if you want
03:22to of course you can start to throw some of these together, using several of
03:30these aggregate functions to in this case return the maximum and the minimum and
03:35the average price of our products.
03:38Again, I don't have to use the AS keyword here. I just think it's a bit more
03:42useful if you're doing multiple functions that return different columns to
03:47give them all names.
03:48Now depending on the kind of data that you're working with, these may be what
03:52you are looking for.
03:53For example, if I open up my AdventureWorksLT database, I know that I have
03:58things like SalesOrderDetails and SalesOrderHeaders.
04:02I am going to just quickly SELECT * FROM SalesOrderHeader and by scanning
04:13some of that information, I can find things like the TotalDue and SubTotal
04:19and Freight amount.
04:20Well, let's say what I wanted to do is find the maximum amount of all our sales orders.
04:26Of course, we can use MAX as we've done a little earlier, but what we can also
04:36do here is use the word SUM.
04:39SUM, instead of just counting them, will total them all up.
04:43When some people new to SQL kind of get a little mixed up between SUM and COUNT.
04:48Remember that COUNT is just going to give us the number of rows that
04:53had something in them.
04:54In this case 32 whereas SUM is going to take the TotalDue column and add
04:59them all together, and that's often very useful when you're working with financial data.
05:04Now going back to the Product Table, I'm going to start off with something quite simple.
05:14Now we've seen how we can use the word COUNT to get a total number of products
05:20or add the WHERE clause to filter that down.
05:25Well, what if I wanted to have the counts grouped by color?
05:31It's quite a common thing that what people will try and do is they will say
05:34well, perhaps I can say SELECT COUNT(*), Color, and execute that, and the problem
05:42is you're going to get this message that it can't give you Color because Color
05:46is not part of an aggregate function.
05:48You get this wonderful phrase it is not contained in either an aggregate
05:51function or the GROUP BY clause.
05:53If you see this message that says the GROUP BY clause, that's kind of a little heads up.
05:57There is another part of SQL that can probably give you what you're looking for.
06:02What I'd like to have here is the different counts for the products with
06:06different colors or grouped together and I don't want to run that as several
06:10different SQL statements.
06:12So this is the way you do that.
06:14I want both the COUNT and the Color, but I need to tell SQL Server that you need
06:23to group the results by color.
06:25Once this GROUP BY clause has been added, it will allow this first column to be
06:30retrieved in that statement.
06:32I execute that, and this is what we get in return: 89 Black, 26 Blue, 1 Gray, 8
06:41Multi and 50 other products that have a NULL value in the Color column.
06:46Now if you start to use things like GROUP BY, you can also use some of the other
06:52aggregate functions as well.
06:53So I could start to kind of mix them up here, say for example MAX (ListPrice)
06:58and we're still showing the color and grouping by color, and in which case we're
07:02getting the maximum list price for each color.
07:04So, very useful ways of getting to the important parts of your data, simply by
07:09using some of these aggregate functions.
Collapse this transcript
Finding unique values
00:01Okay, next up is a fairly easy one.
00:02Let's say I have got a straightforward SQL statement here, just selecting star
00:07from SalesLT.Address, and I scan some of this information and see that I've
00:12got a lot of information such as different states and different countries and regions.
00:16And what I might be interested in this is this.
00:19I don't really care about the individual pieces of data, but I'd like to know,
00:23for example, what the different countries are that we ship to.
00:27Well, I could start off by just filtering this data down.
00:31Rather than SELECT *, I'll just say well, SELECT CountryRegion FROM
00:35SalesLT.Address and execute that.
00:39It still returns 450 rows that I could scan through, and it certainly doesn't look
00:43like there are too many countries, but just to be sure how could I start to ask
00:48for those individual values.
00:50I really just want to know it is Canada, the US, and whatever counties there are.
00:54Well, this is what I do.
00:56I use the word DISTINCT in the SELECT statement just before the actual column
01:02that I'm interested in, which really means only bring me back an individual value,
01:07just bring it back once for each value, even if it's repeated. I execute that.
01:13We get now the distinct values for this column, which is Canada, United
01:17Kingdom, and United States.
01:18Now, if you find it useful, you can even go a little deeper than that.
01:22If I, for example, wanted to find both the distinct country regions and within
01:27that, we're actually storing StateProvince too.
01:30That's one of the columns in this table.
01:33I don't need to use the word DISTINCT again, but if I execute this line, what
01:38I'll get is the distinct countries and the distinct state provinces, whether
01:42they're repeated or not.
01:44DISTINCT can be a very useful keyword when you find yourself getting too much
01:48data back and you really just want to filter it down, and in fact you'll find
01:51that using this DISTINCT keyword can actually be very useful even if you feed it
01:55into one of the other functions.
01:57For example, I could surround that phrase with the COUNT function, execute that,
02:07and I just get the answer. How many countries do we ship to? Three.
02:11I'm counting the distinct ones and just totaling them up.
Collapse this transcript
Joining multiple tables together
00:00After awhile you are going to find that be very straightforward SQL
00:03statements, like this one where I'm simply selecting three pieces of data from the Product table,
00:09well they don't get all that fantastically useful.
00:12What I'm retrieving here is the name of a product, the color of the product, and
00:17the ProductCategoryID, which in this case is 18 or 27 or 23, which is not all that
00:25helpful if I'm just reading this data.
00:27I want to know what the category is.
00:29Well I know that my database has the category stored in it but it's not in this
00:35table and here's why. I have got a simple diagram
00:38that's just showing these parts of this AdventureWorksLT database.
00:43There is a Product table with all these columns in and then there a
00:47ProductCategory table and this is simply good normalization.
00:53We can have multiple products in category, so it's a good idea to take that
00:57category data out and put it in its own table. And what this really means for
01:02MySQL statement is I want one SQL statement to be retrieving data from two
01:10different tables or even more potentially.
01:13And we do this typically through what's called a join.
01:16We are joining two tables together to get at data in both of them.
01:19Now how we do that?
01:20Well, rather than just say I won't retrieve information from SalesLT.Product,
01:26I'm going to use the word JOIN and say I'm also going to retrieve information
01:31from SalesLT.ProductCategory.
01:35Now, as soon as I do this, we are going to start getting these little squiggly
01:39underlines here, that it's looking at it and saying, "Well, Name is now ambiguous
01:44and ProductCategoryID is now ambiguous."
01:48Meaning that if I look at those definitions of the tables, we have the Name in
01:52the Product table and the Name in the ProductCatagory table.
01:55We have a ProductCategoryID in one and ProductCategoryID in the other.
02:01So SQL Server Management Studio is already telling us there's a problem.
02:05You need to get a bit more specific here.
02:07Now the way we deal with this first problem is we have to say okay, well
02:12SalesLT.Product, I'm going to just put the letter "a" after it and after my
02:17second table I am going to put the letter "b".
02:19Now the letters aren't important.
02:21You could pick a, b, p, c. It's up to you.
02:24"a" and "b" is pretty common and then because you're naming the tables, you will
02:28actually say well, if I have named the first one "a" I am going to say that I am
02:32interested in a.Name.
02:34That is now no longer ambiguous and thus quickly disappear.
02:37It says "Okay, you want a.Name so I am looking for the a table" and in this case I
02:44want a.ProductCategoryID, just to keep everything similar right now.
02:48So that gets us pass the ambiguous column names but then what?
02:52Well I could try and execute this, but the problem is its saying I have got an
02:57issue here. I have incorrect syntax and really what it's complaining about is you
03:01are telling him to join these two tables and it doesn't know how.
03:05Now depending on which database background you come from, you might be looking
03:08at these relationships that you know exist in the database and thinking well,
03:12surely SQL Server already knows how to join these two together. No, it doesn't.
03:17It wants you to tell it.
03:18So we have to say how are these tables joined and the way we do that is we use the word ON.
03:25And well they are joined is on the ProductCategoryID. The ProductCategoryID
03:30for a particular product should connect to the ProductCategoryID for the Category table.
03:36So what I am going to type is ON a.ProductCategoryID = b.ProductCategoryID.
03:45We have now described how the join happens.
03:50I can execute that.
03:51What we get right now is exactly the same date as I got before, because I say
03:57that I'm joining this new table of ProductCategory, but not actually using
04:03anything from it, because what I want is the category name.
04:06Now the category name in that table is just called Name. We already have a
04:11Name in our select statement but we need the second one. Well luckily we've
04:15already suffixed the table.
04:16So we just say I also want b.Name, and do notice that when I am using that b
04:23or a it knows which table I am talking about.
04:26So if I say a., it gives me all the columns from the Product table.
04:30If I say b., it's given me just the fewer columns from the ProductCategory table.
04:35So on b.Name, execute that one and now we're bringing back this information.
04:40We are joining the two together and we're getting the names of the categories back.
04:45In fact because of this I could pretty much say I don't really care about
04:49ProductCategoryID anymore, so I could pull that off the SQL statement.
04:54Now officially what's happening here is we're doing what's called an inner join
04:59and if I was to be very correct about this, and I usually would be, I am actually
05:04going to use the words INNER JOIN instead of just JOIN here.
05:08What INNER JOIN means is we are only interested in those records where
05:13they match on both.
05:15So if there was anything in ProductCategory that didn't have any products
05:19attached to it, we don't care.
05:21If there were any rows in Product that did not have a ProductCategoryID,
05:26we wouldn't care about those.
05:27We are only showing the rows where this information that matches in both tables.
05:33Again if I execute that, changing it to INNER JOIN, we get exactly the same results.
05:38There is also something called in OUTER JOIN.
05:41An OUTER JOIN means we are going to pick one of the tables and say well, we are
05:45interested in say all the Product Categories and all the matching products.
05:51That sounds a little weird.
05:52So let me just demonstrate what the difference would be.
05:54Right now we are doing an INNER JOIN, which gives us 295 rows.
05:59I'm going to do what's called an OUTER JOIN, but I can't just write OUTER JOIN
06:03because it's going to get a little bit puzzled.
06:06With an OUTER JOIN you're always saying one of these tables takes presence over the other.
06:11We are always going to have everything that's in one.
06:14So we have to say am I interested in the second table, the one on the right, or
06:20am I interested in the first table, the one on the left of the JOIN statements.
06:24And what I would actually say is either left OUTER JOIN or right OUTER JOIN.
06:30And what I am actually going to say here is I want to do a right OUTER JOIN on
06:34the ProductCategory table.
06:36Here is what the difference is.
06:37I execute that, bang!
06:39I actually have 299 rows now because we get four new rows and this is the
06:45difference, those first four.
06:47What's actually happening here is we're bringing back the names from the second table,
06:52the ProductCategory table, of Bikes, Components, Clothing, and Accessories,
06:57and it's actually telling us there is nothing in the Product table that is
07:02actually pointing directly to those categories.
07:06But I want to list the categories anyway.
07:09Now the reason for this in this particular example is that this table has
07:14categories that reference each other, but there are no products that actually
07:19have the ProductCatagory Bikes, no products with Components, no product with
07:22Clothing or Accessories. And if we want them to show up, we do a right OUTER JOIN,
07:27giving precedence to the ProductCategory table.
07:30If we are only interested in the match, we say INNER JOIN and they will go away.
07:35Now there are a few other more unusual join things like CROSS JOIN. I'm not
07:41going to go through them in this course.
07:43If you're interested again as with everything, you want to be having a good look
07:48at Books Online to see some further examples of where you can go with this.
07:52Do bear in mind that lynda.com also has a dedicated SQL course that gets into
07:57some of these join ideas as well.
Collapse this transcript
Using subqueries
00:00As you get more comfortable with writing SQL statements, you'll find yourself
00:04wanting to combine information from multiple tables more often than not and
00:10though a join is a classic way of doing it, it's not the only way.
00:14There is another way called a sub- query and let me explain that by kind of
00:17taking you step-by-step through the roadmap of how you might get there.
00:22So we start with something really simple.
00:24We've got a SELECT * statement here.
00:26It'll give me everything from SalesOrderHeader.
00:28Now if I execute that, I'm getting all the totaling of my sales orders, including
00:33things like the customer IDs and the subtotal, and the total due.
00:37And I of course can scan this information just fine and look at it, but what
00:41if I'm more interested in specific stuff.
00:44What about if I want to know information about the biggest order that ever happened?
00:48Well, I could, of course, just do something very simple like an ORDER BY.
00:57TotalDue, which of course will give it to me ascending which might not be that helpful.
01:02So let's do TotalDue descending.
01:05We execute that and we know then that the first row should have the maximum
01:10number that was ever ordered, and we could scan more of that and look and try
01:14to find our customer ID and so on.
01:16But we're getting way more information back than we want.
01:19Really what I'm interested in is customer information. Who did this?
01:23Who placed this order?
01:25Well, I could go to something simple.
01:27Instead of saying SELECT *, we could say SELECT CustomerID, execute, and we're
01:34getting them returned in their order.
01:36But I don't need the rest of them.
01:37I just want this one.
01:38Well, we could, knowing what we know now, even say SELECT TOP 1 and we've
01:44got that customer ID.
01:45But the issue is, I don't know who this is.
01:49What I'm interested in is what company is this, who was the contact, and that
01:53information is stored in a different table, in the Customer table.
01:57Okay, so we could do a join here.
01:59I'm going to show you how you do with the join and then with the sub-query.
02:01So if we want to do a join, we know that we need to add the JOIN word after the
02:08first table and this is going to be an inner join, because I'm not interested in
02:12all the other customers.
02:13I'm just interested in one.
02:16So we're going to say INNER JOIN SalesLT.Customer.
02:20Now, of course, because it's immediately saying this is now an ambiguous column,
02:25because customer ID is in both tables, we better do the little a and b to say
02:32which tables these are.
02:33I'm going to say SELECT TOP 1 a.CustomerID.
02:37But we're still missing one thing.
02:39Even though I've named two tables, it doesn't know how to join them.
02:42So what I'm going to do is the join word, which is ON, and we have to name the columns.
02:47Well, we're joining them based on the column CustomerID that's in both the tables,
02:51so a.CustomerID = b.CustomerID.
02:52Now if I execute this, I should get exactly the same result, because I'm not
03:01asking for any new information to be retrieved yet.
03:03So let's just double-check that. Yup, 29736.
03:05But what I can do is now comma b. and I get CompanyName,
03:14b.FirstName, b.LastName.
03:22If I execute it, we can get all that information coming back.
03:26Now what's the issue? Well, not a lot.
03:28I mean if this was commented, which we could do in the raw SQL with just fetch
03:34details for the biggest order, it might be obvious what it is.
03:40But if I was looking at this SQL six months down the line, I might be a
03:45little challenged to figure out exactly what it was doing because it's not all that clear.
03:49But it works, which is fine.
03:51So let me show you the other way of doing this that doesn't involve doing a join.
03:55So I'll take it back to what we had just a minute ago, which was fetch me that
04:01TOP 1 CustomerID, no join needed.
04:05We're getting that same CustomerID back. Well, here's the deal.
04:12I want the information from the Customer table.
04:15So I am going to approach it from the other end completely.
04:19I'm just going to move that down to the bottom a little bit.
04:21I'm going to write a completely separate SELECT statement that describes the
04:25information I want, which is SELECT FirstName, LastName, CompanyName FROM the
04:36Customers table WHERE CustomerID equals.
04:45The question is, equals what?
04:47Well, I want this information from the Customers table, whether CustomerID is
04:52equal to what I got back from this query, and that's what a sub-query is.
04:58We're going to take the query that returns the CustomerID and just feed it
05:02into the first one.
05:03I'm going to just put it in the parentheses here.
05:06So this first query, the inner query or the sub-query, will be executed first.
05:12We'll end up with the result 29736 and it would be the same as saying give me
05:16the FirstName, LastName, and CompanyName from the Customer table, where
05:19CustomerID equals 29736.
05:22So we execute that and we get the FirstName, LastName, and CompanyName, and
05:27this is a sub-query, a query within a query.
05:31Now it might sound like a sub-query and a join are just choices, you just make
05:36the decision one or the other.
05:38Well, sometimes that's true, but you will find as you get more familiar with them
05:41sometimes it's more natural to do a join, sometimes it's more natural to
05:45do a sub-query, because there is crossover, but they're not always the same.
05:49There are instances when a sub-query is a much better, in fact, sometimes the
05:54only possible way to get some data coming back.
05:57For example, let me take you through another example of a sub-query, but this
06:01time it will be something that will be quite challenging to do in a join.
06:04What I start to do is perhaps I start writing a query like this.
06:08I want the FirstName, LastName, and CompanyName from the Customer table where
06:13these customers have not placed any orders.
06:18The question is well, how do I find that information?
06:22Now particularly when you've got a SQL statement that needs to find some kind of
06:26negative information, give me information where this doesn't join to something,
06:30that can be a little challenging.
06:33And oftentimes, a little trick of this is to turn it on its head.
06:37Well, let's say how would I get the first name, last name, and company name for
06:41all the customers who have ever ordered anything?
06:44But what I really want is that information where the CustomerID is to be found
06:49in the SalesOrderHeader table.
06:53So I could do that with the sub-query.
06:55I'm going to create my sub-query down here and then copy it up into the top level.
06:59I'm going to say SELECT CustomerID FROM SalesLT.SalesOrderHeader.
07:10Now this little SELECT statement will give me a lot of different CustomerIDs and
07:15it could give me the same ones again and again and again.
07:18I only want the same CustomerID once. If CustomerID 75 is placed seven orders,
07:24I don't need seven CustomerIDs of 75. I just want one.
07:27So I'm going to say SELECT DISTINCT.
07:32So if this part, which is going to be my sub-query, returns a whole bunch of
07:37different Customer IDs, how do I tie it into the first one? Well, this is how.
07:48We use the keyword IN, which is going to allow us to provide a range.
07:58Again, the sub-query is evaluated first.
08:00Give me all the distinct customer IDs that are found in that SalesOrde Header
08:05table and then use that to bring me the information from the Customer table.
08:11So I execute that and we are actually getting now 32 rows, which sounds about
08:15right because there were 32 orders.
08:17But what I originally asked for was I want the information of the customers that
08:24have not placed an order, whatever that means. Maybe it was canceled, maybe
08:27didn't make it all the way through.
08:29Well, because we can use IN, we can also use NOT IN.
08:34We're taking a positive result of selecting the distinct customer IDs and
08:39getting it to feed us into really kind of a negative result.
08:43I want the customer information where this is not true.
08:47They're not in SalesOrderHeader.
08:49So I execute that and this one I get 815 rows, a whole bunch of orders that
08:56haven't been completed.
08:57So this is a second example of the kind of things that you could do with a sub-query.
09:03Now word of warning.
09:04This kind of information, particularly on a group of large tables, could be quite
09:09an inefficient query to run if you're starting to scan through tables to find a
09:16bunch of results and then say well, where do all these results not match in some
09:20other scanned table?
09:21So a little word of warning there. Pay attention to that and as you get a bit more
09:26familiar with SQL, we're going to start running into things like analyzing our
09:29queries and taking a look at how quick things are going to run.
Collapse this transcript
Viewing execution plans
00:00After you've written a few more complex SQL statements where you're using
00:04joins and sub-queries, you'll probably end up wondering, well which one is
00:08better performance-wise?
00:10Is one objectively faster or slower than the other?
00:13And of course it depends on how you are doing the join and how you are doing the
00:16sub-query, but there is a way to find out.
00:18So I'm looking right now here at the example I did in the previous module,
00:23which is selecting the company with the most expensive order, one way doing it
00:29using a JOIN, Action Bicycle Specialists, and one way doing a sub-query. Same data, same result.
00:38But what if we want to know which one is faster?
00:42Well there are quite a few ways of doing it to get fairly detailed. You can get
00:46into what's called the SQL Server Profiler, but we don't need to go that heavy.
00:50What I'm going to do is select the two blocks that I want to do. This is now
00:54considered our batch, but formally, the entire file is because there is
00:58nothing else in the file.
00:59I'm going to come up here to my toolbar and I'm going to click this button that
01:03says Display Estimated Execution Plan. Hit that, bang.
01:09And what we are actually going to get if I drag this up a little bit is the
01:13execution plans for both of these.
01:17And the reason that I wanted to do both is so I can get them compared to each other.
01:20So it's actually telling me that the first one was 54% of the batch and the
01:26second one was 46%. Not a huge difference but not tiny either.
01:31There's definitely a difference going on there.
01:33Now the details are being shown up graphically and as they are actually must
01:37over and clicked on it will tell you things like the I/O cost and the CPU cost.
01:42Right now of course we're on a small database and really all of this is going to
01:47just be invisible to any user, but even so, as you get more complex you'll find
01:52yourself looking at execution plans a bit more.
01:54Now what that will actually point out to you is there are certain places where
01:58it's doing what it calls index seeks.
02:03That you'll find them in both of these queries, and there are certain things
02:08that you'll find in the execution plans that are going to show up as having a
02:13significant cost, like here for example 55% was in the sort.
02:18Okay, maybe a good thing, maybe a bad thing.
02:20Now you'll find that a lot this you won't be able to really change or play
02:24with until you've started exploring indexing, because it will expose some issues
02:29where you have to traipse through the entire table because there is no index to
02:34allow you to drill down to a particular point.
02:36But all we can get from this now, and it's still valuable, is that right now it
02:40seems that the 54%, which was the inner join, and 46% was the sub-query, so the
02:47sub query appears to be a bit of a winner here.
02:51Formally though that is the estimated plan. You can actually check this little
02:55button on the toolbar to say Include Actual Execution Plan, and what that means
03:00it is really just the selection.
03:02It's now got the little blue line, blue box around it, and it means when I
03:06execute this again I'll get both my results, any messages that came back, and the
03:12actual execution plan, which again looks identical to the estimated one, which was
03:1754% for the join and 46% for the sub-query.
03:20Again it's not always going to be the case. Sometimes you'll find the join would
03:25be the better way of doing it.
03:26But starting to look at execution plans. Even if you are not at this point
03:30completely comfortable with what each particular part of it means, it's a very
03:34useful habit to start to get into.
Collapse this transcript
7. Inserting and Updating Data
Writing INSERT statements
00:01Selecting information is all very well, but after a while we are going to need
00:05to start inserting some information into those databases.
00:08Now of course if you have SQL Server Management Studio you can do the kind of
00:13cheap and nasty way of doing it, which is right-click one of these tables,
00:17select the Edit Top 200 Rows, and either start clicking and selecting in here to
00:22change things, or you can scroll right down to the bottom and where the asterisk
00:27row is just start typing.
00:29But this is not really a very scalable way of doing some inserts.
00:32It's really just there for emergencies.
00:34We need to understand how to construct SQL statements to do this.
00:38Having said that, it is very useful to often look at this this way to
00:42familiarize yourself with what data you are about to enter.
00:45So I'm going to create a new query and I'll be using AdventureWorksLT, as we
00:51have been all along.
00:53I'm going to insert into the ProductCategory table here, simply because
00:57it's quite a small one.
00:59Again before I start writing the insert statement let me take a quick look at
01:03what I have here, just selecting from it.
01:05We have ProductCategoryID, ParentCategoryID, Name, then a rowguid and a ModifiedDate.
01:14Well even just looking at this I might have the idea that you know some of this
01:18is going to be automatically generated.
01:20If I want to double check that I could right-click this table and select
01:23Design, because if I wasn't a person who had created it I'll certainly want to take a look.
01:29So ProductCategoryID is declared as the primary key.
01:32It's an integer and if I scan the properties of it I can see that it is set up
01:37to be an identity automatically incrementing, so I shouldn't be the person
01:42inserting this value.
01:44I should probably be the person inserting the ParentCategoryID. Yeah, looks like the case.
01:49It's not an identity.
01:51I'll be the person inserting name.
01:52And then we get to rowguid.
01:53Well, this is a globally unique identifier and it's set up with a default value
01:58of the newid function, which means I shouldn't insert this either and nor should
02:03I do the modified date which is getdate.
02:06So it looks like I'm only inserting two things, the ParentProductCategoryID and the Name.
02:11All right, well back to our insert statement. Well I haven't started it yet.
02:15In fact it's not the word INSERT. It's actually the phrase INSERT INTO and now
02:21we're going to use the name of the table.
02:22We are inserting into SalesLT. ProductCategory. Well then what?
02:28Well I have to have a way of saying I only want to insert those two particular
02:34columns, which was ParentProductCategoryID and Name.
02:38The way I do it is open parenthesis and I type the names of the columns that
02:42I'm wanting to insert.
02:43Then I use the word VALUES, then I have another parenthesis where I enter in,
02:55in the order that I declared them, the values that I want to insert.
02:59In my case I'm going to insert the value of 1 for the ParentProductCategoryID
03:04and I'm going to insert the phrase Hybrid Bikes.
03:07And if you don't supply the column names for the columns that you're going to enter,
03:16the SQL Server will expect that you're going to give it the values of
03:21everything of every column, but we don't need every column.
03:24So I'm going to go ahead and execute that and see what happens.
03:28We hit it and we see one row affected.
03:31It doesn't actually tell us what, but we can get to that a little later on.
03:36This is an INSERT statement.
03:37It just says yes it worked.
03:40If I want to prove that I could just very quickly right-click, say Select Top
03:441000 Rows, and come down right to the bottom and there I can see Hybrid Bikes.
03:49It has a guid that has been generated.
03:51It has a modified date that's been generated.
03:54I have a product category ID.
03:54I've been playing around a little bit with inserting stuff, so I can see that
03:59I'm missing a few numbers here. But that's okay.
04:01It's looking good.
04:03Going back to this, you can also, if you want to insert multiple rows at the same time,
04:08all I could do here is put a comma and then another open parenthesis and
04:14say perhaps here I'm putting in something for Girls Bikes.
04:19Now here is a question. What do I do if I want to put in an spostrophe, because
04:23if I just type it what's going to happen is it's going to close my string here,
04:29my little text thing.
04:30Well the answer is this.
04:31If you want to have the single apostrophe and you have to use the single quote,
04:35you just use 2. So this should allow me to insert another entry here.
04:41Now here interestingly we're going to have a problem because there is a
04:44constraint on this field that means it should only have one product category
04:50name of the same name. We've already inserted Hybrid Bikes, so let's see what
04:53happens if we try and insert this again.
04:55I had Execute and unfortunately gives me a violation of a UNIQUE KEY constraint.
05:01Cannot insert a duplicate key in object ProductCategory. Okay, that's fine.
05:07Let's say that was an accident.
05:08Anyway what I wanted was in this case this should have been Boys Bikes. I'm
05:13going to use the two single quotes as well.
05:15That's not double quotes.
05:17It's two single quotes and I'm going to try and insert that.
05:21There we go, two rows affected, and if I right-click and do another select it
05:28looks pretty good. We have 52, 54 and 55 going on here.
05:33Although it does look like I accidentally put some wrong data in the
05:37ParentProductCategoryID,
05:39all bikes should have a ParentProductCategory of one.
05:42Oh well. So I'll have to see how to update that in just a few minutes.
Collapse this transcript
Writing UPDATE statements
00:00Well just about as often as inserting information you're going to need to
00:04update information too.
00:05Let say the example that I was looking at, I know that I've got a problem
00:09with one of these rows.
00:11This last row here a ProductCategoryID 55 should have had a
00:15ParentProductCategoryID of 1 instead of 2.
00:19So let's see how to go ahead and do a simple example like that.
00:22We will use the word UPDATE and we're going to pick the table name, which is in
00:27this case SalesLT.ProductCategory.
00:29And just as SELECT has a FROM and INSERT has INTO, UPDATE has a SET.
00:38We're going to SET.
00:39We want to say what the values that we want to change are.
00:43In this case it was actually ParentProductCategoryID.
00:46IntelliSense is smart enough to know that if we're working with the
00:49ProductCategory table these are the only options that we have.
00:52And then I can say and in this case I'm going to do quite a specific change SET
00:56ParentProductCategoryID equal to 1.
01:00But if I were to run this code right now it would update every single row in
01:05the ProductCategory table and set back column to one, which is certainly not what we want.
01:10So when you're doing an UPDATE, you tie a WHERE clause to it.
01:14Exactly like a SELECT statement. In fact more important than when you're doing
01:18a SELECT statement.
01:19So I want to say well, WHERE? Under what conditions do I want to do this?
01:24Again the example that I shown was that we had an incorrect one right at the end
01:29here, which was ProductCategoryID equal to 55.
01:32Now I could use any of the same WHERE clauses that I used in the select
01:36statement but that's the easy one. So I'll say WHERE ProductCategoryID equal to
01:4255 and let's execute that.
01:46One row affected. Well right now just like doing the INSERT the UPDATE doesn't
01:51tell you anything about what it did.
01:53It can but not this using a plain old update statement like this, and this
01:58would be something that we could theoretically execute again and again and
02:01again because it's just going to do the same thing.
02:03Not that I recommend it, but it should work and now if I go and take a look at that
02:08ProductCategory table we should find that yes, indeed the
02:11ParentProductCategoryID has been set to 1.
02:14But do bear in mind that whatever you put in the WHERE statement, whether it's
02:19a range or whether you miss it out entirely, if you had retrieved multiple
02:25values, if you put that WHERE in the select, you would update multiple values
02:29if that's in an UPDATE.
02:30Now what about if I wanted to update multiple columns in that one row?
02:37Well that's pretty easy. So I just have that after the SET. We're setting the
02:41first column here ParentProductCategoryID equal to 1, Name equal to, for
02:48example, Commuter Bikes and we just execute that. One row affected.
02:56Now this is the most you're going to get right now of the information about
03:00what actually happened.
03:01Obviously you can do a little bit more than that.
03:03Let's say for example I update a different table.
03:07I'm going to update SalesLT.Product and I'm going to set the list price.
03:13Let's say we've had a conversation, which is we want to increase our prices by
03:18two dollars where our prices are less than 50.
03:22Well I'm going to say SET ListPrice equal to.
03:25Now how do I do this?
03:26Well very easy. I just say ListPrice equal to whatever ListPrice is +2 WHERE
03:34ListPrice < 50. Execute and we see on this 49 rows affected. Your number may be
03:44slightly different if you're following along.
03:46Now if you're constructing an UPDATE statement and you again a little nervous about
03:50how many rows you might affect,
03:51do bear mind that the WHERE clause is what you want to look at and before you
03:56run your UPDATE statement you could, if you're worried, about it just say SELECT
04:02*FROM SalesLT.Product, keeping the same WHERE clause, and figure out well how many
04:09is that going to actually change?
04:12In this case it is telling me 49 rows.
04:15Now I might have had a slightly different result because I've already executed
04:18my update but this would give you the verification of the update.
04:22It's going to update as many things as you expect.
04:25Like everything else in SQL Server you can get a little deeper than this.
04:29I recommend that you check out some of the available options that you will see for
04:32the UPDATE statement on SQL Server Books Online but that's the core of how to do
04:36an UPDATE statement.
Collapse this transcript
Writing DELETE statements
00:01A while ago I mentioned that there is an acronym that you'll occasionally
00:04come across with SQL databases which was that of CRUD for create, read, update and delete.
00:13Create obviously being the INSERT INTO statement, read being our SELECT, update
00:17being UPDATE, and we might as well finish it off with some DELETE.
00:22DELETE has an associated word. Like we have UPDATE, SET and INSERT INTO and
00:28SELECT FROM, we have DELETE FROM and in fact DELETE is probably as close to
00:34SELECT as you're going to find.
00:35In fact this is about the simplest statement there is.
00:38We say we're going to DELETE FROM a particular table.
00:41In this case I'm going to DELETE FROM SalesLT.ProductCategory and I want to be
00:47very careful here because if I hit F5 right now, I would go and delete every row in this table.
00:55The same way that if I said SELECT * FROM we bring back everything, this would
01:00delete everything, and certainly one of the dangers with using SQL Server
01:03management studio or having just admin level access to a database.
01:08Yes, if you do the wrong thing, you can blow away a lot of stuff very easily.
01:13So all your DELETEs should have a WHERE. Unless you're wanting to delete the
01:19entire table you need some kind of WHERE clause here.
01:24Preferably you're going to be using something like a primary key, so in this
01:28case a ProductCategoryID and I know that I have one, 55 for example.
01:31And what I'm hoping as the when I execute this code I get little message that
01:38says one row affected.
01:41Another thing to be very careful of is it's an easy thing to do something like
01:45highlight the DELETE statement and hit Execute but do bear in mind the SQL
01:48Server Management Studio if you highlight some code, it will actually execute
01:53just that code. So be careful what you have selected.
01:56You're going to hit Execute and we have one row affected.
02:00If I were to try that statement again, Execute, zero rows affected. Why?
02:05Well because the row with that ID has gone. It's just not there anymore.
02:11And once again, if you're ever getting ready to do a DELETE statement and you're
02:16not sure how many things it's going to effect, there are a couple of ways of
02:19getting around that.
02:20But one of the simplest ways is just to say SELECT * FROM and copy your WHERE
02:27clause that you're just about to use.
02:28You could find out then now hopefully this will affect just this one row so I'll
02:37comment my SELECT, uncomment my DELETE< and give that go.
02:40One row affected, execute it again, zero rows affected. We're looking good.
02:44The same options are available in your WHERE clause for a DELETE statement
02:48that are available for an UPDATE statement or a SELECT statement. It's the same thing.
Collapse this transcript
Using the OUTPUT clause to return inserted keys and GUIDs
00:00It's very common that as you start inserting information into your databases,
00:06what you're going to need or at least what your applications are going to need
00:09is to find out a bit more information about what you just inserted.
00:13Particularly in the case where you are inserting information into a table that
00:17is going to generate keys for you, either identity columns like this one,
00:22ProductCategoryID just going up one by one, or guids that are being built as
00:27well or perhaps both at the same time.
00:31But in the INSERT example we saw before, all that I'm inserting is a
00:35ParentProductCategoryID and a name, because the guids and the identities are
00:40being generated for us.
00:41Now, we want to get that information after this INSERT has happened.
00:46This used to be a bit of a problematic thing to do.
00:48In fact often what you used to have to do is write little pieces of code to
00:53generate that guid yourself and then insert it yourself, so that you knew what it was.
00:58Well, it's a lot simpler these days.
01:01What we just do is we take our INSERT statement and we change it just a little bit.
01:06See, before the VALUES that we were inserting, I'm using this new clause called OUTPUT.
01:13And we can actually use this on an INSERT, we can use it on an UPDATE,
01:17we can use it on a DELETE.
01:20OUTPUT is a special kind of a unique clause here. What it allows us access to is
01:26the inserted columns that are just about to happen and it knows they haven't
01:30happened yet, but it knows that we will have them.
01:32In fact, if I say OUTPUT inserted, notice that inserted is in IntelliSense, and
01:38I hit that, I hit the dot, and that's even smart enough to tell me here in IntelliSense,
01:42"Well, you are inserting into ProductCategories, so these are your only options.
01:47Which one would you like?" Well!
01:48Let's say that what I want to do is I want to find the ProductCategoryID that's
01:53going to be generated as an identity field.
01:56And now when I hit Execute, what happens is I don't just get the phrase "one
02:01row affected," I get the new ProductCategoryID that I just created, which was
02:06ProductCategoryID number 56.
02:08Now you can even output multiple inserted values.
02:13So I can hit comma.
02:14I want now the inserted guid that's happening there.
02:18I have got to be careful because I'm running another INSERT statement and
02:21Commuter Bikes already inserted there and that has to be unique.
02:24So I'll just change that to Custom Bikes, hit Execute, and we get back 57,
02:30which was to be expected.
02:32And the new generated rowguid.
02:34That again is being set because there's a default constraint on the
02:39ProductCategory table that will generate a new rowguid every time.
02:43Now in essence, what's going to happen here is if you've got an application that
02:48is executing this code, it's almost like having a mini SELECT statement that
02:52returns these values afterwards.
02:54It's just a lot easier to write.
02:56And we can see later how this might be added to and inserted into say a stored
03:02procedure to make it a bit more friendly and a bit more reusable.
03:05But this is the general idea of using OUTPUT clause.
03:09In fact, I will show you another example.
03:11If I wanted to do a DELETE, I'm going to say that I'm going to DELETE FROM, same
03:18table not surprisingly. Again usually I just put the WHERE clause. Always want
03:26a WHERE clause when I'm doing this DELETE.
03:29So ProductCategoryID = well let's say the one that we just entered in a couple
03:35minutes ago, which was 56.
03:36And again, I'm putting in the OUTPUT clause. Well!
03:41It's not going to be inserted this time around.
03:43That doesn't make sense but I will have an OUTPUT deleted. and I can say I'd
03:48like to know what the Name was.
03:50I'd like to know what the deleted guid was.
03:55We hit Execute and it tells us the name was Commuter Bikes and that was the rowguid.
04:00So we can get that information back as well.
04:02I try and execute that again, not surprisingly I get no output because there is
04:08no row with ProductCategoryID equal to 56 anymore.
04:12Now it can also be used on an UPDATE statement but it's probably not what you are expecting.
04:18Here's the deal. When we construct a regular UPDATE statement here such as UPDATE
04:23SalesLT.ProductCategory, of course we have to use our SET, what are we
04:28actually doing here.
04:30I am going to just do a SET parent ProductCategoryID = 1, WHERE
04:39ProductCategoryID = 57.
04:45Now, usually you might be ahead of me, thinking well, we put the OUTPUT in here
04:52and we had an inserted. for insert and a deleted. for deleted.
04:57So presumably we have an updated. And no, we don't.
05:02Here's the deal. What that inserted word and the deleted word represent is
05:08essentially a temporary table in memory.
05:11If we do an INSERT, the inserted table represents what happened after the INSERT.
05:16If we do a DELETE the deleted table represents what got deleted and
05:21if we do an UPDATE, we don't have an updated one. Wat we really have is the inserted table.
05:26It's the changes that were made.
05:28So if I want to say output some information about the roq that's just getting
05:33updated, I could say OUTPUT inserted.Name.
05:38So hit that Execute and we find that we have just updated ProductCategoryID
05:44number 57 with a ParentID of 1 where the name was Custom Bikes and we are able
05:50to get that information out about what we just updated.
05:55Nine times out of ten, the real use for the OUTPUT clause is going to be after an INSERT.
06:00That's the most important thing for most application developers is allow them to
06:06insert a new row and find out immediately what the identity or the guid of that
06:11row was, but it can also be used with your UPDATE and with your DELETE.
Collapse this transcript
8. SQL Functions
Introduction to SQL functions
00:00Earlier we saw a few examples of what are called aggregate functions in SQL,
00:05like using COUNT to return the total number of rows in a table or using MAX to
00:11return in the largest value in a particular column, or average or SUM or MIN.
00:16These functions, like in other languages, are just a way of packaging up a piece
00:21of useful behavior and giving it a name, so that we can use it again-and-again.
00:26When you're writing these in SQL Server Management Studio they tend to show up in hot pink.
00:31We do have a few other aggregate functions, but these are really just the tip of
00:35the iceberg, because SQL Server has over 200 functions.
00:40But don't worry. You don't need to go and memorize every single one of them,
00:43because although some of these 200 things, like COUNT and MAX and MIN, are so
00:48common and so useful that you could end up using them on almost a daily basis,
00:53some functions are specialized enough that you might work with SQL Server for
00:57years and never come across them.
00:59But they're all packaged behavior, packaged operations.
01:03So a simple and common operation like return the total number of rows becomes
01:09packaged up as the COUNT function and we can use it again-and-again.
01:13But we also have operations like "Return the angle in radians between the
01:17positive x-axis and the ray from the origin to the (y, x), where x and y are the
01:21values of the two specified float expressions."
01:24And that is also packaged up inside SQL as the ATN2 or arctangent function.
01:30But just between you and me, I've been working with SQL Server for over ten
01:34years and I've never needed this one.
01:36So how do we get familiar with the good and the useful ones?
01:39I'm going to go through some of my favorite functions and show you how to use them.
01:43But like so many other things, treat Books Online as your friend and companion here.
01:48If you open up Books Online and go to the Index, you'll be able to look
01:53for the word Functions.
01:55Now when you do this, you're going to see them show up in many different areas.
01:58Functions for ODBC, SQL Server. There will be quite a few, depending on what
02:03you're filtering on.
02:04In fact the one that I'm interested in, is actually functions Transact-SQL, so
02:09functions for T-SQL,
02:11because it's a bit more specific and it gets down to the functions quicker.
02:15And this page, I quite like this one, so I'm going to click somewhere in the page
02:20so that the Favorite button becomes active and I'll add that to my list of
02:24favorite, so I can come back to this whenever I need to.
02:27The functions are grouped. We have the Aggregate Functions here and I can
02:30drop-down and explore things like the Count function and see how it's meant to be used.
02:36Click back a couple of times, I've also got configuration functions, things like
02:40the server name and the version, mathematical function, signs and cosigns if I
02:45need those, security and string functions for working with the text.
02:50And I can drill down into each of them and see examples and read about
02:53exactly how to use them.
02:55Now the thing about functions is they're actually stored as part of your database.
03:00In fact if I connect to my SQL Server instance and drill down into any of these
03:05databases, like AdventuresWorksLT, I'll find that each database has a
03:09Programmability folder and each of those has a Functions folder and each of
03:14those has a Systems Functions folder.
03:16And what I'm going to find here is a list just presented differently of the
03:20same available functions.
03:21The aggregate functions that we have, the configuration, the date and time, the
03:26mathematical functions.
03:28And it can be useful to have these showing up in SQL Server just to take a quick
03:32look at what's available. Remind yourself what the name is for example.
03:35Now you'll see that most functions have the parentheses after them, and
03:40that's how they're called.
03:42We've seen examples using COUNT, passing in the star and parentheses, using
03:47average, passing in ListPrice, but you'll find even when you're not passing
03:52something in those parentheses, you still need them.
03:54There is a very popular function called GETDATE, but when you use it you need
03:58the two parentheses even if it's empty.
04:01Now there are a few functions that just use the name of the function with no parentheses.
04:06If you're into geek trivia this is what's known as an Niladic function.
04:10Most of the functions without parentheses are easy to recognize because they
04:14have two @ signs in front of them.
04:16Although there are three or four little odd ones that don't like CURRENT_USER.
04:21It doesn't have parentheses and it doesn't have @ signs.
04:24The two @ signs aren't magical. They're just a way of marking a few of these
04:30functions. In fact most of the ones with @ signs, if you see them in SQL Server
04:35Management Studio, you'll find them in the Configuration section and there are
04:39things like the version of the server.
04:42The server name, the language of the current server, the option set on it.
04:46The thing is you don't want to get worried about whether the two @ signs are
04:50there or whether the parentheses are there, because there is an easy answer.
04:54If you see the function in SQL Server Management Studio or in Books Online
04:58and it has parentheses, then you use parentheses. If it doesn't then you
05:03don't, end of story.
05:04But the thing about all of these functions is that they give you a value back.
05:10COUNT gives me back an integer that represents the number of rows in that table.
05:16Average, well that gives me back whatever I passed into it.
05:19If ListPrice was a money column then I get a money average back.
05:25If it was an integer I get an integer back.
05:28If I used the GETDATE function I will get a date-time value back.
05:33If I used the @@LANGUAGE function I get a varchar back with, in this case US_English.
05:39If I called the CURRENT_USER function, well depending on how my database is
05:43configured, I may get dbo.
05:44That will be a very common response from that.
05:46A few functions can return several results and that would usually be in
05:51the form of the table.
05:52But most functions are like these.
05:54They return what's called a scalar value.
05:57This is an old computing term for a single value.
06:00COUNT is a function that returns one integer.
06:03GETDATE returns one date time value.
06:06They don't return a collection, they don't return an array. They return one scalar value.
06:12So how do you use these functions?
06:13Well we've seen functions used in SELECt statements and that's very, very common
06:17but they can be used in WHERE clauses, in UPDATEs, INSERTs, all over the place.
06:22So next I'll take you through a few of them.
Collapse this transcript
Using SQL configuration functions
00:00Some of the simplest functions to begin with are just using the
00:03configuration functions.
00:04These allow us to get information like the server name and the version and the
00:09connections that have occurred since this server was last booted up.
00:12If I am going to write an SQL statement to read them, I still need to have a
00:16good old SELECT statement here because that's how we fetch information. Well, what
00:20I'm interested in is things like the server name.
00:23I'm going to do the two @ signs because most of the configuration functions
00:29are the ones without parentheses that begin with two @ signs. I use my trusty
00:33IntelliSense and I've got quite a few. There is about 30 something to choose from.
00:38Lets take a little look at @ sign.
00:39@@SERVERNAME.
00:40Now you'll notice that I'm not saying use AdventureWorks because doesn't
00:45really matter where I am.
00:47This functions is going to execute and return the name of my standalone PC here,
00:51which is just SIMON-PC.
00:54I could do @@VERSION to find the version of SQL Server that is currently
00:59running, which tells me that it's the 2008 R2(RTM) and even the point releases of it.
01:05Like any SQL statement that you're executing without an actual official column name,
01:10we're getting the No column name result over here.
01:13If I wanted to give that return value a name I could use AS. Let's call it ServerVersion.
01:21Execute that and we have a named result here. But using these functions is kind
01:27of like specifying any column or list of columns in a SELECT statement.
01:31I can type multiple ones in here.
01:35The main difference is I don't need a FROM statement. So it's telling me now I've
01:39got the version, I've got the server name, and apparently there's been 6847
01:45connections that have been either successful or attempted but unsuccessful
01:50against this server since it was last started up.
01:53So experiment with a few of the available configuration functions. Yu can
01:57either look at them and Books Online or again drilling down into the
02:01individual databases.
02:03You can find your System Functions, Configuration section where you'll see a
02:07lot of those listed.
02:09Some you may not be too bothered about and others you're going to find
02:12essential as you move on.
Collapse this transcript
Using string functions
00:00Every SQL Server database you are going to work with is likely to contain
00:04immense amounts of text in varchar and char columns.
00:09So it's no surprise that there are many string functions to deal with that text
00:14and let me go through a few of those.
00:15So I am going to use AdventureWorksLT just to have some columns to work with and
00:20play around with here.
00:21Let's say we just start off with a very simple SELECT statement, selecting the
00:25LastName FROM SalesLT.Customer.
00:29Now, this is no surprise. We are not using a function yet.
00:33It's simply going to go against that table and fetch every row but just that one column.
00:38But we can use that information and feed that into any of those string functions.
00:44We could start with the simple ones.
00:45There're functions like UPPER. It shows up in hot pink here and just takes one argument.
00:51We feed in LastName into the UPPER function and it will return the result of
00:57that function, which in this case not surprisingly is just turning all of those
01:01LastNames into uppercase.
01:02Now do bear in mind this is not changing the data in the database.
01:07It's performing this operation on the way out as we are reading it and just
01:11making it a convenient way to read through this.
01:14You might know that there's quite a few countries that actually quite like the
01:17presentation of a name to have the LastName uppercased.
01:20So what we could do is start to combine this.
01:23Perhaps say I want FirstName and then a space and then uppercase the LastName
01:29and I will call the result FullName from SalesLT.Customer.
01:35Execute that and we get this presentation returning.
01:39So it's a nice way to add some flexibility to what your applications might want
01:43to do without changing the data that's actually in your tables.
01:47But that's only the beginning of what we can do with strings.
01:49There are other things that we can ask for.
01:51Let's say I'm curious about the maximum length of a FirstName.
01:57Well, I can get both the FirstNames and I can call the function L-E-N or LEN to
02:02be the length of that.
02:03Again, feeding in a character column to get that information back. Hit Execute
02:10and we are retrieving both the FirstNames and the length of the FirstNames as an integer.
02:16One of the great things about using these functions is you can nest one inside the other.
02:22So if I have executed this and I found out that we've got a whole bunch of
02:25different values but I have got thousands of rows, well, what's the largest one?
02:29Well, we've seen already the aggregate function called MAX.
02:33We can use that here.
02:35What I'm going to do is just use the MAX function, open parenthesis, and pass in
02:39the LEN function inside of it.
02:41So now we'll execute the LEN part first.
02:44We will find the length of everything and then we will find the MAX of that.
02:47Execute and we apparently find out that someone has a FirstName with 24
02:52characters. Well, that's fairly long.
02:54Of course, this way of doing it isn't actually telling me who that is.
02:58Well, here's where we can actually take some of these functions and instead of
03:03just using them in the SELECT statement or the SELECT part, we can actually use
03:08them in a WHERE or in this case in an ORDER BY clause.
03:11Plus I want to select FirstName from SalesLT.Customer ORDER BY, not the
03:21FirstName descending, but the length of FirstName descending.
03:26Execute and we find that indeed we have Janaina Barreiro Gambaro is
03:31apparently the longest one.
03:33That's quite a mouthful.
03:34As ever, you will find the Books Online is your friend here.
03:37You will see that there's a String Functions section where it talks about things
03:41like our UPPER, our LOWER, our LEN.
03:44There's ways of finding the LEFT, for example, which will turn the left-hand
03:48side of the character strings, say the first three characters or the
03:52first four characters.
03:54We have the ability to reverse them, to get things on the right.
03:58If you're working with fixed length strings, you might look at LTRIM and
04:02RTRIM, which have the ability to trim off any trailing or leading spaces from that length.
04:07And the wonderfully titled STUFF allows you to insert one string into another string.
04:14You'll also find a few odd ones, which you may not even have thought existed in the database.
04:19Let me show you an example of using SOUNDEX.
04:22I am going to jump back over here into my SQL and what I am going to do is
04:26select FirstName, LastName and then call the function called SOUNDEX on LastName.
04:37FROM SalesLT.Cutstomer.
04:39And what is this going to do?
04:42Well, it actually runs through an algorithm that figures out as much as it can.
04:48What the pronunciation, what the sound alike value is of these particular names.
04:53What will happen is you will get this four-character result for calling
04:57SOUNDEX that starts with the leading letter and then has a series of numbers
05:02kind of representing what it considers to be the overall sound of the
05:07remaining vowels and consonants.
05:10But what does that mean?
05:11Well, let's take it bit by bit.
05:13What would be interesting is if we then decided to ORDER BY the SOUNDEX of the LastName.
05:18Now, not surprisingly, where we have got some duplicate data here, they are matching up.
05:26But if I go down a little bit, what I am going to find is certain parts will
05:30actually starts to turn up where we are getting the same result, the same
05:35SOUNDEX, for actual different LastNames.
05:38So in this case, it's actually giving us the estimate here that Barker and
05:42Berger have kind of the same sound alike ability.
05:45So if we were trying to do a function or perhaps searching through customer
05:50names because we think that somebody's had the wrong name entered and we are
05:53not quite sure what it might've been spelled as, but we could do something like
05:58search for a particular customer WHERE the SOUNDEX LastName is equal to, and let's
06:07say their LastName is Brown.
06:09But we want to actually find that if it was possibly entered in under some other title.
06:14So if I execute that, what I am now actually getting is it's finding the overall
06:19sound of Brown and then matching that against anything else that sounds the same
06:25in the Customer table and returning that.
06:27So in this case, we are getting Brian, Brown, Bruno.
06:31We might debate if they actually sound alike but you could certainly see how
06:34this could be useful, particularly if you are trying to do, say, customer
06:38lookup applications.
06:39Again as ever, Books Online is going to be something that you want to have
06:44bookmarked and favorited until you become familiar with the different string
06:49functions and pick your little favorites.
06:51So take a look in Books Online at those available string functions.
06:55You'll find that some of them you're likely to use all the time, others not so much.
07:00But diving into each of the named functions is going to give you all the
07:04necessary information you need to know about how to use it.
07:07Most of the examples I showed just took one parameter.
07:10You will find a few of these functions such as SUBSTRING and STUFF will take
07:15more than one parameter because they're working with larger strings and they
07:19need to know where to start and where to finish.
07:22But other than that, these string functions kind of work the same way.
Collapse this transcript
Using date functions
00:00When you're new to SQL Server you can often find the date and time data types and
00:04functions a little bit confusing just because there appears to be so many of them.
00:08We have multiple different data types for dates. We have multiple
00:12functions, DATENAME, DATEPART, DATEDIFF, DATEADD, but really you'll probably
00:18find yourself using the same two or three over and over again and it starts off
00:22with a very common function in SQL Server.
00:26The ability to say GETDATE. Even though it doesn't take any arguments we do need
00:33to use the parentheses here, both the opening and closing ones. I execute that and
00:38we get the date and time right now.
00:41This is local on the machine, working with the machine's time zone. If you're more
00:45interested in getting the universal time code, you can say GETUTCDATE and
00:50execute that, but for most people most of the time GETDATE is just fine.
00:56Now what's often the case of course is that we don't want to have the full
01:00date and time. We're interested in one particular piece of this, maybe it's the
01:05month, maybe it's the day.
01:06Well if it's that straightforward, we can't actually just jump into that and I
01:10could say well what I'm interested in is the DAY.
01:14Now you can just say GETDAY. There is a function called DAY. As we can see,
01:19it's showing up in hot pink, but when I open it it's expecting a date time to be passed into in.
01:26Well we just saw how to get a daytime, which was GETDATE, so I'll wrap GETDATE
01:33in the call to DAY, hit Execute,
01:37and we have 14 and it is the 14th of November right now so that seems to make sense.
01:42Now you'll find that there are the equivalents for MONTH and YEAR, but beyond
01:51that you might want to get a little bit more granular. Well you can do.
01:57If you want to get to something very specific such as the day of the week,
02:01we use a different function called DATEPART but we call it the same way. DATEPART
02:06and let me just remove the rest because again you want to use IntelliSense when
02:11you getting used to this. DATEPART takes two arguments: an interval, really
02:17meaning what part of the date you are interested in, which piece of it, and what
02:23date do you want that part of. It sounds little silly but say we wanted to know
02:28the weekday off today's date.
02:30Well, I can actually put in the word WEEKDAY, comma, and then we're getting the
02:35date right now, which we'd just call trusty old GETDATE and execute that and
02:40we get one back here. Because the way that this server is configured right now
02:45Sunday, which is the day I'm recording this on, is considered the first day of the week.
02:49Now that can differ depending on what country you've installed SQL Server on.
02:54If you actually want to check you can look for the configuration variable called
02:58DATEFIRST, execute that,
03:01and what it's actually telling me is, it sounds a little counterintuitive, but
03:05it's actually telling me is that as far as this server is concerned the seventh day
03:10of the week is the first day of the week.
03:12Let me explain what that means. It basically means Sunday is considered day 1 and
03:18that's what DATEPART is actually returning for me here.
03:21Well what if I didn't want this one? What if I actually wanted a text value here?
03:27Yes I've got the DATEPART and it's the weekday, but I wanted the name of the week.
03:32How can I do that?
03:33Well there is another useful function that's called to DATENAME and what I can
03:39do with that one is actually just pass it the same information. I'm interested
03:43in the weekday, its that interval, and I'm saying tell it for today.
03:48Of course I am using GETDAY. You'll typically be using one of the date fields
03:52that's in your tables, but this will the trick here. I execute that and it tells
03:57me yup we are Sunday.
03:59Now what you're probably seen is as I've started typing say the word DATE,
04:03we get things like DATEADD, DATEDIFF, DATENAME, DATEPART and we've seen DATEPART
04:09and we've seen DATENAME, so let's do DATEADD. This gives us the ability to add
04:14days or weeks or months or whatever you want to an existing date.
04:19So if I want to say what is the date 30 days from today, I'm going to call
04:24DATEADD and tell it I'm interested in adding on days. How many? 30.
04:30Onto what? Onto today's date.
04:34Close that.
04:35It takes those as parameters, it figures out the result, and it returns the result
04:40here that says 30 days from today is the 14th of December.
04:44Well what if we wanted to go backwards?
04:47Now depending on how your mind works, you might be expecting to find a date
04:50subtract function, but no there isn't a date subtract function.
04:54But what you can certainly do is pass in a negative amount, -30 days, or in this
04:59case let's say I'll go back to seven months ago.
05:05We return that and that apparently was the 14th of April. No particular surprise there/
05:11Of course it is the 14th today here.
05:14The great thing is what we could decide to do is start to combine all of this.
05:19If we know that that's the date time value that we're retrieving I could then
05:24start to wrap it and say I want to call DATENAME, I'm interested in the weekday,
05:31and the value that I'm going to pass is what's return from combining DATEADD
05:37with GETDATE, again being very careful here on making sure that your parentheses
05:43are lining up where you have the right opening and the right closing ones, all
05:47needing to match of course. Then execute this and we find that seven months ago
05:53on the 14th of April it was a Wednesday.
05:57As I'll continue to suggest, Books Online is a great help here the date and
06:02time data types and functions will actually break it down, even splitting those
06:07up into functions that modified date and time values, functions that get the
06:11difference were it be using the DATEDIFF function, just passing in two dates instead of one.
06:17And then being able to use some of the things we've already seen like DATEPART
06:21and DATENAME to pick certain pieces out of those date time objects.
Collapse this transcript
Creating user-defined functions
00:00Not only do you have the over 200 functions defined in SQL Server, you can also
00:06define your own, what are referred to as user-defined functions, sometimes
00:10abbreviated to UDFs.
00:12So let's say I've got a piece of SQL here which is doing a sub-query and what
00:17this is doing right now is finding the company name for the customer who has
00:22placed the largest order.
00:24Okay, it's a simple example but this should do the trick.
00:27Let's imagine that I want to get this page of information 25 times a day.
00:32So I'd like a function that returns it. Well I can start off with this SQL that
00:37I have, but I do need to provide a few more pieces of information for me to
00:42describe what this function is and for a start, I am going to have to give it some kind of name.
00:48Now functions often take parameters.
00:51So I do have to tell SQL Server does this one have any parameters or does
00:55it have no parameters. And then I've also got to say what is returned by the function.
01:03So most of what I am going to have to write is actually wrapping around the
01:08contents the actual behavior of this function that I'm going to do.
01:11Well, luckily however SQL Server can help you a little bit with that and there
01:16are two ways of getting that.
01:18I have my Template Explorer open on the right-hand side.
01:21If yours is closed, you can get it from the View menu and just hitting Template
01:25Explorer and you'll find that in the Function folder, you actually have several
01:31boilerplate sets of code for creating functions.
01:35Inline function, table-valued function, multi-statement function, scalar
01:39function new menu, and scalar-valued function.
01:42Well, this one is quite simple.
01:44We are going to return a scalar function, which is the most common.
01:47It's the single value.
01:49So I have a couple of choices here.
01:51Well, which one should I pick?
01:52They both do the same thing but just let me show you the difference.
01:56If I pick me Create Scalar-valued Function and double click that, what it gives
02:01me is some boilerplate code here that sets me through what I've got to do.
02:05Now this is all right, but I actually prefer the other one, which is this guy,
02:09Create Scalar Function (New Menu).
02:13It's a bit longer but most of it's a comment.
02:15Now this is the same one that you would get if you drilled down into the
02:21database that you want, found in your Programmability folder, opened up
02:26Functions, opened up Scalar-valued Functions and then right click and set New
02:30Scalar-valued Function.
02:31Now that's what we want.
02:33So I might as well do that.
02:34Now the boilerplate code that Microsoft provides has a lot of green
02:38comments and I am just going to pull those out for readability right now.
02:42But there's a couple of places you could work with them.
02:44Let me close this to give us a bit more room to breathe.
02:48The first lines up at the top are just a couple of good settings.
02:52SET ANSI_NULLS ON SET QUOTED IDENTIFIER OFF and these are really about how
02:57things like equality comparisons are happening.
02:59I could actually remove these lines from this code.
03:02It would work nonetheless but I am just going to leave them there, because this
03:06is the stuff that we're interested in. Create function.
03:10well, I said on my other tab here that I needed a section for a name, I needed a
03:14section for parameters and a return value, and then what does the function do.
03:20And we are going to do all of that here.
03:22Here's where we actually say we are giving the function a name.
03:25Unlike any other object in our database we are really going to name it with an
03:30object name that will be inside a schema.
03:32I have currently got AdventureWorksLT selected as my default database.
03:37I could otherwise say use AdventureWorksLT here if I wanted to be doubly assure.
03:40Well, what I'm going to do is create a function and call it SalesLT. and
03:46then the function name.
03:47Well it's quite common that you'll see user-defined functions with the
03:51prefix such as UDF_ or UDF by itself or something like UFN I have seen quite a lot lately.
03:59I am going to call this ufnBigSpender.
04:03The reason for the UFN is it just makes it obvious if there's an error being
04:07generated later on that this is a user function.
04:11This next section is when I add any parameters. Does this take any arguments and
04:16in the way that say the MAX function requires a column name.
04:20Well, it actually doesn't.
04:21So I am just going to delete that, no parameters, but I'll leave the that
04:27just to make that clear.
04:28And I said we've got to tell it what it returns and you notice here it's saying
04:32you have got to say the function data type.
04:35Well, this is going to return the name of the company, in which case it's going
04:38to be a NVARCHAR, and I have got to actually declare it here because it's not
04:43returning the column.
04:45it's whatever we decide this is.
04:47So I am going to say it returns an NVARCHAR with a length of 50, but I haven't
04:52yet said what does this do.
04:55Now for the first-time what we are hitting is a way of defining a block in SQL.
05:00We have got this phrase begin and end and this is really the contents of our function.
05:06We've have set it up here with a name and return type and then what it's asking
05:11us to do is declare a return variable, have the two SQL statements for what the
05:16function actually does, and then return the result of the function.
05:20So we haven't worked with variables yet, so this is a good place to declare our first one.
05:25What I have to do is just have a placeholder, some named bucket to hold that
05:31company name, so I can return it later.
05:33The way we typically do it in SQL, on SQL Server certainly, is we will use the @ sign
05:39and that's just a little bit of shorthand. We could call it whatever we
05:42wanted, but it becomes obvious this is our own variable and I will call it
05:47company and then I give it the data type of NVARCHAR(50) because of course
05:53that is what I'm going to return.
05:55Then it says Add the T-SQL statements to compute the return value.
06:00Well, I'm going to copy them across from my other file.
06:04It isn't perfect, but we can do it here.
06:07So I am saying SELECT CompanyName.
06:09Now it's highlighted a problem here where I don't just have a boring old
06:15regular SELECT statement.
06:17what I need to describe inside this SQL is where does this new variable that I
06:23just get created get filled.
06:25What I need to say is this.
06:27SELECT @Company = CompanyName FROM SalesLT.Customer where our CustomerID is the
06:36top one based on the SalesOrderHeader table.
06:38That looks all right.
06:40So we declare the variable, we fill it, and then finally we return it and we are done.
06:49Now, bear in mind what I'm describing here.
06:52I am not describing the function. I'm writing the code to create the function.
06:59We have this line up here. That's what I'm telling as SQL Server to do is
07:03create this function.
07:05I only want to execute all this code once.
07:09So I take a scan of it.
07:11It looks all right.
07:12I'm going to hit Execute. Bang!
07:15What I get is Command(s) completed successfully.
07:17Now you may be wondering well, why don't I see the name of that company? Because
07:23I wasn't asking to do it.
07:25I just created that function. Prove it, you say.
07:28Well, I can.
07:29Let me go over to the section of the database where I created it.
07:33I am looking at the folder that says Scalar-valued Functions.
07:36It's not showing up here and they should be there.
07:38That's because I need to go up to the top of my Object Explorer and hit Refresh
07:42and then I see that I do have SalesLT.ufnBigSpender.
07:47Okay, things are looking good.
07:48In fact, I can actually close that code that I used to create it and then
07:52close that one as well.
07:54But it wouldn't be much good if we couldn't prove that this works.
07:57So let's open a new query window. I'm looking at AdventureWorksLT as my default.
08:03So I'm just going to say Select and drag this across.
08:08It is a function so it should end in the parentheses and I will hit F5 and yes
08:13indeed, calling the function ufnBigSpender brings me back Action Bicycle
08:17Specialists, which right now is the customer that's placed the largest order.
08:21Now you will notice that I'm getting the red squiggly here.
08:25It can't find this user-defined function or aggregate or the name is
08:29ambiguous, and usually the reason for that is that the IntelliSense just
08:33hasn't updated itself.
08:35Now there is a couple of tricks for getting around that, but the easiest and
08:38most simplistic way is just reopen SQL Server Management Studio. Bang!
08:45Open a new query and the next time I type it, SalesLT.ufnBigSpender is now
08:57actually showing up and we don't get the red squiggly anymore.
08:59Now, when you're new to SQL Server, creating your own functions isn't something
09:05that you should be thinking that you need to do a lot.
09:08In fact, it's quite rare compared to other parts of SQLite stored procedures
09:13that we are going to get into a little later.
09:15But it is a possibility.
09:17These functions can even be used in- line as part of WHERE clauses or INSERTs or
09:22UPDATEs, but they can't be used to actually do an UPDATE or INSERT themselves.
09:28It wouldn't allow me to actually write an UPDATE or an INSERT statement
09:32inside that function.
09:34Now the last question is what happens if I want to change it a little bit?
09:37Well, I can go back into that function which will be in my
09:41Programmability > Functions > Scalar- valued Functions and notice that when I pick
09:47my user-defined function here, I can right-click and say Modified.
09:51What I will have here is something that's very similar to the SQL I had before,
09:57but notice the phrase here.
09:59When I run this, it's not saying create function.
10:02It's saying alter function.
10:03So this is the SQL I would run to make any changes to it and open it up make a
10:10few tweaks, maybe change my sub-query to adjoin or what have you, and then run
10:15this SQL again to update that function.
Collapse this transcript
9. Stored Procedures and Transactions
Introduction to stored procedures
00:00All these functions that we have are great and they're very useful and you'll
00:04use them all the time, but you probably won't end up writing a lot of them.
00:09However, spend any time with SQL Server and you will write stored procedures.
00:14A stored procedure is simply a chunk of SQL wrapped up, given a name, and then
00:21stored in the database.
00:24It can then be executed multiple times from SQL Server Management Studio, called
00:28from an application.
00:29It's available to anyone who can get to the database.
00:32And these will be the main way that you reuse SQL.
00:36Talk to developers and database administrators and they'll tell you that almost
00:41all of the SQL they write is intended for a stored procedure.
00:45Now we've written a lot of SQL in the past few hours, but if I'd wanted to save
00:50and reuse any of this I simply would have just have the option to go to my File menu
00:56and save this as a text file on my desktop.
00:58Well, that's fine for some personal learning and testing.
01:02But when you write some more complex SQL that you're happy with and you want to
01:05reuse it, you want to wrap it up in a stored procedure so you can use it later.
01:10Like there are lots of built-in functions,
01:12there are hundreds of stored procedures already in SQL Server.
01:15If I expand any of the databases here, even the AdventureWorksLT, open up
01:21Programmability, this is where we saw our functions earlier.
01:24We also have stored procedures, and if I expand the System Stored
01:29Procedures here I see just quite a ridiculous amount of stored procedures,
01:34There is hundreds of them.
01:36Having said that, what you'll find is that many of these are intended for
01:41advanced administration tasks.
01:44Working with Active Directory policy management, logging, optimizing your
01:49indexes and your partitions, and it's very common that that'll begin with Sys
01:54for sys.sp_, meaning stored procedure of something.
01:59I'll find in some of these test databases, there are also a couple of
02:04user-defined stored procedures, which you'll often see with USP at the start of them.
02:08There is couple of them in AdventureWorks Light.
02:11There is a few more if you look at the larger AdventureWorks examples.
02:16Now there are similarities between creating a stored procedure and creating a function.
02:22They both need a name, and they can be defined with parameters. They can return values.
02:27Well, that does sound very similar to a function, but there are some substantial
02:32differences really about what they're intended for.
02:35Functions are typically designed to return a scalar value and they're SELECT only.
02:41they're not allowed to change anything.
02:44If you write a function, you cannot put an INSERT, an UPDATE or DELETE inside it.
02:49It's designed to be a quick way of returning a value.
02:54Functions themselves are designed to be used in-line as part of other SQL statements.
02:59So, while a function can be used inside of an INSERT or an UPDATE, it can't
03:04itself do an INSERT or an UPDATE.
03:07Stored procedures on the other hand are much bigger in scale and much bigger in scope.
03:12And they are executed directly.
03:14There is in E-X-E-C, EXEC statement.
03:17You execute the entire stored procedure.
03:20Stored procedures can do multiple things including inserting and updating and deleting.
03:25Stored procedures themselves can use functions, but functions won't be
03:29using stored procedures.
03:30Stored procedures can do multiple things at the same time.
03:34They can contain batches of SQL.
03:37Particularly useful if you want to do say an insert to several different tables
03:42at the same time. They're great for that.
03:45And after just taking your SQL and wrapping up and saving it as a stored
03:49procedure we can add parameters, variables, error handling, even flow control,
03:54having basic control in the case of IF statements.
03:57It's very much like turning it into a mini program.
04:01And in fact I've worked in several places, which did not allow developers to
04:05write their own INSERTs, UPDATEs, DELETEs, even SELECT statements in their programs.
04:10They had to execute stored procedures.
04:13One of the benefits of that is all the SQL stays in the database and can be
04:18controlled and hopefully optimized by the database administrator.
04:21But let's see how to do one.
Collapse this transcript
Creating stored procedures
00:00The process of creating a stored procedure is quite similar to that of doing a
00:04function, or indeed of defining any procedure, function, or method in any other
00:10programming language.
00:11We're going to name it. We have to give it a name.
00:13We're going to define any parameters that it takes.
00:16We're going to actually describe what it does, which in our case will be run
00:20either a single SQL statement or even batches of T-SQL.
00:25And then return any values, describe what it is that gets sent back.
00:30So how do we do that?
00:31Well, luckily, SQL Server Management Studio has some helpers for us.
00:36If I actually drill down into the database in which I want to create my stored procedure,
00:40I'll use AdventureWorksLT,
00:41I'll open up the Programmability section and we see that we have the Stored
00:46Procedures folder right beside the Functions folder.
00:48I'll open that guy up.
00:50I'll right-click the Stored Procedures folder and say New Stored Procedure.
00:55Again, we get a lot of boilerplate code.
00:58Just to make things clearer, I could get rid of some of the comments.
01:01I'm going to just ignore the settings here for ANSI_NULLS and QUOTED_IDENTIFIER.
01:05You just as a general rule leave those alone and then what we have is this.
01:10We have the CREATE PROCEDURE. Give it a name.
01:13Define the parameters if it takes any.
01:17If it doesn't, you can just scrap that.
01:19Then describe what it is the stored procedure does, which by itself if it's got
01:25SELECT statements is also defining what it returns.
01:29So, creating the simplest stored procedure in the world, let's give it a name.
01:34Well, we do have to put a schema for it, which could be the usual schema of SalesLT.
01:39Or quite commonly you'll also seem under dbo.
01:42For our case, it really doesn't matter.
01:44We just have to give it a name.
01:46I'll call this uspSimple, a user stored procedure. Very simple one.
01:54I'm going to make one that takes no parameters, so I can just delete those lines.
01:58I'm going to leave the usual message here of NOCOUNT ON.
02:02We'll talk about that in a second, and then I'm going to describe what actually
02:05happens in the stored procedure.
02:07Well, we could be doing INSERTs, we could be doing UPDATEs, we could be doing
02:10DELETEs, or we could just be doing standard SELECTs.
02:13I could just type in a very straightforward one.
02:16SELECT * From SalesLT.Customer.
02:22Perhaps that's what I want the stored procedure to be.
02:25It's a little bit of SQL, wrapped up and given a name.
02:31So let me create it.
02:33I'll run this SQL right now.
02:36Again, this SQL is creating the stored procedure.
02:40So when I say Command(s) completed successfully, I don't expect to see any results yet.
02:44We've now made the stored procedure.
02:46We just haven't run it.
02:47If I go over to the Object Explorer, it doesn't show up and that's because
02:50I need to refresh it.
02:52So I'll come up here to the Refresh button and then I can see,
02:55yes, I've got dbo.uspSimple. Great, looks good.
03:00I'll close these two files down.
03:01I don't need them anymore, and open up a new query window so I can actually run this.
03:08Well, we don't use the word SELECT.
03:10If we're running a stored procedure, we do this.
03:13We say EXEC for execute.
03:16I could type in dbo.uspSimple.
03:21That's not showing up in my IntelliSense right now, because the IntelliSense
03:25just hasn't refreshed itself.
03:27But we should be able to get this to work.
03:28If I hit F5, yep, it does the same thing as would have happened if I had
03:34manually typed SELECT * FROM SalesLT.Customer.
03:39If I get annoyed about the red squiggly lines that it couldn't find that stored procedure,
03:43I can either restart SQL Server Management Studio, or with my cursor in this
03:49SQL box, I can go to the Edit menu, click IntelliSense, and just say Refresh Local Cache.
03:55That takes a second and then the red squiggly should go away.
03:59Yes of course, this is the simplest stored procedure on the planet.
04:03You're not really going to be doing things like this all the time, although it's possible.
04:07If I want to change that, what I do is go back to the Object Explorer.
04:12I'll right-click that stored procedure and click Modify.
04:16It brings up a very similar SQL, except changing the CREATE PROCEDURE word
04:21to ALTER PROCEDURE.
04:23I'll just make my changes and run this file again.
04:27You can also execute it by right clicking the stored procedure directly.
04:32Execute Stored Procedure.
04:33It throws up a dialog box here that gives you the opportunity to type in any
04:38parameters you might use.
04:39In our case, we don't have any.
04:41So I'll just click OK and we get the generated code to run a stored procedure here.
04:46There is a little bit more than we need, because it's setting up for return values,
04:50which we don't really care about.
04:53But that's the general process.
04:55So, what if I want to do something that's a bit more complex?
04:58Well, rather than type it out bit by bit, I'm going to use one of the few
05:04pre-created files that I have here.
05:06I have got a similar looking file here to create a stored procedure, except this
05:12has a bit more to it.
05:13In this one, what I'm actually doing is inserting three rows into
05:18three different tables.
05:20Now, the question is why?
05:22Well, here is the deal.
05:23If I come over to my AdventureWorksLT, I have three tables called
05:29ProductDescription, ProductModel, and ProductModelProductDescription. What's this about?
05:34Well, the idea is that in this database if you're inserting a new ProductModel,
05:40you basically just insert the name.
05:42If I took a look at this, I can see that there are things like Classic Vest,
05:46Cycling Cap, Half-Finger Gloves, Road Frame, and so on.
05:50Well, the idea is that this table is going to support multiple international
05:55languages potentially.
05:56So, I'm not just going to put in a description straight for that ProductModel.
06:02I have a separate ProductDescription table.
06:04If I look at that one, I can see that I have certain descriptions that are being stored.
06:10But as I come down, I've actually got some odd text showing up, because we're
06:14actually using multiple languages here.
06:16We've got French. We have got some of the big five languages.
06:19Now the issue is that we could have different descriptions for the product
06:25models and potentially with descriptions like Chromoly steel, we can have
06:31different product models for the descriptions.
06:33So we've got a many to many relationship going on here.
06:36That's what this third table represents.
06:39PublicModelProductDescription.
06:41Well all this one really does is hook up a particular ProductModelID from the
06:46ProductModel table, to a ProductDescriptionID from the ProductDescription table,
06:51and then adding the Culture.
06:54So we could have English, we could have French, we could have Thai.
06:58Okay, even if that seems a little vague right now,
07:02it's a very common situation. When you're inserting some information, all I want
07:07to do is I've just got some new ProductModel and Description information.
07:11I don't really care about how many tables have to be created and how many rows
07:17I need to do and in what order I need to do the rows.
07:19But of course, it is drastically important.
07:22If I want to make this work, I have to insert the model, then the description,
07:27and get both of the keys that have been generated before I can insert the third one.
07:32So this is a great thing for a stored procedure.
07:35In fact, that's what this stored procedure example does.
07:39It takes three parameters.
07:41A productname that will be used to go into the ProductModel table,
07:46a productdescription that will be used to go into the ProductDescription table,
07:51and a culture, which will be used to go into the one that joins them together.
07:56In fact, culture I've set up here with an equal sign.
07:59That means this is a default value.
08:01I don't have to pass that information into the stored procedure.
08:05The idea is I can call the stored procedure once, give it a couple of pieces of
08:09information, and end up with three rows.
08:13So breaking it down a bit further.
08:15Inside the contents of the stored procedure, I define two variables that are
08:20going to hold the keys of the two inserted rows.
08:23So in a moment, we'll insert a new ProductModel and we'll grab
08:27the productmodelid.
08:29Then we'll insert a new ProductDescription and we'll grab the generated
08:32productdescriptionid.
08:34Then we'll take both of those and insert them into ProductModelDescription.
08:38So, step one is we insert the customer row with the parameter of productname.
08:43Then we use of this line, SELECT @productmodelid, that's our variable that we
08:48defined, = @@IDENTITY.
08:51This is a useful function that will actually grab the last inserted identity field.
08:59Step two, we do the equivalent for the ProductDescription table, inserting
09:04Description, and then we grab its IDENTITY into our productdescriptionid.
09:10Then finally, step three.
09:11We do our third INSERT statement.
09:14Inserting into the join table,
09:16the ProductModelID, the ProductDescription, and the Culture.
09:21So let me run this SQL to create this stored procedure.
09:26Execute, Command(s) are completed successfully.
09:28All right, well, let's find out.
09:31I open up a new query.
09:33I'm going to refresh my IntelliSense.
09:38Make sure I'm using AdventureWorksLT.
09:40Then I'll say EXEC uspCreateModelAndDescription.
09:48It's telling me here that I should be giving it a couple of parameters.
09:52The one that's highlighted in bold says "Well, give me the productname."
09:55That should be in nvarchar.
09:57I'll call it Groovy new product.
10:01I hit the comma and it highlights the next one, productdescription.
10:06Groovy new product description.
10:10If I wanted to, I could add a different culture.
10:14If I did the comma, it would tell me that I should put in a string for the culture.
10:19But you know, I'm not going to.
10:20I'm just going to leave that blank, because there is a default value of en for English.
10:25Well, let's give this stored procedure call a try. I hit Execute.
10:30Command completed successfully. Okay, looks good.
10:33Let's prove it.
10:34The quickest way is going to be, let's take a look at the entire table, right-click,
10:40Select Top 1000 Rows.
10:42Zoom right down to the bottom
10:44and we see that we have a Groovy new product inserted with a ProductModelID of 130.
10:51Take a look at the ProductDescription.
10:53Select that Top 1000 Rows.
10:55All the way down to the bottom and we have a Groovy new product description of 2012.
11:01So what I'm hoping to see now is that in ProductModelProductDescription.
11:05I will see 130 looking up to 2012 with a culture of en.
11:11Right down at the bottom, that's what we have.
11:13130 key looking up to ProductDescriptionID of 2012 with the Culture of en and
11:19all of the default values generated as they should be.
Collapse this transcript
Introducing transactions
00:00When we're updating data in our databases we'll often need to work with transactions.
00:05A transaction is an incredibly important thing in the world of databases.
00:09And to understand them the best way is to think about what we think of as a
00:13transaction in the regular world.
00:16If you talk about making a transaction, you often mean something in the world of commerce.
00:21Say you hand over $15 to the bookseller;
00:23the bookseller gives you the book.
00:25That's the transaction.
00:26It's important that both of these things happen. If you hand over the money you
00:31expect to get the book.
00:32If they hand you the book they expect to get the money.
00:35Either both of these things happen or neither of them do.
00:39In a computing system, a classic example of a transaction is a banking system.
00:45Imagine that you log on to your bank's web site and you want to transfer $1000
00:50from one of your accounts to the other.
00:53That's going to require to update operations. One, to subtract $1000 from
01:00account A, and the next to add $1000 to account B.
01:05If the first part of this transaction happened and then we tried to add a
01:08thousand dollars to account B and there is a problem with it,
01:11say it was locked for editing,
01:13we need to reverse the first part of the transaction, because we never want to
01:17be in a situation where $1000 had been debited from the first account but not
01:23added to the second.
01:24This transaction needs to happen.
01:26It's a unit of work. Either both of these things happen or neither of them do.
01:32So there is a common acronym that you will come across when working with
01:36transactions in a database.
01:38ACID, A-C-I-D. A transaction must be Atomic, Consistent, Isolated and Durable.
01:46A transaction being atomic refers to the core idea that the transaction must
01:50completely happen or not at tall.
01:52Whether there are two steps to the transaction or 20, they all contained in the
01:58transaction, they all complete successfully, or they all return to the original
02:03state, they roll back.
02:05There is no such thing as a transaction that halfway occurs.
02:09Now whether the reason for the transaction failing is that the database had
02:12a power failure, or ran out to space to hold the new data, or there was an application error,
02:18it doesn't matter.
02:19It's an all or nothing rule.
02:23For a transaction to be consistent, it means that any transaction must take
02:27the database from one valid state to another valid state based on the rules of your database.
02:34So even if a transaction is successfully atomic it cannot result in a situation
02:39that violates any of the integrity rules defined in the database.
02:44A transaction being isolated refers to the idea that the data in that
02:48transaction is essentially locked for the moment in which the transaction is occurring.
02:53So while we're trying to change a balance say on one of our account records,
02:58another part of the system must not be allowed to access that data until the
03:02first transaction has finished.
03:05And for a transaction to be durable, it means the transaction must be considered robust.
03:10If it happens and the database management system says this transaction is
03:15happened successfully then that transaction is guaranteed.
03:19Say if you go to a travel website and purchase a fly on an aircraft and you've
03:23been guaranteed seat 2A, you should be able to regard that transaction as being durable.
03:29Even if half a second later that database itself had a power failure and shut down,
03:34when it's rebooted the transaction will have survived that failure.
03:38They're not going to go and sell your seat to someone else, simply because there
03:42was a glitch in the system half a second after you made your purchase.
03:46Now the great thing about working with the database management system like SQL
03:50Server is that these capabilities are built into the system.
03:54When you're writing code there has to be a transaction, you don't have to worry
03:58about how to program these.
04:00You just need to know when to tell the database system, "now this is a
04:04transaction, that these two things, these three things, these four things must
04:10be done together," and that's the little code that you add.
04:13Bear in mind, not everything you do as a transaction, you need to decide what
04:18your transactions are and then you will be adding just a little code to control them.
Collapse this transcript
Creating transactions
00:00So I'm going to take this stored procedure a little further.
00:03The stored procedure I'm working with right now has three INSERT statements.
00:08It inserts an entry into the ProductModel table, it inserts a
00:13ProductDescription, and then it inserts a row that hopefully joins these two new rows together.
00:18Okay, so let's see it work.
00:20I'm going to flip over to where I have a prewritten piece of SQL that
00:26will execute that stored procedure, passing in ABCD123, and ABCD123
00:31product description.
00:33So I execute that code.
00:35We get a result, the Command(s) completed successfully.
00:38To double check that, I have a little bit of code over here that's simply
00:42selecting the most recently added row to each of those three tables. I'll run that.
00:48And I get what I hoped for, that the first row that was added was for
00:53ProductModelID 139, so that primary key was generated, ABCD123.
01:00We have the Description that's under 2016, and ABCD123 product description, and
01:06then we have the third row that is joining 139 to the first row and 2016 to the second.
01:14So everything looks correctly happened and in the right order. Here is the issue.
01:19What happens if I run that again?
01:22Well, it should work just fine, right? I execute it.
01:25Well, we have a problem going on here.
01:28The message isn't all that friendly, but it's telling me there is a Violation of
01:31UNIQUE KEY constraint. Cannot insert duplicate key in object
01:34SalesLT.ProductModel.
01:36Maybe I'm getting this message and I'm the developer and I know what that means.
01:39It's not the friendliest thing in the world, and here is the issue.
01:43If I go back and run this code again to see the most recently added records,
01:48what I'm going to see is that the row for ProductModel will nothing got added
01:53with that second call there.
01:55The issue is that there is a constraint on the table that says this name must be unique.
02:00So we didn't get a new entry.
02:02Having said that, the problem is we did get a new row for the Description.
02:08Because the last time around, it was 2016.
02:11We now have a new row in there with this Description, but it won't be connected
02:15to anything because we couldn't insert the first one, and because we didn't get
02:19the first one done, we didn't get the third one done either, because there was
02:22no ProductModelID to insert.
02:24So they're all now out of step.
02:26We've got a bit of a problem.
02:27We have this orphaned record hanging around.
02:30We'd have to go in and manually hook up.
02:32Well, what can we do with that?
02:35Well, really the deal is this.
02:37I want my stored procedure to work completely.
02:41I want either all three of these inserts to occur correctly or not at all.
02:47There are a couple of steps of doing that. Let's do step one.
02:50It's very, very simple.
02:51What I'm going to do is surround my three INSERT statements with BEGIN TRAN.
02:57It doesn't have to be in uppercase, but that's by convention, and COMMIT TRAN.
03:05So we're beginning the transaction.
03:06We're going to try and do the first INSERT, then the second INSERT, then the third INSERT.
03:12Hopefully, by the time we get past it, we're going to commit those changes to the database.
03:16So this is just the edited SQL for altering that stored procedures.
03:21So I'm going to execute that.
03:23Command completed successfully, looks good.
03:25Let's go over to this and we're going to shake up this entry a little bit.
03:31Let's say we're now trying to enter in a couple of other things here.
03:35We're entering a model description for DEF123, just so we can be sure
03:39that that's unique. I'll execute that.
03:42Command completed successfully.
03:44Go back over and check what the three most recent rows were.
03:47We have 141 for DEF123, 2018 for DEF123, and then 141 and 2018 joined. Looks good.
03:57Now, here is the proof.
04:00Let's try and run this because we have the transaction around them. I execute it.
04:05Okay, we have the error.
04:08That's to be expected.
04:09Just having a transaction doesn't mean that there wasn't a problem. We have a problem.
04:13We're trying to insert a duplicate key into the ProductModel.
04:17Here is the question though. What happened?
04:20Well, if I go over and run this code again, we're actually running into this situation.
04:25141.
04:27Okay, that was the one from the previous time.
04:29We didn't get one inserted there, but we did get a new one inserted here,
04:33because if you look at the last entry on the joining table, it was 141 to 2018.
04:40We've now got 141, but we have a 2019.
04:42So, what's going on?
04:45Here is the problem.
04:46Just because we said that we wanted to work with the transactions and we
04:54surrounded our three statements with this BEGIN TRAN and COMMIT TRAN, well, that
04:59doesn't really mean anything.
05:01Because just because there was a problem, well, we didn't actually tell it that
05:04we wanted to roll anything back.
05:07So here is what we do instead.
05:08Now to make this a little bit more readable, I'm just going to get rid of a
05:12couple of the blank lines here.
05:14Before I do my BEGIN TRAN, I'm going to write this.
05:18BEGIN TRY and END TRY.
05:24Now these officially don't have to be nested, but to make it a bit more
05:27readable, I'll do that.
05:32So we open up what's called a TRY block.
05:34Our TRY block means we're trying things from the opening of the TRY to the end of the TRY.
05:39That's where we're trying to do our transaction.
05:41The flipside of that is that if there is a problem, what happens is our TRY
05:47block is going to leap out and go looking for a CATCH block.
05:57What I need to do in here is say this.
06:00I want to ROLLBACK the transaction, which will just reverse anything that
06:06happened inside the BEGIN TRAN and COMMIT TRAN keywords.
06:10What would also be useful is if I do a little bit of the tracing information.
06:17I'll print out a message that says there was an error.
06:20That won't be all that helpful so I better output a little bit more and this is
06:25the way I would do that.
06:26I'm going to use one of the built-in functions called ERROR_MESSAGE ().
06:33This only works if you use ERROR_MESSAGE inside a CATCH block.
06:37It's only actually ever going to have anything if there was a problem.
06:41So this has changed the stored procedure.
06:43Now I'm going to of course execute that to make sure that the database knows
06:48that the stored procedure has been changed.
06:50So again, we need to go back and test it and check it.
06:53I'm going to jump across here in the first try and make one that works.
06:57Let's verify that everything is actually working okay.
07:02We're going to try and enter in the GHI123 and GHI123 product description. Execute.
07:10Come over and check the recently retrieved records, and we have 143, GHI123. 2020, GHI123. [00:07:20.00 ] 143, 2020.
07:23So the hope that I would have is if I go back and run that stored procedure
07:27exactly the same as it was, then when I come back to this page, I see
07:31exactly the same thing.
07:33I don't suddenly get an extra ProductDescription row in there.
07:37Let's go and find out. We jump across.
07:40Again, I am executing this again.
07:42Just because we have a transaction doesn't mean there isn't a problem. There is.
07:45Execute. There we go.
07:48We can see that we jump to our CATCH block.
07:50I've got there was an error.
07:52Then the message that pops out says Violation of UNIQUE KEY constraint,
07:56Cannot insert duplicate key in object SalesLT.ProductModel.
08:00Same problem. We want to see if there is a different behavior now.
08:03So hopefully, we'll see 143, 2020, and 143, 2020 when I execute this.
08:09We run it, and yes indeed, 143, 2020, 143 and 2020.
08:15Because of having those BEGIN TRAN and COMMIT TRAN inside the TRY block, and
08:21then catching it, rolling it back, and printing out the message, we've managed
08:25to roll back all those changes.
08:27So, very easy to start to wrap your different blocks of SQL with that TRY and
08:33TRAN in a very controllable way of handling that stuff.
Collapse this transcript
10. Core Administration Tasks
Understanding and creating indexes
00:00Flip to the back of any thick technical book and you're going to find an index.
00:05If you need to look up some content that's buried somewhere in a thousand pages
00:09of text, you're going to turn to the back of the book, scan through the index to
00:13find what you're looking for, say deviation analysis, realize it's on page 322 and
00:19then turn directly to that section to read that content.
00:23Indexes give you a quick way to look something up.
00:26That's what they do in books and that's what they do in databases.
00:30When we're working with SQL Server 2008, we can create two kinds of index.
00:35The first is what's called a clustered index.
00:37A clustered index is applied to one of the columns in our table and it will
00:42order our table based on that index.
00:46In this case I'm looking at it as the CustomerID in ascending order.
00:50If for example I remove the clustered index from that column and applied it to
00:55the LastName column, it would require that the data itself be reorganized
00:59internally based on that clustered index.
01:02So this method of indexing will actually have an effect on the internal
01:06structure of your tables.
01:08It'll order it internally based on the index.
01:11Now because of that, you can only ever have one clustered index per table.
01:16We can't actually order our data by both last name and customer ID at the same time.
01:22Now because of this, it is actually quite common that it's your primary key and
01:27in fact when you apply a primary key to a new table it will cluster that by default.
01:33And it's a pretty good idea to have your primary key be the clustered index.
01:37You'll be using that key to directly access those rows.
01:41It's a very fast way to work with your content.
01:44So in this example that I'm looking at, I almost certainly wouldn't create the
01:48clustered index on the last name. I'd leave it on the CustomerID.
01:52And in that situation we might be in a place where we would use two kinds of indexes.
01:57We go back to the idea that our CustomerID was our clustered index, but we know
02:02that we also want to directly look up people by say last name or company name.
02:07It's all really dependent on what the needs of your business are.
02:10So we'll create a non-clustered index, and we'll do that on the LastName column.
02:16What we'll do is scan through the LastName column and create its own index, a
02:21separate index that points to the different places in our table.
02:26So if we now have a non-clustered index based on LastName, and we're looking
02:30for the last name of Beck,
02:32I can scan quickly through that index.
02:34Find the entry that will actually point me to the row 16.
02:38And if you are writing SELECT statements that need to select again say in this
02:42case last name or company name,
02:44you'd want to think long and hard about do I need a non-clustered index? Because
02:48that will improve the performance of the selects of queries, of joins, with all
02:53my retrieval statements.
02:55Non-clustered indexes than are a separate index based on a column.
02:59You can actually add multiple, so you can have several non-clustered indexes per table.
03:05Now you might be thinking well, why I don't just make everything a non-clustered
03:09index, why don't I index every single part of every single table of my database?
03:14Well here is the issue.
03:16Because you're storing that in separate indexes, if you insert a new row into that
03:21table it's going to cause an extra right per index.
03:24If you have 10 indexes and insert one row, it's going to have to update
03:29those indexes 10 times.
03:31So, well it can improve the performance of your SELECT statements.
03:35It can certainly have a detrimental effect to your INSERTs, to your UPDATEs, to your DELETEs.
03:41We want to use non-clustered indexes when we know that we're going to be
03:46searching on that column, using that column to look something up, using it in a
03:50where clause or in ordered by, because it will help to those queries.
03:55So how do we make one?
03:56It's actually very simple.
03:58We drill down into our database and find the table that we're looking for.
04:02Let's say for example we're working with the Customer table here.
04:10Now I'm working with a very small database that only has 847 rows.
04:15But let's say I was working with one with 100,000, 200,000, two million rows.
04:21I'm going to leave the CustomerID as the clustered index, because it's a great
04:25one to have, we use the CustomerID a lot.
04:27But I might want to ask how we can improve the performance of SELECT statements
04:34that go against the LastName.
04:36Oh, here is how I do it.
04:37I'm going to right-click Customer and jump into Design.
04:41Well it doesn't become immediately apparent, the best way to look at your
04:44indexes is right click in the blank area here and select the
04:48Indexes/Keys section.
04:50And one that is actually telling me is that I have a couple of indexes already there.
04:54I've got the PK_Customer_customerID, PK representing not surprisingly primary key.
05:01So our primary key index is on CustomerID ascending and I can see down here
05:07it's created as clustered.
05:09And that would have been the case when this table was first defined.
05:11There's also an extra index here on EmailAddress, so I've got the IX, which
05:17tends to represent index.
05:19It is non-clustered. Again it can't be.
05:21We can't change this to clustered, because we've already got a clustered index.
05:26But somebody has obviously indexed EmailAddress if they think they're going to
05:29use that, which is a pretty good idea.
05:31And an EmailAddress is a very good exact match.
05:35Well, I'm going to add one for the LastName. Click Add and we get IX_Customer something.
05:42What we need to do first of is say what column that we want the index to be on.
05:46I'll click the ellipsis.
05:48It's not going to be CustomerID.
05:50It's going to be a LastName and Ascending is fine. Click OK.
05:54Doesn't have to be unique. That's fine.
05:56We would expect that in a larger or even a small company we would have multiple last
06:02names, and the type is an index.
06:04We can't just say to unique key.
06:06Nope, that's not what we're after. It's just index.
06:08So give it a name by standard.
06:11It's the table name and the field name, so I'll just call this
06:13IX_Customer_LastName.
06:18And that's pretty much it. Close.
06:20This is still considered an unsaved change to this table.
06:24So I'm going to save this now, and we now have a non-clustered index added
06:30to our Customer table.
Collapse this transcript
Monitoring and rebuilding indexes
00:00Because your indexes will get updated whenever there is a change to your main table,
00:06if there is an insert, we have to change the index, if there is an update, we
00:09change the index, if there is a delete, we change the index.
00:12Or your indexes can become what's called fragmented. They can become
00:16inefficient and split across multiple parts of the files that SQL Server uses
00:21to organize itself.
00:23Well, we can find out if that's an issue.
00:26Though I will tell you in a typical production database, it will be an issue and
00:29you want to defragment, even rebuild your indexes from time to time.
00:34Luckily, in SQL Server Management Studio, I can right-click any of my databases,
00:39come down to Reports and I will about find that in the Standard Reports section
00:43there's both an Index Usage Statistics, how much of these index is being used,
00:48and Index Physical Statistics.
00:49Both of these are great.
00:51If I click on Index Usage Statistics, it will tell me and this is going to be
00:55fairly recent information, how my indexes are being used.
00:58There's my new one that I created a moment ago.
01:01The index for the last name of the customer has been used a whole one time.
01:06Well, of course, we are on a development server. I wouldn't expect to see huge
01:10amounts but it would let us know if a particular index is or is not being used.
01:14Doesn't tell us whether it's fragmented though. For that we need the other report.
01:18And to come down into Reports > Standard Reports and Index Physical Statistics,
01:24which will scan all the indexes of all the tables, clustered and non-clustered,
01:29and you will often see a recommendation here to Rebuild and an index needs to be
01:34rebuilt when it's above a certain level of fragmentation.
01:37Now here's the issue.
01:39It's telling us that most of these indexes have to be rebuilt.
01:42The problem is, is this database and all its indexes are so small, so very, very tiny.
01:49There are only a few hundred rows in them, that these indexes will almost always
01:53show up as fragmented.
01:55Let's see if we can find something that's a little bit bigger.
01:57I am going to drop into the AdventureWorks Data Warehouse, AdventureWorksDW.
02:01Even though that's still technically a very small database,
02:04it's bigger than AdventureWorksLight.
02:05So I am going to go over into Standard Reports and look at our Index
02:11Physical Statistics.
02:14And as I scan, I see some recommendations.
02:16One says Rebuild here, the fragmentation's at 50%.
02:20Another says Reorganize, the fragmentation's at 6.
02:22It's still not a very big index.
02:27I can tell here that it's says this. The number of pages this indexes made of is
02:3132 and the pages only an 8K chunk of how SQL Server organizes itself.
02:37So there's still not a lot going on.
02:38I am going to come down a little bit further.
02:42And let's say here we have got FactResellerSales as one of the tables in this data warehouse.
02:47If I expand that, it says it's recommending a rebuild on this FactReseller_Sales
02:53EmployeeKey- NonClusteredIndex.
02:55That's a 43% Fragmentation with 113 pages. We might see a difference here.
03:01So how do I do this?
03:02Well, I need to find the indexes for this table, FactResellerSales.
03:08So I will expand the Table section in Management Studio.
03:11I will find FactResellerSales.
03:14I will expand that and inside the table, we're mostly been working with Columns
03:18and Keys but there is an Indexes folder which shows me all those indexes.
03:24I could find the FactReseller_Sales EmployeeKey, which is the one I'm looking at here,
03:29and I could right-click that and I have a choice to Rebuild or Reorganize.
03:34I can also right-click the Indexes folder and Rebuild All or Reorganize All.
03:38What's the difference?
03:39Well, If I say Reorganize, what it's going to do is take some of those pages,
03:45those little 8K chunk that are been used to store this index and shift them
03:49around in a bit of a better order.
03:51Reorganizing is quite quick and we will get you some of the way there.
03:55If I say Rebuild, it actually dumps the index, completely drops it and
04:00rebuild it from scratch.
04:01A much better way of getting a better index but it will take a while. In fact
04:05if you have a very large database, it may be hours or even for terabytes of
04:10data, days to do this.
04:12This one shouldn't take anywhere near as long.
04:14I am going to right click this folder and say Rebuild All.
04:17What will happen is its scans all those Indexes, tells me the fragmentation, that
04:21we really don't have anything apart from this last one.
04:24But I am going to click OK anyway.
04:27It bangs through all of them and just rebuilds those indexes.
04:30So, let's see if we can get a better report for FactResellerSales.
04:35I will run the report again, come to the Database, right-click, go to
04:39Reports > Standard Reports > Index Physical Statistics, and then come all the way
04:44down to FactResellerSales and I am not being recommended anymore to rebuild it.
04:51If I expand that one, it looks actually pretty good.
04:53We have a 0% fragmentation across 113 pages, nice.
04:58Now typically, what you're going to look at when you get into index
05:02defragmentation and this will be part of your overall maintenance plan that
05:06you and/or your DBA are going to help build and create.
05:10Like a lot of other things when using Management Studio, if you are
05:15rebuilding all your indexes, you do have the opportunity to script that to a
05:20new query window and it will actually generate the SQL to rebuild those
05:25indexes if you want to.
05:26And that might be a starting point for creating say a larger SQL script
05:31that will execute a maintenance plan, the maintenance structure on your entire database.
05:37When you're getting into that, you certainly want to go out to the Internet and
05:41start looking at some blog posts and finding some good routines.
05:44Because you'll find that certain people gather up their suggested scripts and
05:48make them available for general plans for database maintenance.
05:52But for us on our development server, this is how we approach monitoring and
05:57defragmenting our indexes.
Collapse this transcript
Monitoring database size and integrity
00:00Looking at index fragmentation, can give us a very specific idea of a tiny
00:06little piece that we want to keep an eye on and fix and maintain in our
00:09database, but what about the larger idea of the database itself and the
00:14consistency and integrity of the database?
00:16Well we can use some of those reports to get some very useful information.
00:20If I right-click any of my databases I can drop into my reports, my standard reports,
00:26and start off with something very simple and straightforward like the disk usage idea.
00:30How much space is this taking up?
00:32Getting this little pie chart that will tell me how much of it is data the
00:36purple stuff, how much of it is index, how much of it is unallocated and just
00:40being held back for new rows and new information.
00:43And that's useful, but won't really guide you in any particular direction to doing anything.
00:48However, some of the other reports can be a bit more useful that way.
00:52We can drop into disk usage by top tables, allowing you to quickly scan which of
00:56your tables are taking up,
00:58both of them have a space and have the most rows in them, not necessarily the same thing.
01:03We can drop into our reports, and one of the ones that I like is the Disk
01:07Usage by Partition.
01:09Now we don't cover partitioning in this course.
01:12Partitioning is the ability to take large tables and actually split them across
01:16multiple physical file structures.
01:18It's not something we can do with SQL Server Standard edition.
01:22It's only actually available in the Enterprise editions.
01:25But the report nonetheless gives us some interesting stuff to look at.
01:29Because it will show us all our tables and allow us to scan things like our
01:34indexes and how much space those take up.
01:37So here at the DimCustomer table 18,000 rows, 9,000 K, and I can scan the
01:44indexes as well, but seems to show some consistency.
01:47I've got 18,000 rows in them, but it shows me how space the indexes are taking up.
01:53Dropping back in to the Reports we have things like view all our transactions.
01:58I haven't executed anything against this database for quite a while so there is
02:02nothing that's been run.
02:04But we would have that if we wanted.
02:06We've got user statistics, schema changes, the index usage and physical
02:10statistics we saw in the previous movie.
02:13I also have this one, Database Consistency History.
02:16Now the first time you run it, you're likely to see no messages at all.
02:20Because what it's telling you is this.
02:22It's a history of executions of DBCC CHECKDB.
02:26Well what the devil does that mean?
02:29DBCC CHECKDB is a command that you can run if you're in the SysAdmin role and
02:34it does internal consistency checks on the database.
02:38Let me show you how you do it.
02:39Open up a new query window, making sure that AdventureWorksDW is selected or
02:44whatever database that I want to run this on, and I simply type DBCC CHECKDB. Execute.
02:51Depending on the site of your database it may take a while to do it.
02:55But when it comes back, it's going to run a whole bunch of internal checks on
03:01your tables and some of the background objects that support your tables.
03:05And hopefully you will end up with CHECKDB found 0 allocation errors and 0
03:10consistency errors in the database.
03:13Even if you're doing backups regularly, even if you're taking a look at your
03:17indexes and defragmenting those, you should be running this DBCC CHECKDB on a
03:23fairly regular basis, at least weekly.
03:25Possibly more often if you've got a very volatile database.
03:28Again like with your indexes this should be part of your enterprise level
03:33maintenance plan idea.
03:34But the nice thing about it is every time it's run, it is then stored in the log,
03:39and if I run my report again on the Database Consistency History, I should
03:43expect to see 2 instance of this and how long they took.
03:47Now obviously we're starting to build very complex structures here.
03:51And a real deep discussion on what we should be doing for database monitoring
03:55and optimization is beyond the scope of this course, but there's an immense amount of material, both in
04:00Books Online and on the web, that you'll probably want to take a look at as you
04:04get deeper into your databases.
Collapse this transcript
11. Backing Up and Restoring
Creating backups
00:00We have all these great features of SQL Server to keep our data fast and clean.
00:05We've got constraints for integrity, transactions, indexes, but at the end of
00:10the day all these databases are being stored on files on a hard drive and hard drives break.
00:18So even if you've created your database files on a nice RAID 10 unit, you still
00:23need to be it backing up and backing up regularly.
00:25Or we can do this using SQL Server Management Studio or even just SQL commands.
00:30We can get this going.
00:32We backup one database at a time.
00:34So open up the databases, select the one you're interested in, not that it really matters,
00:39and come down to tasks where you'll find an entry for backup. I select that.
00:45The reason why I say it doesn't matter which database you right-click on is
00:49because the first question you get in the backup window is which database you're
00:54interested in backing up.
00:56Most of the time for a straightforward situation, you're going to keep
01:00the default values.
01:02Really the only choice you're interested in here is this backup type. Is it full
01:06or is it deferential?
01:08And in fact the first time through the only option you'll be able to do is a full backup.
01:13After you've done one full backup, you can select to do a differential one,
01:18which will be smaller in size, because it's essentially the changes between your
01:22last full backup and the state of the database right now.
01:26The reason that SQL Server is able to do this is because it keeps track of the
01:31different backups that you've done.
01:32It likes to understand when you did your full backups, when you did your
01:35differentials, and one of the impacts of that, I mean that if you're selecting a
01:39full backup, you also have this option here to do
01:42what's called a Copy Only Backup, which really detaches from the main chain of
01:48backups, creating a self-contained one.
01:50You'll notice that you're not able to do a copy only backup when it's
01:53differential, because you haven't got anything to make a difference on.
01:57So we're going to do a full backup.
01:59It's not a copy only, And backing up the database.
02:02We can give it a name.
02:03I'll accept the default here.
02:04There is an expiration option here that the backup set will expire after
02:09certain amount of days.
02:10If it's zero that just means it won't expire at all.
02:14Now this section down here, the destination, this can trick people and trip
02:19people up. I've known several people that have been bitten by this.
02:23This is the default location that your backups will go to.
02:27It's actually just the path that SQL Server is installed at and we've got a
02:30Backup\AdventureWorksLT. bak file. Okay, that's fine.
02:36But people see this button. They can click Add and it gives them the ability to
02:41add a new path or add a new destination on disk.
02:45That looks all right.
02:46Here's the issue. If you do this and you can add multiple locations in here
02:51to back out to, but it does not copy the backup and duplicate it from place-to-place.
02:56What it will do is almost like a basic RAID pan that will split up the backup
03:02across multiple files.
03:03Now that means it can be faster, which is a good thing, but it means if you have
03:07multiple locations and you have a need to do a restore, you have to get the
03:12backup files from every location.
03:14So this is not a redundancy option. It's just a speed one.
03:17I'm going to click over here on the left to the Options page.
03:22Now I've already done a couple of backups, both full and differential, and it's
03:26asking me here, do I want to back up to the existing backup set?
03:29And yes I do. I'm continuing it but I do have the options where I could say back
03:33up to a new media set or overwrite all the old ones.
03:37I do have some options here to verify the backup when finished.
03:40Yup, I think I'll do that.
03:42Click back to the General page and I'm just going to go for it. Click OK.
03:46This is a fairly small database.
03:48So it's a pretty quick job, but obviously if you have lots of rows it's going to
03:52take a little while to work.
03:54That's how you'd get your first backup started.
03:56Now your organization may have and hopefully does have a more strategic backup
04:02strategy that should include SQL Server.
04:04You may have third-party tools that you're dealing with, because backup and
04:09restore should really be an enterprise-wide idea.
04:13But if you're going to be involved even in basic administration of SQL Server
04:17you certainly want to be comfortable with doing a casual backup.
Collapse this transcript
Creating differential backups and using backup compression
00:00Once we've done at least one full backup, we can actually continue to do the
00:04next ones by doing differential ones, although every now and then we'll do a full backup again.
00:10All we do with this is go in and just change our backup type to Differential,
00:14and I can just click OK.
00:15Now that's not very exciting, so let me show you a couple of other options
00:19that you have there.
00:21Once again just going back into the database, Tasks and Backup. To get a bit of
00:26information about how many times we have done a full backup, how many times
00:30you've done a differential backup,
00:32we can select the destination path and come down here to the Contents button.
00:35And that will allow us to kind of step through or different backup sets,
00:39which ones were full, which ones were differential, and how long we've been
00:42keeping them around for.
00:44This whole collection, this whole suite of full and differential backups, are in
00:48order and they are known as a media-set.
00:50SQL Server likes to understand the history of how you've done your full and your
00:56differential backups.
00:57It makes a lot of use of that.
00:59That can be impactful if you want to change a few other options.
01:03So let's say I drop into the second page here and I can see that my default is
01:07I am backing up to the existing media-set.
01:09And I start to look at some of the other information like Verify backup and
01:13Perform checksum on media.
01:15And then I look at this guy here. Compression, Set backup compression.
01:19This is a reasonably new feature with SQL Server.
01:22If you have large databases it's a good thing to have, because you can't
01:26compress the backup files.
01:27With the choice of whether we do or not is based by default on the server setting.
01:32What does the server want to do, and I'll show you how to change that in a minute.
01:36But let's say I've looked at this and think, oh well I can apparently
01:39override this, great.
01:40While I'll decide that I'm going to compress my backup.
01:43So I'll click OK and it says "No, you won't." That's because when you've got a
01:48media-set of full and differential backups they either have to be all
01:53uncompressed or all compressed, you can have a bit of both.
01:57So my options here, if I wanted to compress the backup is that I have to click
02:01this guy here to backup to a new media-set and erase all the old ones.
02:06I don't want to do that, so I'm actually going to leave it at the default and
02:11just run through it there.
02:12One last thing that I could do, if I don't want to come into SQL Server
02:16Management Studio and perhaps I just want to script a job to occur,
02:20we can, once we've set this up, let's say I want to get ready to do differential
02:24one, I'll check my options and they look all okay,
02:27I can come out to the top of the window and click the script button, which
02:31gives me a few options.
02:32Generate a script action to a new query window, script action default to
02:36clipboard, to job, what does this do? Well,
02:39it's quite simple.
02:41If I hit that, Cancel, I'm going back now here it's generated the SQL version of
02:47what we were just doing with SQL Server management studio.
02:52This is the SQL command for doing a backup. We are backing up the database to
02:57this particular address and it's a differential backup.
03:02So if you want to start writing your own commands, you could always start off
03:05with that window and then just script it and make any changes that you wanted to do.
03:11But finally on that idea of setting the server default for our backup
03:16compression, if you want to, you can change this at the server level.
03:19I'm going to right-click the instance name itself, come down to properties, and
03:23you'll find that particular setting under the database settings here, where
03:28we've got the option here to compress backup or not.
03:32Now I'm going to leave it unchecked as it was before, but that's where I could
03:35change it, if I was very much intending to do backup compression on everything.
Collapse this transcript
Restoring databases
00:00One of the reasons I like to get into the habit of backing up regularly even
00:05when I'm just working with sample databases on a development server is that
00:09it's way too easy to run a little bit a code that maybe does a little bit
00:13more than you expected.
00:14So I'm pasting in a few delete statements here. This is not the kind of code you
00:19want to execute a lot.
00:20Well this is going to do with the no WHERE statements is it's going to go
00:24through and delete everything in all of those tables.
00:28So say this accidentally happened yesterday, a few hours ago, a few seconds ago.
00:34And I'm now in the problem that when I go and look at my Customer table, for example,
00:41I have nothing whatsoever and this is not what I want.
00:44Well luckily I was doing regular backups so I can restore these.
00:49I'm going to do this from SQL Server Management Studio, though you can do it from
00:54the SQL command line.
00:56I'll right-click one of the databases again it doesn't matter.
00:59Even if the database wasn't showing up, you can still pick any of them.
01:03Go into Tasks and then Restore. We're going to restore the database.
01:07And again it gives you the drop-down. Is this the one that you want? Yes it is.
01:11So a point in time most recent possibly? Yeah absolutely.
01:15If you had a specific date and times in a long chain of backups, you can pick it
01:20from a particular place. We don't need to here.
01:23And because I've been using SQL Server management studio to do my backups, it's
01:27taken a look and says okay I have the available files. I know the backup set. I'm ready to go.
01:33What else would you like to do?
01:34I do have a few options.
01:36I'm going to leave everything as is. There are some options you might want
01:39to look at if your situation is a bit more unusual, because there's what's
01:44called a recovery state.
01:45What state do we want the database to be in when it brings up?
01:50Perhaps you're trying to recover a state of the database where you're just
01:54interested in getting it as accurate as possible, but as far as you're concerned
01:58it could be in read only mode.
02:00Well that's not usual. I want to have this database ready to go and ready to use.
02:04I'm going to leave all the options there.
02:06I'm going to click OK.
02:11It's throwing me up an error that exclusive access could not be obtained because the
02:15database is in use. Well we've got a couple of things we can try there.
02:18It's probably just a little bit more concerned with the fact that I only just
02:23did those DELETEs and it's hanging on to some locks here, so I'm just going to
02:27reconnect to the instance.
02:33Go back in and try to select to restore the database again, leaving all the
02:38default options, and then click OK.
02:43And this time we're looking good.
02:45Of course, the proof is can we go back into that database and find to say the
02:51customer information? And yes, we can.
02:53Now similar to working with your incremental and your full backups, if you are
02:59working with a restore, that can be scripted if you need to. Hopefully you are
03:05not having to do a restore of your database often enough that you actually need
03:08to have a permanent script file for doing it.
03:11And certainly if your database is in a more complex situation, if you're doing
03:17not just the regular full and differential backups, but you have split your
03:21backups into the more granular pieces of file groups and transaction logs,
03:26you are going to want to take a little bit more care than just the quick
03:29way that we've done it.
03:30But certainly when working with a development server, like we're doing right now,
03:35this should be a perfectly and acceptable way to do both your backups and
03:38your restores.
Collapse this transcript
12. Security Features
Introduction to SQL Server security and permissions
00:00Here's what the SQL Server understands about security.
00:03It knows you make a database because you want to people to use it.
00:07There are users and groups of people.
00:09There are Web applications and desktop applications that want what it is you
00:13have in your database.
00:14The SQL Server calls these principals.
00:17A principal is something that will connect to SQL Server to request a resource.
00:22They'll select from a table or update a table, or execute a stored procedure, or
00:27even create a database.
00:28And on the other end SQL Server knows about the things that exist inside of it.
00:34The databases, the tables, the stored procedures, the functions, the
00:38schemas, the views, all these things we have worked with our considered
00:41objects in SQL Server.
00:44And yes, many objects contain other objects.
00:47Databases contain schemas.
00:48The schemas contain tables and stored procedures, and so on.
00:52But just because a principal can get into SQL Server at all doesn't mean
00:57that it should have blanket access to use and change and control to all of those objects.
01:01So in between your principals and your objects are the idea of permissions.
01:07What can the principal do with that particular object?
01:11And we have multiple permissions that include select, insert, update, delete,
01:16the standards, but we also have execute permissions for stored procedures.
01:20We have the ability to alter or change parts of the database or control the
01:25entire database or any object in it.
01:27And these are of course different based on the person and the object.
01:31Bob might need permission to update the Sales database, but not the
01:35Customers database.
01:36Alice should be able to execute a stored procedure but not modify it.
01:41Fred can select from a table, but not insert into it.
01:45And us, well we want to be able to log on to the SQL Server and do everything
01:50everywhere, Select, Insert, Delete, Update, make new databases, edit stored
01:56procedures, create backups, restore databases.
01:59Now if you have to define all of these different permissions and connections
02:04individually, it's going to get very tedious.
02:06But to make it easier SQL Server helps us out by defining several common roles
02:13that actually fit between your people and your permissions.
02:17These roles are setup with a set of permissions already.
02:22You take your people or your groups and you attach them to particular
02:26roles inside SQL Server.
02:29And this makes the management process much easier.
02:33Now some roles exist for each separate database.
02:37Every time you make a database you get a new set of these roles just for that database.
02:42Those include the role called db_datareader.
02:46If you're in the datareader role for that database you can by default read from every table.
02:52A bit better than that you have db_datawriter.
02:55You can also write to that table.
02:58You have some quite specialized roles like db_backupoperator.
03:03You might never use this but if you had someone where that was their particular
03:06job role, you could put them in this SQL Server role.
03:10And also for the database you have something called db_owner.
03:14The db_owner role gets to do everything in that database.
03:19There are a few more but this is the core of them.
03:22And because these roles exist separately for each database, you could be granted
03:27the db_owner role on one database and just be a db-datareader on another.
03:33Now some other roles are not duplicated. There is just one role across the entire server.
03:38For example, there is a role called dbcreator.
03:41And now as you might imagine, if you're in the dbcreator role you can create
03:47db's, you can create databases.
03:50There's a securityadmin role that gets to and in fact security for this server,
03:54set up new users, set up new principals.
03:57There is a serveradmin role, and then at the top of the tree there is sysadmin.
04:02Even though there are a few others which you can explore and books online,
04:05sysadmin is godlike.
04:07If you're a sysadmin on that server, you can do anything.
04:11It effectively makes you DB owner of every single database inside that instance.
04:17So this whole security thing breaks down into principals, roles, permissions and objects.
04:23For example, the principal Fred can be given the role db_datareader,
04:29which grants the permission to select and that's attached to the say the Orders database.
04:34Or me. Well I'll get the role of sysadmin, which gives me the permission control
04:39over everything, over the entire instance and everything inside it.
04:42And your principals can also be Windows groups too.
04:45If you have good groups defined in your domain, you could for example give the
04:50managers group the role db_datawriter and gives them the insert, update, delete
04:55permission on, for example, the ProjectPlans database.
04:59But in the same setup they don't have permission to write to the AdventureWorks
05:04data warehouse. They just have permission to read from it.
05:08And using the built-in roles in SQL Server will make handling your
05:12permissions much easier.
05:14Now technically speaking a role actually is a principal.
05:18But I find a bit more useful to separate them out like this.
05:21Thinking about giving that principal a particular role, which gets the
05:25permission to an object, and you still have to choose what these hookups are and
05:31you get a lot of flexibility to do that.
05:33We're going to see how to do that in SQL Server Management Studio in just a moment.
05:37Now do bear in mind, you can define your own roles.
05:41But only look into that after you've really become comfortable with the built-in ones.
05:45Because most of the time the available ones like, db_datareader, sysadmin, datawriter,
05:50db_owner, they're going to work just fine.
Collapse this transcript
Adding a Windows user to the database
00:00The best practice for working with security in SQL Server is that you're using
00:04Windows authentication.
00:06That means before anybody actually accesses your SQL Server, they have had to
00:10log on to the domain or even log on to the local machine, as a legitimate Windows
00:15user, that user already exists.
00:18What that means is your job inside SQL Server is not declaring that there's a
00:23certain user called Barbara, certain user called Alice,
00:26but you're saying what is that user allowed to do in SQL Server.
00:30And this is the way that we do it.
00:32Instead of opening up my Databases section, I am going to expand my Security and
00:36take a look right now at my Logins.
00:38Now, right now, I don't have a lot. In fact most of these are essentially
00:42background and service accounts because I haven't added anybody to access this instance yet.
00:47So I am going to right click the Logins folder and say New Login.
00:51Let's be very clear about what we're doing.
00:53We are not actually creating a user here.
00:55The user already needs to exist outside SQL Server.
00:58We are just saying what are they allowed to do.
01:00Now, if I knew the name of the user, I could stop typing it in or I can use the
01:05Search button over here, even do a little search on the users that I have.
01:11And I am going to use this user here called AmayaW.
01:14Now I have got a stand-alone machine, so I'm just using a local user, preferably
01:19in a production environment. You've got a domain user obviously.
01:22So that user is already defined. We are saying what is she allowed to do.
01:26And the two most important parts of adding this user as a new login is the
01:31Server Roles section, which says does that user have any special server role abilities?
01:36And the User Mapping section, which really means how is this user attached to or
01:41defined in any of the databases that we have that exist?
01:45So question one, should this user be granted anything particularly special?
01:50Should she be given sysadmin rights and be effectively the administrator for the
01:55entire SQL Server instance?
01:56Well, let's say in this case maybe not.
01:59Though I could give her dbcreator and allow her to make new databases.
02:03By default, everybody gets put in the public role but the public role just
02:06doesn't get to do very much.
02:08So that's okay. I will leave it as it is.
02:10I'm going to jump across to the User Mapping section.
02:13What I want is for this new user to be added to the AdventureWorksLT database
02:19and the NorthwindSQL.
02:21If I just select the database, I can't change the role.
02:24I have to click this checkbox that says Map.
02:27Once I do that it's telling me I'm mapping this user to certain roles in that database.
02:33So here's what I could give her db_ datareader and db_datawriter and leave her in public.
02:40I could come down a bit more and with NorthwindSQL perhaps just make her a reader.
02:46Click OK.
02:47That user has now been added to SQL Server and mapped to those two databases.
02:53Now of course if you had to do dozens or even hundreds of users, that's going
02:57to be a little tedious.
02:58So one of the things you can also do is when you're creating logins, if you have
03:02a group that you know of that already gathers the people together,
03:06it's being managed say in Active Directory,
03:08you can just use the name of the group.
03:10I do have a simple group that's been defined here locally.
03:12It is called sales.
03:14Not showing up right now just because I'm only showing the built-in principles.
03:18Let's select Groups.
03:20Find again and there is the Sales group.
03:24Click OK, click OK.
03:26I am leaving the other options here.
03:28I don't need to map them to a certificate.
03:30I don't need to change the default database.
03:32But what I'm going to do is say no, they have no specific server role but in the
03:38User Mapping I'm going to map this and add everybody and give them the
03:43db_datareader privilege.
03:48Click OK and that means from this point on, anybody who's defined on this
03:53machine is being in the Sales group will allow to be execute statements that
03:57read against that database.
04:00Now if Amaya was in the Sales group and given her own privileges, she's going
04:04to get the most, she is going to get the highest ones of whatever she has been granted.
04:08Even if I was defined in that Sales group, because I'm also defined as sysadmin
04:14in this instance I get to own everything everywhere.
04:17Adding groups of course makes it a lot easier to manage because you then
04:21actually manage your people into the groups in Active Directory.
04:24You don't really have to worry too much about working with that in SQL Server.
04:29Now, just to show you what's actually happening when I map a user into a
04:33database is you will find that the databases I mapped to, such as
04:37AdventureWorksLT, themselves have their own security section with their own users defined.
04:43And when I selected to map them, we see the individual entries just for that
04:47database showing up here.
04:49I can right-click, select Properties, and see that in this case Amaya has
04:53datareader and datawriter.
04:55I wouldn't get the choice here to add her to a server role but this is okay here,
05:00and again looking at Sales, I can see that Sales has just the
05:03db_datareader role membership.
Collapse this transcript
Creating SQL Server logins and switching authentication modes
00:00One of the best practice for defining your principals is to make sure that they
00:05already exist as users on your domain or on your local machine and that way
00:09we're using Windows authentication as that first barrier to entry, they have to
00:14have logged on to your machine or to your domain to get there,
00:17it's not the only choice that we have.
00:20When I'm creating a new login one of the options I'll have here is to switch
00:24from Windows Authentication to SQL Server Authentication.
00:28This is not a minor difference. This is a big deal.
00:31With Windows Authentication selected the login name that I type has to exist
00:36outside. It must exist on this machine or on that domain.
00:40When I select SQL Server Authentication it really means define a new user, give
00:45them a name, give them a password. They didn't exist before. I'm just writing them
00:50and they may now exist in SQL Server.
00:51A very common reason to do this is that you have, for example, a web service
00:56application, something that needs to connect to this machine, but because of the
01:01way your network is setup it can not actually authenticate as legitimate Windows user.
01:06They might be running on a Linux box, they might be running as a Java
01:10application. They're outside your firewall and they can't logon as a Windows user.
01:15They need to connect with a username and a password.
01:19So we'll set this up. We'll give them the name WebServiceApplication and I'm
01:23going to type in a password here.
01:27I'm going to uncheck the user must change password at next login. In case this
01:32was an application was connecting, we don't want to throw that problem in
01:35their way, and it's up to you whether you want to enforce password expiration and so on.
01:40All the other choices that we have are the same. Do I want to give this new user
01:45any server role? And no I don't and in fact the only thing I'm going to give them
01:50is the ability to read from AdventureWorksLT.
01:54So I'll switch to my User Mapping, I'll select the box beside AdventureWorksLT,
01:59and I'll give this user the db_datareader role and click OK.
02:05If I expand Logins I'll see that user listed there. Again this user has not
02:10been created on this machine, he is not a Windows user, not a domain account.
02:15He only exists inside SQL Server and the question is well, prove it. What can that user actually do?
02:22Well we can actually emulate that and try and log on to this database as that user.
02:28I'm going to click this button up in my Object Explorer that says Connect Object
02:32Explorer and it throws up the usual dialog box we've seen. What I'm going to say is
02:36instead of connecting as me I want to connect to the same machine using SQL
02:41Server authentication, and I had an old account there, but I'm going to type
02:46WebServiceApplication and type in the password that I had and click Connect.
02:53We're going to have a problem.
02:54Now it just says login failed for user WebServiceApplication and I
03:00perhaps could think maybe I type my password wrongly, maybe I type the name
03:03wrongly, does it need some prefix, and unfortunately the problem is a bit bigger than that.
03:08Here's the issue. If I go back and I'm still connected as myself right now, so
03:12I do have admin privileges. I'm going to right-click the Instance and click
03:17Properties. I am looking at the properties of SQL Server instance on this machine.
03:22Now if I come down to Security I'll see that my server authentication is a
03:28Windows Authentication mode. That means in order to log on to this machine I have
03:33to be a Windows account.
03:34Now you might think, well why did it let me create a SQL Server account then?
03:39Well, it could be useful. You might want to set up several SQL Server accounts
03:43but you're not actually going to switch your authentication mode until you're
03:47ready to go live. So it's perfectly acceptable.
03:50But I need to switch it so I can check it.
03:52You can do this after the fact, but it's a fairly significant switch.
03:56You wouldn't want to do it in the middle of a busy day because as soon as I
03:59select this and click OK, it's going to tell me that my configuration change will
04:03not take effect until SQL Server is restarted.
04:06Well it's just me so I can restart it.
04:09I'm going to drill down into my SQL Server 2008 R2 programs, find my
04:14Configuration Tools, and open up SQL Server Configuration Manager.
04:19The only thing I need to restart is the SQL Server Engine itself which shows up
04:24as this SQL Server address and I have the choice of hitting the Stop button and
04:29then waiting and then clicking Play, but we do have a little Restart Service
04:33buttons as well. Right-click would work too. This will take just a moment because
04:37it's a small server, and when it's done we can close Configuration Manager.
04:43That's the only thing we needed it for and I'm going to try to connect again.
04:47I'll click this button. I'll say that I want to use a SQL Server Authentication and
04:52the accounts I had set up was Web ServiceApplication, type in the password, and
04:59connect, and we're connected successful. In fact right now we're connected twice.
05:04This top row in the Object Explorer is telling me I'm logged on as myself with
05:08Windows authentication and this is the view of it we'd that would log on as the
05:13SQL account and that should be a lot more restricted.
05:16For example if I drill down onto the databases and think, well maybe I'll take a
05:20look at AdventureWorks,
05:22well I don't get to see that one. I don't get to see AdventureWorks data
05:26warehouse or data warehouse 2008 R2.
05:29I do get to expand AdventureWorksLT.
05:31I do get to expand Tables.
05:33I could even right-click and say I want to select the Top 1000 Rows because I'm
05:38allowed to read from this database.
05:40It would give me the ability to right- click and say Edit Top 200 Rows, but if I
05:45actually try to do something here like change the title, it's actually going to
05:52throw an exception here that the UPDATE permission was denied on that object.
05:57I don't have permission to write to this database.
06:00So it wouldn't let me save this so I'm just going to hit the Escape key to jump out of
06:05having that change in there.
06:06I'm going to go back and just disconnect this part again. Disconnect as being the
06:13WebServiceApplication and just go back to being a regular administrator here.
06:17Again one of the things you want to be very careful of is when you're using
06:21SQL Server accounts and you're allowing that login ability of just a username
06:27and password, that you are using complex passwords and even complex user names
06:32as well, but if you do have to expose your connection this way, this is the way
06:37to create them.
Collapse this transcript
13. Reporting Services
Introduction to SQL Server Reporting Services
00:00Reporting Services is a separate component of SQL Server 2008.
00:05It is available in both the Standard and the Express editions and the idea
00:09behind it, not surprisingly, is to be able to create reports without requiring a
00:14developer to write a custom application.
00:16So it's really a business intelligence layer, allowing you to create a
00:20presentation of the data in your databases and then share that across an
00:24organization, and to understand it, it's best to understand there are really
00:29three different parts to it.
00:31First is the server-side part of the platform, the engine of Reporting Services
00:36that can manage and deliver these reports, and this is considered a
00:40self-contained component of SQL Server.
00:43You can choose to either have that installed or not.
00:46If you're not going to use it, it shouldn't be installed.
00:49It even comes with its own application for managing its configuration.
00:52Now the second part is an application to help you design those reports.
00:58Well, that's where it gets tricky because there are a few different ways to create reports.
01:03The something called Report Builder, which is a stand-alone desktop
01:07application for developing reports for reporting services and with the R2
01:11release of SQL Server 2008 there is a new addition of this application
01:15called Report Builder 3.0.
01:16That lets you create complex reports not only including the data itself but
01:22including visualizations of the data to charts including maps, data bars and spot lines.
01:29Now there is also an application called the Business Intelligence Development Studio.
01:34This comes with an advanced project template called Report Designer.
01:37If your developer used to Visual Studio, this might be the way that you would want to go.
01:43Now, however, this is the third part.
01:45Once your report is created, how do you view and manage these reports?
01:49We don't want to take the old-school view that the report has to be something
01:53that's printed off and handed around.
01:56So typically, the reports can be viewed by going to a web application
01:59called Report Manager.
02:01This is where you can find the different reports that have been created and view
02:05them online. If it's a large report, you can page through it, you can zoom in or
02:09search it and even all to the parameters of the report.
02:14Reporting Services can also be integrated with SharePoint, which can be used as
02:18a location to publish a report to this. There's even a SharePoint web part so you can
02:22embed reports in SharePoint web pages.
02:25But really that's the three parts to it.
02:27We have to understand how to install and configure it, we have to then work with
02:32creating those reports and using an application to do that, and then there is the
02:36idea of managing and distributing those reports.
02:39Reporting Services and all its surrounding applications is a fairly
02:42complex technology.
02:44We could create an entire course on Reporting Services easily.
02:47So we're going to cover a brief introduction of what it can do and the basics
02:50of how to use it.
Collapse this transcript
Connecting to the Report Manager
00:00When I installed SQL Server I had checked the boxes to install Reporting
00:04Services as well. Now it's time to go and see what that means.
00:08You've probably seen that in Management Studio we have a couple of databases
00:12with the word Report on them, ReportServer and ReportServerTempDB.
00:16Those are support databases for this part of SQL Server called Reporting Services,
00:21though we're not going to touch them manually in SQL Server Management Studio.
00:25So I am just going to exit out of that and if you'd also taken a look at
00:29Configuration Manager, the regular SQL Server Configuration Manager, you'd see
00:33that Reporting Services is actually running in the background as its own
00:38application, its own service, and it really is a very substantial part of this
00:43whole enterprise application.
00:46But how do we work with it?
00:47Well for a start we have a new tool to work with.
00:50I am going to go to my SQL Server 2008 R2 applications into Configuration Tools
00:56and pick this time the Reporting Services Configuration Manager and not the SQL
01:01Server Configuration Manager.
01:02Like opening up Management Studio, the first thing asked is what server am I
01:07supposed to be talking to, because you could be connecting to several.
01:10I am going to connect to my local machine and just click OK and this
01:14application has quite a few different options in for working with the backend
01:19of Reporting Services.
01:21Who is it running as?
01:22What's the service account that's happening?
01:24What are the databases that we're using?
01:26I'm not going to touch any of this because we're really just trying to get a
01:29taste of Reporting Services.
01:32The bit that I'm interested in is this, the Report Manager URL.
01:37Report Manager is a part of Reporting Services that allows you and other people
01:42to connect to a web site and view and edit reports, and right now it's pointing
01:49and saying that the URL of this website is the local machine/reports, because
01:55it's installed it already.
01:56I am going to click that and depending on what operating system I have and how I
02:00am configured, I may run into a problem.
02:03I'm using Windows 7 right now. I have installed this locally and one of the
02:07issues is it's not authenticating correctly to Reporting Service in the backend.
02:11You may get the same issue.
02:13You may get a slightly different one.
02:15Now I could type my credentials all day long if I wanted to but it's not
02:19going to do me any good.
02:19Here is couple of steps that I need to do to make this work with Internet
02:24Explorer in Windows 7.
02:26I am going to come down to this area where it says Internet Protected Mode Off
02:31and double-click that.
02:32Because what I need to do is list this address, the address of my own
02:37machine, as a trusted site.
02:39So I am going to select the checkbox for Trusted sites and click the Sites button.
02:43And it say,s "Do I want to add this website to this Trusted sites zone?" Yes, I do.
02:48Though now it's choking and saying this must use HTTPS.
02:52Well no. For my local machine I don't need to care.
02:55I am going to add that and just for safety's sake I'm also going to add the word
02:59localhost, which can also be how this machine refers to itself.
03:04Click Add and click Close.
03:07I'm going to uncheck this box that says Enable Protected Mode, which will
03:12require us to restart Internet Explorer. That's okay.
03:14I am going to do one more thing for this trusted site.
03:18I don't want to keep getting that prompt to have to type in my username and
03:21password all the time, so I can either grab the slider all the way down to Low
03:26or I can click this little button that says Custom Level and right down at the
03:32bottom I'll see the option that I can logon automatically with the current
03:37username and password, so I am doing that too.
03:41Yes, I'm sure I want to change those settings.
03:43So there are really three things you have to do here.
03:45You want to make sure that Protected Mode is unchecked, you want to add the
03:49address of the site to Trusted sites, and you want to make sure that in the
03:53levels you're logging on automatically.
03:56I am going to click OK.
03:57I do need to shut down Internet Explorer but hopefully when I click the link
04:01this time, ah, there we go.
04:02Now the first time you do this you probably see that it will take a while.
04:07It may take several minutes in fact because it will do a lot of background
04:11compilation and building things, but eventually you should get to this Report Manager
04:17and what we're looking at after all at first is something fairly boring
04:22because there's no reports to look at.
04:24There is nothing for Reporting Services to do, so we'll fix that in just a moment.
Collapse this transcript
Using Report Builder
00:01The point of this web site is to allow us to view and interact with reports.
00:05But to do that, we need to make some first.
00:08There is a couple of ways to do it, but the one I'm going to pick is this icon
00:12here on the bar called Report Builder.
00:14You might first think this is a little web site or web application, but it actually isn't.
00:19In fact, the first time you click it, you may have to click OK once or twice,
00:23because it's going to install an application on your Desktop.
00:27This is SQL Server Report Builder, and we're in the 3.0 version of this
00:31application with SQL Server 2008 R2.
00:35This is an Office style application that you'll use to design your reports, to
00:40build your reports, and then they'll be pushed back, saved up to that web site,
00:45the Report Manager web site, so that other people can use them and view them.
00:50So when it opens up it does have a Getting Started panel that offers to step
00:54you through several different wizards.
00:56But I'm just going to dismiss that
00:58and kind of go back to basics with Report Builder.
01:02This application is open.
01:03I've got a blank report waiting to be designed here.
01:06Down towards the bottom, I can see a message that says we're currently
01:10connected to the report server that I was just in.
01:13Yes, that looks correct.
01:14But what I have to do is two things.
01:17I have to define where it is my data is coming from. My Data Sources.
01:22So I've got a Data Sources, a folder over here.
01:25I'm going to right-click it and say Add Data Source, describe a connection to
01:30some source of data.
01:32Now the more advanced you get with Reporting Services, you're going to be likely
01:36to setup shared connections.
01:39Connections you want to use again and again.
01:40I don't have any, so I'm going to have to select the other option to Use a
01:44connection embedded in my report.
01:46I'm going to give this data source a name.
01:48I want to use it to connect to AdventureWorksLT, so I might as well call it
01:53something like dsAdventureWorksLT.
01:56I'm just using ds as a prefix to mean data source.
02:00It is a SQL Server connection I want to talk to, though I do have a whole bunch
02:04of other options, including Azure, Parallel Data Warehouse, ODBC, SharePoint List,
02:10Hyperion Essbase and TERADATA, but we're sitting with good old SQL Server.
02:16Then I'm going to click the Build ellipses button here to help me describe my
02:21connection, and it's actually a very simple.
02:23It's essentially the same information
02:25I have to tell it when I'm connecting to SQL Server Management Studio. What server is it?
02:30Well, it's the local server, which I'll represent with a dot.
02:33I'm going to use Windows Authentication, and as soon as I type that, I'll have a drop-down list.
02:38What database you want to talk to?
02:40Well, AdventureWorksLT.
02:42I'll click the button to test the connection.
02:44Everything succeeded. Click OK.
02:47Click OK. So that's step one.
02:50My data source is defined, a connection to AdventureWorksLT that this report can use.
02:55But I haven't actually said what data I want. Because I'm not just going to dump
03:00the entirety of the database on to the page.
03:04So this is step two, define the dataset.
03:05Now you've got your data source. What do you want to do with it?
03:10So I'll right-click and say Add Dataset.
03:13Once again, we can give it a name.
03:14I'll just leave the default name of DataSet1.
03:16And it says, do you want to use a shared one? No, I don't.
03:19I'm going to have to use a dataset embedded in my report, because there is
03:22nothing out there to share.
03:24Well, what connection, what data source are we using?
03:26Well, the only one we have.
03:28The AdventureWorksLT one that I just made.
03:31After that, here is where I described the query.
03:35I can write standard SQL SELECT statements here.
03:38I could check this box to select a stored procedure, which would be a great way
03:44of working with reports because we can have a nice efficient stored procedures
03:48stored in the database.
03:49But unfortunately I don't have one, so I've got the Text option.
03:53However, I'd like to think that I'm pretty lazy about this.
03:56So rather than type it, I'm going to click this button to say Query Designer.
04:01This gives me a great builder of content that I can start to expand the
04:06different parts of my database.
04:07I've already described my connection, so it knows the options for me.
04:11I could then start drilling down into Tables and saying yes, I'd like to
04:15have information about the customer.
04:17I'd like to know their FirstName, their LastName, and their CompanyName,
04:22and their EmailAddress.
04:24I can start to select information from other tables.
04:27Perhaps I want to know my customers information and what city and
04:31what country they're in.
04:33Well, if I do that, as we know, we've got to describe some kind of
04:38relationship between the two.
04:41Now, while I'm doing this process, I do have a Run Query button up at the top
04:46that I can run and click this.
04:48It's telling me well, the problem is the following tables aren't related to each other.
04:53I don't know what to do.
04:53I don't know how to connect them.
04:54Well, it actually does a pretty good job of connecting them.
04:58You just have to make sure you're selecting options from tables that are
05:01connected to each other.
05:02Now, right now, Address and Customer actually aren't connected to each other.
05:08They have a join table in between, which is CustomerAddress.
05:11So all I'm going to come down-- is come in to CustomerAddress and select one from
05:17one table and one from the other.
05:19The great thing is while this Auto Detect has the blue box, meaning it's selected,
05:25the next time I hit Run Query, it's going to detect those relationships.
05:29Yes, indeed, it's going to bring me back both the information from the Customer
05:33table and the information from the Address table where the two are joined
05:37together, which is great.
05:39We can go on a little further than that.
05:41Maybe what I want is customer information from the Customer table, address
05:45information from the Address table, and some order information from say the
05:49SalesOrderHeader table.
05:51Well, from there, the things I'm interested in are the CustomerID and let's say the TotalDue.
05:59Do another quick Run Query.
06:00I like to do these step-by-step, where it seems to be actually connecting all of these together.
06:06One of the great things is if we're not sure about the SQL that's being generated,
06:10up here we have this Edit as Text button.
06:12I can jump into that and it actually shows me the SQL that is trying to build,
06:17which in this case has the JOIN statements.
06:19In fact, that looks pretty good.
06:21I don't want to mess with it.
06:22I'll just click the Edit as Text button again to take me back into the Designer view.
06:27There are ways I can start to actually add aggregate functions over here too.
06:33I could decide to use my COUNTs, my MAX, my MIN, my SUM, all of that.
06:38I don't need to use any right now.
06:40This is actually just fine for me.
06:42Below this, we have applied filters that we could start to type in things like
06:46the FirstName, like a particular Value.
06:48We see a little later how we can actually parameterize this.
06:52I'm just going to leave it as is.
06:54Although, just to show one last thing, I'll show how you could jump into say the
06:58Edit as Text part and just if I wanted to, use there SQL as a starting point
07:04and then just finish it off with mine. ORDER BY TotalDue DESC. Click OK.
07:13This is all I need to define this dataset, although I could step through
07:17renaming options for the fields, change things like collation, add my own
07:24filters, add my own parameters.
07:25I'm not going to do that right now. I'm just going to click OK. Well, then what?
07:29We have this information that could just be dragged and dropped on to our report.
07:35Now right now, that wouldn't be a very useful report if all I had was a State or
07:39a FirstName in the middle of it.
07:40There are a couple of helpers if I know that I'm going to have multiple rows of information.
07:45I click up here on my Insert tab.
07:48I have what are called data regions.
07:50The simplest is a List, and you just drop your fields anywhere in that
07:54and it'll repeat them.
07:55But we also have the Table here.
07:58Go to Table Wizard, which is quite useful. Click that guy.
08:01It's going to say, is there a dataset already defined?
08:03Well, yes, there is, this one.
08:05So I'll select that new dataset and click Next.
08:08The next setup is asking to arrange the fields, because of course the data I
08:13brought back could be being used for any ID. I could be wanting to total things
08:18up or group them together.
08:20The main part that we're interested in here is the values.
08:25I can drag these things across, FirstName, LastName, CompanyName, EmailAddress,
08:31City, State, and all I need is TotalDue.
08:38You'll see that there is a little drop- down here where you still can add on your
08:42choice of aggregate functions.
08:43I'm just wanting a fairly flat list, so that's okay.
08:47If I was expecting multiple customers from the same company, I could drag over
08:51CompanyName into Row groups.
08:54To start breaking that apart into groups of rows. I don't have to here.
08:58I am just wanting something fairly flat so I'm going to click Next.
09:02It gives me a preview of what it's going to show, organizes the table.
09:07If I was grouping multiple things with aggregate functions, I'd have an option
09:10to show some totals and grand totals, but I'm not.
09:13It's just a flat list drawn from multiple tables here.
09:16I've got a choice of various styles that we could use.
09:22Let's pick Slate and click Finish.
09:26This is our report now.
09:27I have button over here that I can click Run to take a little preview of it.
09:32It actually seems to be working okay.
09:33We're actually drawing the information.
09:35It's in descending order of TotalDue.
09:38I'm going to click the button up here to say Design, switching back into the Design mode.
09:43Maybe I think that my fonts are actually showing up a little bit large right now,
09:47and I want to affect the fonts on the top column.
09:50I'm going to do a select anywhere in here, and then you'll notice that I have a
09:53little great handle over here on the left.
09:56I can select that and then as long as I've got the Properties Inspector open,
10:00and if I haven't, I can get that from the View part of the Ribbon here.
10:05I can select that first row.
10:08Come down to Font and I think I'm going to change that to a FontSize of 8 points.
10:15It looks good.
10:16I'll also do 8 points on the second row too.
10:19I can actually do a lot of formatting.
10:21In fact, most of what you end up doing with Report Designer is going to be in
10:26the formatting and arranging of this.
10:28I still don't have a title for my report.
10:30So I'm going to click to add some title here. Customer Amounts perhaps.
10:38Then go up to the Ribbon and hit Save.
10:40Now what it's going to ask is, where to?
10:44And here is the default.
10:45Save up to the website, up to the Report Manager website.
10:49Well I am about to give this a name. I'm going to call this Customer Totals and click Save.
10:57Well, the question is so what?
11:00Now I can come back and design this a little later.
11:02But what I'm going to do is just close down Report Builder.
11:05Go back to the Report Manager.
11:07I need to refresh this page, because it doesn't know that I've uploaded it yet.
11:11But when I do, I actually finally have something on the page to look at.
11:14Now there is a bunch of options in the drop-down such as Delete, Subscribe,
11:19View Report History, Manage, Security, Edit in Report Builder.
11:24But I could just click the hyperlink itself. Customer Totals.
11:28Click that and notice that within the web site, it opens it up.
11:31It retrieves that information.
11:33It gives us options up here to print it, to even select from this, to allow us
11:38to export it in CSVs and PDFs and Word files.
11:42We've got the ability to zoom in a little bit, and just start it do some basic
11:48interaction with that report.
11:50So while we can take our reports a lot deeper than that,
11:53we can add parameters to them,
11:55we can add more formatting,
11:56this is the general process for creating and saving a new report using Report Builder 3.0.
Collapse this transcript
Formatting values in reports
00:01I'm a big fan of an incremental approach to writing reports in Report Builder.
00:05Start slowly, get something working, and then add things to it.
00:09So right now we do have this report working and it's drawing information from
00:13three different tables, but there is a little bit I could do to this.
00:16Total Due is basically being output right now just as a money column.
00:21There is no currency formatting and it could do with some. I don't really need
00:25FirstName and LastName to be separated into two columns here.
00:30So I'm going to go back to the homepage of Reporting Services and then select
00:34the drop-down button to edit this in Report Builder.
00:38Changing the formatting of a report and changing the entire layout is very easy,
00:43even after you've saved it for the first time.
00:45So again, back to this report, selecting any of these cells here will highlight
00:50the field that we're using and allow us to tweak it, allow us to change the
00:55font, allow us to change the background color.
00:57However, some of the options can be a little bit deceptive.
01:00If you're used to working with things like Visual Studio or other programs like that,
01:03you're quite used to looking at the Properties panel here.
01:07But in fact that's not what I want.
01:09I've got highlighted the Sum(TotalDue) here and there's nothing down here that
01:13immediately shows me how to show it up in a different format like currency. But that exists.
01:19It's up here on the ribbon.
01:21You'll notice that every time I highlight somewhere there is a dropdown here.
01:26That's set to Default normally but you can change it.
01:29So I can just select that cell and say I want this to be Currency.
01:34Just to prove that it works we'll click the Run button.
01:36We don't have to save it backup to the server yet; we just need to test it.
01:40And that looks correct.
01:42Okay, what else can we do?
01:43Well clicking back on the Design what I'd like to do is kind of combine
01:47these two cells into one.
01:50Now there is a couple of things I can do here.
01:54I can drag across and sometimes it's little tricky to drag and get both of them
01:58and then I can click right- click and get Merge Cells.
02:01The problem is that's kind of collapsing them into the first one.
02:04So now that all I've got here is first name and it wasn't quite what I was
02:09after but we take it step by step.
02:11Here's how the cells knows its value.
02:13If you go over to the Property section there is a Value area, which says "Well
02:19right now I'm drawing my information from Fields!FirstName.Value," but if I click
02:25the drop-down there is little Expression Builder.
02:28This is fairly simple stuff, but what it allows you to do is click through
02:33some of the built-in fields that it knows about, Page Number, Page Name,
02:37Language, Parameters.
02:39You got to section which is all the fields that we declared in DataSet 1 which
02:44include First Name, Last Name, Email Address and so on.
02:47We do have some common functions for Financial and Inspection and Math and a few
02:53basic operators for Comparison and Concatenation.
02:56That's what I want to do is Concatenation.
02:58I want to join FirstName and LastName together.
03:01I could have done that in the SQL statement but it's a little too late now, so
03:05let's just do it here.
03:07So I can actually use either the ampersand or the plus sign.
03:10I do want to separate them by a space but we're not in SQL anymore, so what I
03:15need to do is use the " " & and then I can just use the field in Data Set 1.
03:24I could either manually type it or double-click LastName and now it's
03:29concatenating both of these together.
03:32Click OK and that should evaluate to the full name.
03:35That means that up here on the header I should see if I can join these
03:40two together as well.
03:41Again, sometimes a little difficult to grab hold of them but if you
03:44persist you'll get there.
03:45Once both of them are selected I'll right-click and say Merge Cells and we'll
03:49just change that to say Name.
03:53Run and we're starting to look a little better.
03:57We're starting to experiment with the formatting and make this a bit more
04:01presentable, and when I'm done with my formatting changes, I'll simply hit the
04:06Save button to save it back up to the Report Server.
04:12We run it again from there and we can see the changes that we made.
Collapse this transcript
Adding indicators to reports
00:00Next up, I'm going to change my report to add a little bit more interesting
00:04graphical information.
00:05I am going to click the drop-down and select Edit in Report Builder.
00:11And what I'd like to do is have the report show up with some kind of visible
00:17graphical information about whether the number that we are showing is good,
00:21bad, or indifferent.
00:22Are these people either a good customer, a great customer, or a poor customer?
00:27There is a few ways I could do that.
00:29I might decide that I want to show the name up in bold.
00:32I might decide I want to show the TotalDue in red or green or orange but here's
00:38what I'm going to do.
00:39I am going to add a new column to this report.
00:42I am going to select in that cell and occasionally you'd be a little bit
00:46careful about selecting it.
00:48Sometimes you have to experiment a little bit.
00:50I've got the cell selected and I am going to right-click and I'm going to insert a
00:56column to the right, which gives me this, a new cell to do something with. But what?
01:02Here is what. Up on the Insert tab of the ribbon, there are several graphical
01:08elements that we can add to this report.
01:11The one I'm going to pick is one of the simplest ones, but it's a great one
01:14to get started with.
01:15It's the Indicator.
01:17When I select that, it doesn't actually add anything to the report but it will
01:21change my cursor to this little red graphic.
01:25And what I'm then going to do is just click in the cell that I want
01:29this indicator to be in.
01:30Click once and it's going to say, okay what kind of indicator do you want?
01:36And it offers me various choices.
01:37We have got Ratings.
01:39We have got a series of symbols for red, amber and green.
01:44I am going to select this one here and click OK.
01:49The question is what is this an indicator for?
01:53Well, that's up to you.
01:54You get to say what data this is hooked up to.
01:58It might be something like the total amount of an invoice.
02:02It might be an outstanding amount of bucks, anything that can be
02:06numerically evaluated.
02:09Right now, it doesn't know what it's supposed to represent.
02:11So, if I were to run this, I'm going to see nothing.
02:15But going back to Design, what I will do is I will select that indicator.
02:19Again, sometimes, it's a little difficult to grab hold off, but when I have got
02:23it selected, it will pop-up a little panel.
02:26I am going to drag to the right
02:27so I can see that. It says okay how do I show information?
02:32It needs to be faded with a value.
02:35And this lowest drop-down box says, where do I get it from?
02:38Well, it's not from FirstName, LastName or CompanyName.
02:41It's going to be getting its value from TotalDue.
02:44It's not going to be showing the number.
02:45It's going to be to be showing a graphic that represents that.
02:48I am not going to run it again.
02:51And very interestingly what we are getting is indicators here.
02:55We are getting the green checkmarks here, we are getting the yellow exclamation marks,
03:01and we're getting the red crosses, which don't look good at all.
03:05So what's actually happening?
03:06Well, by default, what it's doing is evaluating what it shows based on the
03:12TotalDue and it does it on a range.
03:15The lowest third numerically will show up in red, the middle third will show up
03:21in yellow, the top third will show up in green.
03:23That might be what we want. That might not.
03:26We can change that.
03:27Selecting back to this indicator, I can click the little Properties panel and
03:32what it says is first give it a Name. Is there a ToolTip?
03:35But in Value and States, it's saying here's where I get my value from, and I am
03:40measuring it in a percentage of the range available.
03:43Now I want to say no. Maybe I don't want that.
03:46Maybe I just want to have a flat range, that if it's above 50,000, it's really good.
03:50If it's below 20,000, it's bad but anywhere in the middle, it'll show up in the yellow icon.
03:57So I am going to switch that to Numeric.
03:59I am going to say red is from 0 through 10,000.
04:06Yellow is from 10,001 to 50,000 and green should show up from 50,001 to a million.
04:19Click OK.
04:20Now we will run this again and I have got my own defined rules going on.
04:25It's actually fairly similar but I know now that this range, for example, is
04:30being controlled by me.
04:32Below 50, we get the yellow exclamation mark.
04:34Above 50, we are getting the green checkmark.
04:37In other places you might hear these referred to as Status Indicators or
04:41even KPIs, Key Performance Indicators, in places like PerformancePoint and SharePoint.
04:46Now of course, in our example, we're ordering by TotalDue descending anyway.
04:51So it's quite visible information, but if you're mixing a lot of these up and
04:56you have got massive amount of data, just these checkmarks and these status
04:59indicators can make it very easy to scan a report and see where something is going wrong.
05:04So when I am finished, I simply save that back up to the server and
05:09close Report Builder.
Collapse this transcript
Adding charts to reports
00:00If you want to get even more graphical, we can add a chart to our reports.
00:05Now I'm going to add a chart. This can either come before our table here or afterwards.
00:10I'm going to put it afterwards.
00:11I'll click Insert and then if you notice, we have a Chart option here.
00:16We can directly insert it and then format it, or the Chart Wizard will help us
00:20through some of that.
00:22So the question is, is the data that we want on the chart already in the data set,
00:26and let's say for our purposes it will be.
00:29What I'm going to do is show a bar chart of the sales that we've made based on
00:35the Region or the Province that we've done it in.
00:37I'm going to click Next, and it will ask is it a Column, is it a Line, is it a Pie,
00:42and this of course is going to be a choice that you'll make more based on
00:46what the data actually is and how many data points you're expecting to have.
00:51The best thing is just experiment with these.
00:53Try several different ones.
00:55I'm going to pick a Column to begin with, and click Next.
00:58What it's telling me is there are my available fields and I can give it
01:03Categories and Values and a Series that read up at the top.
01:06What it's telling you is a field in the Categories list is displayed on the x-axis.
01:11A field in the Values list shows aggregated data on the y-axis and it'll take
01:16care of aggregating or grouping it together.
01:19I'm not going to bother creating a new series in the chart, what I want is for
01:23the StateProvince to be in the Categories section across the x-axis and then
01:30I'm going to have the TotalDue aggregated in the y-axis.
01:35I'll click Next, then it gives me the same choice I got for the table, what style
01:40would you like. I think we chose Slate for the last one, so I'll choose it for
01:44this one and click Finish.
01:46And the Chart is inserted, although it's kind of overlapping my table, and it
01:50looks as if I was going to drag it below that I'd kind of drag it off the
01:54bottom of the screen.
01:55But I don't have to worry about that.
01:57If I just keep dragging it down, what you'll find is the page itself will
02:02expand, even expanding the footer here.
02:05So what I really need to do is just drag it to underneath my table, because the
02:12table itself will expand.
02:13Now I'll drag it wide to fit the width of the page and that should do.
02:18I can come in and give it a Chart Title. Let's call it Sales By Region.
02:24Now we've got the Axis Titles that we could do here, which is let's say Dollars
02:32Amount and the region across the bottom.
02:36And let's try this. Click Run.
02:39We get our normal part of the report coming back.
02:42These are what have listed by the States and Provinces.
02:45We are across multiple countries, so it can end up being a little weird.
02:48So we are obviously getting places like England as the country and then
02:52California is the state, but that's all based on how your database is setup.
02:56If I come down, what I can actually see is here's the chart and it appears that
03:01we've got California with the certain amount.
03:03Notice that if I move my mouse over the top of it, I'll actually get that amount
03:08and same with England.
03:09In this case, it's aggregating the TotalDue up and then showing lesser amounts
03:13as well for Colorado, Nevada, and New Mexico and Utah.
03:17So, very easy process of adding a chart.
03:20If you select the chart in your Design view, you will find that it itself has a
03:25lot of properties that you can start to mess around with.
03:29Any of the individual bars can be affected.
03:32When you select these you'll also find again the little pop-up menu here that
03:36would allow you to jump back in and change more of the properties of the chart itself.
03:42Again, your choice of what kind of job that you're going to insert is really
03:47going to be much more based on the available data that you have, but the process
03:51for adding them is the same as this.
Collapse this transcript
Working with report security
00:00When you get your reports to a point where you don't mind to sharing it with
00:04someone else, well you're going to have to share it with someone else.
00:07And most of this is done through the SQL Server Reporting Services Report
00:12Manager web site, and if I selected the report that I've created and click the
00:16drop-down box I have a whole bunch of different options here.
00:20We've seen Edit in Report Builder, but I can also click Security or go to the
00:24more generic Manage.
00:26Manage would allow me to change things like the Name and the Description if I wanted to.
00:30There is a whole series of other options, most of which I can see because I am
00:34an administrator, things like the data sources, and you can also look at setting
00:39up subscriptions to report so that automatically push to people.
00:44But what I'm interested here is security. But something to bear in mind about
00:50Reporting Services security is it's kind of like SQL security.
00:54There are wheels within wheels.
00:56And this report is contained inside the homepage of Reporting Services.
01:02Well, that's a good thing because what it actually means is I can setup the
01:06permissions for people at this homepage level and then any new report added to
01:11this page will be available to them. Or I can set up separately.
01:15But you'll notice if I go directly into Security and say that I want to edit the
01:20security of just this report, it's going to say "Well your security for this is
01:25inherited from a parent item.
01:27Did you want to apply different security settings from that of home, of the homepage?"
01:31No, I didn't.
01:32Actually what I'm going to do is Cancel and just go back to Home here, because
01:36what I'm going to do is manage security at the top level.
01:40I'm going to click this Folder Settings area.
01:43Now of course if you're creating multiple reports from multiple people what you
01:47are going to end up doing is creating multiple folders and adding security
01:50differently on those.
01:51So I'll hit that and what it's actually saying is "Well, right now the only
01:57people who can access this page and therefore any report on this page are people
02:03in the BUILTIN\Administrators group who have the role of Content Manager."
02:09Reporting Services likes roles like any other part of the SQL Server.
02:12So what I'm going to do is create a new role assignment and you'll notice there
02:18aren't actually all that many roles here.
02:20At the low end we have someone in the role of Browser.
02:23They may view folders, reports and subscribe to reports.
02:26At the high end we got Content Manager who can take control of everything in the
02:31Report Server and then there's a few someone in-between like a Publisher who may
02:35publish reports and link reports to the Report Server, but they can't actually
02:39control the Report Server.
02:41Here's what I'm going to do.
02:42It says "Give me the group or username."
02:44I could give it the name of an individual user.
02:51I know that LucianE exists and I'm going to make LucianE a Browser. Click OK.
02:56That's then added, or I could click New Role Assignment and add say the
03:01Windows group of Sales, which has multiple people in it, and make everybody
03:05in Sales a Browser.
03:07Simply that would mean now that going back to the homepage and going in to look
03:12at the security just of that report, you'd find that people in the Sales group
03:17and the person Lucian also now have the ability to read and view this report.
03:22Again, if you do need to setup multiple containers for reports, an easy way to
03:29do it is just actually to work with new folders.
03:32So I could create say the Sales folder for Sales Reports.
03:36Click OK and then I could create security just on that.
03:41You probably don't want to get too much more complex than one or possibly two
03:44levels deep. After all the whole idea is that you're going to make this
03:48available to other people in your organization.
03:50You want your Report Server to be easy to navigate.
Collapse this transcript
14. Integration Services
Introduction to SQL Server Integration Services (SSIS)
00:00It'd be we nice to think that our data magically appears in our database, is
00:05updated there, is manipulated and reported on there, and never needs to go
00:09anywhere else or come from anywhere else. But that's not the case and that is
00:14where SQL Server Integration Services comes in.
00:18This is the part of SQL Server 2008 that deals with getting data into our
00:22databases or extracting it and moving it somewhere else.
00:26This can range from the very simple, where Integration Services will take care
00:30of importing a one-off collection from an old access database, to very complex indeed.
00:37Importing data from multiple sources including other databases, having flat files
00:43and XML documents, and being able to clean this information all up, in some cases
00:48of standardizing it, in other cases transforming it and performing computations
00:53on it, and then merging all this and storing the results in the database.
00:58And then perhaps most importantly, being able to take all these operations and
01:02package them all up so they can be repeated on demand. And in fact doing this is
01:08called a SQL Server Integration Services or SSIS Package.
01:13Now the simpler kinds of operations in SQL Server Integration Services can be
01:18done just by using the SQL Server Import and Export Wizard that you'll find in
01:23SQL Server Management Studio, but more complex packages can be created using
01:28the Business Intelligence Development Studio or Visual Studio and creating
01:32what's called an Integration Services project where you can describe your
01:36different data sources, how you connect to them, and what you do with that data
01:41when you've gotten it.
01:42Unlike many other parts of SQL Server you can probably tell that this in itself
01:46can get to be a very substantial area that you could spend a lot of time in, but
01:50we can certainly get started by exploring some of the more common tasks with SQL Server Integration Services.
Collapse this transcript
Using Business Intelligence Development Studio (BIDS)
00:00To work with SQL Server Integration Services we're going to open a new program.
00:05Well, for some of you it might not be that new.
00:08I'm opening up my SQL Server 2008 R2 applications.
00:11I'm looking for SQL Server Business Intelligence Development Studio,
00:16sometimes known as BIDS.
00:17But when I open this up, it's really not a completely new application. As I can
00:23see here it's actually Visual Studio.
00:25In fact Business Intelligence Development Studio is just a cut-down version of Visual Studio.
00:30If you have the full version of Visual Studio, every time you go to
00:34File > New > Project you might have dozens of choices to choose from, but here we
00:39only have a handful.
00:41We have a couple of Analysis Services Projects, we have a couple of
00:45specialized Reporting Services Projects and then the Integration Services one,
00:50and that's what I need to do.
00:52So I've gone into my File > New > Project and I'm going to create a sample here.
00:56I'll call it SSISTest and just accept the default location.
01:00So this is creating a full Visual Studio project with a lot of subfolders and
01:06support files and this is what we get.
01:10And that can look little bit intimidating particularly if you're new to Visual Studio.
01:14And even if you're not this is certainly not a classic ASP.NET environment,. It's
01:19something very different, very visual. Inside this package and that's what we're
01:23working on a Package.dtsx file,
01:26we've got four tabs here, Control Flow, Data Flow, Event Handlers, Package
01:30Explorer. Well what are we actually doing here?
01:33Well this is all about taking in some data, performing some transformations on it,
01:39moving it somewhere else, copying it, merging it, exporting it.
01:43And to do that we have a fairly significant toolbox.
01:46I'm going to pin my toolbox here just to show you this, but what you'll find is
01:51if you have the Control Flow tab highlighted we have all sorts of control flow items,
01:57dozens of them, things like Send an Email Task, do something with FTP Task,
02:04do something on the File System, Execute some SQL, Execute a Process.
02:09Down below that we have what are called Maintenance Plan Tasks, things like Check
02:13the Integrity of the Database, always a good idea
02:16if you're moving a lot of content from one place to another.
02:19History Cleanup, Notify Operator Task , sending an e-mail message to a SQL Server
02:25Agent Operator, Rebuild an Index, Reorganize an Index, lots of stuff that you
02:29can build and plan for doing some substantial work with moving data around.
02:35And the idea is that what you do is you start to drag on the things that you
02:40want to do onto the Designer.
02:42So if you wanted to start off with a Check Database Integrity Task we drag that on there.
02:48It adds it visually.
02:49Now of course, it doesn't know which database we're actually talking about here.
02:53So to configure this I could either select it and start to play around with
02:57the Properties, or I can just double- click it and it's going to kind of step
03:01me through the idea that it doesn't know what database to check so it's asking for a connection.
03:06What connection do I use?
03:08If I do where to find one they will be one in the drop-down, but we didn't.
03:11So I'll describe a connection to AdventureWorksLight (AWLT).
03:16It's going to be the dot, which is fine, and yes it's Windows
03:19Integrated Security.
03:21When I do that it says, well select a database please. I can say all databases.
03:26Do I want to check the integrity of everything, just system databases, user
03:29databases, or these following databases?
03:32It allows me to select through them. I'm going to say set this one. Click OK.
03:37I could even view the T-SQL.
03:38Now surprisingly, what this is generating is a DBCC CHECKDB.
03:47So if that completes successfully, we could go onto the next task. Maybe at
03:52this point what I want to do is grab something from the file system, or send an email.
03:58Perhaps notifying people that we're about to start a big SQL Server Integration
04:03Services package, but really the big task, the big important one doesn't look
04:09like it's that important, but it's this one, the Data Flow Task.
04:13The idea is that all of your packages at some point do data flow.
04:17They take data from one place and move it somewhere else.
04:21And in fact you could have a SSIS package that is just one Data Flow task, so I
04:28have deleted that first one that checks the database integrity.
04:32And here is what happens when you drag this on and you double-click it,
04:36it's going to jump you to the second tab.
04:39We're basically jumping inside of that Data Flow task where we get to describe
04:44exactly what's happening just in that little piece of the flow.
04:48And if you notice what happens is your toolbox changes.
04:51We switch from things like FTP tasks and Email tasks to Data Flow sources of
04:57Excel, and flat files, and raw files, and Data Flow tasks. We can do things like
05:04character mapping to change things to lowercase or uppercase, data conversion,
05:09moving things from integers to floating points and back again.
05:13We can lookup some other piece of data.
05:15We can do a row count.
05:17If you have the Enterprise level editions of SQL Server we can even do things
05:22like Fuzzy Lookup and Fuzzy Grouping to have very flexible questions that we're
05:27asking about our data.
05:29And then we end up with Data Flow Destinations.
05:32That's the whole point. If something is flowing it's flowing from somewhere to somewhere.
05:37So what's the destination?
05:38Now are we moving from a flat file into SQL Server, are we moving from SQL
05:43Server into an OLE DB database? What are we doing with it?
05:47And the benefit of defining all these is that at every step of the way whether
05:52choosing to for example start off with the flat file, then do some character
05:58mapping of it, sorting that information, and then outputting it into for
06:07example SQL Server,
06:10we can configure each step of the way.
06:12We have the two arrows, the green one representing what happens if the output of
06:16that was successful and the red one representing what happens if it wasn't.
06:20So in this case we can just click on the steps and define them by
06:25double-clicking each part and setting up, or what is that input flat file, how
06:29would SQL Server Integration Services find it, how does it know where to look?
06:34What kind of mapping would we do on it?
06:35What would we convert to uppercase?
06:37What would we convert to lowercase?
06:38What are we sorting on?
06:40And then where does it go?
06:42As you can imagine, SQL Server Integration Services packages can get very complex
06:47indeed, the great thing about doing them in this environment is you even get a
06:51built-in debugger that allows you to take them visually step-by-step through
06:55each part and we'll see a simple example of that next.
Collapse this transcript
Creating and executing a simple SSIS package
00:00I am going to illustrate how to create a basic package by doing something very simple.
00:05And I'll make a new project here in the Business Intelligence Development Studio.
00:10So create a new project.
00:11It's going to be Integration Services and I am going to call it
00:15the FlatFileTransform.
00:17Because we don't actually need to move our data in or out of SQL Server.
00:23That's an easy mistake to make.
00:25Yes, Integration Services runs as part of SQL Server but it's quite happy to
00:29move information from an Access database into an Oracle database or from a flat
00:35file into Access or from SQL out into Teradata.
00:39Whatever you can connect to, it will work with.
00:42So I am going to begin with about as simple as you can get.
00:45One Data Flow Task.
00:48Our entire package is going to consist of this one step.
00:51Now inside it, we will have several steps,
00:54what are we going from, what are we going to, but this will do.
00:58So I double click it and I jump into the one Data Flow Task.
01:02What I am going to set up is a Flat File Source.
01:05I am going to then affect some of the text files inside it by doing a Character Map
01:14and if I mouse over character mapping, see that it says it applies string
01:18operations to character data so that's the one I want.
01:21I am going to sort and then I am going to have the output be another flat file,
01:28a Flat File Destination.
01:30So let me go through and configure this step-by-step.
01:33Again, we are doing this to get the idea of the process.
01:35I define the source. I will double- click this first part. Bring this dialog box
01:40a little smaller so it's readable.
01:41And it's going to say, do I know about a flat file right now? No, I don't.
01:44I will have to click New.
01:45Now anytime, you're defining a connection to something in the outside world, a
01:52database, a flat file, an Access database, you typically have to give it a
01:56connection and give that connection a name.
01:58So I'll call this FlatFileConnection.
02:01I will leave the description and it's asking, "Where is that file?"
02:06It could be on a network share, could be, as is my case, just a simple text
02:10file on my desktop.
02:12It's called names.txt.
02:15If I click through and look at the columns, what I actually find is that I have
02:18got some columns being picked up right now.
02:22Its name, first name, last name, email.
02:24It does look like I've got some columns in that first row.
02:29And if I click back on the General tab, it will tell me.
02:31There is why I describe that. Column names are in the first row.
02:34This is just a very plain delimited text file.
02:39Clicking through it, it's read as much as it can out of that.
02:41It's done the usual thing but it tries to take a guess of what this data should be.
02:46We have got first name, last name, email.
02:48Well, I know that my email might be a little longer than 50.
02:50So I am going to make that 100.
02:52If this looks familiar, it's because it is very, very close to what you get when
02:57you are using the Import/Export wizard.
02:59It's a little different than when you have to define a connection but the rest of
03:03it is kind of the same thing.
03:04You're describing what the data actually is.
03:08Going back, I can pick up the columns and it says "Yes, as far as I know, we've
03:11got FName, LNmae, Email, City, and Zip."
03:15You can define the Error Output, which is if there's any problems with
03:18this, what do we do?
03:20If there is an error, do we fail this part of the process?
03:22Do we ignore failure?
03:23Do we send that one particular row somewhere else?
03:26I'm going to say if there's ever any error and if there's ever any truncation,
03:30we are going to fail this part. So I click OK.
03:34I'm going to say that data was good.
03:36I'll drag the little green arrow down to Character Mapping.
03:39What does Character Mapping do?
03:41If I double-click on it, I have defined what the input is.
03:44So it says okay, these are my available input columns. Which ones do you want to affect?
03:48What I would like to do is take any of the last names and I just want to
03:52change them in place.
03:54And in fact, I have the option of creating a new column or I can do an In-place change.
03:59And then, it says well, what's your operation? Lowercase, Uppercase, Byte
04:02reversal, Japanese Hiragana and Katakana?
04:05I'm not going to do any of that.
04:06I'm just going to say let's convert the last name to uppercase.
04:10Click OK, click OK.
04:11That's a basic character mapping. Well, then what?
04:14Taking the output of that on to the Sort.
04:17Double click Sort to define him.
04:20What are we sorting on?
04:21Well, let's sort on last name ascending and then first name ascending,
04:26if there're any people with the last name.
04:28Do I want to remove rows with duplicate Sort values?
04:31No, definitely not.
04:32So I click OK and we have the Sort.
04:35The output of the Sort goes to the Flat File Destination.
04:38We will double-click it.
04:40I can say do we want to use the same manager as last time?
04:44Basically, we are getting the option here,
04:46do we want to overwrite the file that we had before?
04:49Well, I could, but I'm going to say no, let's make a new one.
04:52It's going to ask me, do I want to create a delimited flat file of fixed width,
04:57fixed width with row delimiters? What do I want to do?
05:00Let's just accept the default as delimited.
05:03I click OK and it's going to ask me again describe very similar stuff except
05:08what's the output file is going to be called.
05:10Well, I'll browse to the desktop and just invent a name.
05:13I'll call the output as output.txt and click Open.
05:17Then it's going to ask all these questions. Do I want this in Unicode?
05:20Yes, certainly I do.
05:22Do I want the column names in the first row? Sure, why not.
05:25I can take a look at what it thinks the columns are going to be with FName,
05:29LName, Email, City, and Zip. It looks okay.
05:33We can preview if I want to, but I am just leaving it because we haven't run this process.
05:37I am going to click OK.
05:39I do need to take a look at the Mappings page here before it lets me click OK, but
05:44I'm not going to change anything.
05:45It's still following the fact that because I hooked up the green arrows,
05:49it knows what the available input is and what the output destination is.
05:53So, I am going to just click OK.
05:54And I am going to say I think I am done.
05:57So, I'm going to save this. I am going to go ahead and run it.
06:00I can click this little Start Debugging arrow up here.
06:04The benefit of doing it that way is even though this will happen very quickly, in
06:08a full production environment it will be quite slow and you'll have the steps
06:12showing up in green or red when they work. So let's try.
06:16Now we have green, green, green, green.
06:20Okay, looks interesting.
06:22I get this message down here that all the different components have occurred,
06:26that the end result is apparently a success.
06:29So let's take a look.
06:30I will minimize this and go to my desktop.
06:33My input was this file.
06:35Names, comma delimited, and my output is this file and it certainly looks as if it did the trick.
06:42We've got comma delimited with line breaks in between them. We've got the first row
06:49with the columns in it and we have the capitalized and sorted output.
06:54Yes, SQL Server Integration Services is a little bit of overkill for that,
06:59although even transforming a text file like that can certainly take a little
07:02time if you have to do it manually.
07:05But that's the general process.
07:07The real key of what you're doing is always going to be in the Data Flow and
07:10then you can typically surround that with Control Flow.
07:14Control Flow doing things like checking the integrity of the database,
07:18notifying an operator, sending it in the email, accessing something from FTP.
07:23But I find working with SQL Server Integration Services projects, it's really easy to
07:27begin with a simple one and then you can start injecting and dropping in the new
07:32and the more complex tasks around it.
Collapse this transcript
Importing packages into SQL Server Management Studio
00:00The end result of working with Business Intelligence Development Studio and SQL
00:06Server Integration Services is you end up with a SSIS Package. It ends in .dtsx.
00:13In fact, I'm going to come out of Integration Services because I have that
00:18folder open here, and this is the package file that I'm interested in.
00:22Yes, I can run it from inside Business Intelligence Development Studio, but if I
00:26wanted to have this occur and automated, I can actually take it into SQL Server
00:32Management Studio and make it part of the database.
00:35Now again, it's just a simple package.
00:38This is the one that's actually affecting a flat file but the operation is still the same.
00:42I'm going to open up SQL Server Management Studio.
00:45Except this time rather than connect directly to the database engine, I'm going
00:50to connect to Integration Services using the same administration information,
00:55and what we'll get here is running packages and stored packages, and if I wanted
01:02to take in that package into the database, make it part of the database, what
01:08I'd actually do is come to the section that says MSDB.
01:11That's one of the system databases that's involved in queuing and setting up jobs.
01:17Right-click and say Import Package.
01:20It's going to ask "Well, where is the package? Is it in SQL Server?"
01:23No, it's actually on the File System.
01:24So I'll go and grab the package path.
01:27It's going to be in my Visual Studio 2008 > Projects, in FlatFileTransform.
01:33It's several levels deep, but it's where I had created it, and click Open and click OK.
01:39This package is now considered part of the database.
01:43We can execute it at our leisure or we can even set it up to be a scheduled job.
01:49Just to prove that it's going to work, it's just fine from within the confines
01:52of SQL, let me just minimize this window a little bit. Because I can actually
01:57see that my desktop here contains the names, which is my input.
02:02And I can actually come over here and right-click the Package and run it.
02:07Running within the confines of the database I've not got BIDS open.
02:11So I'm not using anything in Visual Studio.
02:15I have all sorts of questions here about configuration and who I'm running as
02:18and connection managers and options and reporting and logging, but of course
02:22this is a fairly simple one.
02:23So I'm just going to execute it.
02:25And I can actually see behind the scenes right now that here's my progress.
02:30I can close that but I've already got the output file just sitting there with
02:35all the transformations applied to it.
02:37Now the window that I just saw is the same one that you'd get
02:40if you went directly into your SQL Server 2008 R2 programs into Integration
02:46Services and find the Execute Package Utilities.
02:49Just a standalone utility that would execute a package, whether it's on the file
02:53system or stored in SQL Server.
02:55Of course, the big benefit of doing it this way is you don't need to have
03:00Business Intelligence Development Studio opened and running for everybody
03:04whoever wants to execute this package.
03:06You build it in that application and then it can be executed from the database,
03:11even from things like stored procedures. Or you can of course schedule it to
03:16happen automatically on a daily, hourly, monthly or yearly basis.
Collapse this transcript
Scheduling jobs with SQL Server Agent
00:00If you want to execute a package as a regularly scheduled occurrence there is a
00:05part of SQL Server that can do that for you but it's a part that's actually
00:09turned off when you first install it.
00:12In fact if I open up another instance of Management Studio, I'm going to connect
00:18to the database engine and you may have seen this before, that there is an entry
00:22here that says SQL Server Agent. You may have seen it in Management Studio or if
00:27you had opened up the Configuration Manager you would also see it there.
00:32SQL Server Agent is stopped by default and its Start Mode is Manual, which means
00:37when the machine is restarted it will not start itself.
00:41It's again because of what Microsoft tend to do these days, which is they ship
00:46products with only the necessary part of them turned on.
00:50Now SQL Server Agent isn't absolutely necessary so it's off by default and you
00:55have to turn it on if you want to use it.
00:57I can just turn it on quite easily from SQL Server Configuration Manager, just
01:02hit the little Play button and we'll turn it on, and if I wanted to I could also
01:07right-click, select the Properties of it, and then make sure that it actually
01:12changes the Start Mode to Automatic.
01:15I am not going to do that right now because I don't want to make it
01:17Automatic but I could do.
01:20What is SQL Server Agent?
01:22Well it is a supporting service that allows us to have jobs that are
01:27scheduled in the background.
01:28It allows us to have an asynchronous kind of queuing mechanism.
01:33It can control tasks to be repeatedly happening in the database.
01:37You just have to know it's there in order to turn it on.
01:40Once it is on, then what?
01:42Well I am going to expand this and it says, well we've got Jobs, we've got
01:45Alerts, and we've got Operators, Proxies, and Error Logs.
01:48SQL Server Agent works with the idea of a Job, I am going to make a new one.
01:55I right-click and say a New Job. I give it a name and then I say what it is.
01:59Let's say this is a job that I want to run every week or so that converts new
02:05customers that are being loaded into some kind of text file.
02:12Convert Customers. Yup, I'm the owner.
02:14It doesn't really fit into a Database Engine Tuning Advisor or Database
02:19Maintenance. I am just going to leave it as Uncategorized right now, and then we
02:23step through the steps.
02:25What do we do, and the schedules, when do we do it?
02:28So the step first, nothing is happening right now. I'll say I want to create a
02:33new step, and the step name will be Do The Convert and it's asking is it a
02:39T-SQL script or Replication Merge, ActiveX Script or is it a SQL Server
02:44Integration Services Package? And of course that's what it is, exactly.
02:47What are we going to run as the SQL Server Agent Service Account?
02:51Yes, absolutely that should do the trick with this one.
02:54It doesn't really need any kind of significant account there.
02:56It's going to ask where is the package?
02:58It's in SQL Server? Which server?
03:01This one, the dot?
03:03We are going to use Windows Authentication and then we'll drop down and
03:06select the package.
03:07Would have been useful if I'd given it a more interesting name but that will do
03:11the trick. We click OK. I don't need to change any of the other setups here.
03:16That will do just fine and I click OK.
03:19We've got the step selected.
03:21Now I need to define the schedule.
03:24I click the Now button and it asks, okay, what's the name for this schedule is?
03:27Let's call it on Monday Morning.
03:31Just so it's descriptive when we read this later.
03:35It is a reoccurring schedule.
03:37It will happen weekly, on a Monday Morning let's say at 5 a.m. and there's no end date.
03:44Pretty much like setting up or occurring appointment in Outlook.
03:47We have some more choices about alerts and actions to perform when the job
03:52completes, I want to get an e-mail, when the Job succeeds, when the Job fails,
03:57this does require you to have database mail turned on, which we don't have at
04:01this particular moment, but you could do that, and that's pretty much it. Going to click OK.
04:07We have the job, SQL Server Agent is started, we would want to make sure that it
04:11was set up to start automatically if the machine ever go restarted but that's
04:16the general process for doing it.
04:18And simply by doing this we can start to build more complex, more
04:23automated situations.
04:25Of course SQL Server Integration packages are the only jobs that you can
04:30schedule in SQL Server Agent. As you saw there is the ability even to just write
04:34raw T-SQL so you can have database consistency checks, you can automate the
04:40rebuilding or the re-organizing of indexes, and you really can start to build a
04:45more automated, more controlled database structure.
Collapse this transcript
Conclusion
Goodbye
00:00Thanks for joining us for SQL Server 2008 Essential Training.
00:04You have seen the core features, applications and techniques to get started with
00:08creating and working with databases in SQL Server and you should now have a
00:13pretty good idea of what parts of SQL Server you want to take further.
00:17You might want to do more work with designing and creating databases or dive
00:21deeper into indexing and performance or see what can be done with the
00:24surrounding components like Reporting Services and Integration Services.
00:29So good luck with your databases!
Collapse this transcript


Suggested courses to watch next:

SQL Essential Training (2h 22m)
Bill Weinman

Access 2010 Essential Training (3h 30m)
Alicia Katz Pollock


SharePoint 2010 Essential Training (6h 58m)
Simon Allardice


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,069 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,024 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked