zoukankan      html  css  js  c++  java
  • 表的操作

    一:修改表 alter table

    语法
    
    1 修改表名: 
     
        alter table 表名  
                                    rename 新表名;
    
    
    2 增加字段:
         
        alter table 表名  
    
                                     add 字段名   数据类型[完整性约束条件。。。。]
                                     add 字段名   数据类型[完整性约束条件。。。。]
        alter table 表名  
                                     add 字段名   数据类型[完整性约束条件。。。。] first
                                     add 字段名   数据类型[完整性约束条件。。。。] after 字段名
    
    3 删除字段:
       alter table 表名
                 drop 字段名;

    4 修改字段:
      
       alter table 表名
                  modify 字段名 数据类型[完整性约束条件。。。];
       alter table 表名
                  change 旧字段名 新字段名 旧数据类型[完整性约束条件。。。];
       alter table 表名
                  change 旧字段名 新字段名 新数据类型[完整性约束条件。。。];
    
    
    示例:
    1. 修改存储引擎
    mysql> alter table service 
        -> engine=innodb;
    
    2. 添加字段
    mysql> alter table student10
        -> add name varchar(20) not null,
        -> add age int(3) not null default 22;
        
    mysql> alter table student10
        -> add stu_num varchar(10) not null after name;                //添加name字段之后
    
    mysql> alter table student10                        
        -> add sex enum('male','female') default 'male' first;          //添加到最前面
    
    3. 删除字段
    mysql> alter table student10
        -> drop sex;
    
    mysql> alter table service
        -> drop mac;
    
    4. 修改字段类型modify
    mysql> alter table student10
        -> modify age int(3);
    mysql> alter table student10
        -> modify id int(11) not null primary key auto_increment;    //修改为主键
    
    5. 增加约束(针对已有的主键增加auto_increment)
    mysql> alter table student10 modify id int(11) not null primary key auto_increment;
    ERROR 1068 (42000): Multiple primary key defined
    
    mysql> alter table student10 modify id int(11) not null auto_increment;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    6. 对已经存在的表增加复合主键
    mysql> alter table service2
        -> add primary key(host_ip,port);        
    
    7. 增加主键
    mysql> alter table student1
        -> modify name varchar(10) not null primary key;
    
    8. 增加主键和自动增长
    mysql> alter table student1
        -> modify id int not null primary key auto_increment;
    
    9. 删除主键
    a. 删除自增约束
    mysql> alter table student10 modify id int(11) not null; 
    
    b. 删除主键
    mysql> alter table student10                                 
        -> drop primary key;

    二  复制 表

     1:复制表结构+记录(key 不会复制:主键、外键和索引)

    create  table  新表名  select  *  from  旧表名

    2:只复制表结构

    create table 新表名 select * from  旧表名  where 条件(当永远不可能的条件成立时 例如:1>2时) 

     #只复制表结构,不复制数据, 所以查询表的时候 里面的值为空, 查询新表的结构, 与旧表的结构 是一样的。

    表示只复制表结构成功。

    三 删除表

    语法:
    drop table 表名
  • 相关阅读:
    从汇编看c++中的placement operator new
    从汇编看c++的new和delete
    从汇编看c++中全局对象和全局变量
    javascript中的this
    好工具
    js压缩解压工具
    IE的documentMode属性
    77. sqlserver 锁表解决方式
    75. ID重新走过,备份表
    5. Java中序列化的serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/lx3822/p/9014750.html
Copyright © 2011-2022 走看看