Basic SQL Commands:

SQL: Structured Query Language

Basic SQL Commands:

Introduction

Structured Query Language, commonly known as SQL, is a vital tool for managing and manipulating data within relational database management systems (RDBMS). SQL empowers organizations to store, retrieve, and manipulate data efficiently. In this blog, we will explore the world of SQL, from its fundamental concepts to advanced techniques, and discuss its significance in the modern data-driven world.

What is SQL

  • SQL stands for Structured Query Language, you access and manipulate databases

  • SQL is very flexible, based on applications SQL is portable they can be run on any system.

  • SQL is a high-level language and it is free-format syntax which gives users the ability to structure SQL statements on any of the screens.

SQL Clauses

  1. Select:

    It is used to retrieve or fetch data from a database.

    Example: select * from Customer;

  2. Where:

    It corresponds to select operation. This clause is used to define a predicate.

    Example: select ADDRESS from Customer where ID=3;

  3. From:

    SQL from clause is the source of a rowset to be operated upon in a DDL statement.

    Example: select NAME, ADDRESS from Customer;

Create a database

--Syntax: 
Create database database_name;
--Example:
Create database Myadmin;

--To drop database
drop database Myadmin;

SQL Commands

DDL(Data Definition Language)

DDL commands are used for the definition and creation of objects in a database like a table. DDL commands are mainly used for the design and definition of the structure of the database. SQL DDL commands are CREATE, ALTER, DROP, RENAME, and TRUNCATE as explained in the following topics.

  1. Create: It is used to create a new table.

    The Create Table command consists of the following constraints:

    Unique Constraint ensures that no two rows have the same value in the specified columns.

    Primary key Constraint declares a column as the primary key of the table.

    Check constraint limits value that can be inserted into a column of a table.

    Default Constraint assigns a default value that can be specified for a column using the default clause.

     ---Creating a basic table involves naming the table and defining its columns and each column's data type.
     ---Syntax:
     create table table_name(column1 datatype, column2 datatype, ...);
     ---Example:
     create table Customer(id int primary key, name varchar(20),age int);
    
  2. Alter

    The alter table command is used to modify the definition of table by modifying the definition of its columns.

     ---Syntax
     alter table table_name add column_name datatype;
     ---example:
     alter table Customer add address char(50);
    
  3. Drop

    The drop table statement is used to remove a table definition and all data, indexes, constraints and permission specifications for that table.

     ---Syntax:
     Drop table table_name;
     ---example:
     drop table Customer;
    
  4. Truncate

    Truncate table command is used to delete complete data from an existing table.

     ---Syntax:
     truncate table table_name;
     ---example:
     truncate table Customer;
    
  5. Rename

    rename command is used to rename a table

     ---syntax:
     rename table old_table_name to new_table_name;
     ---example:
     rename table Customer to CUSTOMER1;
    

DML(Data Manipulation Language)

DML Commands are used for manipulating data in the database. DML commands are used for storing, retrieving, modifying, and deleting data.

  1. Insert

     ---Syntax first:
     insert into table_name(column1, column2,...) values(value1, value2,...);
     ---Example:
     insert into Customer(ID, NAME,AGE) values(1,'Dhanashri',22);
     ---Syntax Second: 
     insert into table_name values(value1,value2,...);
     ---Example:
     insert into Customer values(1,'Dhanashri',22);
    
  2. Update

    The update statement is used to modify existing rows in a table. we can use the WHERE clause with an update query to update selected rows otherwise all the rows would be affected.

     ---Syntax:
     update table_name set column1=value1, column2=value2..., columnN=valueN where[condition];
     ---Example:
     update Customer set address='Pune' where ID=6;
    
  3. Delete

    The delete statement is used to delete rows(records) from a table. Here we use the WHERE clause with a delete query to delete selected rows, otherwise, all the records would be deleted.

     ---Syntax:
     delete from table_name where [condition];
     ---Example:
     delete from Customer where ID=6;
     ---If we want to delete all records from Customer table, we do not need to use where clause and delete query would be:
     delete from Customer; ---now Customer table would not have any record
    

DQL(Data Query Language)

DQL statements are used for performing queries on the data within schema objects. The purpose of the DQL Command is to get some schema relation based on the query passed to it. It includes the SELECT statement. This command allows getting the data out of the database to perform operations with it. When a SELECT is fired against a table or tables the result is compiled into a further temporary table, which is displayed or perhaps received by the program i.e. a front-end.

Select

--Syntax:
Select * from Table_name; -- It extracts all data from a database
--Example:
Select * from Customer;
--Syntax:
select column1, column2 from Table_name where[condition]; --for extracting perticular data from database
--Example:
select NAME, ADDRESS from Customer where ID=3;

DCL(Data Control Language)

DCL includes commands such as GRANT and REVOKE which mainly deal with the rights, permissions, and other controls of the database system.

  1. Grant

     --It gives users access privileges to the database.
     GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
    
  2. Revoke

     --It withdraws the user’s access privileges given by using the GRANT command.
     REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
    

TCL(Transaction Control Language)

Transactions group a set of tasks into a single execution unit. Each transaction begins with a specific task and ends when all the tasks in the group are completed. If any of the tasks fail, the transaction fails. Therefore, a transaction has only two results: success or failure.

  1. Commit

     --Commits a Transaction.
     COMMIT;  --Syntax
    
  2. Rollback

     --Rollbacks a transaction in case of any error occurs.
     ROLLBACK;  --Syntax
    
  3. Savepoint

     --Sets a save point within a transaction.
     SAVEPOINT SAVEPOINT_NAME;  --Syntax
    

Summary
In this article, we provide an overview of SQL, a crucial tool for managing and manipulating data within relational database management systems. We discuss its fundamental concepts and advanced techniques, covering SQL clauses, database creation, and various SQL commands such as DDL, DML, DQL, DCL, and TCL. With examples provided for each command, this guide serves as a comprehensive introduction to the world of SQL and its importance in today's data-driven world.

Did you find this article valuable?

Support Dhanashri Shelake by becoming a sponsor. Any amount is appreciated!