Getting started with SQL commands
By: Bansi Patel
Tags: Stackoverflow

#An introductory guide

#DDL, DML, TCL (I will be using PostgreSQL for performing operations)

We live in a data-driven world people search through data to find insights to inform strategy, marketing, operations of other categories. There are a lot of companies that use relational databases which makes a thorough understanding of SQL a great employable skill for all.

STRUCTURED QUERY LANGUAGE

Introduction:

SQL stands for Structured Query Language.SQL is used to communicate (store data/manage data) with RDBMS(Relational Database Management System). It is used to perform data operations on RDBMS.

SQL follows different ways to manipulate data. The basic commands in SQL are bifurcated into 3 commands:

DDL

DML

TCL

1. DDL: Data Definition Language

This language consists of SQL commands to perform particular changes to the database. It gives descriptions of the database schema and is used to create and modify the structure of database objects in the database.

Note: All the DDL commands are auto committed. That means the changes are permanently saved in the database.

The following are DDL commands:

Create: It is used to create a new table or database.

CREATE DATABASE

Creating a table student under database test:

CREATE TABLE STUDENT

2. Alter: It is used for the alteration purpose of the entire data structure.

Adding a column in student table:

Adding an Email column to the student table

3. Drop: It is used to delete data/tables from the database.

Drop table student

4. Truncate: It is used to remove all records from a table, including all spaces allocated for the records are removed.

Truncating student table(Assuming that data already exists in the table):

Truncating student table

5. Rename: It is used to rename a table in an existing database.

Changing the column “name” to “full_name”

2. DML: Data Manipulation Language

DML commands are used for manipulating the data stored in the table and not the table itself. DML commands are not auto-committed. It means changes are not permanent to the database, they can be rolled back.

The following are DML commands:

Insert: insert a new row i.e new record in the table/values in the table.

To insert values in student table:

Inserting values

2. Update: This command is used to update entries in the table.

Updating student name ‘Ben’ to ‘Taylor’:

updated table

3. Delete: The DELETE statement is used to delete existing records in a table.

Now delete a record whose name is Prince:

3. TCL: Transaction Control Language

These commands are executed to check on other commands and their effects on the database. These commands are annulled changes made by other commands by rolling the data back to its original state. It can also make any temporary change permanent.

TCL commands are as follows:

Commit: The COMMIT command is the transactional command used to save changes invoked by a transaction to the database.

To delete student information where id=3:

Commit

2. Rollback: The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database. This command can only be used to undo transactions since the last COMMIT or ROLLBACK command was an issue.

To delete student information where id=3 and then rollback the changes in the database:

Rollback

3. Savepoint: A SAVEPOINT is a point in a transaction when you can roll the transaction back to a certain point without rolling back the entire transaction.

Creating savepoint before deletion of students with id=2 and 1d=4

Savepoint

And we are done thank you for reading.

Reference: Stackoverflow

HAPPY CODING :)