zoukankan      html  css  js  c++  java
  • mysql

    select version();
    -- 5.7.31-log
    
    
    -- mysql - alter的运用
    use mysql_study;
    
    drop table if exists `test_alter`;
    
    create table if not exists `test_alter`
    (
    	`id` int unsigned auto_increment,
    	`name` varchar(50),
    	`pwd` char(48),
    	`add_time` date,
    	primary key(`id`)
    )engine = innodb default charset=utf8;
    
    show columns from `test_alter`;
    
    -- 添加字段
    alter table `test_alter` add `test` tinyint;
    show columns from `test_alter`;
    
    -- 删除字段
    alter table `test_alter` drop `test`;
    show columns from `test_alter`;
    
    -- 添加字段并位于第一列或者某一列之后
    alter table `test_alter` add `field_one` int first;
    show columns from `test_alter`;
    alter table `test_alter` add `after_pwd` tinyint after `pwd`;
    show columns from `test_alter`;
    
    -- 调整字段的顺序
    alter table `test_alter` modify `name` varchar(50) first;
    show columns from `test_alter`;
    alter table `test_alter` modify `name` varchar(50) after `id`;
    show columns from `test_alter`;
    
    -- 修改存储引擎
    alter table `test_alter` engine = innodb;
    show table status like 'test%';
    
    -- 删除外键关系
    #alter table `test_alter` drop foreign key keyName;
    
    -- 修改字段类型 modify
    alter table `test_alter` modify `pwd` varchar(50);
    show columns from `test_alter`;
    
    -- 修改字段类型 change
    alter table `test_alter` change `pwd` `pwd` varchar(48);
    show columns from `test_alter`;
    alter table `test_alter` change `pwd` `old_pwd` char(48);
    show columns from `test_alter`;
    
    insert into `test_alter`(`name`) values('张三');
    delete from `test_alter`;
    update `test_alter` set add_time = now();
    select * from `test_alter`;
    -- alter 对null值和默认值的影响
    alter table `test_alter` modify `add_time` date not null; #在有数据的情况下不能修改为非空,可以将null的数据默认指定值
    -- 所以在MYSQL中,如果想要插入时自动获取当前时间,则需要使用timestamp类型,然后赋默认值就可以了。
    alter table `test_alter` alter column `old_pwd` set default '123456';
    show columns from `test_alter`;
    select * from `test_alter`;
    
    -- 删除默认值
    alter table `test_alter` alter `old_pwd` drop default;
    show columns from `test_alter`;
    
    -- 修改表名
    alter table `test_alter` rename to `test_alter1`;
    alter table `test_alter1` rename to `test_alter`;
    

      

  • 相关阅读:
    可以使用多少列创建索引?
    如何显示前 50 行?
    简单描述 MySQL 中,索引,主键,唯一索引,联合索引的区别,对数据库的性能有什么影响-从读写两方面?
    实践中如何优化 MySQL ?
    列的字符串类型可以是什么?
    MySQL 里记录货币用什么字段类型好 ?
    什么是通用 SQL 函数?
    对于关系型数据库而言,索引是相当重要的概念?
    为表中得字段选择合适得数据类型?
    SQL 注入漏洞产生的原因?如何防止?
  • 原文地址:https://www.cnblogs.com/gygtech/p/13674474.html
Copyright © 2011-2022 走看看