Home  >  Article  >  Database  >  What command is used to create a basic table in sql

What command is used to create a basic table in sql

下次还敢
下次还敢Original
2024-05-07 04:39:14887browse

The command to create a basic table in SQL is CREATE TABLE. It is used to create a basic table with column names, data types and constraints like NOT NULL, DEFAULT.

What command is used to create a basic table in sql

Commands to create basic tables in SQL

In SQL, use CREATE TABLE command to create basic tables.

Syntax:

<code>CREATE TABLE table_name (
  column_name1 data_type [NOT NULL | NULL] [DEFAULT default_value],
  column_name2 data_type [NOT NULL | NULL] [DEFAULT default_value],
  ...
  column_nameN data_type [NOT NULL | NULL] [DEFAULT default_value]
);</code>

Parameters:

  • table_name: Table name.
  • column_name: Column name.
  • data_type: The data type of the column (for example, INT, VARCHAR, DATE).
  • NOT NULL | NULL: Specify whether the column allows NULL values.
  • DEFAULT default_value: Specifies the default value of the column (if specified).

Example:

Create a table named customers with customer_id (primary key), first_name, last_name and email columns:

<code class="sql">CREATE TABLE customers (
  customer_id INT NOT NULL AUTO_INCREMENT,
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  email VARCHAR(100) UNIQUE NOT NULL
);</code>

Note:

  • Primary Key Columns are defined using PRIMARY KEY constraints.
  • Unique index columns are defined using UNIQUE constraints.
  • Auto-increment columns are defined using the AUTO_INCREMENT keyword.

The above is the detailed content of What command is used to create a basic table in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn