DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • SQL Commands: A Brief Guide
  • SQL Query Performance Tuning in MySQL: Part 2
  • The Ultimate Guide To Repair MySQL Database
  • SQL Query Performance Tuning in MySQL

Trending

  • Do We Need Data Normalization Anymore?
  • Vector Tutorial: Conducting Similarity Search in Enterprise Data
  • How To Get Started With New Pattern Matching in Java 21
  • How to Submit a Post to DZone
  1. DZone
  2. Data Engineering
  3. Databases
  4. An Introduction to DDL, DML, and DCL Commands in MySQL: A Comprehensive Guide With Examples

An Introduction to DDL, DML, and DCL Commands in MySQL: A Comprehensive Guide With Examples

This article delves into each category of MySQL commands and their practical applications, providing definitions and illustrative examples.

By 
Vijay Panwar user avatar
Vijay Panwar
·
Feb. 22, 24 · Tutorial
Like (1)
Save
Tweet
Share
2.5K Views

Join the DZone community and get the full member experience.

Join For Free

MySQL is widely recognized as one of the most popular open-source relational database management systems. It holds immense significance in the realm of web development, data analytics, and beyond. Its adaptability and user-friendly nature have positioned it as the preferred choice for managing structured data. 

MySQL commands are classified into various types, primarily based on their purpose within the database. These types encompass Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL). A comprehensive understanding of these commands and their practical applications is essential for individuals involved in MySQL database operations. This article delves into each category, offering precise definitions and illustrative examples to enhance comprehension.

Data Definition Language (DDL)

DDL commands are utilized to establish, modify, or eliminate the framework of the database entities, such as tables, indexes, and schemas. These commands do not manipulate the actual data but rather determine the organization of data within the database. Some typical DDL commands are CREATE, ALTER, DROP, TRUNCATE, and RENAME.

DDL Command Examples

1. CREATE - This command is used to create a new table in the database.

CREATE TABLE students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
enrollment_date DATE
last_update CAST(GETDATE() AS DATE)
);


2. ALTER - Used to modify an existing table, such as adding a new column or changing a data type:

ALTER TABLE students ADD email VARCHAR(255);


3. DROP - Removes the existing table and its data:

DROP TABLE students;


4. TRUNCATE - Deletes all data from a table without removing the table itself:

TRUNCATE TABLE students;


5. RENAME - Changes the name of a table

RENAME TABLE students TO alumni;


Data Manipulation Language (DML)

DML commands are used for managing data within table objects. This includes inserting, updating, deleting, and selecting data. DML commands directly affect the data itself, making them essential for everyday database operations. The primary DML commands are INSERT, UPDATE, DELETE, and SELECT.

DML Command Examples

1. INSERT - Adds new rows of data to a table:

INSERT INTO students (name, enrollment_date) VALUES ('Author Name', '1984-09-01');


2. UPDATE - Modifies existing data in a table:

UPDATE students SET email = 'john.doe@example.com' WHERE student_id = 1;


3. DELETE - Removes rows from a table:

DELETE FROM students WHERE student_id = 1;


4. DELETE, multiple students - Removes multiple rows from a table:

DELETE FROM students WHERE student_id between 2 and 6;


5. SELECT - Retrieves all data from one table:

SELECT * FROM students;


6.  SELECT - Select data between the student_id:

SELECT * FROM students where student_id between 5 and 8;


Data Control Language (DCL)

DCL commands primarily revolve around managing permissions and controlling access to database objects. These commands play a vital role in upholding database security by limiting access exclusively to authorized users. The two commonly used DCL commands are GRANT and REVOKE.

DCL Command Examples

1. GRANT - Gives a user permission to perform specific actions on database objects:

GRANT SELECT, INSERT ON students TO 'author@email.com';


2. REVOKE - Removes specific permissions from a user:

REVOKE INSERT ON students FROM 'author@email.com';


Conclusion

Mastering the utilization of DDL, DML, and DCL commands in MySQL is crucial for proficiently creating and administering databases, manipulating data as required, and guaranteeing secure control over data access. A comprehensive understanding of these commands is essential for harnessing the complete capabilities of MySQL in any data-centric application or system.

Control Language Data definition language Data manipulation language MySQL Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • SQL Commands: A Brief Guide
  • SQL Query Performance Tuning in MySQL: Part 2
  • The Ultimate Guide To Repair MySQL Database
  • SQL Query Performance Tuning in MySQL

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: