MySQL-DDL-table structure operation

DDL (table operations)

  • table creation

    • Show how to create a data table with the display of specific code

    • CREATE DATABASE <database name>;
      
      CREATE TABLE <table name> (
        <column name 1> <data type 1> <constraint>,
        <column name 2> <data type 2> <constraint>,
        ...
      )
    • Specific code example:
      • create table tb_user
        (
            id       int comment '用户ID,唯一标识',
            username varchar(20) comment '用户名',
            name     char(20) comment '姓名',
            age      int comment '年龄',
            gender   char(1) comment '性别'
        ) comment '用户信息表'
    • The result of the operation is as follows 

    •  Constraints: Constraints are rules that act on fields in a table to limit the data stored in the table
      • 1. PRIMARY KEY: Used to specify the column as the primary key to ensure its uniqueness and non-nullness.

        2. UNIQUE: The value used to specify the column must be unique, but can be empty.

        3. NOT NULL: The value used to specify the column cannot be empty.

        4. DEFAULT: It is used to specify the default value of the column. When inserting a new row, if the value of the column is not provided, the default value will be used.

        5. FOREIGN KEY: Used to create a foreign key relationship, the specified column is associated with columns in other tables.

        6. CHECK: Used to specify the value range or condition of the column to ensure that the value of the column meets the specified condition.

    • Purpose: To ensure the correctness, validity and integrity of the database
    • Constraints created by the above code

      • The use of specific constraints is demonstrated in the form of code
        • Basic format: attribute name data type constraint
          • The specific code is as follows:
            • create table tb_user
              (
                  id       int primary key comment '用户ID,唯一标识',
                  username varchar(20) not null unique comment '用户名',
                  name     char(20)    not null comment '姓名',
                  age      int comment '年龄',
                  gender   char(1) default '男' comment '性别'
              ) comment '用户信息表'
            • The result of the operation is as follows: 

The primary key in the data table generally adds an auto-increment attribute 

-- 查询所有数据库
create table tb_user
(
    id       int primary key auto_increment comment '用户ID,唯一标识',
    username varchar(20) not null unique comment '用户名',
    name     char(20)    not null comment '姓名',
    age      int comment '年龄',
    gender   char(1) default '男' comment '性别'
) comment '用户信息表'
  •  type of data

    • There are many data types in MySQL, mainly three types: numeric type, string type, date type
      • value type
      • string type

         

      • date type

         

Guess you like

Origin blog.csdn.net/weixin_64939936/article/details/131725790