SQL Fundamental

Overview

SQL is a flexible computer language that you can deploy in different ways to communicate with relational databases. This software has some distinct features that differentiates it from other programming applications. First and foremost, SQL is a nonprocedural language. Most computer programs (e.g., C, C++ and Java) solve problems by following a sequence of commands that is called a procedure. In this case, one specific operation is performed after another until the required task has been accomplished. The flow of operation can either be a linear sequence or a looping one, depending on what the programmer had specified. This is not the same for SQL.

Keywords in SQL

Screenshot 2022-11-29 at 23.37.16.png

SQL structure

  • Schema – This is also called the conceptual view or the complete logical view that defines the entire database structure and provides overall table organisation. Such schema is considered metadata – stored in tables and part of the database (just like tables that consist of regular data).

  • Domain – This specifies the set of all finite data values you can store in a particular table column or attribute.

  • Constraint – Often ignored but one of the important database components, this sets down the rules that identify what data values a specific table attribute can contain. Incorporating tight constraints assures that database users only enter valid data into a particular column. Together with defined table characteristics, column constraints determine its domain

SQL command types

Data Definition Language (DDL)

Data Definition Language (or simply DDL) enables you to create, change or restructure, and even destroy the basic elements that are contained in a relational database. DDL focuses only on the structure, not the data contained within the elements. These basic elements or data objects include tables, schemas, views and more. Stated below are some of the most common DDL commands:

CREATE

This command statement is responsible for building the database structure. Its syntax is:

  • CREATE TABLE
  • CREATE VIEW

ALTER

This command statement is in charge of changing the database structure after it has been created. Its syntax is:

  • ALTER TABLE

  • ALTER VIEW

DROP

This command is the reverse of the CREATE statement, which destroys the database structure. Its syntax is:

  • DROP TABLE

  • DROP VIEW

Data Manipulation Language (DML)

Data Manipulation Language (or simply DML) consists of SQL commands that handle data maintenance functions. This means that you are able to manipulate the data contained within the relational database objects. The command statements, which read like normal English sentences, will allow you to enter, change, remove or retrieve data. The following are the DML statements commonly used:

INSERT

This command statement is used to insert new data

UPDATE

This command statement is used to modify or alter pre-existing data values in a table, not add or remove records. The update is done one table at a time or multiple rows/columns of one table within a database.

DELETE

This command statement deletes certain records or even the entire table, not data values from specific columns.

Data Query Language (DQL)

Data Query Language (or simply DQL) consists of commands that perform data selection, which is the main focus of relational database users in the world of SQL. The statement used is SELECT that can be accompanied by other clauses or options so that your extracted results will be in an organized and readable format

Data Control Language (DCL)

Data Control Language (or simply DCL) consists of commands that allow you to manage data access within the database. Furthermore, the database is protected from accidental or intentional misuse by controlling user privileges. DCL concentrates on transactions, which capture all SQL statements that perform database operations and save them in a log file. The following are the common DCL command statements:

GRANT

This statement provides you with certain privileges, like giving you the permission to access the database.

GRANT PRIVILEGE1, PRIVILEGE2, … TO USER_NAME

REVOKE

This statement revokes your privileges, like removing your permission to access the database. Its syntax is:

REVOKE PRIVILEGE1, PRIVILEGE2, … TO USER_NAME

Transactional Control Commands

Transactional control commands allow users to manipulate various transactions in maintaining database integrity. In SQL, transactions begin when applications are executed. The very first transaction is started at the onset of the SQL application, while the last transaction is ended when the application is terminated. The following are the common transactional control commands:

COMMIT

This statement completes a transaction by making the changes you made to the database permanent, or simply saving the transactions. Its syntax is:

COMMIT [WORK]; 
#In the previous command line, the keyword WORK is optional

ROLLBACK

This statement’s primary function is to restore the database system to its previous state or undo all the actions that took place in the transaction log. Its syntax is:

ROLLBACK [WORK];
 #In the previous command line, the keyword WORK is optional

SAVEPOINT

This statement works with the ROLLBACK command, wherein it creates sections or points within groups of transactions in which you will be performing the ROLLBACK command. Its syntax is:

SAVEPOINT SAVEPOINT_NAME