MySQL 增删改查(基础 + 详解)

🤞目录🤞

💖1. CRUD

💖2. 新增(Create)

 2.1 单行数据 + 全列插入

 2.2 多行数据 + 指定列插入

💖3. 查询(Retrieve)

3.1 全列查询

3.2 指定列查询

3.3 查询字段为表达式

3.4 别名

3.5 去重:DISTINCT

3.6 排序:ORDER BY

3.7 条件查询:WHERE

3.8 分页查询:LIMIT

💖4. 修改(Update)

💖5. 删除(Delete)


1. CRUD🚄

注释:在 SQL 中可以使用 “-- 空格 + 描述 来表示注释说明
-- "-- "表示注释说明

CRUD 即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写。

2. 新增(Create)🚄

案例:

-- 创建一张学生表
DROP TABLE IF EXISTS student;
CREATE TABLE student (
    id INT,
    sn INT comment '学号',
    name VARCHAR(20) comment '姓名',
    qq_mail VARCHAR(20) comment 'QQ邮箱'
);

 2.1 单行数据 + 全列插入

(大小写都可)
-- values 中的值的顺序,必须严格按照建表时的顺序
INSERT INTO student VALUES (100, 10000, '张三', NULL);
INSERT INTO student VALUES (101, 10001, '李四', '11111');
insert into student values (102, 10002, '王五', '22222');

 2.2 多行数据 + 指定列插入

insert into student values (103, 10003, '宋江', null), 
(104, 10004, '卢俊义', '44444');
-- 没有出现的字段,填充默认值
insert into student (id,sn,name) values (105, 10005, '公孙胜'),
(106, 10006,'吴用');

3. 查询(Retrieve)🚄

 案例:

-- 创建考试成绩表
DROP TABLE IF EXISTS exam_result;
CREATE TABLE exam_result (
	id INT,
	name VARCHAR(20),
	chinese DECIMAL(3,1),
	math DECIMAL(3,1),
	english DECIMAL(3,1)
);

-- 插入测试数据
insert into exam_result (id, name, chinese, math, english) values
    (1,'武松', 80, 92, 67), 
    (2,'石秀', 86, 72, 97),
    (3,'花容', 75, 98, 88),
    (4,'柴进', 97, 85, 78),
    (5,'李应', 87, 93, 87),
    (6,'鲁智深', 89, 90, 96);

3.1 全列查询

-- 慎用
SELECT * FROM exam_result;

3.2 指定列查询

select id, name from exam_result;

 

3.3 查询字段为表达式

select id from exam_result;
select name, id from exam_result;

select 103 from exam_result;
select id, name, 103 from exam_result;
select 100 / 2 from exam_result;
select id * 10 from exam_result;
select id, name, english + math + chinese from exam_result;
-- 和null比较值都为null, 不是 0 or 1
-- 有 null 参与的运算结果还是 null
-- 即使是 null,做bool值的时候,也看作 false

select * from student;
select id = 102 from student;
select id,id = 102 from student;
select id,id != 102 from student;
select id , name, qq_mail != null from student;
select id , name, qq_mail != 120  from student;
select id , name, qq_mail != null  from student;
select id , name, qq_mail + 10  from student;
select id = 102,id + 9901 from student;
select id = 102,id - 9901 from student;
select id , name, qq_mail + 10  from student;

3.4 别名🚄

  as
select id, name, english + math + chinese as 总分 from exam_result;

select id, name, english + math + chinese as total from exam_result;
select id,name,chinese as 语文,math as 数学,english as 英语 from exam_result;
select id,name,chinese as 语文,math as 数学,english as 英语 ,chinese + math + english as 总分 from exam_result;
-- as 可以省略
select id, name, english + math + chinese 总分 from exam_result;

3.5 去重:DISTINCT

使用 DISTINCT 关键字对某列数据进行去重
select distinct math from exam_result;

3.6 排序:ORDER BY

语法:
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...] 
 ORDER BY column [ASC|DESC], [...];
1. 没有 ORDER BY 子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序
2. NULL 数据排序,视为比任何值都小,升序出现在最上面,降序出现在最下面
select * from student order by qq_mail;
select * from student order by qq_mail asc;
-- 结果相同,asc可以省略

select * from student order by qq_mail desc;

 3. 使用表达式别名排序

select id,name,chinese + math + english from exam_result order by chinese + math + english desc;
select id, name, chinese + math + english as 总分 from exam_result order by 总分 desc;

 4. 可以对多个字段进行排序,排序优先级随书写顺序

-- 表示先以math 降序比较,若math相同则以chinese比较,若chinese也相同,则以english比较
select * from exam_result order by math desc ,chinese,english;

3.7 条件查询:WHERE

指摘出较为重要的一部分:
-- 1. >= 
select id ,name ,math from exam_result where math >= 80;

 

-- 2. <=>  也等于,NULL 安全('=',NULL 不安全),例如 NULL <=> NULL 的结果是 TRUE(1)
select * from exam_result where null <=> null;
-- 因为全为真,所有都查询

-- 3. between .. and ..   [ .. ]
select id,name ,math from exam_result where math between 80 and 95;

-- 4. in(option ..) 如果是option 里的就查询,指定查询
select * from exam_result where  name in ('武松', '鲁智深');

 

-- 5. is null 
select * from exam_result where null is null ;
-- 因为全为真,所有都查询

-- 6. like 模糊匹配。% 表示任意多个(包括 0 个)任意字符;
select * from exam_result where name like '鲁%';
select * from exam_result where name like '%智%';
select * from exam_result where name like '%深';
-- '_' 表示任意一个字符
select * from exam_result where name like '鲁__';

-- 结果相同

-- 7. and or not 
select * from exam_result where chinese > 80 and english >80; 
select * from exam_result where chinese > 80 or english >80; 
select * from exam_result where not english >80; 
1. WHERE 条件可以使用表达式,但不能使用别名。
2. AND 的优先级高于 OR ,在同时使用时,需要使用小括号 () 包裹优先执行的部分

3.8 分页查询:LIMIT

-- 分页
-- limit : 限制  每次多少个结果
-- offset : 起始位置,从 0 开始计算
-- limit l offset o;
-- limit o, l;
-- limit l;   <-> limit l offset 0;

-- 起始位置:0,限制:2
select * from exam_result order by math, id limit 2;
-- 起始位置:3,限制:2
select * from exam_result order by math, id limit 2 offset 3;
-- 起始位置:3,限制:2
select * from exam_result order by math, id limit 3, 2;

4. 修改(Update)🚄

语法:

UPDATE table_name SET column = expr [, column = expr ...]
 [WHERE ...] [ORDER BY ...] [LIMIT ...]
-- 将武松同学的数学成绩变更为 80 分
UPDATE exam_result SET math = 80 WHERE name = '武松';
-- 将鲁智深同学的数学成绩变更为 60 分,语文成绩变更为 70 分
UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '鲁智深';
-- 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT
3;

5. 删除(Delete)🚄

语法:
DELETE FROM  table_name [WHERE ...] [ORDER BY ...] [LIMIT ..
DELETE FROM exam_result WHERE name = '武松';

DROP TABLE IF EXISTS for_delete;
CREATE TABLE for_delete (
 id INT,
 name VARCHAR(20)
);
-- 插入测试数据
INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
-- 删除整表数据
DELETE FROM for_delete;

C# VS2012 86系统 mysql-5.5.27-win32 功能:利用动软代码生成器 从 数据库表或者视图中生成 的三层结构代码 实现 数据增删改查。 如果可以,请下载资源中 修改 的动软代码生成器 C#模板生成 1、由于之前使用 动软生成 java 网页源码,比较成功,此处编写C#程序时沿用,感觉更加适合。 2、直接调用动软的相关dll和生成的三层代码,可以较快的实现增删改查操作。 3、由于一些dll版本的问题及动软生成器自身的一些不完善,产生了一些问题并查找了挺久,所以把可以实现的版本发布出来共享。 前提: 使用的是 mysql数据库时才可能会出现以下问题 问题: 1、MySql.Data.dll 必须是5.6.1以上版本,否则会出现 “向信号量添加给定计数将导致其超出它的最大计数” 的问题。 2、动软代码生成时,必须增加该命名空间 using MySql.Data.MySqlClient; 3、动软代码必须修改 “工具”-“选项”弹出窗 后,点击 ”代码生成设置“-”字段类型映射“-”参数符号“中删除 mysql @,添加mysql ? 4、如果不修改3的设置,在增删改时 参数设置会失败。 5、mysql保存或者修改时,中文会出现乱码,这时必须 在DbHelperMySQL类的 连接字中增加Charset=utf8;即 protected static string connectionString = "Server=localhost;User Id=root;Password=root;Persist Security Info=True;Database=mnzfz;Charset=utf8;"; 6、如果要在局域网中远程访问,请 修改 mysql 权限:grant select,update,insert,delete on *.* to 'root'@'192.168.0.1' identified by "123456";
评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱干饭的猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值