
For an instructor lead, in-depth look at learning SQL click below.
Welcome to our blog post! This tutorial , meant to familiarize you with the creation of tables in SQL. If you’re new to SQL or need a refresher on how to create tables, then this is the right guide for you.
What are Tables in SQL?
In SQL, a table is a collection of related data entries and it consists of columns and rows. Tables are the core of every database and SQL provides us with a number of commands to create and manipulate these tables.
Creating a Table
Before we can start performing operations, we first must create a table. In SQL, the CREATE TABLE statement is used to create a table.
1 2 3 4 5 6 7 8 |
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... ); |
Here, the table_name specifies the name of the table that you want to create. Each column name in the table is followed by its datatype.
Example
Let’s take an example of creating a “Students” table, which has three columns, namely, StudentID, FirstName and LastName.
1 2 3 4 5 6 7 |
CREATE TABLE Students ( StudentID int, FirstName varchar(255), LastName varchar(255) ); |
Populating a Table
Now that we’ve created a table, let’s populate it with some data. The INSERT INTO statement is used to insert new data into a table.
1 2 3 4 |
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); |
Example
Now let’s insert some data into our Students table. We’ll insert a single student with a StudentID of 1, a FirstName of ‘John’, and a LastName of ‘Doe’.
1 2 3 4 |
INSERT INTO Students (StudentID, FirstName, LastName) VALUES (1, 'John', 'Doe'); |
To insert multiple rows into the table, we use multiple INSERT INTO statements.
Hope that cleared up how to create and populate tables in SQL for you! In upcoming posts we’ll delve more into manipulating these tables in SQL. Until then, happy coding!