Month: April 2020

Mysql – DDL, DML, DCL and TCL

MySQL – DDL, DML, DCL and TCL Commands

DDL:

DDL is stands for DATA DEFINITION LANGUAGE, which deals with database schemas and descriptions, of how the data should reside in the database.

  • CREATE – To create a database and its objects like (table, index, views, store procedure, function, and triggers)
    //Syntax
    CREATE TABLE table_name (
          column1 datatype,
          column2 datatype,
        ....
     );
    
    //Ex:
    CREATE TABLE Tbl_Employee (
    EmployeeID int,
    LastName varchar(255),
    FirstName varchar(255),
    Gender varchar(255),
    City varchar(255) 
    );
    
    Try It Now
    
  • ALTER – Alters the structure of the existing database. The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
    //Syntax
    ALTER TABLE table_name ADD column_name COLUMN DEFINITION; 
    //to add new column
    ALTER TABLE table_name MODIFY(COLUMN DEFINITION....); 
    //to modify existing coulmn
    
    //Ex:
    ALTER TABLE Tbl_Employee ADD Email varchar(255);
    ALTER TABLE Tbl_Employee MODIFY Email varchar(50);
    
    Try It Now
  • DROP – Delete objects from the database. It is used to delete both the structure and record stored in the table.
    //Syntax:
    DROP TABLE table_name;
    
    //Ex:
    DROP TABLE Tbl_Employee;
  • TRUNCATE – Remove all records from a table, including all spaces allocated for the records are removed. In this case primary key will get reset to 1.
    //Syntax:
    TRUNCATE TABLE table_name;
    
    //Ex:
    TRUNCATE TABLE Tbl_Employee;
  • RENAME – Rename an object. It might be Table name
    //Syntax:
    RENAME tablename to newtablename;
    
    //Ex: RENAME Tbl_Employee TO Tbl_Employee_New; 
    //rename table name

DML:

DML is stands for DATA MANIPULATION LANGUAGE which deals with data manipulation and includes most common SQL statements such SELECT, INSERT, UPDATE, DELETE, etc. and it is used to store, modify, retrieve, delete and update data in a database.

  • SELECT– Retrieve data from a database.
    //Syntax:
    SELECT column1, column2, ...
    FROM table_name;
    
    //Ex:
    SELECT * FROM Tbl_Employee;
    
    Try It Now
  • INSERT– Insert data into a table.
    //Syntax:
    INSERT INTO table_name (column1, column2, column3, ...)
    VALUES (value1, value2, value3, ...);
    OR
    INSERT INTO table_name
    VALUES (value1, value2, value3, ...); 
    
    //Ex:
    INSERT INTO Tbl_Employee 
    VALUES (1,'Smith','James','Male','Mumbai');
    
    Try It Now
  • UPDATE– Updates existing data within a table.
    //Syntax:
    UPDATE table_name
      SET column1 = value1, column2 = value2, ...
      WHERE condition;
    
    //Ex:
    UPDATE Tbl_Employee SET City = 'Delhi' 
    WHERE EmployeeID = 1;
    
    Try It Now
  • DELETE– The DELETE statement is used to delete existing records in a table.
    //Syntax:
    DELETE FROM table_name WHERE condition;
    
    //Ex:
    UPDATE FROM Tbl_Employee WHERE EmployeeID = 1;
    
    Try It Now

DCL:

DCL is stands for DATA CONTROL LANGUAGE which includes commands such as GRANT and mostly concerned with rights, permissions and other controls of the database system.

  • GRANT – Allow users access privileges to the database.
    //Syntax:
    GRANT SELECT, UPDATE ON MY_TABLE 
    TO SOME_USER, ANOTHER_USER;
  • REVOKE – Withdraw users access privileges given by using the GRANT command.
    //Syntax:
    REVOKE SELECT, UPDATE ON MY_TABLE 
    FROM SOME_USER, ANOTHER_USER;

TCL:

TCL is stands for TRANSACTION CONTROL LANGUAGE which deals with a transaction within a database.

  • COMMIT – Commits a Transaction.When a successful transaction is completed, the COMMIT command should be issued so that the changes to all involved tables will take effect.
    //Syntax:
    COMMIT; //Execute after any mysql execution
    
    //Ex:
    DELETE FROM Tbl_Employee  WHERE EmployeeID = 1; 
    COMMIT; ##transaction has been complete
    
    Try It Now
  • ROLLBACK – Rollback a transaction in case of any error occurs.
    //Syntax:
    ROLLBACK; //Execute after any mysql execution 
    
    //Ex: 
    DELETE FROM Tbl_Employee WHERE EmployeeID = 1;
    COMMIT; ##rollback previous transaction
    
    Try It Now
  • SAVEPOINT – To rollback the transaction making points within groups.
    //Syntax:
    Mysql query statment1;
    SAVEPOINT tran1; //Belongs to statment1
    
    Mysql query statment2;
    SAVEPOINT tran2; //Belongs to statment2
    
    ROLLBACK TO tran1; 
    //only output of statement1 will remain 
    and output of statemnt2 will rollback.
    
    //Ex:
    DELETE FROM Tbl_Employee  WHERE EmployeeID = 1; 
    SAVEPOINT tran1;
    
    DELETE FROM Tbl_Employee  WHERE EmployeeID = 2; 
    SAVEPOINT tran2;
    
    ROLLBACK TO tran1; ##roll back trans1
    
    Try It Now
  • SET TRANSACTION – Specify characteristics of the transaction.

MySQL indexes

What is MySQL indexes ?

A database index is a data structure that improves the speed of operations in a table. Indexes can be created using one or more columns, providing the basis for both rapid random lookup and efficient ordering of access to records. An index helps to speed up SELECT queries and WHERE clauses, but it slows down data input, with the UPDATE and the INSERT statements. Indexes can be created or dropped with no effect on the data.

The index can also be a UNIQUE index, which means that you cannot have duplicate values in that column, or a PRIMARY KEY which in some storage engines defines where in the database file the value is stored.

Create Index Syntax:
Create index on column and allow duplicate values.
CREATE INDEX id_index ON table_name(column_name);

//Example
CREATE INDEX id_index ON Tbl_Employee(EmployeeID);
Try It Now
Create Index Syntax:
Create unique index which can't allow duplicate values.
CREATE INDEX UNIQUE INDEX ON table_name(column_name);

//Example
CREATE UNIQUE INDEX city_index ON Tbl_Employee(City);
Try It Now
ALTER command to ADD and DROP INDEX

ALTER ADD INDEX
ALTER TABLE table_name ADD INDEX(column_name);
//Example
ALTER TABLE Tbl_Employee ADD INDEX(City);

ALTER DROP INDEX
ALTER TABLE table_name DROP INDEX(column_name);
//Example
ALTER TABLE Tbl_Employee DROP INDEX(City);

Try It Now
Show Index
Syntax:
SHOW INDEXES FROM table_name;
//Example
SHOW INDEXES FROM Tbl_Employee ;
Try It Now

How does MySQL indexes work?

Database table does not reorder itself every time the query conditions change in order to optimize the query performance: that would be unrealistic. In actuality, what happens is the index causes the database to create a data structure. The data structure type is very likely a B-Tree. Advantages of the B-Tree is that it is sortable. When the data structure is sorted in order it makes our search more efficient.

Test  Employee Table:

EmployeeID LastName FirstName Gender City
1 Smith James Male Mumbai
2 Johnson Maria Female New York
3 Miller David Male Mumbai
4 Wilson Maria Female Mumbai
5 Thomas Mary Female New York
6 Martin Steven Male New York
7 Lee Karen Female Mumbai

From above table, We have to search Employee fitstname which have city Mumbai then We write query.
SELECT FirstName FROM Tbl_Employee WHERE city= ‘Mumbai’;

Without indexing

In order to non indexing, database search for City = ‘Mumbai’ in all rows of table and return the result. It is time consuming for large data table.

When we apply indexing then index table look like:

EmployeeID LastName FirstName Gender City
1 Smith James Male Mumbai
3 Miller David Male Mumbai
4 Wilson Maria Female Mumbai
7 Lee Karen Female Mumbai
2 Johnson Maria Female New York
5 Thomas Mary Female New York
6 Martin Steven Male New York

In indexing, the query search only the rows in the column City that have Mumbai and then using the pointer that can go into the table to find the specific row where that pointer lives. The query can then go into the table to retrieve the fields for the columns requested for the rows that meet the conditions.

If the search were presented visually, it would look like this:

Index to non index relation