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

Creating tables

From: SQL Server 2008 Essential Training

Video: Creating tables

To create new tables, we need to drill into the database. We want to add those tables too, then expand the Tables folder and just right-click it and select New Table. Now you would expect to first say what the table is called but in fact your cursor is blinking away now at a column name. That's because it's giving you this temporary title for the table here, Table_1. We will get the chance to change that in just a minute but I'm going to describe a few columns because apparently that's what I have to do here.

Creating tables

To create new tables, we need to drill into the database. We want to add those tables too, then expand the Tables folder and just right-click it and select New Table. Now you would expect to first say what the table is called but in fact your cursor is blinking away now at a column name. That's because it's giving you this temporary title for the table here, Table_1. We will get the chance to change that in just a minute but I'm going to describe a few columns because apparently that's what I have to do here.

Let's say what I wanted to do was create a table to hold employee information. So I have got things like employee first name, employee last name, date joined, birth date, phone, that kind of thing. So I'm going to just create a few simple columns here. The first would be FirstName. Again I'm splitting this up into first name and last name rather than just having a whole name. Then when I hit Tab, it gives me a drop-down of Data Type. This is, again, the type of data that this is going to hold.

If I click the drop-down box, we have a whole bunch here. We have got geography and geometry, is it decimal, is it date time, is it a bit? We will talk about quite a lot of these different data types but what I am going to change it to is nvarchar(50), and what this means is a variable length text field, variable length character field that can store up to 50 characters, all Unicode data. I am going to talk more about those data types in a later movie but this will get us started.

The next column is do we allow nulls or not. Can this field be blank? A lot of this is going to be done to your particular business process but let's say that I imagine everybody in my company has a first name. I am going to say no, I don't allow nulls. Same with LastName and I will also make that nvarchar(50). Now I will just go through and do a couple of different things. Title can be nvarchar(50). HireDate will be a date.

In fact, what you will is there are multiple date types. There's datetime, datetime2, but date is fine for me. BirthDate, that I will also have be a date. Phone, that's going to be an interesting one because we'll see how that one could be stored. We might want to allow people to put in dashes, for example, or even letters, who knows. So I am going to change that to nvarchar. It certainly won't be 50 characters long but I'll give it say 20 or 25. 20 will do.

And let's perhaps say we have an employment status field. I am going to call it Status and this might just mean they're employed or they're not employed. It's just a yes or no. Sure, you could store that as the text word Yes or the text word No in a character field but what I am going to do instead is have a field that is a bit and this basically is a little flag, the equivalent of a checkbox. It's either true or false. And I would say that that has to be required. So let me imagine for right now that this might be it.

So this is all I'm going to have for my employee table. I can assure you it's really not complete yet but let's pretend that it is. What I am going to do is hit Ctrl+S to save this. That's going to now tell me finally, enter a name for the table and I'm going to call this Employee. Now there is a bit of a tedious debate over whether to use plural or singular names for your table. Should this be an employees table that contains multiple rows each representing an employee or is it employee singular? I've worked in places where there's been a standard one way or the other.

I really don't care. I just think you need to be consistent. If, for example, I'll save this as a singular table name and if you look at say what Microsoft is doing with AdventureWorks, you can see that this appears to be singular. It's Product, Location, Culture, Address, Contact, rather than contacts, products and so on. So I'll just take my lead from them at this particular moment. And the question is so what, what can we do? Well, now that we've got at least one table created and I can see that in my Two Trees Test database.

It's showing up as dbo.Employee and this again is using the dbo as the default schema, the default container, if you will, for our tables. I'm just going to leave most of our tables as that, which is perfectly acceptable for our case. If you get into more complex database development, such as something on the size of the AdventureWorks one, you may want to start grouping your tables together into different schemas. Schema don't really mean anything other than a way to group and understand objects in your database and potentially to give them different owners.

We are not really going to use them in this course. So you'll see a lot of dbo for our tables. One thing I could do at this point is open on my Database Diagrams panel. It's going to prompt me that the database does not have one or more of the support objects required to use database diagramming and do you want to create them. Well, yes, I do. And the question is what does that mean? Well, right now nothing. I have got the support files to create them. I haven't made one yet. If I click this, and then right-click, say New Database Diagram, it's going to throw open the diagram and then pop up this prompt that says "Well, I'm going to give you the option to add your existing tables to the diagram." Of course, I only have one table so I might as well add that. I am going to click Close.

Then we can see that we are actually looking at a diagram of our employee table with all our different columns listed in it. Now one thing you can do if you are so inclined is you can actually use this database diagram technique to create new tables. I could right-click anywhere in the blank area and actually say that I want to make a new table. It's not going to ask me to give a name for this table. I will call it Product. And in this, I can directly starts typing and I will zoom back into 100%, column names such as a ProductID. Let's call that the nchar data type of 10 is fine.

ProductName, nvarhcar(50). Well, let's say we have long products. Maybe this will be 200 and so on. Now do bear in mind, you are not just creating a pretty diagram here. I can see that I have got this little asterisk after the name of product and what that really means is its detecting there is a change made to this table definition that has not been saved. But if I were to save it, I am just hitting Ctrl+S, I am saving the diagram here and that's kind of a little deceptive sometimes because as I'm saving that, it's actually telling me, "Well the following tables will be saved to your database, " do you want to continue? Yes I do.

That has created not only a diagram but whatever I did on the diagram really did affect the database. So I now have dbo.Product. I could continue to edit it over here in the diagram or I could right-click over here and say Design, which would allow me to jump into the standard view of editing. As you'll see below, anytime I have one of these columns selected, I do have a whole host of more properties that I can change about it. And we will visit some of these a little later on.

But that's the general process of how we start to add tables to our database and how we start to manipulate columns in those tables.

Show transcript

This video is part of

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

74 video lessons · 38564 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. Using subqueries
      9m 33s
    9. Joining multiple tables together
      8m 0s
    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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.