SQL CheatSheet for Developers
Introduction-What-is-SQL?
To get introduced to SQL, we first need to know about Databases and Database Management Systems(DBMS). Data is basically a collection of facts related to some object. A Database is a collection of small units of data arranged in a systematic manner. A Relational Database Management System is a collection of tools that allows the users to manipulate, organize and visualize the contents of a database while following some standard rules that facilitate fast response between the database and the user side.
After getting introduced to the concept of data, databases and DBMS/RDBMS, we can finally learn about SQL. SQL or Structured Query Language is basically the language that we (the user) use to communicate with the Databases and get our required interpretation of data out of it. It is used for storing, manipulating and retrieving data out of a database.
SQL-Features?
The following functionalities can be performed on a database using SQL:
Create a DatabaseorDelete a DatabaseCreate a tableorAlter a tableorDelete a tablefrom a DatabaseSELECT data from tablesINSERT data into tablesUPDATE data in tablesDELETE data from tablesCreate Views in the databaseExecute various aggregate functions
Basic SQL
Create Database and drop database
| COMMAND | SYNTAX | DESCRIPTION |
|---|---|---|
| CREATE DATABASE | CREATE DATABASE database-name | Used to create new SQL database in the server |
| DROP DATABASE | CREATE DATABASE database-name | Used to drop the existing database |
String Datatype
The table below lists all the String datatype available in SQL, along with their descriptions:
| Datatype | Description |
|---|---|
| CHAR(size) | A fixed-length string containing numbers, letters or special characters. Length may vary from 0-255. |
| VARCHAR(size) | Variable-length string where the length may vary from 0-65535. Similar to CHAR. |
| TEXT(size) | Can contain a string of size up to 65536 bytes. |
| TINY TEXT | Can contain a string of up to 255 characters. |
| MEDIUM TEXT | Can contain a string of up to 16777215 characters. |
| LONG TEXT | Can contain a string of up to 4294967295 characters. |
| BINARY(size) | Similar to CHAR() but stores binary byte strings. |
| VARBINARY(size) | Similar to VARCHAR() but stores binary byte strings. |
| BLOB(size) | Holds blobs up to 65536 bytes. |
| TINYBLOB | It is used for Binary Large Objects and has a maximum size of 255bytes. |
| MEDIUMBLOB | Holds blobs up to 16777215 bytes. |
| LONGBLOB | Holds blobs upto 4294967295 bytes. |
| ENUM(val1,val2) | String object that can have only 1 possible value from a list of size at most 65536 values in an ENUM list. If no value is inserted, a blank value is inserted. |
| SET(val1,val2,…) | String object with 0 or more values, chosen from a list of possible values with a maximum limit of 64 values. |
Numeric Datatype:
The table below lists all the Numeric Datatype in SQL along with their descriptions:
| Datatype | Description |
|---|---|
| BIT(size) | Bit-value type, where size varies from 1 to 64. Default value: 1 |
| INT(size) | Integer with values in the signed range of -2147483648 to 2147483647 and values in the unsigned range of 0 to 4294967295. |
| TINYINT(size) | Integer with values in the signed range of -128 to 127 and values in the unsigned range of 0 to 255. |
| SMALLINT(size) | Integer with values in the signed range of -32768 to 32767 and values in the unsigned range of 0 to 65535. |
| MEDIUMINT(size) | Integer with values in the signed range of -8388608 to 8388607 and values in the unsigned range of 0 to 16777215. |
| BIGINT(size) | Integer with values in the signed range of 9223372036854775808 to 9223372036854775807 and values in the unsigned range of 0 to 18446744073709551615. |
| BOOLEAN | Boolean values where 0 is considered as FALSE and non-zero values are considered TRUE. |
| FLOAT (p) | The floating-point number is stored. If the precision parameter is set between 0 to 24, the type is FLOAT() else if it lies between 25 to 53, the datatype is DOUBLE(). |
| DECIMAL(size,d) | Decimal number with a number of digits before decimal place set by size parameter, and a number of digits after the decimal point set by d parameter. Default values: size = 10, d = 10. Maximum Values: size = 65, d = 30. |