AfterAcademy Tech
•
23 Jan 2020

A database query refers to the request of data or information from a database. A database query is performed using a database query language that generates data of different formats according to function. A number of query languages have been developed according to different database engines and purposes, one such language is SQL. SQL stands for Structured Query Language. It is the most widely used and well-known database query language which generates result in the form of rows and columns. It is designed for the retrieval and management of data in a relational database. SQL can be used to perform the following operations in the database :
All these operations are performed using the SQL Commands. SQL commands are instructions, coded into SQL statements, which are used to communicate with the database to perform specific tasks, work, functions, and queries with data in the database. SQL Commands can be grouped into following depending on their functionality:
DDL stands for Data Definition Language. It consists of the SQL commands that can be used to define the database schema. It deals with descriptions of the database schema. These commands are used for creating, modifying, and dropping the structure of database objects. The database automatically commits the current transaction before and after every DDL Command. Some DDL commands are CREATE, ALTER, and DROP.
** Only with DDL Command we need to specify the database object with keywords(like table, view, procedure, index, function, etc)
Examples of DDL Commands :
CREATE DATABASE database_name;
This query will create a new database in SQL and name the database as the provided name.
CREATE TABLE table_name
{
column1 data_type(size),
column2 data_type(size),
.
.
columnN data_type(size)
};
This query will create a new table in SQL and name the table as specified. The table will contain N columns of associated data_types and size.
CREATE TABLE Employees {
Emp_Id int(3),
Emp_Name varchar(20)
};
This will create a table Employees with column Emp_Id which stores integer data of length 3 and Emp_Name which stores string data of length 20.
ALTER TABLE Employees ADD Salary DOUBLE(8,2);
This query will add a column Salary to the existing table Employees. If the table Employees do not exist, an error will be generated.
ALTER TABLE Employees DROP COLUMN Salary;
This query will delete the column Salary from the table Employees.
ALTER TABLE Employees MODIFY Name VARCHAR(40);
This query will modify the existing column Name in table Employees and change the size to 40 from 20.
DROP object object_name;
object: Keyword that represents the database object.
DROP DATABASE Company;
The database Company is deleted from the relational database management system.
DROP TABLE Employees;
This SQL command will remove the table structure along with its data from the database.
DML stands for Data Manipulation Language. These commands are used for retrieving, inserting/storing, modifying and deleting the data stored in the database. DML Commands deals with the manipulation or managing of data present in the objects of a relational database. Some DML commands are SELECT, INSERT, UPDATE, and DELETE commands.
** The SELECT statement is a limited form of DML statement such that it can only access data in the database. It cannot manipulate data in the database, although it can operate on the accessed data before returning the results of the query. This is why SELECT is also called DQL(Data Query Language).
Examples of DML Commands:
# Both Column names and values: In this method, we need to specify both the column names of the table in which we are inserting the data as well as their corresponding values.
INSERT INTO table_name(col1, col2,…) VALUES(value1, value2,…);
INSERT INTO Employees(Emp_Id, Emp_Name) VALUES(007, “John Doe”);
This command creates a new record in the table Employees and inserts values for the specified column with corresponding values.
# Only Values: In the second method we provide only the values of the data to be inserted into the column without their column name. In this method, we need to provide values for every column in the table and they must be in the order of the columns.
INSERT INTO table_name VALUES(value1, value2,… valueN);
INSERT INTO Employees VALUES(007,”John Doe”,20000);
This command insert values in all 3 columns of the table Employees(Emp_Id,Emp_Name and Salary).
SELECT col1, col2,… colN FROM table_name WHERE condition;
col1,col2,… colN: represent the fields of the table that we want to retrieve. We can also use ‘*’ in place of column names to retrieve all the columns.
table_name: specifies the table which acts as the data source or from where we watch to fetch the data.
condition: the condition is specified in the WHERE clause which is optional. It is used to filter out the data according to the specified condition.
SELECT * FROM Employees;
Retrieves all the records with data from every field form the Employees table.
SELECT Emp_Id, Emp_Name FROM Employees;
Retrieves all the records with data of only field Emp_Id and Emp_Name form the Employees table.
SELECT * FROM Employees WHERE Emp_Id = 7;
Retrieves all field information form the Employees table of the Employees whose Emp_Id is 7.
DELETE FROM table_name WHERE condition;
table_name: name of the table from which the records are to be deleted.
condition: provides filter conditions to identify the particular records that are to be deleted.
DELETE FROM Employees WHERE Emp_Id = 7;
Deletes the record (single row) of the Employee whose Emp_Id is 7.
DELETE FROM Employees WHERE Salary = 20000;
Deletes records (multiple records) of the Employees whose salary is 20000.
** WHERE clause in DELETE command is optional. If we omit the WHERE clause then all of the records of the table will be deleted and the table will be empty.
DELETE FROM Employees;
Deletes all the records from the table Employees.
UPDATE table_name SET col1 = value1, col2 = value2,… colN = vlaueN WHERE condition;
table_name: name of the table whose data we are going to update.
SET: keyword used to specify fields and values.
col: name of fields to be updated.
value: the updated value of the corresponding field.
condition: condition specified to select rows in which data needs to be modified.
UPDATE Employees SET Salary = 10000 WHERE Emp_Id = 007;
Modifies the salary(single column) of the employee with Emp_Id 7 and sets Salary to 10000.
UPDATE Employees SET Salary=10000, Name=“John” WHERE Emp_Id = 007;
Modifies the salary and name (multiple-column) of employees with Emp_Id 7 and sets Salary to 10000.
** WHERE clause in the UPDATE command is optional. If we omit the WHERE clause then the specified field(s) of all the records of the table will be updated.
UPDATE Employees SET Salary = 10000;
Modifies the salary of every employee and sets Salary to 10000.
TCL stands for Transaction Control Language. TCL commands deal with the transaction within the database. A transaction is a unit of work that is performed against a database. Transaction results in a change in the state of data. TCL commands are used for managing changes affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT. TCL commands are used with DML Commands only. DDL Commands automatically saves the state of the data.
Examples of TCL Commands
COMMIT;
This command will save the current state of data, in other words, it will make all the changes done to the database permanent.
ROLLBACK;
This command will undo all the changes made to the database since the last COMMIT or ROLLBACK.
ROLLBACK TO save_point;
This command will undo all the changes made to the database up to the specified save point.
SAVEPOINT My_Savepoint;
This commands creates a savepoint of name My_Savepoint in the current transaction. Now rollback command can be used to undo changes to the database up to the savepoint My_Savepoint.
ROLLBACK TO My_Savepoint;
DCL stands for Data Control Language. These SQL commands are used for providing security to database objects( like table, views, procedures, etc). Data control commands in SQL allow us to control access to data within the database. These DCL commands are normally used to create objects related to user access and also control the distribution of privileges among users. These commands are GRANT and REVOKE. To perform any operation in the database, such as for creating tables, sequences or views, a user needs privileges. Privileges are of two types:
Examples of DCL Commands
FOR OBJECT PRIVILEGES:
GRANT privilege_name ON object_name TO {user_name |PUBLIC |role_name} [WITH GRANT OPTION];
privilege_name: refers to the access right or privilege granted to the user. Some of the access rights are ALL, EXECUTE, and SELECT. Multiple privileges can be granted to the users simultaneously.
object_name: name of a database object like TABLE, VIEW, STORED PROC and SEQUENCE. It represents the objects on which the permission is to be granted. Permissions on multiple objects can be granted simultaneously.
user_name: it is the name of the user or role to whom an access right is being granted. Multiple users can be granted privileges simultaneously. PUBLIC keyword grants privileges to all the users.
[WITH GRANT OPTION]: this clause is optional. it is used to allow users to grant the same privileges to other users. It is applicable only for object privileges.
GRANT SELECT ON Employees TO user1;
This query will grant SELECT privileges on Employees object to user1, i.e., user1 is now able to run the SELECT query on Employees.
GRANT SELECT ON Employees TO user1 WITH GRANT OPTION;
This query will grant SELECT privileges on Employees object to user1 and user1 can also grant this privilege to the users.
FOR SYSTEM PRIVILEGES:
GRANT privilege_name TO user_name [WITH ADMIN OPTION];
GRANT CREATE TABLE TO User1;
This will grant system privilege CREATE TABLE to User1;
GRANT CREATE TABLE TO User1 WITH ADMIN OPTION;
This will grant system privilege CREATE TABLE to User1 and User1 will be able to grant the same privileges to other users;
** When revoking privileges, the list of privileges can only be either all system privileges or all object privileges, i.e., we cannot revoke both system and object privileges in the same SQL REVOKE statement.
REVOKE privilege_name ON object_name TO user_name [CASCADE | RESTRICT];
privilage_name: refers to the access right or privilege granted to the user.
object_name: name of a database object like TABLE, VIEW, STORED PROC and SEQUENCE. It represents the objects on which the permission is to be revoked.
user_name: it is the name of the user or role from whom an access right is to be revoked.
[CASCADE|RESTRICT]: This clause is optional. By default, it is a cascade. It determines whether the privileges from the subsequent grantee should be revoked or not.
REVOKE SELECT ON Employees FROM user1;
This query will revoke the SELECT privilege granted to User1 on Employees.
REVOKE SELECT ON Employees FROM user1 ;
This query will revoke the SELECT privilege granted to User1 on Employees.
These are the SQL commands that we use in our Database. Hope you learned something new today.
Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content. You can read more blogs from here.
Connect with us on Twitter and LinkedIn.
Happy Learning 🙂
Team AfterAcademy!
AfterAcademy Tech
In this blog, we will learn about the various differences between SQL and SQL server based on some points.

AfterAcademy Tech
In this blog, we will learn the difference between SQL and MySQL. People use these terms intechangeably. But both are having different meanings.

AfterAcademy Tech
In this blog, we will learn the difference between SQL and NoSQL databases.

AfterAcademy Tech
In this blog, we will study various types of relationships in DBMS which help in defining the association between various entities. Also, we will discuss the types of participation constraints which may exist between the relationship and the entity type.
