{"id":77,"date":"2017-03-09T06:29:25","date_gmt":"2017-03-09T06:29:25","guid":{"rendered":"http:\/\/abhiandroid.com\/database\/?p=77"},"modified":"2017-03-09T06:29:45","modified_gmt":"2017-03-09T06:29:45","slug":"data-type-command-sqlite","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/database\/data-type-command-sqlite.html","title":{"rendered":"Data Type And Commands In SQLite"},"content":{"rendered":"<p><strong>Data Type in SQLite:<\/strong> It basically specify the kind of data that object will store in it i.e the value it can take inside for storage. There are different type or data like numeric, alphabetic, alphanumeric etc. So to store all this type of data different data types are there. Also the data types defines the value to be stored or the operations that can be carried out over the data.<\/p>\n<p><strong>SQLite database is dynamic type<\/strong> i.e the type of value to be stored in the container is decided by the value itself. In other words SQLite uses static and rigid typing i.e allows us to define data as per value.<\/p>\n<p>Let&#8217;s take example to understand\u00a0the point more clear:<\/p>\n<pre>create table demo_table( Number int(15), Name varchar(50) );\r\ninsert into demo_table (Number , Name) values ('EA', '102');<\/pre>\n<p>Here in the above example we define datatype of Number\u00a0as Integer and Name\u00a0as Varchar. In second line we inserted character value in Number and Number in Name\u00a0using insert command. This query will run without any error because\u00a0SQLite engine allows rigid typing usually try to automatically convert values to the appropriate datatype.<\/p>\n<hr \/>\n<h4><strong>Storage Classes<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Null :\u00a0<\/strong><\/span>The value is a NULL value.<br \/>\n<strong><span style=\"color: #008000;\">Integer :\u00a0<\/span><\/strong>The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.<br \/>\n<strong><span style=\"color: #008000;\">Real :\u00a0<\/span><\/strong>The value is a floating point value, stored as an 8-byte IEEE floating point number.<br \/>\n<strong><span style=\"color: #008000;\">Text :\u00a0<\/span><\/strong>The value is a text string, stored using the database encoding (UTF- 8, UTF-16BE or UTF-16LE)<br \/>\n<strong><span style=\"color: #008000;\">Blob :\u00a0<\/span><\/strong>The value is a blob of data, stored exactly as it was input.<\/p>\n<p>We have\u00a0used above classes in commands of this article.<\/p>\n<hr \/>\n<h4><strong>SQLite Affinity Type:<\/strong><\/h4>\n<p>In order to maximize compatibility between SQLite and other database engines, SQLite supports the concept of &#8220;type affinity&#8221; on columns. Type affinity is the recommended type for data stored in that column. Any column can store any type of data. The preferred storage class for a column is called its &#8220;affinity&#8221;.<\/p>\n<p>Each column has assigned following type affinities:<\/p>\n<ul>\n<li>TEXT<\/li>\n<li>NUMERIC<\/li>\n<li>INTEGER<\/li>\n<li>REAL<\/li>\n<li>NONE<\/li>\n<\/ul>\n<h4><strong>Affinity Type Names:<\/strong><\/h4>\n<table border=\"1\" width=\"600\">\n<tbody>\n<tr>\n<th style=\"text-align: center;\"><span style=\"color: #008000;\">DATA<\/span><\/th>\n<th style=\"text-align: center;\"><span style=\"color: #008000;\">TYPE AFFINITY<\/span><\/th>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>INT<\/td>\n<td rowspan=\"9\"><strong>INTEGER<\/strong><\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>INTEGER<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>TINYINT<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>SMALLINT<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>MEDIUMINT<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>BIGINT<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>UNSIGNED BIGINT<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>INT2<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>INT8<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>CHARACTER(20)<\/td>\n<td rowspan=\"8\"><strong>TEXT<\/strong><\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>VARCHAR(225)<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>VARYING CHARACTER(225)<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>NCHAR(55)<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>NATIVE CHARACTER(70)<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>NVARCHAR(100)<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>TEXT<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>CLOB<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>BLOB<\/td>\n<td><strong>BLOB<\/strong><\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>REAL<\/td>\n<td rowspan=\"4\"><strong>REAL<\/strong><\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>DOUBLE<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>DOUBLE PRECISION<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>FLOAT<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>NUMERIC<\/td>\n<td rowspan=\"5\"><strong>NUMERIC<\/strong><\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>DECIMAL(10,5)<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>BOOLEAN<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>DATE<\/td>\n<\/tr>\n<tr style=\"text-align: center;\">\n<td>DATETIME<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h4><strong>SQLite Commands<\/strong><\/h4>\n<p>The SQLite commands are similar to that of SQL. SQLite works as Relational Database Management System. Like in other relational database the request is send from client to the database server, further the request is processed. But here there&#8217;s nothing like that as it as self-contained and serverless.<\/p>\n<h4><strong>SQLite Commands are categorized as<\/strong><\/h4>\n<p>There are 3 main divisions in this, but before this let&#8217;s know about how to create a database in SQLite.<\/p>\n<p><strong>Create Database:<\/strong> Database is a container which contain tables , views etc.<\/p>\n<p><span style=\"color: #008000;\"><strong>.open<\/strong><\/span> : User can just define database name with .open command it will open the existing database or create a new if database does not exist.<\/p>\n<pre>SQLite version 3.16.2 2017-01-06 16:32:41\r\nEnter \".help\" for usage hints.\r\nConnected to a transient in-memory database.\r\nUse \".open FILENAME\" to reopen on a persistent database.\r\nsqlite&gt; .open new_Database               \r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">1) DDL Commands :<\/span><\/strong><br \/>\nDDL stands for Data Definition Language. This is used to define the structure of the database. It include commands like create, alter and drop.<br \/>\n<strong>CREATE :<\/strong> This command is used to create a table, index, trigger, view or other database objects. etc.<br \/>\n<strong>DROP :<\/strong> This is basically to drop the structure of the table, view etc. But user can not drop a column in SQLite.<br \/>\n<strong>ALTER :<\/strong> This command allow us to alter or modify the existing structure like rename table, add column etc but cannot alter a column.<\/p>\n<p><strong><span style=\"color: #008000;\">2) DML Commands :<\/span><\/strong><\/p>\n<p>DML stands for Data Manipulation Language which basically manipulates the data\/record like insert , update or delete.<br \/>\n<strong>INSERT :<\/strong> This command is used to insert a record in the table.<br \/>\n<strong>UPDATE :<\/strong> It update the table record.<br \/>\n<strong>DELETE :<\/strong> It delete the table record.<\/p>\n<p><strong><span style=\"color: #008000;\">3) DQL Commands :<\/span><\/strong><br \/>\nDQL stands for Data Query Language\u00a0which basically is used to retrieve the records in the table.<br \/>\n<strong>SELECT :<\/strong> It allow us to retrieve the records.<\/p>\n<hr \/>\n<h4><strong>Now let\u2019s we explain all these commands one by one in detail:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>CREATE TABLE :<\/strong> <\/span>This command is used to create database objects like tables. User can create table in any database, by defining table name and its columns with specifications(datatypes).<\/p>\n<pre>create table &lt; Table_Name &gt;\r\n(\r\n column 1 datatype, \r\n column 2 datatype,\r\n .... \r\ncolumn n datatype \r\n);\r\n<\/pre>\n<p><strong>Example for Creating a table :<\/strong><\/p>\n<pre>create table student\r\n(\r\nRoll_no int,\r\nName text,\r\nFees real,\r\naddress varchar\r\n);<\/pre>\n<p><span style=\"color: #008000;\"><strong>ALTER TABLE :<\/strong> <\/span>This command is used to alter the database objects like tables. User can alter table add columns variants and\u00a0can rename the table.<\/p>\n<pre>alter table &lt; Table_Name &gt;\r\nadd new_column _name column_defination;\r\n<\/pre>\n<p><strong>Example of alter table : Adding new column<\/strong><\/p>\n<pre> alter table student add Father_name varchar(50);<\/pre>\n<p><strong>Example to alter table : Renaming Table<\/strong><\/p>\n<pre> alter table student rename to stu;<\/pre>\n<p><span style=\"color: #008000;\"><strong>DROP TABLE :<\/strong> <\/span>This command is used to drop the table structure. With the table structure associated data, views, index , constraints etc are dropped.<\/p>\n<pre>drop table &lt; Table_Name &gt;;\r\n\r\n<\/pre>\n<p><strong>Example to drop table<\/strong><\/p>\n<pre> drop table student;<\/pre>\n<p><span style=\"color: #008000;\"><strong>INSERT:<\/strong> <\/span>This command is used to insert record in the table. User define the table name and the data to be specified in the table.<\/p>\n<pre>insert into &lt; Table_Name &gt;\r\nvalues(value 1, value 2,... value n);\r\n<\/pre>\n<p><strong>Example to insert record in table<\/strong><\/p>\n<pre>insert into student values ('12','Rohit','2500','Karnal','Shyam Lal');<\/pre>\n<p><span style=\"color: #008000;\"><strong>DELETE:<\/strong> <\/span>This command is used to delete record in the table. User define the table name and the data to be deleted from the table.<\/p>\n<pre>delete table  &lt; Table_Name &gt;\r\nwhere &lt;condition&gt;;\r\n<\/pre>\n<p><strong>Example to delete records in table<\/strong><\/p>\n<pre>delete table student;<\/pre>\n<pre> delete table student where Roll_no='12';<\/pre>\n<p><span style=\"color: #008000;\"><strong>UPDATE:<\/strong> <\/span>This command is used to update the record in the table. User define the table name and the data to be modified from the table with specified condition.<\/p>\n<pre>update table  &lt; Table_Name &gt;\r\nset column1 = value1 ,  column2 = value2 ......  columnN = valueN\r\nwhere &lt;condition&gt;;\r\n<\/pre>\n<p><strong>Example to update records in table<\/strong><\/p>\n<pre> update student set Fees = '30000' where Roll_no='1';<\/pre>\n<p><span style=\"color: #008000;\"><strong>SELECT :<\/strong> <\/span>This command is used to retrieve records from the table. User can retrieve records as per your requirement from multiple tables by applying conditions.<\/p>\n<pre>select column1, column2, ...columnN from table  &lt; Table_Name &gt;\r\n<\/pre>\n<p><strong>Example to update records in table<\/strong><\/p>\n<pre> select Name, Roll_No from student;<\/pre>\n<p>To retrieve all data use:<\/p>\n<pre>select * from student;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Data Type in SQLite: It basically specify the kind of data that object will store in it i.e the value it can take inside for storage. There are different type or data like numeric, alphabetic, alphanumeric etc. So to store all this type of data different data types are there. Also the data types defines &hellip; <a href=\"https:\/\/abhiandroid.com\/database\/data-type-command-sqlite.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Data Type And Commands In SQLite<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-archive"],"acf":[],"psp_head":"<title>Data Type And Commands In SQLite \u2013 Android Database Tutorial In Android Studio: Store Your Data<\/title>\r\n<meta name=\"description\" content=\"In this tutorial you will learn different data types and commands used in SQLite with example.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/database\/data-type-command-sqlite.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/database\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}