In this article, we will discuss the overview of DDL commands and will understand DDL commands like create, alter, truncate, drop. We will cover each command syntax with the help of an example for better understanding. Let's discuss it one by one.
Overview :
Data Definition Language(DDL) is a subset of SQL and a part of DBMS(Database Management System). DDL consist of Commands to commands like CREATE, ALTER, TRUNCATE and DROP. These commands are used to create or modify the tables in SQL.
DDL Commands :
In this section, We will cover the following DDL commands as follows.
- Create
- Alter
- truncate
- drop
- Rename
Let's discuss it one by one.
Command-1 :
CREATE :
This command is used to create a new table in SQL. The user has to give information like table name, column names, and their datatypes.
Syntax -
CREATE TABLE table_name
(
column_1 datatype,
column_2 datatype,
column_3 datatype,
....
);
Example -
We need to create a table for storing Student information of a particular College. Create syntax would be as below.
CREATE TABLE Student_info
(
College_Id number(2),
College_name varchar(30),
Branch varchar(10)
);
Command-2 :
ALTER :
This command is used to add, delete or change columns in the existing table. The user needs to know the existing table name and can do add, delete or modify tasks easily.
Syntax -
Syntax to add a column to an existing table.
ALTER TABLE table_name
ADD column_name datatype;
Example -
In our Student_info table, we want to add a new column for CGPA. The syntax would be as below as follows.
ALTER TABLE Student_info
ADD CGPA number;
Command-3 :
TRUNCATE :
This command is used to remove all rows from the table, but the structure of the table still exists.
Syntax -
Syntax to remove an existing table.
TRUNCATE TABLE table_name;
Example -
The College Authority wants to remove the details of all students for new batches but wants to keep the table structure. The command they can use is as follows.
TRUNCATE TABLE Student_info;
Command-4 :
DROP :
This command is used to remove an existing table along with its structure from the Database.
Syntax -
Syntax to drop an existing table.
DROP TABLE table_name;
Example -
If the College Authority wants to change their Database by deleting the Student_info Table.
DROP TABLE Student_info;
Command -5
RENAME:
It is possible to change name of table with or without data in it using simple RENAME command.
We can rename any table object at any point of time.
Syntax -
RENAME TABLE <Table Name> To <New_Table_Name>;
Example:
If you want to change the name of the table from Employee to Emp we can use rename command as
RENAME TABLE Employee To EMP;
Similar Reads
Essential Unix Commands Unix commands are a set of commands that are used to interact with the Unix operating system. Unix is a powerful, multi-user, multi-tasking operating system that was developed in the 1960s by Bell Labs. Unix commands are entered at the command prompt in a terminal window, and they allow users to per
7 min read
Basic SQL Commands Structured Query Language (SQL) is the standard language used for managing and interacting with relational databases. Whether we are retrieving data, updating records, or defining the structure of our data, SQL commands provide a powerful and flexible way to handle these tasks.This article will expl
5 min read
Basic Linux Commands for day to day life Linux is a popular desktop, embedded, and server operating system that is strong and adaptable. Knowing the basics of commands will greatly increase your productivity and simplicity of use if you use Linux. Essential Linux commands that are useful for daily operations will be covered in this article
3 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
df Command in Linux with Examples There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e. 'df' command that
9 min read
du command in Linux with examples The `du` command in Linux is a powerful utility that allows users to analyze and report on disk usage within directories and files. Whether you're trying to identify space-hogging directories, manage disk space efficiently, or simply gain insights into storage consumption, the du command provides va
6 min read
od command in Linux with example The od (octal dump) command in Linux is a versatile tool used to display file contents in various formats, with the default being octal. This command is particularly useful for debugging scripts, examining binary files, or visualizing non-human-readable data like executable code. It allows users to
6 min read
fc Command in Linux with Examples As we all know that LINUX is command friendly and while working on LINUX, you may deal with very long commands that may include long paths or really difficult syntax, and imagine what if working with such commands you do a minor mistake which will require re-writing of the entire command synopsis an
3 min read
SQL | DDL, DML, TCL and DCL Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL) form the backbone of SQL. Each of these languages plays a critical role in defining, managing, and controlling data within a database system, ensuring both structural
6 min read
rm command in Linux with examples rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file w
5 min read