In This Tutorial
What is table in database
The tables in the database store the data. A database may contain dozens or even hundreds of tables, depending on the database schema or architecture as per the requirements of the project.
An SQL table consists of the columns and rows. Each column is also referred as a field. Each column or field has its own data type that specifies the type of data to be stored in that column.
For example, a table namely tbl_employee may contain emp_name, emp_age, emp_salary and joining_date fields. The field/column emp_name contains the characters to store employee name, emp_age can contain only numbers to store the employee’s age and emp_salary contains data with decimal points to store the employee’s salary.
Syntax of using the Create table command
The syntax of creating a table involves Create table SQL command. The syntax is as follows:
Create Table table_name(
Primary key (column name(s))
So, the Create Table statement is followed by the table_name, that should be descriptive according to your project. Generally, the table names are started with tbl_, however, you may have your own specifications for the table names. After the table name, the column names are given in the parenthesis.
The column names should also be descriptive i.e. according to data it will store. For example, we have to store employees data that includes employee name, employee age and employee salary. As creating the names, we should assign descriptive column names e.g. emp_name, emp_age, emp_salary, and joining_date.
For each field to be created, a data type must be specified in the create table command of SQL. This will be according to data this field will store. For example, the emp_name should be able to store the characters from A-Z in the capital or small letters. After that, emp_age should be able to store numbers without the decimals. Whereas, the emp_salary field should be able to store the numbers with the decimal point and joining_date should store the date of joining of the employees.
This is how we can create our example table, tbl_employee, with the above-mentioned fields along with its data types.
An example of creating a table
The following example shows how to use the SQL create table statement. The example creates a table, tbl_employee, with four fields i.e. emp_name, emp_age, emp_salary, and joining_date.
Create table tbl_employee
As we have gone through how to use the create table command for creating a simple tbl_employee table in our test_db database, it is time to go ahead with creating a table with the primary key.
Related – The Select statement | The delete statement