Day 2 of SQL Series || Basic Commands

akshat0610

Akshat Sharma

Posted on November 23, 2024

Day 2 of SQL Series || Basic Commands

Hey there πŸ‘‹

Hope you are doing well πŸ™‚

In the last blog we have discussed about basic introduction to SQL. In this blog we are going to see some of the basic commands of SQL. We will be discussing DDL (Data Definition Language) commands in this blog.

So let's get started πŸ”₯

Image description

SQL Commands

Before starting our discussion I want tell you that you can practice SQL on the available online SQL compilers. I am attaching link of one here πŸ‘‡

https://www.programiz.com/sql/online-compiler/

In SQL, DDL (Data Definition Language) commands are used to define, modify, and manage the structure of database objects such as tables, indexes, and schemas. Here’s a comprehensive list of all commonly used DDL commands:

  • CREATE

  • ALTER

  • DROP

  • TRUNCATE

  • RENAME

  • COMMENT

  • USE

  • SHOW

CREATE Command

The CREATE command is create new database objects such as tables, views, indexes, and databases.

  • Create a Database -:
    Image descriptionNote -: The statement will throw an error when you are working on an online compiler.

  • Create a Table -:
    Image description Example -:
    Image description
    So here we have created a table named student_record that contains three type of data student's name which will be a string of atmost 100 characters, student's age which will be of integer type and student's ID which will also be a string of atmost 30 characters.

Note that duplicate column names (same name and same type) are not allowed.

SHOW Command

Used to display the metadata about database objects, such as tables or databases.

  • Show Database
    Image description

  • Show Tables
    Image description

  • Show Table Structure
    Image description
    This command DESCRIBE is used to see complete table structure it gives complete detail of all columns in table.

USE Command

This command is used to select a specific database to work with in a session.
Image description

COMMENT Command

It is used to add comments to database objects like tables or columns for documentation purposes.

  • Add a Comment to a Table
    Image description

  • Add a Comment to a Column
    Image description

ALTER Command

It is used to modify existing database objects such as tables, columns, and constraints.

  • Add a Column Image description For example, Suppose we have a table that stores customers' data. Image description Now we will add an "Email" column here. Image description Image description

-Modify a Column
Image description
Here we are changing data type of column.
Note that the ALTER TABLE syntax can vary depending on the SQL dialect (e.g., MySQL, PostgreSQL, SQL Server, etc.) you're using. Some may support a particular keyword some may throw error.

DROP Command

It is used to delete database objects such as tables, databases, or indexes. This operation removes the object and its data permanently.
Image description

RENAME Command

It is used to rename a database object, such as a table or a view.
Image description

TRUNCATE Command

It is used to remove all rows from a table without logging individual row deletions, resetting the table for new data. This does not affect the table structure. This means the table become empty.
Image description

So these were all DDL Commands of SQL. In the next blog we will see some DML commands and will run different queries on table.
I hope you liked my blog. Please leave some ❀ and don't forget to follow me.
Also have you checked my Instagram page where I upload necessary resources for software engineers.
If not check it out here -:
πŸ‘‰ Instagram: https://www.instagram.com/fluxx_96/
πŸ‘‰ YouTube: https://www.youtube.com/@Fluxx-n4q/shorts

Thankyou πŸ’™

πŸ’– πŸ’ͺ πŸ™… 🚩
akshat0610
Akshat Sharma

Posted on November 23, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related