03-命令行_数据库表操作

    1.创建表  
      create table 表名(
        字段名 数据类型 一个或多个约束条件,
        字段名 数据类型 一个或多个约束条件,
        .....
      );   
      例: 
        创建班级表 
        create table classes(
             id int unsigned auto_increment primary key not null,
             name varchar(10)
        );  
        创建学生表 
        create table students(
             id int unsigned auto_increment primary key not null,
             name varchar(20) default '',
             age  tinyint unsigned default 0,
             height decimal(5,2),
             gender enum('male','female','unknown'),
             class_id int unsigned default 0
        );   
        注意:
            1.sql语句都是以英文分号结尾,最后一个字段名后面不能加逗号 
            2.sql语句中注释是用--表示   

    2.删除表 
      drop table 表名;
      例: drop table students;  

    3.修改表 
      (1).添加字段 
          alter table 表名 add 列名 类型;
          例: alter table students add birthday datetime; 
      (2).重命名字段 
          alter table 表名 change 原字段名 新字段名 类型 约束;
          例: alter table students change birthday birth datetime not null; 
      (3).修改字段类型或约束 
          alter table 表名 modify 字段名 类型 约束;
          例: alter table students modify birth date not null; 
      (4).删除字段 
          alter table 表名 drop 字段名;
          例: alter table students drop birth;  


    4.查看 
      (1).查看当前数据库中的所有表 
          show tables;
      (2).查看表结构 
          desc 表名;
      (3).查看表的创建语句 
          show create table 表名;
          例: show create table students;

Last updated

Was this helpful?