Click if you like the project. Pull Request are highly appreciated.
An index is a database structure that you can use to improve the performance of database activity. A database table can have one or more indexes associated with it. An index is defined by a field expression that you specify when you create the index. Typically, the field expression is a single field name, like EMP_ID.
An index is used to speed up the performance of queries by reducing the number of database pages that have to be visited:
Sl.No | Query | Description |
---|---|---|
01. | CREATE INDEX index_name ON t(c1, c2) | Create an index on columns c1 and c2 of the table t |
02. | CREATE UNIQUE INDEX index_name ON t(c3, c4) | Create a unique index on columns c3 and c4 of the table t |
03. | DROP INDEX index_name | Drop an index |
Example:
SHOW INDEX FROM table_name;
ALTER TABLE `table_name` ADD INDEX(`column_name`);
DROP INDEX index_name ON tbl_name;
A JOIN
clause is used to combine rows from two or more tables, based on a related column between them.
Take users
table and orders
table for example.
Users Table:
user_id | name | mobile |
---|---|---|
1 | John | 123 |
2 | Joe | 124 |
Orders Table:
order_id | user_id | total | created_at |
---|---|---|---|
1 | 1 | 500 | 2022-12-19 18:32:00 |
2 | 1 | 800 | 2021-12-03 08:32:00 |
3 | 2 | 50 | 2020-12-13 12:49:00 |
4 | 1 | 80 | 2021-12-15 21:19:00 |
So to get the list of orders with names and mobile nos. for each order, we can join orders
and users
on the basis of user_id
.
SELECT
o.*,
u.name,
u.mobile
FROM
ordes o
JOIN users u ON o.user_id = u.user_id;
Using Join in a query, we can retrieve referenced columns or rows from multiple tables.
Following are different types of Joins:
Sl.No | Query | Description |
---|---|---|
01. | SELECT c1, c2 FROM t1 INNER JOIN t2 on condition | Select columns c1 and c2 from a table named t1 and perform an inner join between t1 and t2 |
02. | SELECT c1, c2 FROM t1 LEFT JOIN t2 on condition | Select columns c1 and c2 from a table named t1 and perform a left join between t1 and t2 |
03. | SELECT c1, c2 FROM t1 RIGHT JOIN t2 on condition | Select columns c1 and c2 from a table named t1 and perform a right join between t1 and t2 |
04. | SELECT c1, c2 FROM t1 FULL OUTER JOIN t2 on condition | Select columns c1 and c2 from a table named t1 and perform a full outer join between t1 and t2 |
05. | SELECT c1, c2 FROM t1 CROSS JOIN t2 | Select columns c1 and c2 from a table named t1 and produce a Cartesian product of rows in tables |
06. | SELECT c1, c2 FROM t1, t2 | Select columns c1 and c2 from a table named t1 and produce a Cartesian product of rows in tables |
07. | SELECT c1, c2 FROM t1 A INNER JOIN t2 B on condition | Select columns c1 and c2 from a table named t1 and joint it to itself using an INNER JOIN clause |
SELECT * from employees order by salary limit 2,1;
Both of them are used for string type data. char
has fixed length and if the inserted data is less than the defined length, required no. of blank spaces are added as padding. varchar
has variable length and no padding is used to fill up the left out space. So technically, varchar will save space.
The two principal rules for the relational model are as follows:
The differences between them are as follows:
Stored procedure stored inside the database. This also includes the executable code that usually collects and customizes the operations like insert, encapsulation, etc. These stored procedures are used as APIs for simplicity and security purposes. The implementation of it allows the developers to have procedural extensions to the standard SQL syntax. Stored procedure doesn’\t come as a part of relational database model, but can be included in many implementations commercially.
Index is a way to provide quick access to the data and structure. It has indexes maintain and can be created to combine attributes on a relation. Index allows the queries to filter out the searches faster and matching data can be found earlier with simplicity.
For example: It is same as the book where by using the index you can directly jump to a defined section. In relational database there is a provision to give multiple indexing techniques to optimize the data distribution.
There are many relational operators that are used to perform actions on relational database. These operators are as follows:
There are 9 normalizations that are used inside the database. These are as follows:
Non-first normal form (NFA)
– It describes the definition of the database design which is different from the first normal form.
There are three levels of data abstraction available in database model and these are as follows:
Data independence tells about the independence of the data inside the application. It usually deals with the storage structure and represents the ability to modify the schema definition. It doesn’\t affect the schema definition which is being written on the higher level.
There are two types of data independence:
NOTE: Logical Data Independence is more difficult to achieve.
E-R model stands for entity-relationship model and it is used to represent a model with their relationships. This is an object oriented approach and it is based on real world that consists of objects which are called entities and relationship between them. Entities are further used inside the database in the form of attributes.
where stringToChange is the string which will have the characters those we want to overwrite, startIndex is the starting position, length is the number of characters in the string that are to be overwrited, and new_characters are the new characters to write into the string.
For Example: SELECT RANK() OVER(ORDER BY BirthDate DESC) AS [RowNumber], FirstName, BirthDate FROM EmpDetails
When we execute any SQL operations, SQL Server opens a work area in memory which is called Cursor. When it is required to perform the row by row operations which are not possible with the set-based operations then cursor is used.
There are two of cursors:
1. Implicate Cursor:
2. Explicit Cursor:
COMMIT statement is used to end the current transaction and once the COMMIT statement is exceucted the transaction will be permanent and undone.
Example::
BEGIN
UPDATE EmpDetails SET EmpName = ‘Arpit’ where Dept = ‘Developer’
COMMIT;
END;
-ROLLBACK statement is used to end the current transaction and undone the changes which was made by that transaction.
Example::
BEGIN
Statement1;
SAVEPOINT mysavepoint;
BEGIN
Statement2;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO mysavepoint;
Statement5;
END;
END;
Example::
SELECT * FROM EmpDetails WITH(NOLOCK)
WITH(NOLCOK) is similar as READ UNCOMMITTED.
Nested subquery executes only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.
Correlated Subquery: select e.EmpFirstName, e.Salary, e.DeptId from Employee e where e.Salary = (select max(Salary) from Employee ee where ee.DeptId = e.DeptId)
SQL commands can be divided in three large subgroups.
1) DDL: The SQL commands which deals with database schemas and information of how the data will be generated in database are classified as Data Definition Language. -For example: CREATE TABLE or ALTER TABLE belongs to DDL.
2) DML: The SQL commands which deals with data manipulation are classified as Data Manipulation Language. For example: SELECT, INSERT, etc.
3) DCL: The SQL commands which deal with rights and permission over the database are classified as DCL. For example: GRANT, REVOKE
Following are the reasons for the poor performance of a query:
1. InnoDB:
The default storage engine in MySQL 8.0. InnoDB is a transaction-safe (ACID compliant) storage engine for MySQL that has commit, rollback, and crash-recovery capabilities to protect user data. InnoDB row-level locking (without escalation to coarser granularity locks) and Oracle-style consistent nonlocking reads increase multi-user concurrency and performance. InnoDB stores user data in clustered indexes to reduce I/O for common queries based on primary keys.
To maintain data integrity, InnoDB also supports FOREIGN KEY referential-integrity constraints.
2. MyISAM:
These tables have a small footprint. Table-level locking limits the performance in read/write workloads, so it is often used in read-only or read-mostly workloads in Web and data warehousing configurations.
3. Memory:
Stores all data in RAM, for fast access in environments that require quick lookups of non-critical data. This engine was formerly known as the HEAP engine. Its use cases are decreasing; InnoDB with its buffer pool memory area provides a general-purpose and durable way to keep most or all data in memory, and NDBCLUSTER provides fast key-value lookups for huge distributed data sets.
4. CSV:
Its tables are really text files with comma-separated values. CSV tables let you import or dump data in CSV format, to exchange data with scripts and applications that read and write that same format. Because CSV tables are not indexed, you typically keep the data in InnoDB tables during normal operation, and only use CSV tables during the import or export stage.
5. Archive:
These compact, unindexed tables are intended for storing and retrieving large amounts of seldom-referenced historical, archived, or security audit information.
6. Blackhole:
The Blackhole storage engine accepts but does not store data, similar to the Unix /dev/null device. Queries always return an empty set. These tables can be used in replication configurations where DML statements are sent to slave servers, but the master server does not keep its own copy of the data.
7. NDB:
This clustered database engine is particularly suited for applications that require the highest possible degree of uptime and availability.
8. Merge:
Enables a MySQL DBA or developer to logically group a series of identical MyISAM tables and reference them as one object. Good for VLDB environments such as data warehousing.
9. Federated:
Offers the ability to link separate MySQL servers to create one logical database from many physical servers. Very good for distributed or data mart environments.
10. Example:
This engine serves as an example in the MySQL source code that illustrates how to begin writing new storage engines. It is primarily of interest to developers. The storage engine is a “stub” that does nothing. You can create tables with this engine, but no data can be stored in them or retrieved from them.
In database management, an aggregate function is a function where the values of multiples rows are grouped to form a single value.
Sl.No | Function | Description |
---|---|---|
01. | COUNT | Return the number of rows in a certain table/view |
02. | SUM | Accumulate the values |
03. | AVG | Returns the average for a group of values |
04. | MIN | Returns the smallest value of the group |
05. | MAX | Returns the largest value of the group |
Sl.No | Query | Description |
---|---|---|
01. | SELECT c1 FROM t1 UNION [ALL] SELECT c1 FROM t2 | Select column c1 from a table named t1 and column c1 from a table named t2 and combine the rows from these two queries |
02. | SELECT c1 FROM t1 INTERSECT SELECT c1 FROM t2 | Select column c1 from a table named t1 and column c1 from a table named t2 and return the intersection of two queries |
03. | SELECT c1 FROM t1 MINUS SELECT c1 FROM t2 | Select column c1 from a table named t1 and column c1 from a table named t2 and subtract the 2nd result set from the 1st |
04. | SELECT c1 FROM t WHERE c1 [NOT] LIKE pattern | Select column c1 from a table named t and query the rows using pattern matching % |
05. | SELECT c1 FROM t WHERE c1 [NOT] in test_list | Select column c1 from a table name t and return the rows that are (or are not) in test_list |
06. | SELECT c1 FROM t WHERE c1 BETWEEN min AND max | Select column c1 from a table named t and return the rows where c1 is between min and max |
07. | SELECT c1 FROM t WHERE c1 IS [NOT] NULL | Select column c1 from a table named t and check if the values are NULL or not |
Sl.No | Query | Description |
---|---|---|
01. | INSERT INTO t(column_list) VALUES(value_list) | Insert one row into a table named t |
02. | INSERT INTO t(column_list) VALUES (value_list), (value_list), … | Insert multiple rows into a table named t |
03. | INSERT INTO t1(column_list) SELECT column_list FROM t2 | Insert rows from t2 into a table named t1 |
04. | UPDATE tSET c1 = new_value | Update a new value in table t in the column c1 for all rows |
05. | UPDATE tSET c1 = new_value, c2 = new_value WHERE condition | Update values in column c1 and c2 in table t that match the condition |
06. | DELETE FROM t | Delete all the rows from a table named t |
07. | DELETE FROM tWHERE condition | Delete all rows from that a table named t that match a certain condition |
A view is a virtual table that is a result of a query. They can be extremely useful and are often used as a security mechanism, letting users access the data through the view, rather than letting them access the underlying base table:
Sl.No | Query | Description |
---|---|---|
01. | CREATE VIEW view1 AS SELECT c1, c2 FROM t1 WHERE condition | Create a view, comprising of columns c1 and c2 from a table named t1 where a certain condition has been met. |
A stored procedure is a set of SQL statements with an assigned name that can then be easily reused and share by multiple programs:
Sl.No | Query | Description |
---|---|---|
01. | CREATE PROCEDURE procedure_name @variable AS datatype = value AS – Comments SELECT * FROM tGO | Create a procedure called procedure_name, create a local variable and then select from table t |
A trigger is a special type of stored procedure that automatically executes when a user tries to modify data through a DML event (data manipulation language). A DML event is an INSERT, UPDATE or DELETE statement on a table or view:
CREATE OR MODIFY TRIGGER trigger_name
WHEN EVENT
ON table_name TRIGGER_TYPE
EXECUTE stored_procedure
WHEN:
EVENT:
TRIGGER_TYPE:
!-- Delete a specific trigger
DROP TRIGGER trigger_name
01. Import “.sql” file from command prompt:
SOURCE C://database.sql;
02. MySQL Performance Queries:
OPTIMIZE TABLE table_name;
--Displays description of the table
SHOW TABLE STATUS;
DESC table_name;
SHOW VARIABLES;
SHOW STATUS;
SHOW GLOBAL STATUS;
SHOW TABLES FROM INFORMATION_SCHEMA;
EXPLAIN SELECT * FROM table_name
SHOW TABLE STATUS FROM database_name;
--Shows you which threads are running
SHOW FULL PROCESSLIST;
--IP Address of the Mysql Host
SHOW VARIABLES WHERE Variable_name = 'hostname';
04. Table Related Queries:
SELECT max(RIGHT(`field_name`,4)) FROM `table_name`;
-- Converts to upper case
SELECT UCASE(column_name) FROM table_name;
--Select nearest value
SELECT * FROM TABLE ORDER BY ABS(VALUE - $MYVALUE) LIMIT 1
SELECT sentence, wordcount(sentence) as "Words" from test;
--Useful in pagination
SELECT * FROM table_name ORDER BY field_name LIMIT 5 OFFSET 5;
--Find duplicate entries
SELECT *, COUNT(field_name) FROM table_name GROUP BY field_name HAVING ( COUNT(field_name) > 1 )
ALTER TABLE table_name AUTO_INCREMENT =1
ALTER TABLE `table_name` DROP PRIMARY KEY
ALTER TABLE `table_name` ENGINE = InnoDB
ALTER TABLE table_name CHANGE `id` `id` INT( 11 ) NOT NULL AUTO_INCREMENT
ALTER TABLE `table_name` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST ;
ALTER TABLE table_name ADD column_name datatype AFTER column_name
ALTER TABLE table_name DROP COLUMN column_name
ALTER TABLE table_name MODIFY column_name datatype
ALTER TABLE table_name CHANGE oldname newname datatype
RENAME TABLE `table_name_old` TO `table_name_new`
--Update particular character
UPDATE mytable SET city = REPLACE(city, 'ï', 'i')
--Swaping field value
UPDATE swap_test SET x=y, y=@temp where @temp:=x;
--COPYING
UPDATE table_name SET field_name1=field_name2;
UPDATE table_name SET field_name = UPPER(field_name)
TRUNCATE TABLE table_name;
DROP TABLE table_name;
05. Date and time:
SHOW VARIABLE LIKE '%time_zone%';
--Current timestamp
SELECT NOW();
--Current day
SELECT DAYNAME(NOW());
--Subtract time
SELECT SUBTIME('1:30:00', '00:15:00');
--Date Format
SELECT DATE_FORMAT(NOW(), '%W, %D of %M, %Y');
SELECT TIMEDIFF('2007-10-05 12:10:18','2007-10-05 16:14:59') AS length;
SELECT * FROM `table_name` WHERE DATE_FORMAT(field_name, "%Y-%m-%d") = '2010-01-01'"
06. MySQL Miscellaneous Queries:
--Use to generate unique id
SELECT uuid();
--Get numeric values
SELECT * FROM `TABLENAME` WHERE `field` REGEXP '^-?[0-9]+$'
--w3resource.com
SELECT CONCAT('w3resource','.','com');
--bit datatype
CREATE TABLE table_bit (id SERIAL, a BIT(3), b BIT(8), c BIT(16));
--Enum datatype
CREATE TABLE table_enum (id SERIAL, status ENUM('Active', 'Deactive'));
--Get the length
SELECT CHAR_LENGTH(field_name) AS Length FROM `table_name`;
SELECT * FROM `table_name` WHERE LENGTH(field_name) < 10
--Copying the previous rows to the next rows
INSERT INTO table_name (`col1`, `col2`, `col3`, `...`, `coln`) SELECT `col1`, `col2`, `col3`, `...`, `coln` FROM table_name
SELECT COUNT(DISTINCT column) FROM table;
SELECT field_name, LEFT(field_name, 3), RIGHT(field_name, 3), MID(field_name, 2, 3) FROM `table_name`;
--Flow control with CASE
SELECT CASE WHEN a THEN 'true' ELSE 'false' END AS boolA, CASE WHEN b THEN 'true' ELSE 'false' END AS boolB FROM table_name;
A stored routine is a set of SQL statements that are stored on the database server and can be used by any client with permission to use them. This provides a number of benefits.
There are two different kinds of stored routines.
a) Stored functions return a value, and are used in the context of an expression. b) Stored procedures are called separately, using the call statement, and may return result sets or set variables.
Example 01: Stored Functions
DROP FUNCTION IF EXISTS track_len;
CREATE FUNCTION track_len(seconds INT)
RETURNS VARCHAR(16) DETERMINISTIC
RETURN CONCAT_WS(':', seconds DIV 60, LPAD(seconds MOD 60, 2, '0' ));
SELECT title, track_len(duration) FROM track;
SELECT a.artist AS artist,
a.title AS album,
t.title AS track,
t.track_number AS trackno,
track_len(t.duration) AS length
FROM track AS t
JOIN album AS a
ON a.id = t.album_id
ORDER BY artist, album, trackno
;
Example 02: Stored Procedures
DROP PROCEDURE IF EXISTS list_albums;
DELIMITER //
CREATE PROCEDURE list_albums ()
BEGIN
SELECT * FROM album;
END
//
DELIMITER ;
CALL list_albums();
Example 03: Stored Procedures with parameter
DROP PROCEDURE IF EXISTS list_albums;
DELIMITER //
CREATE PROCEDURE list_albums (a VARCHAR(255))
BEGIN
SELECT a.artist AS artist,
a.title AS album,
t.title AS track,
t.track_number AS trackno,
track_len(t.duration) AS length
FROM track AS t
JOIN album AS a
ON a.id = t.album_id
WHERE a.artist LIKE a
ORDER BY artist, album, trackno
;
END //
DELIMITER ;
CALL list_albums('%hendrix%');
Example 04: Drop Stored Procedures & Stored Functions
DROP FUNCTION IF EXISTS track_len;
DROP PROCEDURE IF EXISTS total_duration;
-- Exmaple - 01
CREATE TABLE widgetInventory (
id SERIAL,
description VARCHAR(255),
onhand INTEGER NOT NULL
);
CREATE TABLE widgetSales (
id SERIAL,
inv_id INTEGER,
quan INTEGER,
price INTEGER
);
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'rock', 25 );
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'paper', 25 );
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'scissors', 25 );
START TRANSACTION;
INSERT INTO widgetSales ( inv_id, quan, price ) VALUES ( 1, 5, 500 );
UPDATE widgetInventory SET onhand = ( onhand - 5 ) WHERE id = 1;
COMMIT;
SELECT * FROM widgetInventory;
SELECT * FROM widgetSales;
START TRANSACTION;
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'toy', 25 );
ROLLBACK;
SELECT * FROM widgetInventory;
SELECT * FROM widgetSales;
---Example - 02 INSERT Query using Transaction
CREATE TABLE test (
id SERIAL,
data VARCHAR(256)
);
-- Insert 1,000 times ...
INSERT INTO test ( data ) VALUES ( 'this is a good sized line of text.' );
--- Insert 1000 times using Transaction
START TRANSACTION;
INSERT INTO test ( data ) VALUES ( 'this is a good sized line of text.' );
COMMIT;
Example - 01: Updating a table with a trigger
CREATE TABLE widgetCustomer ( id SERIAL, name VARCHAR(255), last_order_id BIGINT );
CREATE TABLE widgetSale ( id SERIAL, item_id BIGINT, customer_id BIGINT, quan INT, price DECIMAL(9,2) );
INSERT INTO widgetCustomer (name) VALUES ('Bob');
INSERT INTO widgetCustomer (name) VALUES ('Sally');
INSERT INTO widgetCustomer (name) VALUES ('Fred');
SELECT * FROM widgetCustomer;
CREATE TRIGGER newWidgetSale AFTER INSERT ON widgetSale
FOR EACH ROW
UPDATE widgetCustomer SET last_order_id = NEW.id WHERE id = NEW.customer_id
;
INSERT INTO widgetSale (item_id, customer_id, quan, price) VALUES (1, 3, 5, 19.95);
INSERT INTO widgetSale (item_id, customer_id, quan, price) VALUES (2, 2, 3, 14.95);
INSERT INTO widgetSale (item_id, customer_id, quan, price) VALUES (3, 1, 1, 29.95);
SELECT * FROM widgetSale;
SELECT * FROM widgetCustomer;
---Example - 01 : Creating a View
SELECT id, album_id, title, track_number, duration DIV 60 AS m, duration MOD 60 AS s
FROM track;
CREATE VIEW trackView AS
SELECT id, album_id, title, track_number, duration DIV 60 AS m, duration MOD 60 AS s
FROM track;
SELECT * FROM trackView;
---Exmaple - 02 : Joined view
SELECT a.artist AS artist,
a.title AS album,
t.title AS track,
t.track_number AS trackno,
t.duration DIV 60 AS m,
t.duration MOD 60 AS s
FROM track AS t
JOIN album AS a
ON a.id = t.album_id
;
CREATE VIEW joinedAlbum AS
SELECT a.artist AS artist,
a.title AS album,
t.title AS track,
t.track_number AS trackno,
t.duration DIV 60 AS m,
t.duration MOD 60 AS s
FROM track AS t
JOIN album AS a
ON a.id = t.album_id
;
SELECT * FROM joinedAlbum;
SELECT * FROM joinedAlbum WHERE artist = 'Jimi Hendrix';
---Example - 03 : Drop View
DROP VIEW IF EXISTS joinedAlbum;