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

Creating a table

From: MySQL Essential Training

Video: Creating a table

Just like a database contains tables, a table contains The table definition contains all of the details of a dataset. And I'll paste that into the SQL box here in SID.

Creating a table

Just like a database contains tables, a table contains the rows and columns for a given set of data. The table definition contains all of the details of a dataset. Because of this, the CREATE TABLE statement has a lot more options than the CREATE DATABASE statement. We'll be working with the scratch database here in this lesson. And I'm going to start by copying and pasting the statement out of the exercise files here in the Chapter 03 Exercise Files. Under 02 Create Table, I'm just going to grab this whole Create Table statement all the way down to the semicolon.

And I'll paste that into the SQL box here in SID. The Create Table statement is pretty simple, it has a lot of options; but it's basically just CREATE TABLE followed by the name of the table and then the table definition inside the parenthesis there. So inside the parenthesis here is the definition of the table. And each of these lines is the definition of a particular column in the table, of course because SQL doesn't care about white space, these don't need to be on separate lines, they can all just be listed there on one line and it would work exactly the same.

I spread them out to different lines so that it's easier to read. So we'll get into all the column types later in this course but briefly this is how it works. A relational database table has a set of columns with particular characteristics. The definition of these columns is the part within the parentheses in the create table statement. For example, the id column is of an INTEGER type and that's followed by a comma. The name column is of type VARCHAR(255). Which means that it is a 255 character long string.

And likewise for address and city, state and zip are fixized where as the VARCHAR types can be larger or smaller. And each of these column definitions is separated by a comma, with no comma after the last one. And that's actually important. If you put a comma after that last one, you'll get an error. So, when I press Go here, that table will be created with those definitions. So, we can take a look at that by using the DESCRIBE statement.

This is a MySQL specific statement, this is not part of the standard. If I say DESCRIBE test. It will give me a definition of the test table, give me all of the column definitions. We can see more detail about the table with the SHOW TABLE STATUS statement. If I say, SHOW TABLE STATUS, semicolon. This shows that all the tables in the. Database, if you want to see just a particular one you can say SHOW TABLE STATUS LIKE and the name of the table and it will just show you that one.

So this tells us a few things about the table, it tells us the name of the table, the data base engine that's being used, the InnoDB. Database engine is the default form modern versions of MySQL. Actually versions later than 5.5 so, this is the 5.6 version and so, it defaults to InnoDB. Other interesting information, you have the create time of the table, and the collation, you notice it says latin1_swedish_ci. CI stands for case insensitive.

Which means that lowercase and uppercase letters are treated the same in sorting and searching. And latin1, it's closely related to the old ASCII standard. It's not the modern UTF standard. I'll show you how to change that a little later. And you notice that the language defaults to Swedish, and that's true in most MySQL implementations. I'm not sure if it's a compile option, or something. But it seems to be the default for MySQL. Probably has to do with how and where it was created. So, that's the show table status.

You can also see the create table statement and this is not the actual create table statement that you entered when you created the table, but it's rather. A create table statement that MySQL creates that will create a table exactly like this. So if I say SHOW CREATE TABLE, and the name of the table, then it gives us this create table statement and you notice that it says int 11 rather than just integer because 11 is the default size for an integer.

And a few things like that. It says default null after each one. Then we'll get to some of these other options later on. You notice also that it says ENGINE equals InnoDB, and default character set equals latin-1. So if we go back to our original create table statement, and I just. Pressed Paste because it was still on my copy buffer. We can actually add these options after, we can say ENGINE equals InnoDB and default character set equals.

And actually I want to put utf8. Because that's a more modern character set, it has more capability with international characters with non-Latin characters. And before I execute this, of course, I need to drop the table. If I try to create a table where there already is a table, I'll get an error. So I can say before that. I can say DROP TABLE IF EXISTS test. And then I can execute this and now if I do this SHOW CREATE TABLE, you'll notice that the default character set is now utf8 and if I say SHOW TABLE STATUS.

You notice, our collation is now utf8_general-ci, which is a more reasonable default, actually. So, it's a good idea to always specify the database engine and the default character set. The database engine defaults to InnoDB for MySQL 5.5 and later. InnoDB versus MyISAM which is the original database engine for MySQL. MyISAM is not transactional, it's not very fast for large data sets.

InnoDB has been under development for a number of years, it's just recently become the default. It's fully asset compliant which means that it supports transactions can be committed and rolled back. It has better durability and consistency. Its got much better speed with larger datasets. It's definitely a superior engine for most purposes. So you're going to want to use InnoDB if you can. Once you no longer need a table you can delete it with the drop statement. DROP TABLE or if I don't know if the table exists or not, if the table doesn't exist, this will give us an error.

So we'll go ahead and drop the table, and then we can see the error if I try to drop it again, we get this Base table review not found, unknown table. I can say DROP TABLE IF EXISTS. Which is the common way to do this. And press Go. And now I don't get an error. Even when, the table is already dropped. So a table is created in MySQL using the create table statement. This is where you define your columns and specify any configuration options for your table.

And you can delete a table with DROP TABLE. Or DROP TABLE IF EXISTS.

Show transcript

This video is part of

Image for MySQL Essential Training
MySQL Essential Training

60 video lessons · 5729 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 4m 22s
    1. Welcome
      1m 3s
    2. Using the exercise files
      1m 31s
    3. What is MySQL?
      1m 48s
  2. 45m 37s
    1. Installation overview
      3m 16s
    2. Installing XAMPP on Windows
      5m 55s
    3. Installing XAMPP on the Mac
      6m 38s
    4. Setting up MySQL users
      11m 31s
    5. Installing SID on Windows
      5m 43s
    6. Installing SID on the Mac
      6m 6s
    7. Installing time zone support in MySQL on Windows
      6m 28s
  3. 45m 43s
    1. The SELECT statement
      3m 57s
    2. Selecting rows
      4m 57s
    3. Selecting columns
      3m 8s
    4. Sorting results with ORDER BY
      2m 58s
    5. Filtering results with WHERE
      3m 52s
    6. Filtering results with LIKE and IN
      3m 41s
    7. Filtering results with regular expressions
      8m 21s
    8. Inserting rows
      4m 9s
    9. Updating rows
      2m 21s
    10. Deleting rows
      2m 25s
    11. Literal strings
      3m 12s
    12. Understanding NULL
      2m 42s
  4. 41m 47s
    1. Creating a database
      4m 30s
    2. Creating a table
      7m 18s
    3. Creating indexes
      6m 8s
    4. Controlling column behavior with constraints
      4m 46s
    5. Creating an ID column
      6m 58s
    6. Using foreign key constraints
      7m 58s
    7. Altering a table
      4m 9s
  5. 28m 56s
    1. What are data types?
      4m 1s
    2. Numeric types
      5m 21s
    3. String types
      2m 58s
    4. Date and time types
      7m 2s
    5. Bit type
      2m 26s
    6. Boolean values
      2m 15s
    7. Enumeration types
      4m 53s
  6. 32m 34s
    1. String functions
      6m 57s
    2. Numeric functions
      6m 2s
    3. Date and time functions
      4m 12s
    4. Time zones in MySQL
      3m 37s
    5. Formatting dates
      1m 51s
    6. Aggregate functions
      5m 45s
    7. Flow control with CASE
      4m 10s
  7. 7m 6s
    1. Maintaining database integrity with transactions
      4m 46s
    2. Using transactions for performance
      2m 20s
  8. 16m 49s
    1. Updating a table with a trigger
      5m 11s
    2. Preventing automatic updates with a trigger
      7m 29s
    3. Logging transactions with a trigger
      4m 9s
  9. 14m 11s
    1. Creating a simple subselect
      3m 23s
    2. Searching within a result set
      3m 53s
    3. Creating a view
      3m 32s
    4. Creating a joined view
      3m 23s
  10. 12m 26s
    1. Understanding MySQL stored routines
      2m 0s
    2. Creating a stored function
      4m 34s
    3. Creating a stored procedure
      5m 52s
  11. 14m 4s
    1. The multi-platform PDO interface
      3m 44s
    2. Executing the SQL
      4m 8s
    3. Implementing auto-increment IDs
      2m 3s
    4. Using a stored funciton
      4m 9s
  12. 1m 3s
    1. Goodbye
      1m 3s

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.

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 MySQL 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.