Saturday, 28 April 2018

Sqlite

The reason behind this popularity is some unique characteristics that are unusual and which makes it different from many other SQL database engines like MySQL, PostgreSQL, Oracle, Microsoft SQL Server, etc. Let's get started with it. First, We will install it on Linux and later on cover basic database operations.

Installing Sqlite

  • To install on your debian based(Ubuntu, Debian etc.) machine execute below commands.
$ sudo apt-get update
$ sudo apt-get install sqlite3
  • To install on your RPM based(RHEL, CentOS, Fedora etc.) machine execute below commands.
$ sudo yum update
$ sudo yum install sqlite
Now Open a terminal and Execute "sqlite3", you will see the following lines with prompt.
$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
The very first line shows the version and release date and time of sqlite3.
The second line tells to enter ".help" for instructions.
.help command list all the meta commands and their descriptions. These meta commands are also called "dot" commands because they are preceded by a dot.
Put ".help" to prompt
sqlite> .help
.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail ON|OFF           Stop after hitting an error.  Default OFF
.databases             List names and files of attached databases
.dump ?TABLE? ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.echo ON|OFF           Turn command echo on or off
.exit                  Exit this program
.explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.
                         With no args, it turns EXPLAIN on.
.header(s) ON|OFF      Turn display of headers on or off
.help                  Show this message
.import FILE TABLE     Import data from FILE into TABLE
.indices ?TABLE?       Show names of all indices
                         If TABLE specified, only show indices for tables
                         matching LIKE pattern TABLE.
.load FILE ?ENTRY?     Load an extension library
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.mode MODE ?TABLE?     Set output mode where MODE is one of:
                         csv      Comma-separated values
                         column   Left-aligned columns.  (See .width)
                         html     HTML table code
                         insert   SQL insert statements for TABLE
                         line     One value per line
                         list     Values delimited by .separator string
                         tabs     Tab-separated values
                         tcl      TCL list elements
.nullvalue STRING      Use STRING in place of NULL values
.open ?FILENAME?       Close existing database and reopen FILENAME
.output FILENAME       Send output to FILENAME
.output stdout         Send output to the screen
.print STRING...       Print literal STRING
.prompt MAIN CONTINUE  Replace the standard prompts
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.schema ?TABLE?        Show the CREATE statements
                         If TABLE specified, only show tables matching
                         LIKE pattern TABLE.
.separator STRING      Change separator used by output mode and .import
.show                  Show the current values for various settings
.stats ON|OFF          Turn stats on or off
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
.timeout MS            Try opening locked tables for MS milliseconds
.trace FILE|off        Output each SQL statement as it is run
.vfsname ?AUX?         Print the name of the VFS stack
.width NUM1 NUM2 ...   Set column widths for "column" mode
.timer ON|OFF          Turn the CPU timer measurement on or off
Before explaining these dot commands, let's see some of the basic database operations.
First run ".quit" command to end the session.

Create Database

Execute below command on shell prompt.
It will create "example.db" (You can put your choice of name in place of "example.db") database file ( if not exists). If exists then open the database contained in the file.
$ sqlite3 example.db
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
The difference between "sqlite3" and "sqlite3 dbname.db" command is the first one will create a temporary database for the session and will expire when the session is closed.

Create Table

Let's create a student table with attributes:
reg_no int(4)
Name varchar(20)
Marks int(3)
enter the following commands in terminal
sqlite> create table student (
   ...> reg_no int(4),
   ...> name   varchar(20),
   ...> marks  int(3)
   ...> );
It will create a student table with above attributes.
You can see the list of tables in the selected database by .table command
sqlite> .table
student
There is only one table student in my case.
You can also see the schema of the table using .schema table_name
sqlite> .schema student
CREATE TABLE student (
reg_no int(4),
name   varchar(20),
marks  int(3)
);

Inserting Data

Let's insert 3 records in the table
101, pradip, 87
102, Avinash, 86
103, Rakesh, 90
Enter following command one by one
sqlite> insert into student (reg_no, name, marks) values (101, 'Pradip', 87);
> insert into student (reg_no, name, marks) values (102, 'Avinash', 86);
> insert into student (reg_no, name, marks) values (103, 'Rakesh', 91);

Fetching data

You can fetch data from table with select statement
sqlite> select * from student;
101|Pradip|87
102|Avinash|86
103|Rakesh|91
It shows the result in default mode (list).
You can change the mode with .mode command
Enter .mode column to prompt and then perform select query. It will show the result in a column format.
sqlite> .mode column
> select * from student;
reg_no      name        marks     
----------  ----------  ----------
101         Pradip      87        
102         Avinash     86        
103         Rakesh      91        
Delete, Alter, Drop etc have same syntax as sql.

Special commands to sqlite3(dot -commands)

we saw ".help" commands list all the dot commands.
Let's understand some other important commands.
  • .databases
Shows name of database selected.
sqlite> .database
seq  name             file                                                      
---  ---------------  --------------------
0    main             /home/avi/example.db
  • .table
List the tables in the selected database.
sqlite> .table
student
  • .show
Shows the current settings
sqlite> .show
     echo: off
  explain: off
  headers: off
     mode: list
nullvalue: ""
   output: stdout
separator: "|"
    stats: off
    width: 
    • .header ON|OFF
      You can show attribute name in query result by running .header ON|OFF command
      sqlite> .header ON
      > select * from student;
      reg_no      name        marks     
      ----------  ----------  ----------
      101         Pradip      87        
      102         Avinash     86        
      103         Rakesh      91        
      
    • .mode
The Program is able to show the results of a query in eight different formats: "csv", "column","html","insert", "line", "list", "tabs", "tcl"..mode command is use to switch between these output formats.
sqlite> .mode csv
> select * from student;
reg_no,name,marks
101,Pradip,87
102,Avinash,86
103,Rakesh,91
You can use .separator command to change separator.
sqlite> .separator -
> select * from student;
reg_no-name-marks
101-Pradip-87
102-Avinash-86
103-Rakesh-91

Writing results to a file

by default, It sends query results to standard output.
you can change this using ".output" and ".once" commands.
Just put the name of an output file as an argument to .output and all subsequent query results will be written to that file.
sqlite> .output ex.txt
Or use the .once the command with file name if you want only the result of next query to be redirected.
We have successfully installed SQLite on Linux with basic operations. These operations are only a few out of all available. We can't cover all of them in this article. If you find any difficulties in an installation or in any command, Let me know in comment section. See you in next article.

DATABASE

Relations database was invented in 1970, and since then it has been growing largely due to its capability.
The Management system is the main reason behind its growth as they have implemented it very well for their purpose. It is the effective way to work with large data.

What is Database?

The database is the collection of data which can be referred as information stored in an organized way so that they can be managed.

What is DBMS?

DBMS or Data Base Management System is the application which is used to store, manipulate and retrieve data in the database. DBMS handles all the tasks related to Data.
In simple words, the DBMS created the database using the information you give.

Relational Database Management System

The RDBMS establishes the relationship between the tables in the database. It implements the Relational Model with data to work.
Relational database stores the data in a hierarchy where the attributes are having the relationship with the entity in the other table.
To start with RDBMS, you have to create a structure to define and store the data in the database. The table contains rows and columns.
The row is identified with the Keys.
Columns are identified by attributes

what is the difference between DBMS and RDBMS?

DBMS stores the data as files but here in RDBMS you will store all the data in table format and can be related to each other.

What is Relations?

The relation in the RDBMS products is considered as Tables with the set of data. Also, the data stored, retrieved using mathematical set can be included.
The attributes contain the data types. When you enter data into the column to form a record, the data should match the data type.

What is Data Type?

A data type is simply a classification of variables used to store the data.
Here the data type represents which value a variable should contain and what type of mathematical, logical and relational operations can be performed with that value without error.

The Top Relational Databases

Applications mainly depend on data. Here in this article, we are going to see three Open source RDBMS.
All of them are popular and widely used for application development.
They are

PostgreSQL

PostgreSQL is the most advanced Object-based Relation Database Management System which uses Object, Classes, and Inheritance.

MYSQL

We all know very about this Relational Database Management System. MySQL is very easy to install and use

SQLite

SQLite is the powerful embedded RDBMS used by applications which require database access.

PostgreSQL

PostgreSQL is the open source relational database management system developed by professionals from all over the world.
Any organizations do not own it, and the source code is entirely available with free of cost.
The specialty of the PostgreSQL is its versatility and strong support of platforms.

The platform it supports:

  1. Linux
  2. Unix
  3. MAC OS X
  4. Windows
  5. Solaris
  6. FreeBSD
  7. NetBSD
  8. OpenBSD
  9. Advanced Interactive eXecutive by IBM
It also supports multimedia such as text, photos, videos, and audios.
PostgreSQL differs a lot from RDBMS as it entirely supports Relational system/Object Oriented Model which supports Reliable transactions(ACID).
Here you should know what is ACID in Databases?
ACID stands for Atomicity, Consistency, Isolation and Durability.
These are important for reliable transactions.

What is Atomicity?

Either an operation has to be completed entirely, or it should not happen. This is what we can call atomicity. For, e.g., if you are making a bank transaction and the power goes down, Here the entire operation won't be performed.

What is Consistency?

In a transaction, the database has to go from one state to another state. If any error occurs, they database should not be changed and should be reverted to the previous stats as it was before the transaction.

What is Isolation?

In the Isolation process, the transactions which are being executed sequentially. One Transaction doesn't know about another one here.

What is Durability?

If a transaction is executed, it should be recorded in a non-volatile memory to keep track of it even when the failure occurs due to various reasons.
The PostgreSQL is very powerful, and it supports multiple transactions very effectively without read-lock. Multi-version Concurrency is the reason behind that, and it is ACID compliance.
Here multiple transactions can read the memory at the same time, and they don't know each other's operational change in the memory.
PostgreSQL can be programmed highly, and it is extensible with stored procedures. They are the containers of executable codes which is required to be often executed.
But one thing to note here is that this DBMS does not have much popularity like MYSQL has. But a lot of operating systems have been providing the PostgrSQL application as their default package.

PostgreSQL Data-Types

Numeric Data Type

smallint
Stores whole numbers, small range.
integer
Stores whole numbers.Use this when you want to store typical integers.
bigint
Stores whole numbers, large range.
decimal
user-specified precision, exact
numeric
user-specified precision, exact
real
variable-precision, inexact
double precision
variable-precision, inexact
serial
auto incrementing the integer
bigserial
large auto-incrementing integer

Character Type

character varying(n), varchar(n)
variable-length with limit
character(n), char(n) fixed-length, blank padded
text variable unlimited length

Date/Time Type

timestamp [ (p) ] [ without time zone ]
both date and time (no time zone)
timestamp [ (p) ] with time zone
both date and time, with timezone
date
date (no time of day)
time [ (p) ] [ without time zone ]
time of day (no date)
time [ (p) ] with time zone
times of day only, with time zone
interval [ fields ] [ (p) ]
12 bytes
point
Point on a plane
line
Infinite line (not fully implemented)
lseg
Finite line segment
box
Rectangular box
path
Closed path (similar to polygon)
path
Open path
polygon
Polygon (similar to closed path)
circle
circle
Let us the PostgreSQL advantages and Disadvantages

Advantages of PostgreSQL

Open Source SQL RDBMS

POstgre SQL is open source RDBMS which can be downloaded free of cost.

Good community Support

PostgreSQL has been developed by many people from all over the world. You can ask all the doubts related to the Postgre in their forums.
The dedicated professionals and master users of PostgreSQL will answer all your questions.

Widely Supported

A lot of third parties are supporting PostgreSQL including Operating Systems and Programming.

Extendable

We have discussed Stored procedures usages in PostgreSQL previously. That is the main element for Advanced RDBMS.

Objective Based

It supports class, inheritance, and nests. It takes you o the next level in SQL.

Cloud Supported

It is cloud supported since it uses heavy resources cloud can be the solution for that.

Disadvantages

Hard for Newbies

Installation and Configuration of Postgre are hard for the new one who just starts with it.

Performance

Performance is the trouble part for the usage of Postgre when compared to other RDBMS.

Popularity

We have to agree that Postgre is Advanced RDBMS, but one thing it lacks is Popularity. So it is very hard for finding assistance for using Postgre

When you can use PostgreSQL

If you need data integrity and reliability, then go for ##PosgtgreSQL

Need of Custom Procedures

If you need to run complex task, then custom procedures are a good choice which is extensible.

Integrity concerns

If you want to move your entire database to any big solution provider, then PostgreSQL will be the perfect one for the switch.

Complexity with ease

If you need to create a database which is complex, the PostgreSQL gives you the functionality to maintain such complex database smoothly without limitations.
Do not use Postgre at this situation

Speed

If you need to perform the read operation fast, then Postgre can't help you for that.

Easy Setup

Postgre is hard to install and configure for newbies

Replication

If you only need replication, then MySQL comes in the place. Don't use the elephant to pull bullock kat.

MySQL

Why do we use MySQL?
MySQL is the popular open source RDBMS widely used by many developers. It runs numerous websites and applications online.
Starting with MySQL is very easy for developers.
Note: There are a lot of other applications which might not have the popularity but such those third party applications and libraries help you to work efficiently with RDBMS.
You might not have full SQL standard implemented in your system, but with the support of MySQL, you can use a lot of functionalities.
All the third party apps directly contact with MySQL daemon to get the data directly.

MySQL supported Data Types

There are three types in MySQL Database
Numeric Data Types
Date and time types
String Types

Numeric Data Types in MySQL

INT
Signed INT - Range from -2147483648 to 2147483647
Unsigned INT - Range from 0 to 4294967295
TINYINT
Signed TINYINT Range from -128 to 127
Unsigned TINYINT Range from 0 to 255
SMALLINT
Signed SMALLINT Range from -32768 to 32767
Unsigned SMALLINT Range from 0 to 65535
MEDIUMINT
Signed MEDIUMINT Range from -8388608 to 8388607
Unsigned MEDIUMINT Range from 0 to 16777215
BIGINT
Signed Bigint Range from -9223372036854775808 to 9223372036854775807
Unsigned BIGINT Range from 0 to 18446744073709551615.
FLOAT(M, D)
DOUBLE (M, D)
DECIMAL (M, D)

Date and Time Types in MySQL

DATE
Format YYYY-MM-DD
Range From 1000-01-01 and 9999-12-31
DATETIME
Format YYYY-MM-DD HH:MM:SS
Range From 1000-01-01 00:00:00 and 9999-12-31 23:59:59
TIMESTAMP
Format YYYYMMDDHHMMSS
TIME
Format HH:MM:SS
YEAR(M)

String Data Type in MySQL

CHAR(M)
VARCHAR(M)
BLOB or TEXT
TINYBLOB or TINYTEXT
MEDIUMBLOB or MEDIUMTEXT
LONGBLOB or LONGTEXT
ENUM

Advantages of MySQL

MySQL is very easy to install and use. The third party GUI for the MySQL works well.

High Functionality Support

MySQL supports a lot of functionalities which professionals are expecting from RDBMS. It gives support in both direct and indirect way.

Highly Secure

MySQL has the high-security features which are advanced in level.

Scalable and Powerful

MySQL can handle a lot of data and it is also scalable.

High Speed

It gives up some standards to increase the speed and come out of the corner.

Disadvantages of MySQL

Limitations

MySQL has some limitations, and it makes some applications unable to use MySQL.

Reliability problems

MySQL is not reliable for certain functionalities such as References, auditing, and Transactions.

Slow development

MySQL is open source product, and it has a lot of complaints regarding the development. However there some database which entirely runs with MySQL and provides the value to the user( E.g Marid DB)

When should you use MySQL?

Do you need more than SQLlite then adds MySQL in your deployment process?
MySQL as a stand-alone database server with the operational freedom along with some advanced features.

Highly Secured

MySQL is highly secured and provides the security to the user data access.

Web application usages

More than 50% of websites around the world has the MySQL running its operations. Even though there are some constraints, MySQL advantages hide them.
The Scalable and easy to manage and use tool helps a lot for long time users.

Customised solutions

If you are working on specific and customized solutions then you can use MySQL since it has high configuration setting and operation modules.

When should you not use MySQL

SQL Compliant

MySQL does not implement full SQL standards so that if you need to connect with RDBMS, it will be difficult for you to switch.
MySQL is not entirely SQL compliant.

Problem in Concurrency

MySQL is performing well at reading operations, but when coming to multiple read operations, it will become problematic.

Lack of certain features

Based on the Database engine you use, MySQL will lack certain features such as full-text search.

SQLite

SQLite is the powerful library which is integrated with applications to make use of it.
SQLite is the file based database which offers various tools to handle the data.
You can easily handle the data easily with fewer constraints. It is easier than the process based database.
The specialty of SQLite is it does not use any interface(sockets, ports) to communicate with the database.
If an application requires data, it directly works with functions which make the call to the files which contains the data.
The library underlying technology is the reason behind that. The SQLite works very faster.

Storages Classes in SQLite

NULL It contains Null Value
INTEGER Signed Integer Stores up to 8 byte
REAL It holds floating point (8 - byte IEEE floating point number)
TEXT It uses the encoding technique in database(UTF-8, UTF-16BE or UTF-16LE) to store the data string
BLOB Stores the value blob of data

Affinity Type in SQLite

TEXT It stores all the data using NULL, TEXT or BLOB.
NUMERIC Holds values using all five storage classes.
INTEGER Works like a column with NUMERIC affinity
REAL Works same as a column with NUMERIC affinity
NONE Allows only one storage class

Integer

INT
INTEGER
MEDIUMINT
TINYINT
BIGINT
SMALLINT
INT2
INT8
UNSIGNED BIG INT

Text

CHARACTER(20)
VARCHAR(255)
VARYING CHARACTER(255)
TEXT
CLOB
NVARCHAR(100)
NCHAR(55)
NATIVE CHARACTER(70)

NONE

BLOB
no datatype specified

REAL

REAL
DOUBLE
DOUBLE PRECISION

FLOAT

FLOAT

NUMERIC

NUMERIC
DECIMAL(10,5)
DATE
DATETIME
BOOLEAN

SQLite Advantages

Easily Portable

Since the SQLite is file-based, the database contains the single file on the disk which makes easier to port.

Standards

The SQLite seems to be easy DB Implementation, and it uses the SQL. Here we have to note one point. Some features are omitted here but however, it has some additional features.

Good for Development and Testing

Integrating with applications during the development process is easy. Since the developers want to scale the concurrency ability, they can SQLite for that easily.
SQLite has the rich features, and it offers more than the developer's expectation with simplicity.

Disadvantages of SQLite

No User Management in SQLite

A lot of advanced database management systems supports users. This will help in making privileges to the user access to the database and tables stored there.
But the SQLite does not have the feature.

No Possibility to tinker

The SQLite is easy to tune and simple to use. But there is no possibility to tinker the SQLite for additional performance. The reason is SQLite is not complicated so that you can't tinker to gain additional performance.

When to use SQLite

Embedded Applications

The applications which require portability and do not require expansion can use SQLite.
The single user local used applications, games and the mobile applications.

Disk access replacement

The application which requires to directly read and write on the disk can benefit from SQLite. It comes with Structured Query Language and additional functionality.

Testing

Hard to test multiple applications which are using SQLite.

When not to use SQLite

Applications with Multi-User

If you are using applications and all of them have multiple users, you have to better use full featured RDBMS such as MySQL.

Applications which require frequent writing

You cannot use SQLite for the purpose of Multi-writing because the SQLite allows only one user to write at the same time.
It reduces the speed and throughput.

Conclusion

You might have gained some good knowledge about the PostgreSQL, MySQL and SQLite.
Now you can quickly figure out which one is best for your purpose based on the analysis we have seen above. Now you would understand the SQLite vs MySQL performance.
If you got any question, then kindly leave a comment below, and if you have enjoyed the article..
..then kindly share the article on your preferred social media!