DATABASE : Store your Web-app data here

DATABASE : Store your Web-app data here

Database

It is the collection of data in a format that can be easily accessed (digitally)

DBMS(Database Management System)

A software application used to manage our database is called DBMS

Types Of Databases

  1. Relational Database - where data is stored in tables

  2. Non-Relational Database - where data is not stored in tables but in objects

SQL

It stands for Structured Query Language used to interact with relational databases. It is used to perform CRUD operations such as Create | Read | Update | Delete. In SQL data is stored in a table with rows and Columns & the structure is known as a schema for the MYSQL database.

SQL Datatypes

DatatypeUsage
CHARCHAR(50) - Fixed-length character string.
VARCHARVARCHAR(50) - Variable-length character string.
BLOBBLOB(1000) -Binary Large Object, used for storing large amounts of binary data.
INTUsed to store whole numbers without decimal places.
TINYINTUsed to store small integer values.
BIGINTUsed to store large integer values
BITUsed to store binary data, typically used to represent true/false or 0/1 values
FLOATUsed to store floating-point numbers (numbers with decimal places).
DOUBLESimilar to FLOAT but with more precision.
BOOLEANUsed to store true/false values.
DATEUsed to store dates in the format YYYY-MM-DD.

SQL Datatypes Types

  1. signed - It has positive and negative values. Ex- TINYINT(-128 to 127)

  2. unsigned - It has only the positive values. Ex- TINYINT UNSIGNED(0 to 256)

Types Of SQL Commands

  1. DDL(Data Definition Language ) - DDL commands are used to define and manage the structure of the database. They are responsible for defining, altering, and dropping database objects such as tables, indexes, views, and schemas.

    Examples - create , alter , rename , truncate & drop

  2. DQL(Data Query Language) - DQL commands are focused on querying the database to extract information and are primarily associated with the SELECT statement.
    Examples - select

  3. DML(Data Manipulation Language) - DML commands are used to manipulate the data stored in the database. They allow you to insert, update, retrieve, and delete data from the tables.
    Examples - insert , update & delete

  4. DCL(Data Control Language) - DCL commands are concerned with the permissions and access control within the database. They are used to grant or revoke permissions to users and roles.

    Examples - grant & revoke permission to users

  5. TCL(Transaction Control Language) - TCL commands are used to manage transactions within a database. A transaction is a sequence of one or more SQL statements executed as a single unit of work.

    Examples - start transaction , commit , rollback

Keys

  1. Primary Key - It is a column (or set of columns) in a table that uniquely identifies each row. (a unique id), there is only 1 primary key & it should not be null

  2. Foreign Key - A foreign key is a column (or set of columns) in a table that refers to the primary key, there can be duplicate & null values.

Constraints

They are used to specify rules for data in a table

  1. NOT NULL - columns cannot have a null value

  2. UNIQUE - All values in a column are different

  3. PRIMARY KEY - makes a column unique & not null but used only for one

  4. FOREIGN KEY - prevent actions that would destroy links between tables.

  5. DEFAULT - sets the default values of a column

  6. CHECK - it can limit the values allowed in a column

Operators

  1. AND - To check for both conditions to be true

    Example:- SELECT * FROM student WHERE marks > 80 AND city = "Mumbai";

  2. OR - to check for one of the conditions to be true

    Example:- SELECT * FROM student WHERE marks > 90 OR city = "Mumbai";

  3. BETWEEN - selects for a given range

    Example:- SELECT * FROM student WHERE marks BETWEEN 80 AND 90 = "Mumbai";

  4. IN - matches any value in the list

    Example:- SELECT * FROM student WHERE city IN ("Delhi","Mumbai");

  5. NOT - to negate the given condition

    Example:- SELECT * FROM student WHERE city NOT IN ("Delhi","Mumbai");

Clauses in MYSQL

  • Where clause -

    i) used to define some conditions

       SELECT COL1 , COL2 FROM TABLE_NAME 
       WHERE CONDITIIONS;
    

    ii) using operators in WHERE

  1. Arithmetic Operators - (+ , - , * , / , %)

  2. Comparison Operators - (=, /= , > , < ,>= ,<= )

  3. Logical Operators - (AND, OR, NOT, IN, BETWEEN, ALL, LIKE, ANY)

  4. Bitwise Operators - (& , | )

  • Order by clause - to sort ascending (ASC) or descending order (DESC)

      SELECT col1, col2 FROM table_name
      ORDER BY col_name(s) ASC;
    
  • Group by clause - groups rows that have the same values into summary rows, it collects data from multiple records and groups the result by one or more columns, generally we use group by some aggression function

  • Having clause - similar to where i.e. applies some condition on rows used when we went to apply ay condition after grouping

Aggregate Functions

It performs a calculation on a set of values and returns a single value

COUNT() | MAX() | MIN() | SUM() | AVG()

  1. Update ( to update existing rows)

     Syntax :-
     UPDATE table_name 
     SET Col1 = Val1 , Col2 = Val2
     WHERE condition;
    
  2. Delete ( to delete existing rows)

     Syntax :-
     DELETE FROM tabel_name
     WHERE condition;
    
  3. Alter ( to change the schema)

     Syntaxes :-
    
     ADD column
     ALTER TABLE table_name
     ADD COLUMN column_name datatype constraint;
    
     DROP column
     ALTER TABLE table_name
     DROP COLUMN column_name;
    
     RENAME Table
     ALTER TABLE table_name
     RENAME TO new_table_name;
    
     CHANGE column (rename)
     ALTER TABLE table_name
     CHANGE COLUMN old_name new_name new_datatype new_constraint;
    
     MODIFY column (modify datatype | constraint)
     ALTER TABLE table_name
     MODIFY col_name new_datatype new_constraint;
    
     TRUNCATE (to delete tables data)
     TRUNCATE TABLE table_name;
    

Joins In SQL

Join is used to combine rows from two or more tables, based on a related column between them

  1. Inner Join - returns records that have matching values in both tables

     SELECT Column
     FROM tableA
     INNER JOIN tableB
     ON tableA col_name = tableB. col_name;
    
  2. Left Join - returns all records from the left table and the matched records from the right table.

       SELECT Column
       FROM tableA
       LEFT JOIN tableB
       ON tableA.col_name = table.col_name;
    
  3. Right Join - returns all records from the right table

       SELECT Column
       FROM tableA
       RIGHT JOIN tableB
       ON tableA.col_name = tableB.col_name
    
  4. Full Join - returns all records when there is a match in either the left or right table

     Syntax in MYSQL -
    
     SELECT * FROM Student as a
     LEFT JION course as b
     ON a.id = b.id
     UNION
     SELECT  * FROM Student as a
     RIGHT JOIN course as b
     ON a.id = b.id;
    
  5. Self Join - It is regular join but the table is joined with itself

     SELECT column(s)
     FROM table as a
     JOIN table as b
     ON a.col_name = b.col_name;
    
  • Union - It is used to combine the result - self of two or more SELECT statements, gives UNIQUE records

      SELECT Column(s) FROM tableA
      UNION
      SELECT Column(s) FROM tableB
    
  • SQL Sub-Queries - a subquery inner query or nested query is a query with another SQL query and it involves 2 select statements

      SELECT Column(s)
      FROM table_name
      WHERE col_name operator
      (subquery);
    

MySQL Views - a view is a virtual table based on the result set of an SQL statement

CREATE VIEW view1 AS
SELECT rollno , name FROM student;
SELECT * FROM view1;

Checkout below given link to get the Free MYSQL Command Cheatsheet👇

MYSQL Cheatsheet

Conclusion:-

Databases serve as organized collections of digital data, managed efficiently through Database Management Systems (DBMS). Structured Query Language (SQL) facilitates interaction, defining and manipulating data with various data types, keys, constraints, and operators. SQL commands are categorized into DDL, DML, DCL, and TCL, each crucial for database definition, manipulation, access control, and transaction management. Understanding these elements is fundamental to effective database administration and utilization.

Follow Me On Socials :

Linkedin

Twitter

Github

Like👍|Share📲|Comment💭

Did you find this article valuable?

Support MOHD NEHAL KHAN by becoming a sponsor. Any amount is appreciated!