Navigate back to the homepage

SQL - CREATE TABLE Statement

Vishnu Damwala
April 2nd, 2020 · 1 min read

A tutorial for creating a database table with SQL

A database is an organized collection of data or structured information.

The Database incorporates one or more tables. DBMS (DataBase Management System) usually manages the database.

The CREATE TABLE statement is used to create a new table in the database using SQL.

SQL stands for Structured Query language, pronounced as “S-Q-L” also popularly known as “See-Quel”.

Syntax:

1CREATE TABLE table_name (
2 column1 datatype constraints,
3 column2 datatype constraints,
4 column3 datatype constraints,
5 ....
6);

SQL supports several kinds of data.

The datatype of a column defines what type of value the column will hold: integer, character, date and time, binary, and so on.

The datatype you more often come across are:

  • CHAR
  • TEXT
  • VARCHAR
  • DATE
  • TIME
  • DATETIME
  • TIMESTAMP

Numeric datatype includes

  • TINYINT
  • INT
  • BIGINT
  • SMALLINT
  • DECIMAL
  • FLOAT

Wow! so many numeric types, but why?

All these hold the numeric type of data but have different ranges and precisions i.e A TINYINT value resides between 0 to 255 while for an INT it would be between -231 to +231.

The more storage space will be needed as the number of size in bytes increases.

Example

1CREATE TABLE Users(
2 UserId int,
3 FirstName varchar(50),
4 LastName varchar(50),
5 Email varchar(255)
6 Address varchar(255),
7 City varchar(255),
8 Birthday date,
9);

The UserId column is of type int and will hold an integer value.

The FirstName, LastName, Email, Address, and City columns are of type varchar and will hold characters, and the maximum length for these fields are specified with it. For FirstName here it’s 50 characters, whereas Email has 255 characters.

The structure of the empty “Users” table will look like this:

UserIdFirstNameLastNameEmailAddressCityBirthday

Note: Size, precision or ranges varies with the DBMS that you’re using

Hope you learn something new.

Happy coding

More articles from MeshWorld

How to allow remote access to MySQL database server

How to allow remote access to MySQL database server

March 5th, 2020 · 2 min read

A Program to print upside down triangle pattern with some fuzzy logic in C

A program to print upside down triangle pattern with some fuzzy logic in C.

March 3rd, 2020 · 1 min read
© 2017–2020 MeshWorld
Link to $https://twitter.com/meshworldindiaLink to $https://facebook.com/meshworldindiaLink to $https://instagram.com/meshworld.india/