Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Planning your database

From: SQL Server 2008 Essential Training

Video: Planning your database

It's very useful to have these sample databases to play around and experiment with but let's face it. The main reason we want to have a database server is so that we can have our own database. But it would be a bad idea to start by just jumping into SQL Server Management Studio and going crazy with the right-click mouse button. That is how bad databases get started. So in the next few minutes what I am going to do is talk about the basics of how to plan or model your database before we start to build it.

Planning your database

It's very useful to have these sample databases to play around and experiment with but let's face it. The main reason we want to have a database server is so that we can have our own database. But it would be a bad idea to start by just jumping into SQL Server Management Studio and going crazy with the right-click mouse button. That is how bad databases get started. So in the next few minutes what I am going to do is talk about the basics of how to plan or model your database before we start to build it.

You see while in other areas of development, things like programming and web development, I'm a really big fan of the iterative incremental design idea that you build apps quickly, you get them out, you revise them, you add new features repeatedly over weeks or even days, but that's not what I want to do with the database. You see building a database is like getting tattooed. You'll really want it to be correct the first time you do it. Changes are possible but they are painful.

The great thing is though while version of SQL Server 2008 R2 might be new, relational databases aren't. They've been around since the '70s and that's a good thing, as the methods from modeling a database have been battle- tested over four decades, and to make a good one, you just follow pretty much a 40-year old process. It doesn't really even matter what database management system you're using. Now while you can use a diagramming piece of software like Visio, all you really need to model a database, at least initially, is pencil and paper and be prepared to think deeply about a few questions.

A database modeling is not a place to express your inner creativity and find wild and crazy new ways of doing things. If you want to get wild and crazy, do it in your user interface, in your application, but in your database, I am afraid you want to be patient, methodical, step-by-step. The first thing we need to do is ask ourselves a few questions. Number 1, what's it for? What is the point of this database? Be careful of the first answer that comes to mind.

Sure, in most cases, you're building a database to support an application, whether that's Desktop, or mobile, or a web application, but say you're building an online bookstore. Well that's way too easy to say that, the database stores necessary product and order information. Yes, that might be true but you should be asking what are the goals of the store, or the website, or the application. Because wherever you wanted to go over the next year or five should affect what you build right now.

Even having an elevator picture or a mission statement in mind will help you build a better database. If the idea behind your online store was "We help customers find books, discover what others thought about them, purchase and track their orders, contribute their own reviews and opinions, learn about other products they might like based on people with similar reading habits," you'll build a very different database from that description than you would from the first one. The second question. What do you have already? Do you have an existing database, even in something like Access or FileMaker? If so, well what's wrong with it? Because you don't want to just make the assumption that you're going to import all your existing data straight into this new system.

There is almost certainly problems with everything you have. Take the opportunity to fix any problems before you just re-create those same problems in SQL Server. Do you have an existing process that this database will replace or help, even a manual process? If so, get all your physical assets together, printouts, order sheets, filing cabinets, and people, of course, because understanding the data that you already have is essential before you can answer the next question, which is what tables do you need? Each database you make will consist of one or more tables and these tables are the basic building blocks of a database.

When you create a separate tables for each entity, that is each object or each thing that needs to be represented in your system, some of your tables might represent things that exist in the real world like a customer, or product, or an employee but others can be more abstract. A blog entry, a comment, an appointment, or event. Each of these tables will contain multiple rows of information, whether that's one employee, or a dozen, or a million.

This is one of those places where you could take a look for inspiration at some of the databases that are provided as samples. We can actually expand say AdventureWorks LT, the light version, and look at their tables here. They've got SalesLT.Customer, Product, ProductCategory, ProductDescription, SalesOrderDetail, SalesOrderHeader. Now if you notice these are the table names and they are prefixed here with SalesLT. That is something specific to SQL Server here. It's what's called a schema and it's a way of grouping your tables together into larger containers if you will.

We are not going to do an awful lot with schemas but to give you some idea of where that goes, if we look at one of the larger sample databases, the tables in here are grouped into several tables under the Person schema, such as Person.Address, Person.Contact, Person.ContactType. We have got Production schemas and HumanResources. So the easy perspective to take is it's just a way of grouping some of your stuff together. But we're defining fairly simple tables to begin with and after figuring out what tables need to exist, you then zoom into each individual table to specify exactly what data should be stored for each entity.

So in this case for an employee and here you need to be as specific as possible, defining separate columns for each individual piece of information. As a rule, you're going to go as granular as possible, not just a name but first name and last name or even title and suffix if needed. As you are defining your columns, you're going to define what is the data type for each column. What is it? Is it text data? Or is it numeric? Is it a date or time? Or even binary data or XML? You are going to define how big they should be.

If it's a text column for example, will it represent a few characters of a name? Or will represent the contents of a thousand-page manuscript? Your database management system wants to know so it can be efficient about storing it. Is this column required or is it optional? Are there maximum and minimum values for it? Should it match a pattern like an email address, or phone number, or a credit card number? You see flexibility is usually our friend but it's not what you're looking for in your database. If you want to store an e-ail address, you want to know it will always be an email address, not sometimes an e mail address, and sometimes a date, and sometimes an inspiring quots, and sometimes an MP3 file.

Defining your columns as exactly as possible means that SQL Server will enforce rules on those columns, your data will stay valid, and you won't end up with a database full of garbage. Next up, you need to define what keys you have and that really is how do you get to a particular row. Each row in each table should have something called a primary key and it's something that uniquely identifies that individual row. If we have an employee ID, it should take us to only one employee.

If we have a customer ID, it takes us to only one customer. So we define which column it is that contains our primary key. Now sometimes the key is already naturally in the data but often you'll need to generate a unique key and the database can help you with that. Then you'll define what relationships you need. You are splitting your database up into tables but many of these tables will need to know about each other. If you're creating an order, for example, that order would be typically connected to a particular customer and it will represent the purchase of one or more products.

We never want to duplicate data in our database. So we don't want to copy customer information into order or copy product information into order. We instead describe relationships between our order table, our customer, and product tables. We shortly see how to define what those relationships are. They can be one to many, many to many, one to one, or none at all. You can then use these relationships to answer all sorts of questions. How many orders has a customer had, how many products were in a particular order, or going the whole other way, find a particular product and find all the customers you ever ordered it by going through the order table.

After you've planned out of these tables, these columns, these keys, and relationships, you can technically go ahead and build them in SQL Server, start adding some data, and see if it exposes any issues with your first design, and it typically will. You'll realize something needs to be stored differently or split out into its own tables. There is something called database normalization, a set of guidelines and rules you can go through that will expose issues with your database, and they are super important but I'm going to talk about those after we've seen how to apply some of this in SQL Server.

Show transcript

This video is part of

Image for SQL Server 2008 Essential Training
SQL Server 2008 Essential Training

74 video lessons · 36687 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 2m 21s
    1. Welcome
      1m 19s
    2. Using the exercise files
      1m 2s
  2. 17m 58s
    1. SQL Server core concepts
      9m 4s
    2. SQL Server editions
      3m 8s
    3. Applications included with SQL Server
      5m 46s
  3. 26m 1s
    1. Preparing for installation
      3m 44s
    2. Creating service accounts
      2m 33s
    3. Installing SQL Server
      11m 42s
    4. Post-installation checks
      3m 9s
    5. Installing sample databases
      4m 53s
  4. 13m 35s
    1. Introduction to SQL Server Management Studio
      8m 7s
    2. Introduction to SQL Server Books Online
      3m 6s
    3. SQL Server system databases
      2m 22s
  5. 1h 26m
    1. Planning your database
      9m 39s
    2. Creating a SQL Server database
      4m 7s
    3. Creating tables
      7m 51s
    4. Data types in SQL Server
      12m 25s
    5. Defining keys
      8m 9s
    6. Creating default values
      4m 39s
    7. Creating check constraints
      2m 25s
    8. Creating unique constraints
      4m 34s
    9. Introduction to relationships and foreign keys
      9m 51s
    10. Creating relationships in SQL Server Management Studio
      8m 14s
    11. Database normalization
      11m 47s
    12. Creating computed columns
      3m 10s
  6. 23m 11s
    1. Using the SQL Server Import and Export Wizard
      3m 58s
    2. Importing Excel files into SQL Server
      6m 11s
    3. Importing CSV files into SQL Server
      5m 27s
    4. Importing Access databases into SQL Server
      7m 35s
  7. 55m 29s
    1. Introduction to Transact-SQL
      3m 43s
    2. Using SELECT statements
      7m 16s
    3. Changing the default database
      2m 21s
    4. Creating conditions in SQL
      8m 10s
    5. Sorting your output
      3m 23s
    6. Using aggregate functions
      7m 12s
    7. Finding unique values
      2m 14s
    8. Joining multiple tables together
      8m 0s
    9. Using subqueries
      9m 33s
    10. Viewing execution plans
      3m 37s
  8. 19m 36s
    1. Writing INSERT statements
      5m 47s
    2. Writing UPDATE statements
      4m 38s
    3. Writing DELETE statements
      2m 54s
    4. Using the OUTPUT clause to return inserted keys and GUIDs
      6m 17s
  9. 32m 52s
    1. Introduction to SQL functions
      6m 26s
    2. Using SQL configuration functions
      2m 14s
    3. Using string functions
      7m 26s
    4. Using date functions
      6m 27s
    5. Creating user-defined functions
      10m 19s
  10. 28m 46s
    1. Introduction to stored procedures
      4m 23s
    2. Creating stored procedures
      11m 23s
    3. Introducing transactions
      4m 23s
    4. Creating transactions
      8m 37s
  11. 16m 39s
    1. Understanding and creating indexes
      6m 32s
    2. Monitoring and rebuilding indexes
      6m 0s
    3. Monitoring database size and integrity
      4m 7s
  12. 11m 41s
    1. Creating backups
      4m 21s
    2. Creating differential backups and using backup compression
      3m 40s
    3. Restoring databases
      3m 40s
  13. 17m 40s
    1. Introduction to SQL Server security and permissions
      5m 54s
    2. Adding a Windows user to the database
      5m 7s
    3. Creating SQL Server logins and switching authentication modes
      6m 39s
  14. 36m 41s
    1. Introduction to SQL Server Reporting Services
      2m 52s
    2. Connecting to the Report Manager
      4m 29s
    3. Using Report Builder
      12m 4s
    4. Formatting values in reports
      4m 17s
    5. Adding indicators to reports
      5m 11s
    6. Adding charts to reports
      3m 54s
    7. Working with report security
      3m 54s
  15. 24m 41s
    1. Introduction to SQL Server Integration Services (SSIS)
      1m 57s
    2. Using Business Intelligence Development Studio (BIDS)
      6m 59s
    3. Creating and executing a simple SSIS package
      7m 35s
    4. Importing packages into SQL Server Management Studio
      3m 21s
    5. Scheduling jobs with SQL Server Agent
      4m 49s
  16. 31s
    1. Goodbye
      31s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed SQL Server 2008 Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

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.

Sign up and receive emails about lynda.com and our online training library:

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

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.