To create a table, use the following syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
table_name
: The name of the table to be created.column1
, column2
, ...: The columns you want to add to the table.datatype
: The type of data each column will store (e.g., INT, VARCHAR, DATE).Let’s create a table named employees
with columns for id
, name
, and email
:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
In this example:
id
: An integer column set as the primary key, which automatically increments.name
: A column to store the employee’s name as a string (VARCHAR).email
: A column to store the employee’s email address.Once the table is created, you can begin inserting data into it using the INSERT INTO
statement:
INSERT INTO employees (name, email)
VALUES ('John Doe', 'john@example.com');
The CREATE TABLE
statement is essential for structuring your data in MySQL. By defining columns and their respective data types, you create a table where you can store and manipulate data effectively. Always consider the data type and constraints when designing your tables to ensure data integrity and efficiency.