美文网首页
4. 表相关SQL

4. 表相关SQL

作者: 天空165 | 来源:发表于2018-03-06 01:20 被阅读0次

    [toc]

    表相关SQL

    表是数据库中数据组成单位,类似于Java中的对象 表的字段对应对象的属性

    创建表

        create table person(id int,name varchar(10),age int);
        create table person(id int,name varchar(10)) engine=myisam charset=utf8;
        create table person(id int comment'用户id',age int comment'用户年龄')//表字段备注;
    

    创建表原理:
    在客户端中写完创建表的sql语句后,客户端会把sql语句交给DBMS,DBMS解析后会在数据库中创建表和表中的字段

    表的引擎:

    • InnoDB:支持数据库高级处理 包括事务,外键等 默认是InnoDB
    • myisam:只支持数据的基本储存
      单引号和`的区别

    '单引号:用于给字符串赋值
    `:用户给表名和字段名赋值 可以省略不写
    添加表字段

        - 在最后添加
        alter table person add age int;
        - 在最前面添加
        alter table person add age int first;
        - 在某个字段之后添加
        alter table person add age int after name;
    

    删除字段

        alter table person drop age;
    

    删除表

        drop table person;
    

    1. 修改表名

       rename table person to persong1;
      
    2. 修改表属性

       alter table person engine=myisam charset=gbk;
      
    3. 修改字段名和类型

       alter table person change age age1 varchar(10);
      
    4. 修改字段类型和顺序

       alter table person modify age int [after name/first];
      

    1. 查看所有表

       show tables;
      
    2. 查看表属性 引擎,编码,备注

       show create table person;
      
    3. 查看表结构

       desc person;

    相关文章

      网友评论

          本文标题:4. 表相关SQL

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