0% found this document useful (0 votes)
41 views13 pages

DDL Command Information and Syntax... Assignment 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views13 pages

DDL Command Information and Syntax... Assignment 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

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.
1. Create
2. Alter
3. truncate
4. drop
Let’s discuss it one by one.
Command-1 :
CREATE :
This command is used to create a new table in SQL. The
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

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)
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

);
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.
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

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;
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

Data Definition Language or DDL commands in


standard query language(SQL) are used to
describe/define the database schema. These
commands deal with database schema creation and its
further modifications. Some popularly known DDL
commands are CREATE, ALTER, DROP, TRUNCATE, and
COMMENT.
There is nothing to get overwhelmed by the names of
DDL commands mentioned in the previous sentence.
We will explore each of these commands in this post,
but first, let us have a look at this summary table.

Command Description

CREATE Used for creating database objects like a databas


and a database table.

ALTER Used for modifying and renaming elements of a


existing database table.

DROP Used for removing an entire database or


database table.

TRUNCATE Used to remove all the records from a databas


DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

table.

COMMENT Used to write comments within SQL queries.

Commands of SQL DDL


Now we will try to understand each of the above
mentioned DDL commands in great detail in the
subsequent sections.
1. CREATE
CREATE is a data definition language(DDL) command
that is used for creating database objects such as
database and database table.
The syntax for creating a database is as follows :

CREATE database practice_db;

We Have created the database. Let us create a


database table now. But for that, we first need to know
the syntax.
The basic syntax for creating a table in SQL is as
follows:
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

CREATE TABLE public.customers


(
column_name_1 datatype [NULL | NOT NULL],
column_name_2 datatype [NULL | NOT NULL],
.
.
.
column_name_n datatype [NULL | NOT NULL]
)
Here is an example to illustrate database table creation
using the CREATE command.
CREATE TABLE public.customer_details
(
customer_id character varying NOT NULL,
customer_name character varying(255) NOT NULL,
location character varying(255) NOT NULL,
amount_spent numeric NOT NULL,
order_id character varying NOT NULL
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

);

2. ALTER
ALTER command in SQL is used to add, rename or
modify, drop/delete columns in an existing database
table. It can further be used to add and remove various
constraints on an existing database table.
The syntax used for altering a table in SQL by adding a
new column is as follows :
ALTER TABLE table_name
ADD (Columnname_1 datatype)
Here is an example to add a new column to an existing
table.
ALTER TABLE customer_details
ADD email_address character varying(255);

The syntax used for renaming a table is as follows:


ALTER TABLE table_name_1
RENAME TO table_new_name;
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

Here is an example to rename an existing database


table.
ALTER TABLE customer_details
RENAME TO customer_may;

The syntax used for altering a table in SQL by deleting


existing columns is as follows :
ALTER TABLE table_name
DROP columnname_1 , columnname_2, ...
Here is an example to remove an existing column from
a database table.
ALTER TABLE customer_may
DROP order_id;

3. TRUNCATE
TRUNCATE TABLE command is used to remove all the
data records from the database table. It deletes all the
rows permanently. Ergo, we cannot perform a rollback
operation to undo a TRUNCATE command.
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

The generic syntax used for writing TRUNCATE


command is as follows :
TRUNCATE TABLE table_name;
Now let us have a look at an example illustrating
TRUNCATE command.
TRUNCATE TABLE customer_may;

We have successfully removed all the records from the


database. Let us check it using the following SELECT
statement.
SELECT * FROM customer_may;

So what do we observe? We observe that the


TRUNCATE command erases all the data from the table
but maintains the table structure.
4. DROP
DROP TABLE SQL command is used to delete a
database object from the database. We can even
delete the database using the DROP command. We
cannot perform a rollback operation to undo a DROP
database/table command.
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

The basic syntax for writing DROP command to delete


a database in SQL is as follows :
DROP DATABASE database_name;
The syntax for writing DROP command to delete a
database in SQL is as follows :
DROP TABLE table_name;
Here are a few examples to illustrate the use of the
DROP command in SQL.
DROP TABLE customer_may;

We have successfully deleted the table. In the previous


section, we learned about the TRUNCATE command.
Let us check how DROP is different from the TRUNCATE
command.
SELECT * FROM customer_may;

From the above image, it can be clearly observed that


unlike the TRUNCATE command DROP statement
deletes the entire table including the table structure.
5. COMMENT
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

Comments in SQL are similar to comments in other


programming languages such as Java, C++, Python, etc.
They are primarily used to define a code section for
easier understanding. Comments can be a single line,
multi-line, or even inline types.
Here are a few examples to illustrate commenting in
SQL.
(i) Single line comment.
-- this is a single line comment
SELECT * FROM customers;
(ii) Multi-line comment.
/* this is a multi line comment
SELECT * FROM customers; */
SELECT customer_id FROM customers;
(iii) Inline comment.
SELECT customer_id FROM customers /* WHERE
store_state = 'KA'*/;
Conclusion
SQL DDL commands are used for creating new
database objects (CREATE command), modifying
DDL COMMAND WITH SYNTAX ,EXAMPLES, INFORMATION

existing database objects (ALTER command), and


deleting or removing database objects (DROP and
TRUNCATE commands).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy