The foundations of relational databases are Structured Query Language
Introduction to Structured Query Language
It is important to manage data in databases when people are constantly depending upon the data. SQL is the standard form of language. This language enables the user to have access to manipulate, retrieved, and stored data inside the Relational Database Management System.
A Relational Database Management System (RDBMS) is an accumulation of programs. These programs ensure that the IT experts interact, create, update, and administer using the relational database. The data within the RDBMS is stored in the form of tables. So, to extract and make use of these data, SQL is used.
Functions of SQL
Before you get to know about the functionalities of SQL, it is important to know how you can get structured data from the databases.
Let’s take up an example, the database is a container that contains certain things and you are willing to get the things from the container. So there has to be some way to get the things and things in databases refer to the structured data.
Now, to get these data from the database you need to execute the queries against the databases. These are some of the functionalities that SQL can perform:
- Against the databases, SQL is used to execute queries.
- It also ensures retrieving the data from the databases.
- In databases, SQL can effectively insert records.
- SQL will also help in updating the records in databases.
- For deleting records in databases, SQL can be used.
- SQL can also be used to create new databases.
- For the creation of new tables in new databases, SQL is used.
- The stored procedure can be used in a database using SQL.
- View creation can also be done using SQL.
- Setting up permission and granting access to the databases can also be done using SQL.
Various types of SQL commands
The commands that are mainly used as the SQL are essential to set up communication between RDBMS and the user. These commands are listed in five categories. The illustration is as follows:
- Data Definition Language (DDL)
- Data Modification Language (DML)
- Data Control Language (DCL)
- Transaction Control Language (TCL)
- Data Query Language (DQL)
Data Definition Language (DDL)
DDL has got the SQL commands which are used to define the database schema. The commands are logically used to create and modify the objects within the database. The commands are executed to create, alter, and drop the database structure.
This is used to create the database objects which include tables, functions, indexes, procedures, views, and triggers.
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... );
CREATE TABLE Employee( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
This is used to drop any specific objects from the database.
DROP TABLE table_name;
DROP TABLE Shippers;
This functions to modify certain structures within the objects.
ALTER TABLE table_name
ALTER TABLE Employee ADD Email varchar(255);
The command is specifically used to remove all the data within an object without hampering the actual object structure.
TRUNCATE TABLE table_name;
TRUNCATE TABLE Employee;
if a comment is required in the data dictionary, then the comment function is used.
SELECT * FROM Employee -- WHERE City='California';
This is used to rename an existing object within the database.
ALTER TABLE table_name RENAME TO new_table_name;
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
Data Modification Language (DML)
This is to manipulate the language within the database. Several commands are executed to bring some modifications to the existing database objects.
The list of DML commands is listed as follows:
for inserting data within the objects of the database.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
INSERT INTO Employee(EmployeeName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
for updating the data within the database.
UPDATE table_name SET column1 = value1, column2 = value2, … WHERE condition;
UPDATE Employee SET ContactName = 'Sayantan Giri', City= 'Kolkata' WHERE EmployeeID = 1;
This is to delete unwanted data from the objects. To delete, a specific where clause is being used so that the essential data is not impacted.
DELETE FROM table_name WHERE condition;
DELETE FROM Employee WHERE CustomerName='Abc Def';
- CALL: this is to call a subprogram like JAVA etc.
- LOCK: this is to lock and control the objects that are currently in use.
- EXPLAIN PLAN: this suggests the path through which the data is getting inserted within the objects.
Data Control Language (DCL)
The commands are used to control the access of database objects. It is not necessary that everyone could access the database objects using SQL. Therefore, to prevent unnecessary access to the database DCL commands are used.
The DCL commands include REVOKE and GRANT
When a user is expected not to have access then that particular user is being revoked using the command called REVOKE.
REVOKE select, insert, delete, update on table Employee from user abc;
for some of the objects, the specific user has to have access. At that time the GRANT command is used for granting access to the user within the database objects.
GRANT select, insert, delete, update on table Employee to user bcd;
Transaction Control Language (TCL)
within the database objects, there has to be certain transaction and the TCL commands are used to control the transactions within the database.
Some of the TCL commands are as follows:
- ROLLBACK: when there is an error encountered and you are expecting to eliminate it by going back to the previous state then the ROLLBACK command is performed.
- COMMIT: this is to create an ending line for any of the execution taking place within the database.
- SET TRANSACTION: for certain transactions, specific characteristics are expected. This command will specify those characteristics.
- SAVEPOINT: it creates a saving point within the transaction. Using this command, you can create a partition for a particular transaction.
Data Query Language
The DQL is used for executing queries on the data within the objects of a database. The execution is done by using the SELECT statement. The statement is executed within the database to fetch the data from the database objects and then this data is used for several operations. When a query of a SELECT statement is fired against an object, especially a table then the result gets compiled into a temporary table which gets reflected as output by a program.
This is specifically used for retrieving the data from the database objects.
SELECT column1, column2, ... FROM table_name;
SELECT CustomerName, City FROM Customers;
After going through the above discussions, it becomes easy for an individual to understand SQL. The various types of SQL and its utilities.
This would enlighten users to construct and manipulate a wide range of data within the database. This will also help people to get the correct data that is expected from a bunch or set of data within a table.
[…] this post, we will explore four ways to pick the second-highest salary in MS SQL. For SQL, this is a very frequently asked query during a Job […]