📜  dml vs ddl sql (1)

📅  最后修改于: 2023-12-03 15:14:45.139000             🧑  作者: Mango

DML vs DDL SQL

SQL (Structured Query Language) is a programming language used to manipulate data in relational databases. The two main categories of SQL statements are Data Manipulation Language (DML) and Data Definition Language (DDL). In this article, we will explore the differences between DML and DDL SQL statements.

Data Manipulation Language (DML)

DML statements are used to manipulate the data in a database. The main DML statements are:

  • SELECT - used to retrieve data from one or more tables
  • INSERT - used to insert new data into a table
  • UPDATE - used to update existing data in a table
  • DELETE - used to delete data from a table

These statements are used to manipulate the data stored in a database. For example, if you want to retrieve all the records from a table, you would use the SELECT statement. If you want to add a new record to a table, you would use the INSERT statement.

Here is an example of a simple SELECT statement:

SELECT * FROM customers;

This statement will retrieve all the records from the customers table.

Data Definition Language (DDL)

DDL statements are used to define or modify the structure of a database. The main DDL statements are:

  • CREATE - used to create a new table or database
  • ALTER - used to modify the structure of an existing table
  • DROP - used to delete a table or database
  • TRUNCATE - used to delete all the data from a table

These statements are used to create, modify or delete the structure of a database. For example, if you want to create a new table, you would use the CREATE statement. If you want to modify the structure of an existing table, you would use the ALTER statement.

Here is an example of a simple CREATE statement:

CREATE TABLE customers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL
);

This statement will create a new table called customers with three columns: id, name, and email.

Conclusion

In summary, SQL statements can be divided into two main categories: DML and DDL. DML statements are used to manipulate the data stored in a database, while DDL statements are used to define or modify the structure of a database. As a programmer, it is important to understand the differences between these two categories and how to use them effectively in your code.