1. 程式人生 > 實用技巧 >資料庫常用操作

資料庫常用操作

  • 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能重新命名欄位。