In this video, you'll learn how to create a table in SQL.
This works on any SQL database you use.
You'll learn what the command is, how to write it, and what happens when you run it.
I'll also share a way to quickly check that the table is created as expected.
Let's get into the video.
The first step to creating a table is to write the command for it.
The command is CREATE TABLE. Enter this command into an SQL editor window.
You can do this in any IDE you use, or at the command line,
or in a text editor that you write your SQL in.
The second step is to give your table a name.
Your table name is the name of the type of data stored inside it.
It needs to be only letters, numbers, or underscores. Well, it can have spaces and
other characters, but you need to then enclose it inside quote characters, which can get messy. So,
just stick to letters, numbers, and underscores. If you're creating a table to store books,
you may want to call it book. If your table is going to store products,
you could call it product. We're going to call our table product.
Now, should you call it the singular form, such as book, or plural form, such as books?
This is a topic that's debated in forums and blogs and teams everywhere
There are pros and cons each way, but I prefer to name mine in the singular form where I can.
If you prefer plural, go for that.
For now, let's call our table product. Enter in the word product.
It doesn't need to be in upper or lower case, as SQL commands are case-insensitive.
After the table name, enter a space, then open the brackets.
After the brackets is where we specify the columns for a table.
After we add in the brackets, we can add in some columns
Press Enter to go to a new line to make it easier to read your code.
Now, enter in the name of the first column you want to store. Like a table name, it also should
only include letters, numbers, and underscores. This table will have a column called product ID,
so let's call it product_id After the name, we add a space,
then we specify the data type. Each column needs a data type,
which defines what kind of data it can store. Each database vendor has some data types that
are the same, and some that are different. This is because SQL is a standard,
but not all database adhere to all parts of the standard. So, the exact data type will differ
depending on which database you're using. Let's say you're in MySQL, so you can use
the INT data type for declaring an integer or a number. So enter in INT.
Now, if you want to add a second column, specify a comma. I like to put each of
my columns on a new line, so press enter. On the new line, repeat the same process.
Enter the name, such as product_name. Then a space, then the data type.
In this example, we'll use a text value. The text type is often called VARCHAR, which
is short for variable character. In different databases, this may be different. For example,
Oracle's main text data type is VARCHAR2. So, with VARCHAR, we need to specify the
maximum length of the value. This helps with data storage and efficiency of working with it.
We do this by opening brackets, entering a number, then closing brackets.
In this example, I'll enter a value of 200. It should be long enough to store
whatever values you imagine, but not too big. We can change this later if needed.
So our table now has two columns: product ID and product name.
Now we have the columns, we can add in any constraints or keys for the table
Tables can have different constraints. There are five constraints you can add,
which are primary key, foreign key, not null, unique constraint, and check constraint.
They help improve the quality of your data. I won't go into the details of each of them here,
but you can add any of them here such as a primary key.
Once you've added all of your columns, and any keys, finish your table statement
by entering a closing bracket. This will pair with the open bracket that comes just
after the Create Table tablename at the top. Then, enter a semicolon to end the statement
Now your statement is ready. Run the statement in your SQL IDE,
such as MySQL Workbench, SQL Server Management Studio, or Oracle SQL Developer.
The table will now be created. You should get some kind of confirmation message in your output
If you get an error, it could be one of a few things.
You may have put a comma in the wrong spot, or missed out a comma. So check that it all looks OK.
Perhaps you have used a data type that doesn't exist.
I do this sometimes, as I forget which data types exist in which databases.
If you're not sure about the error still, you can google the error message to see what can be done.
So, the table has been created. You can check that it's there by running a quick SELECT statement.
To do this, run SELECT * FROM your table name. You should see your columns for your table at
the bottom of the screen, but no rows. This just shows your table is created.
So, that's how you can create a new table in SQL, regardless of which database you work on.
If you learned something new from this video, make sure to subscribe to my channel.
If you want to learn more about database design and development, visit
[databasestar.com](http://databasestar.com/).
That’s where I share my best database-related content.
Which step from this tutorial was the most helpful for you? Was it step 4 with adding
the columns and data types, or step 8 about running a quick SELECT *, or something else?
Thanks for watching.