MYSQL NOTES
MYSQL NOTES
—-------------------
Frontend:
-------------------
Backend
-----------------
2. It stores and arranges the data and also make sure every thing on the
client side of the website works fine
API:
---------
Data
------------
Data is a piece of information that can be translated into form for efficient
moment is called for data.
Database:
-----------------
Types of databases
----------------------------------
DBMS
----------------
RDBMS
---------------
Examples of popular RDBMS include Oracle Database, MySQL, Microsoft SQL Server,
PostgreSQL, and SQLite. RDBMSs are widely used in various applications and
industries where structured and relational data management is essential for
efficient data storage, retrieval, and manipulation.
NoSQL DBMS
--------------------
----------------------------------
Ex: db4o
4) graph dbms:
-------------------------
-----------------------------------
6) Distributed DBMS
------------------------
Ex:
----
Amazon Aurora.
7) Columnar Databases:
---------------------------------------
----------------------------
It optimize for handlling time stamped data such as financial market data.
XML DBMS
-----------------
Ex: Basex.
-----------------------------------------------------------
The main differences between DBMS and RDBMS are given below:
No DBMS RDBMS
.
1) DBMS applications store RDBMS applications store data in a
data as file. tabular form.
4) DBMS does not apply any RDBMS defines the integrity constraint
security with regards to for the purpose of ACID (Atomocity,
data manipulation. Consistency, Isolation and Durability)
property.
Storage Areas
To store this Data, we required Storage Areas. There are 2 types of Storage
Areas.
File Systems:
File Systems are best suitable to store very less Amount of Information.
Limitations:
Databases:
Limitations of Databases:
-------------------------------------
1. Data retrival
2. Data security.
3. Data recovery
4. Data manage
5. Data organization
6. Concurrency control
Sql operations
-----------------------
1. Select
2. Insert
3. Update
4. Delete
5. Create
6. Alter
7. Drop
8. Truncate
SQL is A case sensitive.
In sql the keywords are Written in Upper Case for better readabilty.
SQL IDENTIFIERS:-
Allowed chars---A-Z/a-z/0-9/$,_,#.
MySQL Commands
SHOW DATABASES;
Use MyDataBase;
EXIT;
QUIT;
-----------------------------------------------------------------------------------------
It is similar to nexit
Introduction to TABLE’s
---------------------------------------------------------------------------
Table is a fundmental structure and used to organize and stores data.
Cell:
------
Primary key:
--------------------
Table name:
--------------------------
Data Types
-----------------------
Data Types define the kind of data that can be stored in a column.
The type of values are fixed or variable
1.Numeric datatypes
--------------------------------------
Minimum
Storage Minimum Value Value Maximum Maximum
Type (Bytes) Signed Unsigned Value Signed Value Unsigned
MEDIUMIN
T 3 -8388608 0 8388607 16777215
63 63 64
BIGINT 8 -2 0 2 -1 2 -1
Float(m,d):-
-----------------
It is a floating point number that cannot be unsigned
Ex: 12345678.90;
It cannot be unsigned.
Bit(m):-it is used to store the bit values into the table column.
Here m defines number of bits per value that has a range of 1 to 64.
Bool
-----------
------------------
Description
DATE 'YYYY-MM-DD' '1000-01-01' Stores date
to '9999-12- values
31 without any
time
component.
TIME 'HH:MM' '-838:59:59' Stores time
to '838:59:59' values
without any
date
component.
DATETIME 'YYYY-MM-DD '1000-01-01 Stores both
HH:MM' 00:00:00' to date and time
'9999-12-31 values.
23:59:59'
TIMESTAMP 'YYYY-MM-DD '1970-01-01 Stores date
HH:MM' 00:00:01' UTC and time
to '2038-01- values and is
19 03:14:07' generally
UTC used for
recording
timestamps of
when a row
was inserted
or updated.
YEAR YYYY 1901 to 2155 Stores a year
(four-digit value.
format)
----------------------------------------------
----------------------------------------------------
----------------------------------------------
Froma user.
------------------------------------------------------
Current transaction.
To other transactions.
Transactions
Rollback.
—-----------------------------
1. Select:- this commnad is used to retive data from one or more tables
in a database
Day-3:
-----------
SHOW DATABASES;
Create
EMP_ID CHAR(5),
FNAME VARCHAR(50),
LNAME VARCHAR(50),
AGE INT,
DOJ DATE,
ADDRESS TINYTEXT,
DEPT VARCHAR(20)
);
SHOW TABLES;
ALTER
-- Add a new column named PFID of type INT after the ADDRESS column
ALTER TABLE EMPLOYEES ADD PFID INT AFTER ADDRESS;
Rename
-- Rename the LNAME column to LASTNAME and change its data type to
VARCHAR(60)
DESC EMPLOYEES;
drop
-- Drop the PFID column from the EMPLOYEES table
DESC EMPLOYEES;
DESC EMPLOYEES;
-- Show tables in the current database to verify the table name change
SHOW TABLES;
Insert
VALUES
-- Method 2: Insert values into specific columns (not all columns need to be
specified)
VALUES
SELECT 'E005', 'William', 'Taylor', 40, '2023-05-15', '567 Cedar St', 'Sales'
UNION ALL
SELECT*FROM CODEGNAN_EMP;
-- Method 4: Insert values using SET syntax (useful for updating existing rows
or inserting multiple rows)
SELECT*FROM CODEGNAN_EMP;
-- Method 5: Insert multiple rows in a single INSERT statement
VALUES
SELECT*FROM CODEGNAN_EMP;
DELETE
------------
UPDATE
-------------
UPDATE CODEGNAN_EMP
LOCK
---------
START TRANSACTION;--
-- Select the record for EMP_ID 'E001' and lock it for update
-- At this point, the record with EMP_ID 'E001' is locked for update within this
transaction.
-- Other transactions will be blocked from updating this record until the
current transaction is committed or rolled back.
COMMIT;
ROLLBACK
-------------
START TRANSACTION;
-- Attempt to update the AGE column (example update that might fail)
ROLLBACK;
EXAMPLE2
--------------
START TRANSACTION;
ROLLBACK;
-- Select all records from CODEGNAN_EMP to verify changes were rolled back
SAVEPOINT
--------------
START TRANSACTION;
-- Update the AGE column for EMP_ID 'E001'
SAVEPOINT update_savepoint;
COMMIT;
MySQL OPERATORS
===================
1. Arithmetic operators
use mydb;
customer_id INT,
order_date DATE,
shipping_address VARCHAR(255),
payment_status VARCHAR(20),
delivery_status VARCHAR(20)
);
INSERT INTO orders (order_id, customer_id, order_date,
total_amount, shipping_address, payment_status, delivery_status)
VALUES
(3, 103, '2023-02-15', 75.25, '789 Oak St, Bigcity, USA', 'Pending',
'In Progress'),
select*from orders;
UPDATE orders
SET total_amount = total_amount % 100
WHERE order_id = 3;
Comparison operators
----------------------------------
Examples
--------------
-- Not equal to (!= or <>): Select orders where payment_status is not equal
to 'Paid'
-- Less than (<): Select orders where total_amount is less than 100
-- Less than or equal to (<=): Select orders where total_amount is less than
or equal to 150.00
-- Greater than (>): Select orders where total_amount is greater than 300
SELECT * FROM orders WHERE total_amount > 300;
-- NOT BETWEEN: Select orders where total_amount is not between 150 and
250
SELECT * FROM orders WHERE total_amount NOT BETWEEN 150 AND 250;
-- AND operator: Select orders where total_amount is between 100 and 200
AND payment_status is 'Paid'
SELECT * FROM orders WHERE total_amount BETWEEN 100 AND 200 AND
payment_status = 'Paid';
In operators
---------------------
Operator Example
Description
Examples
------------
Null operators
--------------------------
Operator Example
Description
Examples
----------------
Logical operators
------------------------
Examples
----------------------
-- AND operator: Select orders where total_amount is between 100 and 200
AND payment_status is 'Paid'