Data Types in SQL

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

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.

Integers, floating-point numbers, characters, strings and arrays are examples


of common data types. For more specific SQL uses, Dates, timestamps, boolean
values and varchar (variable character) types of formats.

What are Data Types in SQL?

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

 The First/Last Name field will always be alphabetical


 The Contact field will always be a phone number (Example: numeric)

It is worth specifying "First/Last Name" as a character and "Contact" as an


integer because a name can't be a number and a contact number can't be a
character. All fields in any application include one or more types of data. For
example, numeric, alphabetic, date and many others.

Also, different data types have varied memory requirements. As a result, it


makes it more logical to declare the column or variable with the data type that
it will carry to make better use of memory.

Let us take another scenario to understand more about data types in SQL.
Consider the following sample records from a sales table:

The following are the attributes of this 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:

SQL Server Numeric Data Types:

SQL Server Date and Time Data Type:


In an SQL server, a Data Type is defined as the type of data that any column or
variable can store. It is a type of data that an object stores, such as an integer,
character, string and so on.

Types of JOINS in SQL:

The following are types of join in SQL:


Source: medium.com

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

FROM table-1 INNER JOIN table-2

WHERE table-1.column-name = table-2.column-name;

Example:

SELECT Student.admission_no, Student.first_name, Student.last_name, Fee.cour


se, Fee.amount_paid

FROM Student INNER JOIN Fee

ON Student.admission_no = Fee.admission_no;

Output:

In this example, we have used the admission_no column as a join condition to


get the data from both tables. Depending on this table, we can see the
information of the students who have paid their fee.

 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:

SELECT T1.col_name, T2.col_name...

FROM table1 T1, table1 T2

WHERE join_condition;

Example:

SELECT S1.first_name, S2.last_name, S2.city

FROM Student S1, Student S2

WHERE S1.id <> S2.id AND S1.city = S2.city

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.

A database index is similar to the index in the back of a journal. It cannot be


viewed by the users and just used to speed up the database access.

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.

Indexes prevent duplicate entries in the column or combination of columns on


which it is created. Since SQL indexes are primarily a performance tool, they are
most useful when a database grows in size. The clustered index is one of the
most popular types of indexes supported by SQL servers. This type of index is
automatically created with a primary key.
Why SQL Index?

The following reasons tell why an Index is necessary in SQL:

 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.

A table or view can contain the following types of indexes:

 Clustered Index
 Non-Clustered Index

Clustered Index:

A clustered index is an index that specifies the physical arrangement of a


database's table records. There can only be one clustered index per table since
there can only be one method that records are physically maintained in a
database table. It stores the records in sorted order.

The characteristics of a clustered index are as follows:

 Default Indexing Methodology


 can use a single or more than one column for indexing
 Indexes are stored in the same table as actual records
 It supports fragmentation functionality
 Supports Scanning and Seeking using Clustered Indexes

Creating 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:

Create CLUSTERED Index Index_name ON table_name(column_name


ASC/DESC,...);

Example:

Create CLUSTERED Index ABC ON Emp(name ASC)

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:

A non-clustered index is an index that doesn't specify the physical arrangement


of the records maintained in the database’s table. The Non-Clustered Indexes
are stored in a different table. Therefore, as they are maintained in a different
table, there can be numerous non-clustered indexes that can be created for a
single table.

Characteristics of the Non-Clustered Index:

The characteristics of the non-clustered index are as follows:

 Table data is stored in the form of key-value pairs


 Tables or Views can have indexes created for them
 It contains pointers to clustered index records
 Supports index scanning and seeking functionalities
 It provides secondary access to records

Creating a Non-Clustered Index:

As Non-Clustered Indexes are maintained in different tables, they can be


numerous. So we don't need to delete previously created indexes. The syntax
for creating a Non-Clustered Index is similar to the syntax for a clustered index
except for the use of non-clustered Words. We can create a non-clustered
index using the syntax given below:

Syntax:

Create NONCLUSTERED Index Index_name ON table_name(column_name


ASC/DESC,...);

Example:

Create NONCLUSTERED Index ABC ON Emp(name ASC)

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy