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
Relational Database - where data is stored in tables
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
Datatype | Usage |
CHAR | CHAR(50) - Fixed-length character string. |
VARCHAR | VARCHAR(50) - Variable-length character string. |
BLOB | BLOB(1000) -Binary Large Object, used for storing large amounts of binary data. |
INT | Used to store whole numbers without decimal places. |
TINYINT | Used to store small integer values. |
BIGINT | Used to store large integer values |
BIT | Used to store binary data, typically used to represent true/false or 0/1 values |
FLOAT | Used to store floating-point numbers (numbers with decimal places). |
DOUBLE | Similar to FLOAT but with more precision. |
BOOLEAN | Used to store true/false values. |
DATE | Used to store dates in the format YYYY-MM-DD. |
SQL Datatypes Types
signed - It has positive and negative values. Ex- TINYINT(-128 to 127)
unsigned - It has only the positive values. Ex- TINYINT UNSIGNED(0 to 256)
Types Of SQL Commands
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
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
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
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 usersTCL(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
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
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
NOT NULL - columns cannot have a null value
UNIQUE - All values in a column are different
PRIMARY KEY - makes a column unique & not null but used only for one
FOREIGN KEY - prevent actions that would destroy links between tables.
DEFAULT - sets the default values of a column
CHECK - it can limit the values allowed in a column
Operators
AND - To check for both conditions to be true
Example:-
SELECT * FROM student WHERE marks > 80 AND city = "Mumbai";
OR - to check for one of the conditions to be true
Example:-
SELECT * FROM student WHERE marks > 90 OR city = "Mumbai";
BETWEEN - selects for a given range
Example:-
SELECT * FROM student WHERE marks BETWEEN 80 AND 90 = "Mumbai";
IN - matches any value in the list
Example:-
SELECT * FROM student WHERE city IN ("Delhi","Mumbai");
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
Arithmetic Operators - (+ , - , * , / , %)
Comparison Operators - (=, /= , > , < ,>= ,<= )
Logical Operators - (AND, OR, NOT, IN, BETWEEN, ALL, LIKE, ANY)
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()
Table Related Queries
Update ( to update existing rows)
Syntax :- UPDATE table_name SET Col1 = Val1 , Col2 = Val2 WHERE condition;
Delete ( to delete existing rows)
Syntax :- DELETE FROM tabel_name WHERE condition;
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
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;
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;
Right Join - returns all records from the right table
SELECT Column FROM tableA RIGHT JOIN tableB ON tableA.col_name = tableB.col_name
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;
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👇
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 :
Like👍|Share📲|Comment💭