Understanding different data types in SQL and Creating table
Hello, and welcome to the blog. In this blog we are going to talk about different data types available in the SQL and we are going to create our first table in which we are going to store data.
In our previous blog( https://learntocodewithmustafapinjari.blogspot.com/2020/07/understanding-queries-and-creating.html ) we learn how to create database and we created a database named as "EmployeeManagementSystem". Now its time to create some tables in our database so that we can store our data. So in this blog we are going talk about:
- Understanding data types
- Different data types in SQL
- Creating a database table
- Adding Column to existing table
1) Understanding Data Type:
- It is very important to understand "Data types" before creating a database table as your table is going to represent a data for a specific Entity.
- "Entity is thing with distinct and independent existence" like a Person, Car, Shop, Institute, Animal etc.
- Every entity do have some properties. For example: A person has is First Name, Last Name, Age, Date Of Birth, Gender, City, Country, Phone Number, etc. Now each of theses property has value. Some values are represented as words , some are represented as numbers, some are represented as date, some are represented as decimal value.
- So "Data Types" defines what type of value a particular column will hold in a database table. There is a particular data type for storing words, a particular data type for storing numbers, a particular data types for storing date, etc.
2) Different data types in SQL
We are not going to discuss all data types and get bored. We are going to discuss some basic data types which are widely used and other data types we will see as we progress through this tutorial.
Note: There are some data type which are specific to DBMS software you are using.
1. varchar
- This data type is used to store words, sentences, paragraph or any collection of letters.
- Programmers say it like this "varchar is used to store String value".
- In varchar data type you can specify number of characters allowed in a column.
- For example: varchar(50) this will allow you to store words or sentences with maximum 50 characters.
2. int
- This data type is used to store numeric data.
- Numeric data which is stored in this data type don't have decimal value.
3. decimal
- This data type is used to store numeric data with decimal values.
- You can specify number of decimal precision in this data type.
- For example: decimal(16,2) here we can have value with up to 16 digit number and 2 decimal precision. Let's see another example decimal(14,4) we can have value with 14 digit number and 4 decimal precision.
4. datetime
- This data type is used to store date and time value.
- When you want to store date and time, for example: date of purchase and time of purchase, etc. this data type is used.
5. date
- This data type is used to store only date.
- When you use datetime data type by default a time is also stored in the database if you don't want time to be saved you use this data type.
3) Creating Database Table
Creating a database table is super easy thing. You have do just open your DBMS software and press "ctrl+n" or click on "New Query". While running queries you have to make sure that correct database is selected.
You can check selected database in "SQL Server Management Studio (SSMS)" where I have marked red. You have to make sure there "EmployeeManagementSystem" is selected otherwise you will end up running query in "master" database. Now, just run the query written in example. I have marked predefined keywords in blue.
Syntax :
create table table_name
(
column_name datatype,
column_name datatype,
.
.
column_name datatype
)
Example :
create table Employee
(
Id int,
FirstName varchar(50),
LastName varchar(50),
Department varchar(50),
Salary decimal(18,2)
)
4) Adding column in Database Table
As a human we have tendency to forget things. What if you forget to add a column to your database table? Don't worry we have a SQL command to do that.
Syntax :
alter table table_name
add column_name datatype
Note: This syntax is for "Microsoft SQL Server"
Example:
alter table Employee
add Designation varchar(50)
Great we have learned to add a database table and a column to existing table. Upcoming we will see adding and retrieving data from database.
See you in some another blog till then "Keep coding, keep learning because trust me you can make it big".
"Have a great day".

Very informative 👍
ReplyDelete