Home  >  Article  >  Database  >  How to create a database table

How to create a database table

青灯夜游
青灯夜游Original
2021-05-08 11:07:2967079browse

In the database, you can use the "CREATE TABLE" statement to create a table. The syntax format is "CREATE TABLE table name (column name 1 data type (maximum length) 1 [,...] column name n data type ( Maximum length)n);".

How to create a database table

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

Creation of tables in database

The process of creating a data table is the process of specifying the attributes of the data columns, and it is also the process of implementing data integrity (including entity integrity, reference integrity and domain integrity) constraints.

Basic syntax

In MySQL, you can use the CREATE TABLE statement to create a table. The syntax format is:

CREATE TABLE <表名> ([表定义选项]);

Among them, the format of [table definition option] is:

<列名1> <数据类型(size)1> [,…] <列名n> <数据类型(size)n>

The main syntax and usage instructions of the CREATE TABLE statement are as follows:

CREATE TABLE: For creating a table with a given name, you must have table CREATE permissions.

  • 722e3d59fd24604761db25f00f9b264f: Specifies the name of the table to be created, given after CREATE TABLE, and must comply with the identifier naming rules. The table name is specified as db_name.tbl_name to create the table in a specific database. It can be created this way regardless of whether there is a current database. db-name can be omitted when creating a table in the current database. If you use quoted distinguished names, the database and table names should be quoted separately. For example, 'mydb'.'mytbl' is legal, but 'mydb.mytbl' is not.

  • f52e9980a8ff3ab048a5a8e987465670: Table creation definition, consisting of column name (col_name), column definition (column_definition), and possible null value specifications, integrity constraints, or table indexes composition.

  • size: Specifies the maximum length of columns in the table.

By default, the table is created in the current database. If the table already exists, there is no current database, or the database does not exist, an error will occur.

Tip: When using CREATE TABLE to create a table, you must specify the following information:

  • The name of the table to be created is not case-sensitive, and keys in the SQL language cannot be used. words, such as DROP, ALTER, INSERT, etc.

  • The name and data type of each column (field) in the data table. If you create multiple columns, separate them with commas.

Example:

Select the database test_db to create the table

mysql> USE test_db;
Database changed

Create the tb_emp1 data table

mysql> CREATE TABLE tb_emp1
    -> (
    -> id INT(11),
    -> name VARCHAR(25),
    -> deptId INT(11),
    -> salary FLOAT
    -> );
Query OK, 0 rows affected (0.37 sec)

Use SHOW TABLES statement to check whether the data table is created successfully

mysql> SHOW TABLES;
+--------------------+
| Tables_in_test_db  |
+--------------------+
| tb_emp1            |
+--------------------+
1 rows in set (0.00 sec)

Related free learning recommendations: mysql video tutorial

The above is the detailed content of How to create a database table. 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