
For an instructor lead, in-depth look at learning SQL click below.
Creating a table in SQL is fundamental to working with any form of database. Understanding the ‘CREATE TABLE’ statement is the first step in setting up a database.
What is ‘CREATE TABLE’ Statement ?
The CREATE TABLE statement is used to create a new table in SQL. This command requires a table name and at least one column name with its associated data type.
1 2 3 4 5 6 7 8 |
CREATE TABLE new_table( column1 datatype, column2 datatype, column3 datatype, .... ); |
Basic Syntax of CREATE TABLE
Let’s break down the parts of the ‘CREATE TABLE’ statement:
CREATE TABLE
This command tells SQL that you want to create a new table.
new_table
This is the name you want to give to your table. Pick something that makes sense, because this is how you will be referring to your table.
column1, column2, …, columnN
These are the names you want to give to your table’s columns. Just like with the table name, pick something that makes sense.
datatype
This is the type of data that will be stored in the respective column. Some common data types include INT (integer), VARCHAR (string), and DATE.
Example
Let’s say we want to create a table named ‘Students’ where each student has an ID, name, and major. The ID will be an integer, the name a string, and the major a string as well. Here is how we would use the ‘CREATE TABLE’ statement:
1 2 3 4 5 6 7 |
CREATE TABLE Students( ID INT, Name VARCHAR(100), Major VARCHAR(100) ); |
And just like that, we’ve created a table! To add data to our table, we would use the INSERT INTO statement, but that’s for another blog post. Remember, practice makes perfect – so don’t be afraid to mess around with creating tables in your database.
You’re well on your way to becoming an SQL pro!