maria-db
  1. maria-db-create-table

Create Table - (MariaDB Tables)

In MariaDB, tables can be created using the CREATE TABLE statement. This statement allows you to specify the name of the table, the names and data types of its columns, and any constraints or indexes.

Syntax

The syntax for creating a new table in MariaDB is as follows:

CREATE TABLE table_name (
    column1 datatype [optional_parameters] [column_constraint],
    column2 datatype [optional_parameters] [column_constraint],
    ...
    table_constraint
) ENGINE = engine_type [other_parameters];

Here, table_name is the name of the new table that you want to create. column1, column2, and so on represent the names of the individual columns in the table, along with their data types and any optional parameters or constraints. table_constraint represents constraints that apply to the entire table, such as a primary key or a foreign key. engine_type specifies the storage engine that you want to use for the table.

Example

Here is an example of creating a new table in MariaDB that stores information about employees:

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    hire_date DATE,
    job_title VARCHAR(50),
    salary DECIMAL(15,2)
) ENGINE = InnoDB;

Output

Executing the CREATE TABLE statement will create a new table called employees with columns for employee ID, first name, last name, hire date, job title, and salary.

Explanation

In the above SQL code, we are creating a new table called employees with columns for employee ID (which is set as the primary key), first name, last name, hire date, job title, and salary. The size and type of each column are defined using data types such as VARCHAR and DECIMAL. The ENGINE parameter specifies that the table should use the InnoDB storage engine.

Use

Creating new tables in MariaDB allows you to store and manage data in a structured way. Tables can be used to store information about customers, employees, products, and a variety of other entities. They can be linked together using foreign keys to create complex relationships between data.

Important Points

  • MariaDB uses the CREATE TABLE statement to create new tables.
  • Tables consist of columns that are defined by their names, data types, and any constraints or indexes.
  • Constraints can be applied to the entire table or individual columns, such as primary keys or foreign keys.
  • The storage engine determines how data is stored and retrieved from the table.

Summary

In summary, creating new tables in MariaDB is an essential aspect of database management. Tables can be used to store different types of data about various entities and can be linked together using foreign keys to create relationships between data. The CREATE TABLE statement in MariaDB allows you to define the structure of a new table, its columns, and constraints.

Published on: