Cover Image for MySQL CREATE Table
102 views

MySQL CREATE Table

To create a new table in a MySQL database, you can use the CREATE TABLE statement. Here’s the basic syntax:

CREATE TABLE table_name (
  column1 datatype,
  column2 datatype,
  column3 datatype,
  ...
);
  • table_name: Replace this with the name you want to give to the new table.
  • column1, column2, column3, etc.: Specify the names of the columns in the table.
  • datatype: Define the data type for each column, specifying the type of data that the column will store.

Here’s an example that creates a simple table named “employees” with a few columns:

CREATE TABLE employees (
  employee_id INT PRIMARY KEY,
  first_name VARCHAR(50),
  last_name VARCHAR(50),
  hire_date DATE
);

In this example, we create a table named “employees” with four columns: “employee_id,” “first_name,” “last_name,” and “hire_date.” Each column has its own data type, and the “employee_id” column is also defined as the primary key of the table.

You can add various constraints, such as primary keys, foreign keys, unique constraints, and more to your table definition as needed.

The CREATE TABLE statement allows you to specify more complex table structures, including relationships between tables, default values, and additional constraints. Make sure to refer to the MySQL documentation for more details on advanced table creation options. Additionally, you may need appropriate privileges to create tables in a database.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS