0% found this document useful (0 votes)
10 views9 pages

SQL Salary Data Type Overview

The document provides an overview of SQL data types, including number formats (integral and floating-point), text formats (char, varchar, and text), date-time formats, and binary formats. It includes SQL syntax for creating tables and examples of data type usage. Additionally, it discusses the importance of SQL comments for documentation and readability in code.

Uploaded by

aman mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

SQL Salary Data Type Overview

The document provides an overview of SQL data types, including number formats (integral and floating-point), text formats (char, varchar, and text), date-time formats, and binary formats. It includes SQL syntax for creating tables and examples of data type usage. Additionally, it discusses the importance of SQL comments for documentation and readability in code.

Uploaded by

aman mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Day-01

24-02-2025
Datatypes
===================

SQL Syntax:
create table Employee(
empId number,
empName varchar(30),
salary number
);

Datatypes:
=======
-> The main important data formats in SQL are:
1) Number format
2) Text Format
3) Date-Time format

When we have n-number bits, then the range of values to store


are:
-2^(n-1) to 2^(n-1) - 1
1) Number format
============
-> number format includes:
1) Integral types
------------------
-> The numbers with no decimal point
-> Four types:
i) Tiny int
------------
-> 1-byte => 8-bits
-> range ==> -2^7 to 2^7 - 1 ==> -128 to 127

ii) small int


------------
-> 2-bytes
-> range ==> -2^15 to 2^15 - 1 ==> -32768 to
32767

iii) int
--------
-> 4-bytes
-> range ==> -2^31 to 2^31 - 1

iv) big int


-----------
-> 8-bytes
-> range ==> -2^63 to 2^63 - 1

Ex:
create table employee(
sno tinyint,
age tinyint,
empId smallint,
pin int,
mobile bigint
);

2) Floating-point types
--------------------------
-> numbers with decimal point.
-> three types:
1) float
--------
-> when a floating-point number needs to define with
up to 16 places before and after the decimal point, we can use
"float".
Ex: salary float;
56000.7910

2) double
---------
-> when a floating-point number needs to define with
up to 32 places before and after the decimal point, we can use
"double".
Ex: bankBalance double;

3) decimal
----------
-> when a floating-point number needs to define with
up to 32 places before and after the decimal point, we can use
"decimal".
-> for the decimal value, we cannot apply the
approximation.

round()/approx()
round(9.7)/approx.(9.7) ==> 10
round(9.2)/approx.(9.2) ==> 9

2) Text Format:
===========
-> Can always allowed to define with single quote or double
quotes.
-> can define in three ways:
1) char()
---------
-> When we can create a column with char type,
each character holds the memory of 1-byte.
Syntax:
column-name char(size);
Here:
size ==> positive (>0)
When we have specified the size of the column data,
according to the specified size the memory can be created.
Ex: empName char(30);
empName = "Ravi Kumar";
Here:
the specified size ==> 30
So, the memory can be created as: 30-bytes
into this, only 9 characters (9-bytes) can be used to
store and remaining are to be wasted.
-> modifications of the data bit faster.

2) varchar()
------------
-> When we can create a column with varchar type,
each character holds the memory of 1-byte.
Syntax:
column-name varchar(size);
Here:
size ==> positive (>0)
The varchar is not based on the memory which has
specified. It is always based on the value what we have assigned.
Ex: empName varchar(30);
empName = "Ravi Kumar";
-> From the above:
created memory ==> 9-bytes for total 9-chars
-> modification of the data is slower.

3) text
=======
-> huge amount of text data can be represented with "text"
type.

Day-02
25-02-2025
==============
user-name char(30);
user-name = "Ashok IT";

user-name varchar(30);
user-name = "Ashok IT";

Date-time format:
============
-> can be used to store the column fields with date formatted
value or time formatted value or both.
-> can be defined with three datatypes:
1) date
2) time
3) datetime

ex:
create table Student(
stuid smallInt,
stuname varchar(30),
gender char(6),
dob date,
examTime time,
resultedOn datetime,
);
dob = 'yyyy-mm-dd';
ex: dob = '1993-06-20';

examTime = 'HH:MM:SS';
examTime = '15:00:00';

resultedOn = 'YYYY-MM-DD HH:mm:SS';


Ex: resultedOn = '2025-02-20 17:10:10';
---------------------------------------------------------

Binary format
=========
-> Normally the files can be categorized into two types:
1) Text Files
--------------
-> can store the data in the text format
Ex: .txt files
2) Binary Files
---------------
-> the data other than the text in the files are called as
"Binary files".
-> The binary data includes: images, zip files, audio
files, video files etc.,
-> The binary formatted data can be represented using two
different datatypes:
1) binary() -> fixed length
2) varbinary() -> variable length
Ex:
create table Files(
fid smallInt;
fileData binary(16);
fileData1 varbinary(521);
);

SQL Comments
=============
-> SQL comments can be used for documentation purpose.
-> SQL comments can increase the readability of the program.
-> comments can allow to write in any where of the program.
-> There are two ways for commenting:
1) Single line commenting -> --
2) Multi-line commenting --> /* */

Common questions

Powered by AI

SQL syntax allows defining tables with diverse data types by specifying the appropriate type for each column, such as number, varchar, date, or binary, providing the necessary structure to meet application-specific data management needs . This flexibility in dtype selection ensures optimal performance and storage efficiency, as developers can tailor data types to align with data characteristics and usage patterns—'int' or 'bigint' for numeric IDs, 'varchar' for varying-length text fields, 'date' for calendar data, and 'binary' for media files . This adaptability is essential for crafting robust and efficient databases capable of handling a wide spectrum of data types and application requirements effectively.

In SQL, the 'char()' type assigns a fixed memory size to each column value, meaning if a column is defined as char(30), 30 bytes are allocated irrespective of the actual string length, thus leading to potential memory wastage when storing shorter strings . 'Varchar()', however, only uses memory proportional to the actual string length, making it more memory-efficient for variable length strings, albeit slower in modification due to dynamic size management . The 'text' type caters to very large strings allowing substantial text storage, like large articles or logs, making it indispensable for extensive text data management . Choosing between these types depends on the balance between consistent space allocation (char), flexibility and efficiency (varchar), and large volume storage needs (text).

SQL comments provide an invaluable tool for increasing the clarity and maintainability of code by allowing developers to annotate parts of SQL scripts with human-readable explanations . They can be used to explain complex queries, note the purpose of specific columns or tables, and outline logic or expected outcomes, making it easier for others (or the original author later) to understand the code's intent. Comments can be single-line through '--', or multi-line with '/* ... */', allowing flexibility in documenting various aspects of SQL code . Effective commenting can significantly enhance collaboration and reduce misunderstandings in complex SQL environments.

For financial calculations in SQL, 'decimal' should often be prioritized due to its exact representation of decimal numbers without rounding errors, which is crucial for precise financial records . 'Float' and 'double', while offering higher ranges and precision, use approximate calculations that can introduce small errors due to floating-point arithmetic, potentially leading to substantial cumulative discrepancies in large-scale financial computing . Therefore, the choice depends on the necessity for precision versus range, with 'decimal' being ideal when exactness supersedes other factors, such as in financial, scientific, or statistically sensitive data settings.

SQL data types such as 'binary()' and 'varbinary()' are optimized for storing and retrieving digital media files like images and videos by representing them as binary data rather than text. 'Binary()' allocates a fixed length for each file, ensuring predictable space allocation which can aid in faster access for uniformly sized files . 'Varbinary()' provides dynamic storage, adapting to the actual file size and optimizing space usage, which is particularly advantageous for varied file sizes . These specialized data types facilitate efficient storage management and quick retrieval, crucial for performance in applications handling extensive multimedia content.

'Binary()' and 'varbinary()' in SQL are vital for storing non-textual data such as images, audio, and video files. 'Binary()' stores data in a fixed length, which means a set space is consistently reserved irrespective of the actual binary content length, potentially leading to inefficiency in terms of storage . On the other hand, 'varbinary()' allows for variable-length storage, adapting to the actual content size and thus optimizing space usage . These binary types are essential for handling various digital media efficiently in a manner that text-based storage cannot accommodate.

In SQL, the range of values for integral types varies based on the bytes used by each type. A 'tinyint' uses 1 byte (8 bits), allowing for a range of -2^7 to 2^7 - 1, i.e., -128 to 127 . This type is used for small integers. A 'smallint' uses 2 bytes (16 bits) with a range of -2^15 to 2^15 - 1, i.e., -32,768 to 32,767, suitable for larger but limited integer values. An 'int' type uses 4 bytes (32 bits), handling a range from -2^31 to 2^31 - 1, best for most typical integer operations. Finally, 'bigint' uses 8 bytes (64 bits), allowing for values from -2^63 to 2^63 - 1, which is appropriate for significant numbers requiring extensive range . Each type provides specific storage optimizations and should be chosen based on expected data size.

In SQL, the 'date' type should be used when the application requires only the calendar date, such as birthdates or event dates without time specifics . 'Time' is ideal for capturing moments within a day, like opening hours or scheduled appointments, where the date context is irrelevant or fixed . The 'datetime' format combines both, serving scenarios that need precise timestamps like transaction logs or audit trails, capturing when exactly an event occurs including its date and exact time . By selecting the appropriate data type, developers can ensure efficiency and relevance in data capturing and storage.

'Char' provides faster data modification compared to 'varchar' because 'char' has a fixed size for all entries, allowing SQL to immediately know where and how to allocate memory without processing dynamic length calculations . In contrast, 'varchar', while space-efficient, requires additional processing since it adjusts its memory allocation based on data length, slowing down update operations particularly in large databases or operations involving many changes . This difference impacts SQL performance, making 'char' suitable for fields with uniform data lengths where speed is critical, while 'varchar' is ideal for fields requiring flexible yet slower data adjustments.

In SQL, 'float' is used when defining numbers requiring precision of up to 16 digits, useful for typical monetary or percentage values requiring detailed decimal representation . 'Double' extends this precision further to 32 digits, serving scenarios demanding significant data accuracy. 'Decimal' shares similar precision capabilities with 'double' but notably does not apply rounding approximations, making it crucial for financial calculations where exact decimal representation is essential . Precision handling is managed by selecting appropriate data types based on the required decimal places and data criticality.

You might also like