What Are DDL Commands in SQL?

Introductionย ย 

SQL stands for Structured Query Language and is used to store, alter, and retrieve data from a database server. It can handle big datasets and is very adaptable. DDL commands in SQL define database objects. They allow us to create, delete, and modify tables.ย ย 

DDL commands are called Data Definition Language commands as they construct a database structure. When we save information in a database, we must construct tables and describe their layout.ย ย 

This article will cover the most important DDL commands in SQL.ย 

Categories of SQL Commandsย ย 

DDL, DML, DCL, TCL, and DQL are the five types of SQL commands. Each category is discussed below:ย 

1) DDL – Data Definition Languageย 

DDL commands in SQL are used to construct the database structure in the Data Definition Language. It only deals with database schema descriptions and is used to build and alter the structure of database objects.ย 

It is capable of defining the database schema. It enables the addition, modification, and deletion of logical structures that store data or allow users to access/maintain data.ย 

  • CREATE – to construct a database and its associated items.ย 
  • ALTER – modifies the structure of existing databases.ย 
  • DROP – removes things from databases.ย 
  • TRUNCATE – deletes all records from a table, including the memory allocated for the data.ย 

2) DQL – Data Query Languageย 

DQL commands are used to access data stored in database objects. The purpose of the DQL Commands is to return a schema relation based on the query submitted to it.ย ย 

DQL is a component of a SQL statement that allows you to get and sort data from a database.ย 

Data scientists and analysts use this the most.ย 

  • Select command: It is beneficial for data analysis.ย 

3) DML – Data Manipulation Languageย 

SQL instructions that manipulate data in a database are categorized as DML (Data Manipulation Language), which encompasses the majority of SQL statements. The SQL statement element governs who has access to the data and the database.ย 

It allows us to interact with data that is entered into the database, such asย 

  • SELECT – Gathers data from a table or database.ย 
  • INSERT – Add entries into a table or database.ย 
  • UPDATE – Refreshes existing data in a table/database.ย 
  • DELETE – Clears all database information in a table/databaseย ย 

4) DCL – Data Control Languageย ย 

Data Controlling Language (DCL) is a query language that enables users to obtain and update data from databases. Grant and Revoke are two types of Data Controlling Language commands.ย 

It enables us to grant and revoke permissions on a specific database or table.ย 

  • GRANT – Allows people to have access to the database/table.ย 
  • REVOKE – Removes users’ access rights granted by the GRANT command.ย 

5) TCL – Transaction Control Languageย 

In the database, Transaction Control Language commands govern transactions in the database. This command is used to control modifications to DML statements. TCL allows you to group your arguments into logical transactions.ย 

It makes it easier to commit and cancel modifications on the server. It also permits us to save points anywhere we see fit.ย 

  • COMMIT– Permanently stores all modifications made during the prior transaction.ย 
  • ROLLBACK – Cancels all changes made in the previous transaction.ย 
  • SAVEPOINTS – The save points command creates a name transaction save-point based on the identifier’s name.ย 

What is DDL in SQL?ย ย 

  • DDL in SQL is a language that allows users to specify database components and their relationships.ย 
  • These commands deal with table structure, such as creating, removing, and changing tables.ย 
  • While defining the table structure, DDL commands allow us to specify and update the data types of table columns and the integrity requirements. Integrity constraints are rules that govern how data is stored in a table.ย 
  • All DDL commands are auto-committed, meaning any changes made with them are irrevocably recorded in the database but can be undone.ย 

For instance, if a school wishes to keep its data in a database, we must establish tables such as students and teachers. Additionally, the structure of tables must be defined; for example, the student’s table will include columns such as their roll number, name, gender, birth date, address, and so on.ย 

List Of Commands in DDL with Syntaxย ย 

CREATE:ย 

CREATE is a DDL command in SQL used to create tables or databases. Users provide table names, field names, datatypes for each column, and column widths when we create a table. If the table has any integrity requirements or key criteria, such as PRIMARY KEY, UNIQUE, NOT NULL, etc., we must describe them along with the field names and specifications.ย ย 

Important Key Points

1. The following are some of the most important datatypes in SQL:

  • int/number: Integer numbers are stored.ย 
  • varchar : Stores stringย 
  • date : Stores date

2. A table’s data columns must comply with criteria known as integrity constraints.

3. Examples of SQL integrity constraints are:

  • PRIMARY KEY:A primary key is a unique column in a table that is utilized to uniquely identify each table entry.ย 
  • FOREIGN KEY:A foreign key is a column that is used to describe a connection between two tables by citing another table’s primary key.ย 
  • NOT NULL:The integrity constraint not null specifies that this column cannot be null. It must have some data.ย 

4. Not case-sensitive.

Syntax:ย 

CREATE TABLE table_nameย 

(ย 

column1 datatype ( size of the column ),ย 

column2 datatype ( size of the column ),ย 

column3 datatype ( size of the column ),ย 

column4 datatype ( size of the column ),ย 

column5 datatype ( size of the column ),ย 

ย ย ย  …..ย 

);ย 

Let’s use the CREATE DDL command to build a table that stores student data.ย 

–Making a new table using Create commandย 

CREATE TABLE Schoolย 

(ย 

ย ย ย  –Specifying the table’s field names and data type.ย 

ย ย  Admission_no. INT PRIMARY KEY,ย 

ย ย ย  Name VARCHAR(25),ย 

ย ย ย  Age INT,ย 

ย ย ย  Gender VARCHAR(6)ย 

ย ย ย  Address VARCHAR(40)ย 

);ย 

Output:ย 

Admission_no.ย ย ย ย ย ย ย ย  Nameย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย  Ageย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย  Genderย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย  Addressย 

ย  ย  ย  ย  ย 

 

Using the DDL command CREATE, we have built a new table called School. The table has five columns, which are provided in the command, along with their data types and the size of the column. This table PRIMARY KEY is Admission no, and it is provided when the table is defined using the PRIMARY KEY integrity constraint. In the output section, a new table school with the requested columns is created using the CREATE command. We have not yet entered any data into the table. Therefore, it is empty.ย ย 

ALTER:ย 

ALTER is a DDL command that changes the layout of a database table. Using this SQL command, we can add a new column, remove any column, add or remove integrity requirements, or change the data type of an existing column field in the present table. The ALTER command is used to change the table’s present framework.ย 

Syntax:ย 

ALTER TABLE table_name ADD column_name column-definition;ย 

Example – 1:ย 

Let’s use the ALTER DDL command to add a new column to the School table.ย 

ALTER TABLE School ADD Marks Obtained INT;ย 

Output:ย 

Admission_no.ย ย ย ย ย  Nameย ย ย ย ย ย ย ย ย ย ย ย  Ageย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย  Genderย ย ย ย ย ย ย ย ย  Addressย ย ย ย ย ย ย ย ย  Marks Obtainedย 

ย  ย  ย  ย  ย  ย 

We used the ALTER command in SQL to modify the existing table School. Using the ALTER DDL command in SQL, we introduced a new column named Marks Obtained with data type INT. The field Marks Obtained has been inserted into the existing table, as demonstrated in the output section.ย 

Example – 2:ย 

Let’s look at another example of changing an existing column in the table.ย 

ALTER TABLE School ALTER COLUMN Admission_no. VARCHAR; The ALTER statement is used in this command to alter the data type of the existing column Admission_no. from int to varchar in the School table.ย 

TRUNCATE:ย 

TRUNCATE is an SQL command that deletes all records from a table while preserving its structure. This command is used to delete all of the records and free up the table’s space. The table’s format, including field names and information, remains unaffected. As a result, the table can be utilized later to hold additional data.ย 

Syntax:ย 

TRUNCATE TABLE table_name;ย 

Exampleย 

  1. TRUNCATE TABLE School;ย ย 

Consider the following scenario: the school has begun a new session of students and wants to keep their information in the same framework. They can utilize the same table by deleting the records from the existing table while maintaining the table’s layout. The query above successfully eliminated all of the records from the school table.ย 

DROP:ย 

DROP is a DDL in SQL command used to remove an existing table from the database fully. This command discards the table records as well as the layout of the table. The table and all of its elements are permanently removed from the database and cannot be recovered.ย 

Syntax:ย 

DROP TABLE table_name;ย 

Example:ย 

Let us remove the SCHOOL table completely from the database.ย 

DROP TABLE School;ย 

The DROP command shown above discards the School table from the database.ย 

Comment:ย 

SQL comments are similar to comments in other programming languages like C, Java, C, Python, and more. They are mostly used to designate a code area for easy comprehension. Comments can be of three types:ย 

  • Single-line,ย 
  • Multi-line,ย ย 
  • Inline types.ย ย 

Conclusionย ย 

It is critical to grasp the fundamental DDL commands in SQL to use and alter the dataset. The DDL commands assist in creating, updating, altering, and removing data from tables. Now that you understand โ€œWhat are DDL commands in SQL?โ€, itโ€™s time for you to start practicing via various datasets available over the web. This is the most crucial step to becoming proficient with SQL. You can save yourself a lot of time and energy by enrolling in PG Certificate Program in Data Science and Machine Learning by UNext.ย 

Related Articles

loader
Please wait while your application is being created.
Request Callback