程序媛计划——mysql 插入、查找、修改、删除数据
#插入、查找数据
[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 插入、查找、修改、删除数据的更多相关文章
- 程序媛计划——mysql基本操作
本文适用于mac 在官网上下载community 版mysql,选择dmy这种.在终端中安装好mysql. #进入mysql /usr/local/mysql/bin/mysql -uroot -p ...
- 程序媛计划——mysql外键
定义 外键:如果一个表的某个字段指向另一个表的主键,就称之为外键.被指向的表,称之为主表,也叫父表,那么另一个表就是从表,也叫子表 #先新建两个表 mysql> create table aut ...
- mySQL 插入,更新和删除数据
插入数据: 语法: INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); 如 ...
- 程序媛计划——mysql修改表结构
#查看表的结构 mysql> desc score; +------------+--------------+------+-----+---------+----------------+ ...
- 程序媛计划——mysql索引
定义: 索引是一种单独的.物理的对数据库表中一列或多列的值进行排序的一种存储结构 #为字段创建索引 #在表中的字段中创建索引mysql> create index ind_score on ...
- 程序媛计划——mysql连接表
#inner join等值连接/内连接 mysql> select * from info; +------+-------------+----------+ | name | phone | ...
- 教你如何6秒钟往MySQL插入100万条数据!然后删库跑路!
教你如何6秒钟往MySQL插入100万条数据!然后删库跑路! 由于我用的mysql 8版本,所以增加了Timezone,然后就可以了 前提是要自己建好库和表. 数据库test, 表user, 三个字段 ...
- Mysql中查找并删除重复数据的方法
(一)单个字段 1.查找表中多余的重复记录,根据(question_title)字段来判断 代码如下 复制代码 select * from questions where question_title ...
- MySQL入门很简单: 9 插入 更新与删除数据
1. 插入数据:INSERT 1)为表的所有字段插入数据 第一种: 不指定具体的字段名 INSERT INTO 表名 VALUES(值1,值2,...,值n): 第二种:INSERT语句中列出所有字段 ...
随机推荐
- Spring框架的AOP技术(注解方式)
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...
- 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 ...
- Golang之Mysql操作
话说当年武大郎对着电脑一顿噼里啪啦,,,对mysql增删改查 增加insert package main import ( "fmt" "github.com/jmoir ...
- 如何看待阿里 AI 每秒制作 8000 张海报?
看了其他设计老师们的回答,给了我一些启发,于是更新一波. 设计本质上是产品和服务的一部分,如果只站在设计师角度看这问题,免不了会有一种被取代的危机感. 来源:千锋UI 但如果站在整个产品和服务的角度 ...
- [Jmeter]通过批处理调用java,java从CSV动态读取登录的用户名和密码,并将其作为参数组合成字符串,写入外部.bat文件,然后通过Java执行这个外部批处理文件
问题1:怎样通过批处理调用java代码? 问题2:怎样通过java从CSV文件获取到用户名和密码存入变量? 问题3:怎样将获取到的用户名和密码组合成字符串,写入外部批处理文件? 问题4:怎样在批处理文 ...
- db2学习笔记
a.服务端安装 v11.1_win64_expc.zip 官网下载 b.客户端安装 Toad for DB2 Freeware 6.1 百度找找 .建数据库 create database HRA_G ...
- 使用Jsoup获取网页内容超时设置
使用Jsoup获取网页内容超时设置 最近使用Jsoup来抓取网页,并对网页进行解析,发现很好用.在抓取过程中遇到一个问题,有些页面总是报Timeout异常,开始想是不是被抓取网站对IP进行了限制,后来 ...
- 2018.06.29 NOIP模拟 边的处理(分治+dp)
边的处理(side.cpp) [问题描述] 有一个 n 个点的无向图,给出 m 条边,每条边的信息形如<x,y,c,r><x,y,c,r><x,y,c,r>. 给出 ...
- 2018.09.07 codeforces311B. Cats Transport(斜率优化dp)
传送门 斜率优化dp好题. 对于第i只猫,显然如果管理员想从出发开始刚好接到它,需要在t[i]=h[i]−dist(1,i)" role="presentation" s ...
- hdu-1143(简单dp)
题目链接: 思路:利用前一个状态找到本次状态需要的次数,就是递推. 建立一个二维数组dp[i][j] ,i表示行,j表示多余的格子. 可以分为三种状态dp[i][0], dp[i][1] ,dp[i] ...