Data Types in SQL
Data Types in SQL
Data Types in SQL
When establishing a table, the SQL Data Type property determines the type of
data that will be stored inside each column. The data type serves as a guideline
for SQL to comprehend what type of data is expected within each column and
identify how SQL will interact with the stored data.
A Data Type in SQL is defined as the type of data that any column or variable
can store. It is a type of data that an object holds like integer, character, string,
etc. While creating any table or variable, in addition to specifying the name, you
also set the Type of Data it will store.
Let's look at a simple sign-up page for a website application. First Name, Last
Name and Contact Number are the three entry areas.
Source: scaler.com
Let us take another scenario to understand more about data types in SQL.
Consider the following sample records from a sales table:
Customer id: An integer value that increases by one for each new
customer
Sales_id: An integer value that increases by one for each new sale
Currency: Always use the three-character currency code
Purchase_time: The time of the sale
Device: Text, with values such as desktop, mobile app and mobile web
Has_discount: A boolean variable with entries that can be True or False
For any database, data types are primarily categorized into three categories:
String Datatypes
Numeric Datatypes
Date and Time Datatypes
SQL Server String Data Type:
We'll use two tables: Student and Fee to explore each type of join with
examples.
Inner Join:
SQL Inner Join or Equi Join is the simplest join where all rows from the intended
tables are cached together if they meet the stated condition. Two or more
tables are required for this join. Inner Join can be used with various SQL
conditional statements like WHERE, GROUP BY, ORDER BY, etc.
Source: dev.to
Syntax:
SELECT column-name
Example:
ON Student.admission_no = Fee.admission_no;
Output:
Self Join
In SQL Self Join, a table is joined to itself. This means each row of the table is
joined with itself and all other rows concerning stated conditions if any. In other
words, we can say that it is a merge between two copies of the same table. This
is extremely helpful when the foreign key references the primary key of the
same table.
Syntax:
WHERE join_condition;
Example:
ORDER BY S2.city;
Output:
In this example, we have used the id and city column as a join condition to get
the data from both tables.
NTRODUCTION TO INDEX
The Index in SQL is a special table used to speed up the search of the data in
the database tables. It also retrieves a vast amount of data from the tables
frequently. The Index requires its own space in the hard disk.
The index concept in SQL is the same as the index concept in a novel or a book.
It is the best SQL technique for improving the performance of queries.
For example, to reference all pages in a book that address a particular subject,
you go to the index first, which lists all the topics alphabetically, and then you
go to one or more specific page numbers.
SQL Indexes can search the information of the large database quickly
This concept is a quick process for those columns, including different values
This data structure sorts the data values of columns (fields) either in ascending
or descending order. Then, it assigns the entry for each value.
Each Index table contains only two columns. The first column is row_id, and the
other is indexed-column.
When indexes are used with smaller tables, the performance of the index may
not be recognized.
Clustered Index
Non-Clustered Index
Clustered Index:
Clustered indexes are automatically created by the database for any table.
Therefore, to create a new clustered index, we first need to delete the old
clustered index. After deleting the index, we use the syntax given below to
create a new clustered index.
Syntax:
Example:
Explanation: The above query will create a clustered index on the Emp table
using the column name, which will make the records inserted in ascending
order of the name by default.
Non-Clustered Index:
Syntax:
Example:
Explanation: The above query will create a NONCLUSTERED Index on the Emp
table using the column name, which will make the records inserted in
ascending order of the name.