SQL Languages
SQL languages
DDL is short name of Data Definition Language, which deals with database schemas and descriptions, of how the data should reside in the database.
DML is short name of Data Manipulation Language which deals with data manipulation, and includes most common SQL statements such SELECT, INSERT, UPDATE, DELETE etc, and it is used to store, modify, retrieve, delete and update data in database.
DCL is short name of Data Control Language which includes commands such as GRANT, and mostly concerned with rights, permissions and other controls of the database system.
Datatypes
Text types
Data type | Description |
---|---|
CHAR(size) | Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. Can store up to 255 characters |
VARCHAR(size) | Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis. Can store up to 255 characters. Note: If you put a greater value than 255 it will be converted to a TEXT type |
TINYTEXT | Holds a string with a maximum length of 255 characters |
TEXT | Holds a string with a maximum length of 65,535 characters |
BLOB | For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data |
MEDIUMTEXT | Holds a string with a maximum length of 16,777,215 characters |
MEDIUMBLOB | For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data |
LONGTEXT | Holds a string with a maximum length of 4,294,967,295 characters |
LONGBLOB | For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data |
ENUM(x,y,z,etc.) | Let you enter a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted.Note: The values are sorted in the order you enter them.You enter the possible values in this format: ENUM(‘X’,’Y’,’Z’) |
SET | Similar to ENUM except that SET may contain up to 64 list items and can store more than one choice |
Number types
Data type | Description |
---|---|
TINYINT(size) | -128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits may be specified in parenthesis |
SMALLINT(size) | -32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number of digits may be specified in parenthesis |
MEDIUMINT(size) | -8388608 to 8388607 normal. 0 to 16777215 UNSIGNED*. The maximum number of digits may be specified in parenthesis |
INT(size) | -2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*. The maximum number of digits may be specified in parenthesis |
BIGINT(size) | -9223372036854775808 to 9223372036854775807 normal. 0 to 18446744073709551615 UNSIGNED*. The maximum number of digits may be specified in parenthesis |
FLOAT(size,d) | A small number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter |
DOUBLE(size,d) | A large number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter |
DECIMAL(size,d) | A DOUBLE stored as a string , allowing for a fixed decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter |
Date types
Data type | Description |
---|---|
DATE() | A date. Format: YYYY-MM-DDNote: The supported range is from ‘1000-01-01’ to ‘9999-12-31’ |
DATETIME() | *A date and time combination. Format: YYYY-MM-DD HH:MI:SSNote: The supported range is from ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’ |
TIMESTAMP() | *A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch (‘1970-01-01 00:00:00’ UTC). Format: YYYY-MM-DD HH:MI:SSNote: The supported range is from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-09 03:14:07’ UTC |
TIME() | A time. Format: HH:MI:SSNote: The supported range is from ‘-838:59:59’ to ‘838:59:59’ |
YEAR() | A year in two-digit or four-digit format.Note: Values allowed in four-digit format: 1901 to 2155. Values allowed in two-digit format: 70 to 69, representing years from 1970 to 2069 |
Database
Create
1 |
|
Drop
1 |
|
Table
Check if not exit and create
1 |
|
Alter Table
Primary Key
1 |
|
Foreign Key
1 |
|
Constraint
1 |
|
Insert
Selected fields
1 |
|
All fields
1 |
|
Update
Update by condition
1 |
|
Delete
All
1 |
|
Condition
1 |
|
Index
Create
1 |
|
Disable
1 |
|
Rebuild
1 |
|
Reorganize
1 |
|
Drop
1 |
|
Alter
1 |
|
#Type
create
1 |
|
#Login
change password
1 |
|
#User
create
1 |
|
drop
1 |
|
Grant/ Revoke
Available permissions: CREATE DEFAULT, CREATE FUNCTION, CREATE
PROCEDURE, CREATE ROLE, CREATE TABLE, CREATE TYPE, CREATE VIEW,
DELETE, EXECUTE, INSERT, SELECT, UPDATE
Grant rights
1 |
|
Grant refrences
1 |
|
Revoke
1 |
|
#Role
Create
1 |
|
Add member
1 |
|
#Grant Role
Grant rights
1 |
|
#View
Create detailed
1 |
|
Create simple
1 |
|
Create advanced
1 |
|
#Select
Basic
1 |
|
Condition
1 |
|
Order
1 |
|
Distinct
1 |
|
And Condition
1 |
|
Between
1 |
|
Like
1 |
|
Count
1 |
|
Having and without join
1 |
|
#Join the hard way
Inner Join
1 |
|
Multi Join
1 |
|
#Join the right way
Inner Equi Key Joining
1 |
|
Multi Inner Equi Key Joining
1 |
|
#About Joins
1 |
|
Join tables -> Joining two tables together in a query output. The third line is important because it shows how the two tables are related (in this case it is their key values).
1 |
|
LEFT/RIGHT OUTER JOIN -> Takes the table left of the word ‘LEFT’ or ‘RIGHT’ (in this case customers) and joins it regardless of whether it has any values or not. So the above statement shows all users/customers, even if they aren’t selling anything.
#Select with Subqueries
Select max and min values
1 |
|
Select with query in condition
1 |
|
Select query as value
1 |
|
#Union
Unify two result sets with a condition
1 |
|
#Transactions
Run a transaction
1 |
|
#Function
A function can be called from inside a statement just like any other function and can return a scalar value.
Create - Get value from table
1 |
|
Drop
1 |
|
Create - With parameters
1 |
|
#Procedure
Stored procedures are stored as precompilated code (stored routine) and called by the programmer wherever it wants to fire. Stored procedure can return value(s).
Create and execute
1 |
|
#Variables
Declare
1 |
|
#Trigger
Triggers are named database objects fired automatically when insert, delete, update (or other event) occurred, there can be no explicit invocation. Trigger can not return any data.
Create simple
1 |
|
Drop
1 |
|
Checks the referential integrity
1 |
|
Replaces on delete no action
1 |
|
Replaces on update cascade
1 |
|
Replaces on delete cascade
1 |
|
Reference: https://gist.github.com/janikvonrotz/6e27788f662fcdbba3fb