数据库常用操作

  • use students;

  • desc students;

  • 修改表名:alter table 原表名 rename as 新表名

  • 删除指定数据库:drop database 数据库名;

  • 删除指定表:drop table [if exists] 表名;

  • 创建数据表:

    ```
    create table if not exists `students`(
    `id` int(4) not null auto_increment comment '学号',
    `name` varchar(20) not null default '匿名' comment '姓名',
    `pwd` varchar(20) not null default '123456' comment '密码',
     `birth` datetime default null comment '出生日期',
    primary key(`id`) ) engine = innodb default charset = utf8;
    ```
    
  • 添加表的字段:alter table 表名 add 添加的字段 属性(字段的数据类型);

    比如:```alter table students add sex varchar(2);```
    
  • 删除表的字段:alter table 字段所在数据表名 drop 需要删除的字段;

    比如:```alter table students drop sex;```
    

引申:modify和change的区别

  1. modify:能修改字段类型和约束,不能重命名字段。

  2. change:不能修改字段类型和约束,能重命名字段。

  3. 综上所述:modify能修改字段类型和约束,change能重命名字段。

原文地址:https://www.cnblogs.com/OFSHK/p/14160000.html