• Mysql 常用增删改查命令集合教程


    创建:create      插入:insert  更新:update   

    查询:select  删除:delete  修改:alter     销毁:drop

    创建一个数据库:
      create database 数据库名 [其他选项];
      create database `samp_db`;
    创建数据库表:
      create table 表名称(列声明);
      create table `students`
      (
        `id` int unsigned not null auto_increment primary key,
        `name` char(8) not null,
        `sex` char(4) not null,
        `age` tinyint unsigned not null,
        `tel `char(13) null default "-"
        )ENGINE=MyISAM charset=utf8;
    向表中插入数据:
      insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

      insert into `students` set `name`='王刚',`sex`='男',`age`='20',`tel`='13811371377';
      insert into `students` values(NULL, "王刚", "男", 20, "13811371377");
    查询表中的数据:
      select 列名称 from 表名称 [查询条件];
      select `name`,`age` from `students`;
    或者使用通配符查询:
      select * from `students`;
    按特定条件查询:
      select 列名称 from 表名称 where 条件;
      select * from `students` where `sex`="女";
      where 子句条件支持(=、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等,还可以对查询条件使用 or 和 and 进行组合查询)
      select * from `students` where `age` > 21;
      select * from `students` where `name` like "%王%";
      select * from `students` where `id`<5 and `age`>20;
    更新表中的数据:
      update 表名称 set 列名称=新值 where 更新条件;
      将id为5的手机号改为默认的"-":

        update `students` set `tel`=default where `id`=5;

      将所有人的年龄增加1:

        update `students` set `age`=age+1;
      将手机号为 13288097888 的姓名改为 "张伟鹏", 年龄改为 19:

        update `students` set `name`="张伟鹏", `age`=19 where tel="13288097888";
    删除表中的数据:
      delete from 表名称 where 删除条件;
      删除id为2的行:

        delete from `students` where `id`=2;
      删除所有年龄小于21岁的数据:

        delete from `students` where `age`<20;
      删除表中的所有数据:

        delete from `students`;

    创建后表的修改:
    添加列:
      alter table 表名 add 列名 列数据类型 [after 插入位置];
      在表的最后追加列 address:

         alter table `students` add `address` char(60);
      在名为 age 的列后插入列birthday:

        alter table `students` add `birthday date after `age`;

    修改列:
      alter table 表名 change 列名称 列新名称 新数据类型;
      将表 tel 列改名为 telphone:

        alter table `students` change `tel` `telphone` char(13) default "-";
      将 name 列的数据类型改为 char(16):

         alter table `students` change `name` `name` char(16) not null;
    删除列:
      alter table 表名 rename 新表名;
      删除 birthday 列:

         alter table students drop `birthday`;
    重命名表:
      alter table 表名 rename 新表名;
      重命名 students 表为 workmates:

        alter table `students` rename `workmates`;
    删除整张表:
      drop table 表名;
      删除 workmates 表:

          drop table `workmates`;
    删除整个数据库:
      drop database 数据库名;
      删除 samp_db 数据库:

         drop database `samp_db`;

  • 相关阅读:
    传真机换色带
    系统无法安装 OfficeControl.ocx 控件如何解决
    体系结构
    WebOffice 插件
    ArcMAP定义投影坐标
    如何在AutoCAD中将卫星底图变为有坐标参考信息的
    AutoCAD中导入图片
    CGCS2000坐标系与其他坐标系间的差异和转换方法
    FTP服务器(SOCKET)返回异常 500 Command not understood
    Java微信公众平台开发--番外篇,对GlobalConstants文件的补充
  • 原文地址:https://www.cnblogs.com/siichen/p/3766610.html
Copyright © 2020-2023  润新知