• mysql create table 语法详解


    create table 可以分成三类

    一、一般create table 语句

      1  语法

    create [temporary] table [if not exists] tbl_name
        (create_definition)
        [table_options]
        [parttion_options]

      2  例子:创建一个person表它包涵id,name,birthday这几个列

    create table person(id int not null auto_increment,
        name varchar(8),
        birthday datetime,
        constraint pk__person primary key(id));

    二、create table like 参照已有表的定义,来定义新的表

      1  语法  

    create [temporary] table [if not exists] tbl_name
    {like old_tbl_name | (like old_tbl_name)};

      2  例子:定义一个person_like 表,它的表结构参照上面例子中的person表

    mysql> create table person_like like person;
    Query OK, 0 rows affected (0.01 sec)
    
    
    mysql> show create table person_like G
    *************************** 1. row ***************************
           Table: person_like
    Create Table: CREATE TABLE `person_like` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(8) DEFAULT NULL,
      `birthday` datetime DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)

      可以看出使用create table like 方式创建的表,它的表结构和原表是一样的。

    三、根据select 的结果集来创建表

      1  语法

    create [temporary] table [if not exists] tbl_name
        [(create_definition,...)]
        [table_options]
        [partition_options]
        [ignore | replace]
        [as] query_expression

      2  例子:

    mysql> create table person_as 
        -> as 
        -> select id,name from person;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> show create table person_as G
    *************************** 1. row ***************************
           Table: person_as
    Create Table: CREATE TABLE `person_as` (
      `id` int(11) NOT NULL DEFAULT '0',
      `name` varchar(8) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    1 row in set (0.00 sec)

      

    ----

  • 相关阅读:
    shell中括号的特殊用法 linux if多条件判断
    uboot kernel 博客
    meson 中调用shell script
    200. 岛屿数量
    9. 回文数
    53. 最大子序和
    394. 字符串解码
    32. 最长有效括号
    leetcode排序的题 912. 排序数组 215. 数组中的第K个最大元素
    c++引用和运算符重载思考
  • 原文地址:https://www.cnblogs.com/JiangLe/p/7009205.html
Copyright © 2020-2023  润新知