#插入、查找数据

[mysql>create table if not exists exam_score(
..>id int(4) not null primary key auto_increment,
..>name char(20) not null,
   ..>score double(6,2)); #用多个list插入多行数据
[mysql> insert into exam_score values (1,'Zhao',95.33),(2,'Qian',94.33),(3,'Sun',44.55),(4,'Li',33.55);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0

#展示整张表
mysql> select * from exam_score;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
| 3 | Sun | 44.55 |
| 4 | Li | 33.55 |
+----+------+-------+
4 rows in set (0.00 sec)

mysql> insert into exam_score values (5,'Zhou',66.33),(6,'Wu',99.32),(7,'Zheng',33.2),(8,'Wang',99.3);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0

#查询按照字段id在0到2之间的行数据
mysql> select * from exam_score order by id limit 0,2;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
+----+------+-------+
2 rows in set (0.00 sec)

#查询整张表中的前两行
mysql> select * from exam_score limit 0,2;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
+----+------+-------+
2 rows in set (0.00 sec)

#按照name字段升序显示整张表
mysql> select * from exam_score order by name asc;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 4 | Li | 33.55 |
| 2 | Qian | 94.33 |
| 3 | Sun | 44.55 |
| 8 | Wang | 99.30 |
| 6 | Wu | 99.32 |
| 1 | Zhao | 95.33 |
| 7 | Zheng | 33.20 |
| 5 | Zhou | 66.33 |
+----+-------+-------+
8 rows in set (0.01 sec)

#按照name降序显示
mysql> select * from exam_score order by name desc;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 5 | Zhou | 66.33 |
| 7 | Zheng | 33.20 |
| 1 | Zhao | 95.33 |
| 6 | Wu | 99.32 |
| 8 | Wang | 99.30 |
| 3 | Sun | 44.55 |
| 2 | Qian | 94.33 |
| 4 | Li | 33.55 |
+----+-------+-------+
8 rows in set (0.00 sec)

#where条件查询
mysql> select * from exam_score where name='Li';
+----+------+-------+
| id | name | score |
+----+------+-------+
| 4 | Li | 33.55 |
+----+------+-------+
1 row in set (0.00 sec) mysql> select * from exam_score where id=3;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 3 | Sun | 44.55 |
+----+------+-------+
1 row in set (0.00 sec) mysql> select * from exam_score where name='Zhao' and score<=99.0;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Zhao | 95.33 |
+----+------+-------+
1 row in set (0.00 sec)

#修改数据

#连接表所在的数据库
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> select * from exam_score;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
| 3 | Sun | 44.55 |
| 4 | Li | 33.55 |
| 5 | Zhou | 66.33 |
| 6 | Wu | 99.32 |
| 7 | Zheng | 33.20 |
| 8 | Wang | 99.30 |
+----+-------+-------+
8 rows in set (0.00 sec)

#用update修改符合条件的字段的值

#通过条件定位到行,修改行的某些字段的值

#也可以修改多个字段的值,中间用逗号隔开

mysql> update exam_score set score =62.5 where name='Zheng' and score<60.0;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

#配合replace修改符合条件的字段的值

#replace(字段,旧值,新值)
mysql> update exam_score set name=replace(name,'Wu','Xie') where id=6;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from exam_score;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
| 3 | Sun | 44.55 |
| 4 | Li | 33.55 |
| 5 | Zhou | 66.33 |
| 6 | Xie | 99.32 |
| 7 | Zheng | 62.50 |
| 8 | Wang | 99.30 |
+----+-------+-------+
8 rows in set (0.00 sec)

删除记录/数据表

#删除表

mysql> delete from exam_score where id=4;
Query OK, 1 row affected (0.00 sec)
#delete清空整张表,表还在只是空了
mysql> delete from exam_score;
Query OK, 7 rows affected (0.00 sec)

mysql> select * from exam_score;

Empty set (0.00 sec)

#drop清空表

mysql> drop table exam_score;
Query OK, 0 rows affected (0.01 sec) mysql> select * from exam_score;
ERROR 1146 (42S02): Table 'test.exam_score' doesn't exist

程序媛计划——mysql 插入、查找、修改、删除数据的更多相关文章

  1. 程序媛计划——mysql基本操作

    本文适用于mac 在官网上下载community 版mysql,选择dmy这种.在终端中安装好mysql. #进入mysql /usr/local/mysql/bin/mysql -uroot -p ...

  2. 程序媛计划——mysql外键

    定义 外键:如果一个表的某个字段指向另一个表的主键,就称之为外键.被指向的表,称之为主表,也叫父表,那么另一个表就是从表,也叫子表 #先新建两个表 mysql> create table aut ...

  3. mySQL 插入,更新和删除数据

    插入数据: 语法: INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); 如 ...

  4. 程序媛计划——mysql修改表结构

    #查看表的结构 mysql> desc score; +------------+--------------+------+-----+---------+----------------+ ...

  5. 程序媛计划——mysql索引

    定义: 索引是一种单独的.物理的对数据库表中一列或多列的值进行排序的一种存储结构   #为字段创建索引 #在表中的字段中创建索引mysql> create index ind_score on ...

  6. 程序媛计划——mysql连接表

    #inner join等值连接/内连接 mysql> select * from info; +------+-------------+----------+ | name | phone | ...

  7. 教你如何6秒钟往MySQL插入100万条数据!然后删库跑路!

    教你如何6秒钟往MySQL插入100万条数据!然后删库跑路! 由于我用的mysql 8版本,所以增加了Timezone,然后就可以了 前提是要自己建好库和表. 数据库test, 表user, 三个字段 ...

  8. Mysql中查找并删除重复数据的方法

    (一)单个字段 1.查找表中多余的重复记录,根据(question_title)字段来判断 代码如下 复制代码 select * from questions where question_title ...

  9. MySQL入门很简单: 9 插入 更新与删除数据

    1. 插入数据:INSERT 1)为表的所有字段插入数据 第一种: 不指定具体的字段名 INSERT INTO 表名 VALUES(值1,值2,...,值n): 第二种:INSERT语句中列出所有字段 ...

随机推荐

  1. vc通过webbrowser操作ie元素

    1>需要引用 webbrowser2.h,mshtml.h //m_web绑定的webbrowser的变量 CComQIPtr<IHTMLDocument2,&IID_IHTMLD ...

  2. Tomcat:A cookie header was received[xxxxxx] that contained an invalid cookie. That cookie will be ignored.

    搬运来源:https://blogs.yahoo.co.jp/dk521123/36721868.html 原因: *从Tomcat 8,Cookie的解析已经符合RFC 6265. *由于RFC 6 ...

  3. Apache模块开发

    一.简介 Apache HTTP服务器是一个模块化的软件,使管理者可以选择核心中包含的模块以裁剪功能.可以在编译时选择被静态包含进httpd二进制映象的模块,也可以编译成独立于主httpd二进制映象的 ...

  4. linux 下安装gult

    本文假设你之前没有用过任何任务脚本(task runner)和命令行工具,一步步教你上手Gulp.不要怕,它其实很简单,我会分为五步向你介绍gulp并帮助你完成一些惊人的事情.那就直接开始吧. 第一步 ...

  5. jquery获取radio单选框的值

    1.获取原有单选框的值 var value=$("input[name='is_setting']:checked").val(); 2.获取重选后的单选框的值 <tr> ...

  6. DB2错误码

    SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 + 未限定的列名被解释为一个有相互联系的引用 + 动态SQL语句用分号结束 + 没有找到满足SQL语句的行 + 用DATA CAPTUR ...

  7. tensorflow的transpose

    从图中看出来perm=[1,0,2] 表示第一个维度和第二个维度进行交换. 默认的是[0,1,2]   所以perm=[1,0,2] 表示第一个维度和第二个维度进行交换.0,1,2表示index.

  8. 使用bat批处理文件备份postgresql数据库

    @echo offset pgsql_path=d:\"Program Files"\PostgreSQL\9.3\bin\   //安装目录set database=postgr ...

  9. linux上安装tomcat

    这里采用离线解压tar.gz的方式安装 下载: wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.0.33/bin/apache-tomc ...

  10. 记录:Web相关政策之备案号、视频播放

    (一)备案号链接: 服务器在国内的网站受工信部监管,并受其颁布的<管理办法>约束.根据<互联网信息服务管理办法>及<非经营性互联网信息服务备案管理办法>的法律法规, ...