SQL Notes Updated 01 April 2025
SQL Notes Updated 01 April 2025
~~~~~~~~~~~~~~~~~
Suraj T -
Mobile : 7603893398
E-mail : [email protected]
-----------------------------------------------------------------------------------
---
Data
~~~~~~~~~~~
Collection of information
Database
~~~~~~~~~~~~~
Collection of related information stored at one place.
Table
~~~~~~
Table consists of rows and columns
Rows
~~~~~~~~
Horizontal ones
Columns
~~~~~~~~~~
Vertical ones
1 Row = 1 Record
Each data is stored in 1 Record
DBMS
~~~~~~~~~~~
Database Management System
Examples of DBMS
~~~~~~~~~~~~~~~~~~
1. MySQL Open Source Oracle (Sun Microsystems)
2. SQL Server Microsoft
3. Oracle Oracle
4. PostgresSQL Open Source
CRUD Operations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C Create CREATE
R Read SELECT
U Update UPDATE
D Delete DELETE / DROP
Features of DBMS
~~~~~~~~~~~~~~~~~
-- Interface between Database and User
-- Software to store , retrieve, define and manage data in the database.
-- Easy CRUD Operations.
-- Takes care of authentication(security), authorization, concurrency, logging,
backup, optimization etc.
-- should support connection using other applications and other programming
languages.
RDBMS
~~~~~~~~~~~~~
Relational Database Management Systems
Examples of RDBMS
~~~~~~~~~~~~~~~~~~
1. MySQL Open Source Oracle (Sun Microsystems)
2. SQL Server Microsoft
3. Oracle Oracle
4. PostgresSQL Open Source
Datatypes in MySQL
~~~~~~~~~~~~~~~~~~~~~
20 number
"twenty" string
'twenty' string
String
~~~~~~~~~
set of characters
125 ASCII Characters
either double quotes ""
or single quotes ''
Examples:
~~~~~~~~~~
"abcd!@#$"
'sql'
"mysql"
"20"
Example
Text
~~~~~
-----------------------------------------------------------------------------------
--------------------------------------------------------------
Text Type Maximum number of bytes
-----------------------------------------------------------------------------------
----------------------------------------------------------------
TINYTEXT 255
TEXT 65,535
MEDIUMTEXT 16,777,215
LONGTEXT 4,294,967,295
-----------------------------------------------------------------------------------
-----------------------------------------------------------------
BLOB Type
~~~~~~~~~~~~~
TINYBLOB
BLOB
MEDIUMBLOB
LONGBLOB
-----------------------------------------------------------------------------------
-------------------------------------------
Numbers / Numericals
~~~~~~~~~~~~~~~~~~~~~~~
1. Whole Number 1, -1
2. Decimal Number 1.0, -1.0
Whole Number
~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
-------------------------------------------------------------
Whole Number Type Signed Range
-----------------------------------------------------------------------------------
----------------------------------------------------------------
TINYINT -128 to 127
SMALLINT -32,768 to 32,767
MEDIUMINT -8,388,608 to 8,388,607
INT -2,147,483,648 to 2,147,483,647
BIGINT -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
-----------------------------------------------------------------------------------
----------------------------------------------------------------
specifying size of digits for INT datatype is optional INT(10) OR INT
Decimal Number
~~~~~~~~~~~~~~~~~
DECIMAL(TOTAL_DIGITS, TOTAL_DECIMAL_DIGIT)
Example
~~~~~~~~~~~~~~~~~
123.4 => DECIMAL(4, 1)
123.45 => DECIMAL(5, 2)
12345.6789 => DECIMAL(9, 4)
123456789.0 => DECIMAL(9, 0)
12345.10 => DECIMAL(7, 2)
Date
~~~~~~~~~
-----------------------------------------------------------------------------------
----------------------------------------------------
Date Datatype Default Format Allowable Values
-----------------------------------------------------------------------------------
----------------------------------------------------
DATE YYYY-MM-DD 1000-01-01 to 9999-12-31
DATETIME YYYY-MM-DD HH:MI:SS 1000-01-01 00:00:00 to 9999-12-31 23:59:59
TIMESTAMP YYYY-MM-DD HH:MI:SS 1970-01-01 00:00:00 to 2037-12-31 23:59:59
YEAR YYYY 1901 to 2155
TIME HHH:MI:SS -838:59:59 to 838:59:59
-----------------------------------------------------------------------------------
---------------------------------------------------
Example
~~~~~~~~~~~~~
DATETIME Custom Date and Time / User Specified Date and Time
e.g. Date of Birth, Train Time, Bus Time
-----------------------------------------------------------------------------------
-------------------------------
-- comments in mysql
-- two hyphen symbols then space then comments
SHOW DATABASES;
-- delete database monkey in mysql server
DROP DATABASE monkey;
SHOW DATABASES;
SHOW TABLES;
-- to delete table
DROP TABLE student;
SHOW TABLES;
-- it will not throw error if the table is not available. it will show only warning
DROP TABLE IF EXISTS student;
-- column having NOT NULL and UNIQUE will automatically become PRIMARY KEY
CREATE TABLE student (
name VARCHAR(50) NOT NULL,
city VARCHAR(50) DEFAULT "bangalore",
aadhaar BIGINT NOT NULL UNIQUE
);
DESCRIBE student;
DROP TABLE student;
-----------------------------------------------------------------------------------
-------------------------------------------------
-- TASK 1
-- create customer table
+---------------+-----------------+----------+----------+-------------+---------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-----------------+----------+----------+-------------+---------+
| id | int | NO | PRI | NULL |
|
| name | varchar(50) | NO | | NULL | |
| mobile | int | YES | UNI | NULL |
|
| gender | char(1) | YES | | M |
|
+--------------+------------------+---------+----------+--------------+--------+
-----------------------------------------------------------------------------------
-------------------------------------------
-- TASK 2
-- create person table
+-------------+------------------+-------+-------+---------------+---------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+-------+-------+---------------+---------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| aadhaar | bigint | YES | UNI | NULL | |
| city | varchar(50) | YES | | mysore | |
| state | varchar(50) | YES | | karnataka | |
| country | varchar(50) | YES | | india | |
+------------+------------------+--------+-------+----------------+-------+
-----------------------------------------------------------------------------------
------------------------------------------------------
-- ALTER -- RENAME
-- add new column
-- change column datatype
-- change column name
-- delete column
-- change table name
SHOW TABLES;
-- delete column
ALTER TABLE student
DROP COLUMN student_state;
DESCRIBE student;
-----------------------------------------------------------------------------------
-----------------------------------------------
-- TRUNCATE
~~~~~~~~~~~~~~~~
-- Only the data or all the records will be deleted and Table structure will not be
deleted
-----------------------------------------------------------------------------------
-----------------------------------------------------------
-- DML - Data Manipulation Language
-- INSERT
-- UPDATE
-- DELETE
-----------------------------------------------------------------------------------
-----------------------------------------------------
-- INSERT
~~~~~~~~~~~~~
SELECT *
FROM student;
SELECT *
FROM student;
SELECT *
FROM student;
-- ORDER BY
-- Default is ascending
-- ASC ascending
-- DESC descending
-- Ascending Order
SELECT *
FROM student
ORDER BY name;
-- Descending Order
SELECT *
FROM student
ORDER BY name DESC;
-- Constraints in MYSQL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- NOT NULL
-- AUTO_INCREMENT
-- UNIQUE
-- DEFAULT
-- CHECK
-- PRIMARY KEY
-- FOREIGN KEY
----------------------------------------------------------------
-- clauses
-- WHERE clause
-- filter the rows
Mathematical Operators
~~~~~~~~~~~~~~~~~~~~~~~~~
< Less Than
> Greater Than
<= Less Than Or Equal To
>= Greater Than Or Equal To
= Equal To
<> Not Equal To
!= Not Equal To
Logical Operators
~~~~~~~~~~~~~~~~~~~~~
AND Logical And Operator
OR Logical Or Operator
NOT Logical Not Operator
-- equal to operator
SELECT *
FROM student
WHERE salary = 50000;
SELECT *
FROM student
WHERE city = "shimoga";
-- IN
-- NOT IN
-- search for students who are not from bangalore and mysore
SELECT *
FROM student
WHERE city NOT IN ("bangalore", "mysore")
ORDER BY city, name;
-- NOT
SELECT NOT 0;
SELECT NOT 1;
TASK
-- show all the students who are not from bangalore using NOT keyword and without
using IN keyword
SELECT *
FROM student
WHERE NOT city = "bangalore";
-- empty set
SELECT *
FROM student
WHERE city = "bangalore" AND city = "mysore";
-- empty set
SELECT *
FROM student
WHERE name = "naveed" AND city = "bangalore";
-- Logigal Operator OR
-- left condition OR right condition
-- true OR true => output
-- true OR false => output
-- false OR true => output
-- false OR false => empty set