• 表相关SQL


    表相关SQL

    什么是表: 关系型数据库中保存数据的单元,类似于Excel中的表格,创建表时需要指定字段信息.

    表的引擎

    • Myisam : 只支持数据基础的增删改查,不支持高级操作, 如:事务,外键等
    • InnoDB : 支持高级操作,默认为InnoDB

    基本表的语法

    创建表

    格式: create table 表明(字段1名 字段1类型,字段2名 字段2类型...);

    例如:

    create table user(
    	id int,
        username char(32),
        password char(32)
    );
    

    查询所有表

    show tables;
    

    查看单个表属性

    show create table 表名;
    

    创建表并且指定字符集和引擎

    create table t1(
        id int,
        name varchar(10)
    )engine = myisam charset=utf8;
    

    查看:

    show create table t1;
    

    查看表字段信息

    desc 表名;
    

    例如: desc student;

    修改表名

    rename table 原名 to 新名;
    

    例如: rename table student to t_stu;

    修改表属性 引擎和字符集;

    alter table 表名 engine=myisam charset=gbk;
    

    例如: alter table hero engine=myisam charset=gbk;

    添加表字段

    • 最后位置添加

      alter table 表名 add 字段 类型;
      

      例如: 给hero表添加age字段,值为int;

      alter table hero add age int;

    • 最前面位置添加

      alter table 表名 add 字段 类型 first;
      

      例如: alter table hero add money int first;

    • 在某个字段后面添加

      alter table 表名 add 字段 类型 after 字段;
      

      例如: alter table hero add genter varchar(5) after name;

    • 删除表字段

      alert table 表名 drop 字段;
      

      例如: alter table hero drop money;

    • 修改表字段名和类型

      alter table 表名 change 原字段名 新字段名 类型;
      

      例如: alter table hero change name heroname varchar(10);

    • 修改表字段类型和位置

      -- 把age位置调到第一
      alter table hero modify age int first;
      -- 把age位置调到gender后面:
      alter table hero modify age int after xxx;
      

    删除表

    drop table 表明;
    

    例如: drop table hero;

    删除表, 并 创建一个新表

    truncate 关键字

    删除t1表并重新创建t1表

    truncate table t1;
    

    truncate,delete,drop的区别:

    • delete: 删除表中的数据 自增数值不清0 支持事务
    • drop: 删除表 不支持事务
    • truncate: 删除表并且创建一个新表 自增数值清零不支持事务
  • 相关阅读:
    javascript
    javascript
    javascript
    easyui datagrid checkbox multiple columns have been done do
    combogrid获取多个字段的方法
    jquery显示、隐藏div的方法
    纠正jQuery获取radio选中值的写法
    comgrid获取多选值
    xheditor
    java向图片上写字,两个图片合并的方法
  • 原文地址:https://www.cnblogs.com/zpKang/p/12997410.html
Copyright © 2020-2023  润新知