-
Some Basic Terminologies
-
DBMS
- Data Base Management System -> is used to control the Data Base.
-
Data Base
- It's an electronic way to hold (store) information/data
basically they are classified into
relational and non relational.
Relational vs Non Relational DBs
Relational DBs are organized in tables and there is some sort of relation between a table and another (MySQL, MSSQL, Oracle, ..)
Non Relational DB : also referred to as NoSQL DBs are not organized in tables like
-
Tables
- A unit made up of some columns as headers and rows to store actual data
-
SQL
- Structured Query Language is a language used for querying databases
-
DDL Commands -> Data Definition Language
-
CREATE
-
DATABASE
- CREATE DATABASE DBName ;
-
TABLE
- CREATE TABLE TABLEName (
column1 datatype [options],
column2 datatype [options],
column3 datatype [options],
......
) ;
-
DROP
-
DATABASE
- DROP DATABASE DBName ;
-
TABLE
- DROP TABLE TABLEName ;
-
ALTER
- DATABASE PROPERTY
- TABLE
-
DML Commands -> Data Manipulation Language
-
Insert
- INSERT INTO TABLEName (`col1` , `col2`)
VALUES ( `value1`, `value2` );
or you can use this way to
INSERT INTO TABLEName SET `col1` = `value1` , `col2` = `value2` ;
-
Update
- UPDATE TABLEName SET `col1` = `value1` ,
`col2` = `value2` WHERE `col` = `value`;
-
Delete
- DELETE FROM TABLEName WHERE `col` = `value`;
- DELETE FROM TABLEName WHERE col BETWEEN value1 AND value2
-
Select
- SELECT [*/username/or any column in your table] FROM TABLEName ;-> to get listing of all columns and its values in a table
- SELECT * FROM TABLENAME ORDER BY username/id/password ; or any other column -> to get listing of all columns and its values in a table ordered by ...
- SELECT count(*) FROM TABLEName ;
-
Conditions
- IF(condition, value_if_true, value_if_false)
Ex: select if(1=1, sleep(3), null);
here the condition is true so it will trigger a delay for 3 seconds
Ex: select name from content where id=128 and if(substr((select table_name from information_schema.tables where table_schema=database()),1,1)='a',true,false);
Ex: select if((select concat_ws(name,id,image,score) from content where name='user1'),true,false);
select * from content where id=128 and (if((length((select name from content where id=148))=7),true,false));
- When conditions
case when (your condition here) then some result for true else some result for false end ;
Ex: select * from content where id=128 and (select case when (length((select name from content where id=148))=7) then true else false end) ;
select * from content where id=128 and case when (length((select name from content where id=148))=7) then true else false end; -> with out the additional select statement
-
Some useful SQL keywords/clauses/operators
- this operator is used to combine the result of two select statements or
more
basic syntax -> select COLUMN from TABLEName where id=somevalue union select ANOTHER_COLUMN from TABLEName where something ...
- the where command is used to filter queried data by matching some condition you give after the where keyword
ex: select * from TABLEName where username="blablabla"
- the LIKE clause helps you to search the table with input the is not and exact match it's all
about placing the wildcard %
ex : select * from TABLEName where name like "%6" ;
select * from TABLEName where name like "%ser%" ;
- The ORDER BY command is used to sort the result set in ascending or descending order
Ascending by default
ex: select username from users order by id
- the LIMIT clause can be used to select the number of rows to be returned from your query and also select the starting row (or how many rows to skip before returning the output)
- UNION
- WHERE
- ORDER BY
- LIMIT
- LIKE
-
CRUD operations using PHP and MySQL
- Create
- List/Read
- Update
- Delete
-
PHP and connecting to DB
- Method 1
- Method 2
- Column Data Types
- RESET YOUR COUNTER FOR EXAMPLE YOUR ID COUNTER TO COUNT FROM 1 AGAIN IF IT CHANGED DURING ANY MODIFICATION
SET @NUM := 0;
UPDATE YOUR_TABLE SET ID = @NUM := (@NUM+1);
ALTER TABLE YOUR_TABLE AUTO_INCREMENT =1;
- IF Conditions
- When Condtions