SQL Commands Cheat Sheet with Examples PDF

 SQL Commands

  • SQL commands are the instructions. Which are used to communicate with the database. SQL commands are also used to perform specific tasks, functions, and queries of data.

  • SQL commands can perform different tasks like creating a table, adding data to tables, dropping the table, modifying the table set permission for users.
SQL Commands Cheat Sheet with Examples PDF

Types of SQL Commands with Example:

There are five types of SQL commands

1) DDL

DDL stands for Data Definition Language these commands are used change the structure of the table like creating a table, deleting a table, altering a table, etc. All DDL commands are auto-committed that means they permanently save all the changes in the database.

Here are some commands that come under DDL:

i) CREATE: The CREATE SQL command is used to create a new table in the database.

Syntax:

CREATE TABLE_NAME (COLUMN_NAME DATATYPE)

Example:

CREATE TABLE student(Name VARCHAR2(50), Email VARCHAR2(50), DOB DATE)


ii) DROP: The DROP command is used to delete structure and the record stored in the table.

Syntax:

DROP TABLE table_name

Example:

DROP TABLE student


iii) ALTER: The ALTER command is used to change the structure of the database. This may be used to change the characteristics of an existing attribute or to create a new attribute.

Syntax to Add New Column

ALTER TABLE table_name ADD column_name COLUMN-definition

Syntax to Modify Existing Column

ALTER TABLE table_name MODIFY(column_definitions....)

Example

ALTER TABLE student ADD(location VARCHAR2(20))
ALTER TABLE student MODIFY (student_name VARCHAR2(20))


iv) TRUNCATE: The TRUNCATE command is used to delete all the rows from the given table.

Syntax:

TRUNCATE TABLE table_name

Example:

TRUNCATE TABLE student


2) DML

DML stands for Data Manipulation Language and these commands are used to modify the database. The DML commands are responsible for all forms of changes in the database. The command of DML is not auto-committed.

Here is the list of DML commands:

i) INSERT: The INSERT statement is used to insert data into the row of a table.

Syntax:

INSERT INTO table_name (col1, col2, col3,.... col N) VALUES (value1, value2, value3, .... valueN)

Example:

INSERT INTO students (name, website) VALUES ("Raju", "Geeks Help")


ii) DELETE: The DELETE statement is used to remove one or more rows from a table based on the given condition.

Syntax

DELETE FROM table_name [WHERE condition]

Example:

DELETE FROM students WHERE id = 1


3) DCL

DCL stands for Data Control Language and these commands are used to grant and revoke authority from any database user.

i) GRANT: The GRAND statement is used to give user access privileges to the database.

Syntax:

GRANT SELECT, UPDATE ON my_table TO one_user, another_user


ii) REVOKE: The REVOKE command is used to take back permissions from the user.

Syntax:

REVOKE SELECT, UPDATE ON my_table FROM firstUser, secondUser


4) TCL

TCL stands for Transaction Control Languages. The TCL commands can only be used with DML commands like INSERT, DELETE, and UPDATE only.

i) COMMIT: The commit command saves every operation to the database. It enables the user to permanently store each change made to a database or table transaction. Once you execute the COMMIT, the database cannot go back to its previous state in any case.

Syntax:

COMMIT;

Example:

DELETE FROM students WHERE id = 25;
COMMIT;


ii) ROLLBACK: The rollback command is used to undo transactions that have not already been saved to the database.

Syntax:

ROLLBACK;

Example:

DELETE FROM students  
WHERE id = 25;
ROLLBACK;


iii) SAVEPOINT: The SAVEPOINT is used to roll the transaction back to a certain point without rolling back the entire transaction.

Syntax:

SAVEPOINT SAVEPOINT_NAME;


5) DQL

DQL stands for Data Query Language and it is used to fetch the data from the database.

i) SELECT: The SELECT command in SQL is used to select the attribute based on the condition.

Syntax:

SELECT expressions FROM TABLES WHERE conditions

Example:

SELECT student_name FROM students WHERE age > 20


SQL Commands Cheat Sheet with Examples PDF: Download Now

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad