# DDL with MariaDB Connector/C++

C++ developers can use MariaDB Connector/C++ to perform basic DDL (Data Definition Language) operations with MariaDB database products.

## DDL Operations

DDL (Data Definition Language) refers to all SQL-schema statements in the SQL standard (ISO/IEC 9075-2:2016).

Some examples of DDL include [ALTER TABLE](/docs/server/reference/sql-statements/data-definition/alter/alter-table.md), [CREATE TABLE](/docs/server/server-usage/tables/create-table.md), [DROP TABLE](/docs/server/server-usage/tables/drop-table.md), [CREATE DATABASE](/docs/server/reference/sql-statements/data-definition/create/create-database.md), and [TRUNCATE TABLE](/docs/server/reference/sql-statements/table-statements/truncate-table.md).

## Code Example: ALTER TABLE

[ALTER TABLE](/docs/server/reference/sql-statements/data-definition/alter/alter-table.md) is a DDL (Data Definition Language) operation that changes an existing table.

The following code demonstrates how to execute [ALTER TABLE](/docs/server/reference/sql-statements/data-definition/alter/alter-table.md) on the [example table](/docs/connectors/mariadb-connector-cpp/mariadb-connector-cpp-sample-application.md):

```sql
// Includes
#include <iostream>
#include <mariadb/conncpp.hpp>

// Function to Alter Table
void alterTable(std::shared_ptr<sql::Statement>  &stmnt)
{
   try {
      // Alter contacts Table
      stmnt->executeUpdate("ALTER TABLE test.contacts RENAME COLUMN first_name TO f_name");
   }

   // Catch Exception
   catch (sql::SQLException& e) {
      std::cerr << "Error altering table: "
         << e.what() << std::endl;
   }
}

// Main Process
int main(int argc, char **argv)
{
   try {
      // Instantiate Driver
      sql::Driver* driver = sql::mariadb::get_driver_instance();

      // Configure Connection
      // The URL or TCP connection string format is
      // ``jdbc:mariadb://host:port/database``.
      sql::SQLString url("jdbc:mariadb://192.0.2.1:3306/test");

      // Use a properties map for the other connection options
      sql::Properties properties({
            {"user", "db_user"},
            {"password", "db_user_password"},
         });

      // Establish Connection
      // Use a smart pointer for extra safety
      std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));

      // Create a Statement
      // Use a smart pointer for extra safety
      std::shared_ptr<sql::Statement>  stmnt(conn->createStatement());

      // Use Statement to alter table
      alterTable(stmnt);

      // Close Connection
      conn->close();
   }

   // Catch Exceptions
   catch (sql::SQLException& e) {
      std::cerr << "Error Connecting to the database: "
         << e.what() << std::endl;

      // Exit (Failed)
      return 1;
   }

   // Exit (Success)
   return 0;
}
```

Confirm the table was properly altered by using [MariaDB Client](/docs/server/clients-and-utilities/mariadb-client.md) to execute a [DESC](/docs/server/reference/sql-statements/administrative-sql-statements/describe.md) statement on the same table:

```sql
DESC contacts;
```

```sql
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |
| f_name        | varchar(25)  | YES  |     | NULL    |                |
| last_name     | varchar(25)  | YES  |     | NULL    |                |
| email         | varchar(100) | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
```

Code Example: `TRUNCATE TABLE` is a DDL (Data Definition Language) operation that deletes all data from an existing table.

The following code demonstrates how to execute [TRUNCATE](/docs/server/reference/sql-functions/numeric-functions/truncate.md) on the [example table](/docs/connectors/mariadb-connector-cpp/setup-for-connector-cpp-examples.md):

```sql
// Includes
#include <iostream>
#include <mariadb/conncpp.hpp>

// Function to Truncate Table
void truncateTable(std::shared_ptr<sql::Statement>  &stmnt)
{
   try {
      // TRUNCATE contacts Table
      stmnt->executeUpdate("TRUNCATE test.contacts");
   }

   // Catch Exception
   catch (sql::SQLException& e) {
      std::cerr << "Error truncating table: "
         << e.what() << std::endl;
   }
}

// Main Process
int main(int argc, char **argv)
{
   try {
      // Instantiate Driver
      sql::Driver* driver = sql::mariadb::get_driver_instance();

      // Configure Connection
      // The URL or TCP connection string format is
      // ``jdbc:mariadb://host:port/database``.
      sql::SQLString url("jdbc:mariadb://192.0.2.1:3306/test");

      // Use a properties map for the other connection options
      sql::Properties properties({
            {"user", "db_user"},
            {"password", "db_user_password"},
         });

      // Establish Connection
      // Use a smart pointer for extra safety
      std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));

      // Create a Statement
      // Use a smart pointer for extra safety
      std::shared_ptr<sql::Statement>  stmnt(conn->createStatement());

      // Use Prepared Statement to truncate table
      truncateTable(stmnt);

      // Close Connection
      conn->close();
   }

   // Catch Exceptions
   catch (sql::SQLException& e) {
      std::cerr << "Error Connecting to the database: "
         << e.what() << std::endl;

      // Exit (Failed)
      return 1;
   }

   // Exit (Success)
   return 0;
}
```

The following query confirms that the [TRUNCATE](/docs/server/reference/sql-functions/numeric-functions/truncate.md) statement deleted all rows from the [example table](/docs/connectors/mariadb-connector-cpp/setup-for-connector-cpp-examples.md):

```sql
SELECT * FROM test.contacts;
```

```sql
Empty set (0.000 sec)
```

<sub>*This page is: Copyright © 2025 MariaDB. All rights reserved.*</sub>

{% @marketo/form formId="4316" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/connectors/mariadb-connector-cpp/ddl-with-mariadb-connector-cpp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
