Fork me on GitHub

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 ;

 


 

posted on 2017-12-26 17:48  anyux  阅读(2383)  评论(0编辑  收藏  举报