美文网首页
创建表、查看、修改、删除

创建表、查看、修改、删除

作者: 栀心_d553 | 来源:发表于2020-02-14 15:36 被阅读0次

    创建表

    ---创建表(任何一种表都必须得有数据库)
    create table if not exists mydb.student(
    --显示的将student表放在mydb这个数据库中
    name varchar(10),
    gender varchar(10),
    number varchar(10),
    age int
    )charset utf8;
    
    --创建数据库表
    ---首先进入数据库
    use mydb;
    --创建表
    create table class(
    name varchar(10),
    room varchar(10)
    )charset utf8;
    

    查看表

    查看表.png
    ----查看所有表
    show tables
    ---查看部分表(以s结尾),以后查的时候,尽量以什么开头的
    show table like `%s`;
    --查看表的创建语句
    show create table student;
    show create table student\g ---\g等价于分号
    show create table student\G ---\G将查到的结构旋转90度,变成纵向
    
    ----查看表结构(三种格式)
    desc class;
    describe class;
    show columns from class;
    

    修改表

    修改表.png
    ----重命名表:student 改成 my_student
    rename table student to my_student;
    ---修改表选项(以字符集为例)
    alter table my_student charset = GBK;
    
    修改.png
    --修改字段(给学生表增加id,放在第一个位置)
    alert table my_student
    add columns id int
    first;
    
    ---将学生表中的number学号字段变成固定长度,并且放在第二位(id之后
    alert table my_student modify number char(10) after id;
    ----修改学生表中的gender字段为sex
    alert table my_student change gender sex varchar(10);
    
    ----删除学生表中的age年龄字段
    alert table my_student drop age;
    

    删除表

    ----删除数据表
    drop table class;---当删除之后文件也会对应删除,不可逆
    

    相关文章

      网友评论

          本文标题:创建表、查看、修改、删除

          本文链接:https://www.haomeiwen.com/subject/uxodfhtx.html