zoukankan      html  css  js  c++  java
  • SQL入门表定义

    1.DDL管理数据库-表定义

    表名 列定义 列名称  属性 数据类型 约束 默认值 

    1.1创建表

    create table anyux.test (id int);

    创建多个列

    create table anyux.t1(
    idcard int ,
    name char(30),
    sex char(10)
    );

     1.2 create table 语句

    创建表
        CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
            (create_definition,...)
            [table_options]
            [partition_options]
    创建列
        create_definition:
            col_name column_definition
          | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
              [index_option] ...
          | {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
              [index_option] ...
          | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
              [index_name] [index_type] (index_col_name,...)
              [index_option] ...
          | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
              [index_option] ...
          | [CONSTRAINT [symbol]] FOREIGN KEY
              [index_name] (index_col_name,...) reference_definition
          | CHECK (expr)
    列定义
        column_definition:
            data_type [NOT NULL | NULL] [DEFAULT default_value]
              [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
              [COMMENT 'string']
              [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
              [STORAGE {DISK|MEMORY|DEFAULT}]
              [reference_definition]
    View Code

    1.3 查看表定义

    desc anyux.t1;
    
    

    2.修改表定义

    2.1修改表名

    rename table anyux.t1 to anyux.test1;
    alter table anyux.test1 rename to anyux.t1;

    2.2修改表结构

    2.2.1添加新的列

    --添加新的列
    alter table anyux.t1 add addr char(40) NOT NULL;
    --在所有列最后创建列
    alter table anyux.t1 add age int NOT NULL after sex; 
    --在指定列之后创建列
    alter table anyux.t1 add pid int(10) NOT NULL first;
    --将列放在最前面
    --创建多列
    alter table anyux.t1 add ( id int , comment char(40) );

     2.2.2添加删除列

    --删除列
    alter table anyux.t1 drop pid;
    --修改列结构
    alter table anyux.t1 modify name char(20);
    --修改列名称
    alter table anyux.t1 change name pepolename char(30);
    --也可以移动出来
    alter table anyux.t1 change pepolename name char(30) after name ;
    
    
  • 相关阅读:
    CF741C.Arpa’s overnight party and Mehrdad’s silent entering [构造 二分图染色]
    CF719E. Sasha and Array [线段树维护矩阵]
    洛谷7月月赛
    CF666B. World Tour
    BZOJ4668: 冷战 [并查集 按秩合并]
    水题练习 2
    CF715B. Complete The Graph
    关于最短路、负环、差分约束系统的一点笔记
    关于最小生成树,拓扑排序、强连通分量、割点、2-SAT的一点笔记
    hdu1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/anyux/p/8119275.html
Copyright © 2011-2022 走看看