zoukankan      html  css  js  c++  java
  • 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)

      

    ----

  • 相关阅读:
    eclipse中的任务标记(TODO、FIXME、XXX)
    编码规范参考
    MVC,MVP 和 MVVM
    Android的两种事件处理机制
    在Eclipse中自定义类似syso的快捷代码模板
    Android
    eclipse使用tips-Toggle Mark Occurrences 颜色更改
    从 Eclipse 迁移至 Android Studio
    Java的位运算符详解实例——与(&)、非(~)、或(|)、异或(^)
    [POJ 2976]Dropping tests(0-1分数规划)
  • 原文地址:https://www.cnblogs.com/JiangLe/p/7009205.html
Copyright © 2011-2022 走看看