操作MySQL数据库

向表中插入数据

insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下:

insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

其中 [] 内的内容是可选的, 例如, 要给 samp_db 数据库中的 students 表插入一条记录, 执行语句:

insert into students values(NULL, "王刚", "男", 20, "13811371377");

按回车键确认后若提示 Query Ok, 1 row affected (0.05 sec) 表示数据插入成功。 若插入失败请检查是否已选择需要操作的数据库。

有时我们只需要插入部分数据, 或者不按照列的顺序进行插入, 可以使用这样的形式进行插入:

insert into students (name, sex, age) values("孙丽华", "女", 21);

查询表中的数据

select 语句常用来根据一定的查询规则到数据库中获取数据, 其基本的用法为:

select 列名称 from 表名称 [查询条件];

例如要查询 students 表中所有学生的名字和年龄, 输入语句 select name, age from students; 执行结果如下:

也可以使用通配符 * 查询表中所有的内容, 语句: select * from students;

按特定条件查询:

where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;

以查询所有性别为女的信息为例, 输入查询语句: select * from students where sex="女";

where 子句不仅仅支持 "where 列名 = 值" 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。 还可以对查询条件使用 or 和 and 进行组合查询, 以后还会学到更加高级的条件查询方式, 这里不再多做介绍。

示例:

查询年龄在21岁以上的所有人信息: select * from students where age > 21;

查询名字中带有 "王" 字的所有人信息: select * from students where name like "%王%";

查询id小于5且年龄大于20的所有人信息: select * from students where id<5 and age>20;

更新表中的数据

update 语句可用来修改表中的数据, 基本的使用形式为:

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 语句用于删除表中的数据, 基本用法为:

delete from 表名称 where 删除条件;

使用示例:

删除id为2的行: delete from students where id=2;

删除所有年龄小于21岁的数据: delete from students where age<20;

删除表中的所有数据: delete from students;

创建后表的修改

alter table 语句用于创建后对表的修改, 基础用法如下:

添加列

基本形式: 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 表名 drop 列名称;

示例:

删除 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;

最新文章

  1. 【原】让H5页面适配移动设备全家 - 前端篇 - PPT
  2. Image Wall - jQuery &amp; CSS3 图片墙效果
  3. apt-get update更新源时,出现“Hash Sum mismatch”问题
  4. Raspberry Pi 学习笔记之一
  5. java系列: 在eclipse中调试时,输入的jsp或者servlet页面的地址要区分大小写
  6. 滑雪 分类: POJ 2015-07-23 19:48 9人阅读 评论(0) 收藏
  7. JAVA 数组作业——动手动脑以及课后实验性问题
  8. 牛客_剑指offer_重建二叉树,再后续遍历_递归思想_分两端
  9. 原生Js 两种方法实现页面关键字高亮显示
  10. insmod: can&#39;t insert &#39;led.ko&#39;: invalid module format详细解释
  11. 安装php扩展phpredis
  12. CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]
  13. 【转】反编译D-Link路由器固件程序并发现后门
  14. python第十八天 多态 和 私有
  15. 使用WebSocket帮助应用程序群集节点间通信
  16. 使用spark DStream的foreachRDD时要注意哪些坑?
  17. vs2013——单元测试&amp;&amp; 性能图
  18. PAT L2-022 重排链表
  19. keepalive的工作原理和如何做到健康检查
  20. openssl 生成证书

热门文章

  1. Maven项目Update Project...后JRE System Library会自动变回1.5解决办法
  2. Python的paramiko模块ssh操作
  3. html5小趣味知识点系列(一)required
  4. python基础-------python2.7教程学习【廖雪峰版】(二)
  5. J - 组合
  6. java拾遗2----XML解析(二) SAX解析
  7. Excel 文件下载
  8. python学习-4-类的使用
  9. awk 字符串函数
  10. LeetCode:汇总区间【228】