Python学习笔记十八(MySQL、SQL、增删改)

SQL语句

SQL主要操作有增删改查(curd),其中查询的频率要高于其它操作,因为一般来说,进行其它操作之前,你需要明确表中有哪些字段,要修改哪些值,要删除哪条记录。

查看表结构

这里写图片描述

查看数据

-- select * from 表名; 查看表内所有数据
select * from article;

这里写图片描述

添加数据

-- insert into 表名(字段...) values(值...) 一一对应 
insert into article(title,content_file_path) values("Python学习","/article/details/80244167");

这里写图片描述

修改数据

-- update 表名 set 列名=值 where 条件
update article set title ="Python学习2",content_file_path = "/article/details/80244167" where id =2;

这里写图片描述

删除数据

-- delete from 表名 where 条件
delete from article where id = 3 ;

这里写图片描述


上面简单操作了一遍,下面详细演示并说明一下。

建表语句

-- 创建数据表article, 
-- article 有id 无符号int类型 自动增长的主键,
-- title varchar类型长度100 非空
-- content_file_path varchar类型长度100 非空
-- content_num 无符号int类型 非空 默认值为0
-- is_delect tinyint类型 非空 默认值为0

create table article( 
id int unsigned auto_increment primary key comment "文章id",
title varchar(100) not null COMMENT "文章标题",
content_file_path varchar(100) not null comment  "内容文件路径",
content_num int unsigned not null default 0 comment "内容字数",
is_delect tinyint not null default 0 comment "文章是否删除"
) comment "文章表";

添加数据

全部添加

-- 给表里的所有字段添加值
insert into article(id,title,content_file_path,content_num,is_delect)
    values(5,"Python学习","/article/details/80244167","2000",0)
insert into article values(7,"Python学习","/article/details/80244167","2000",0)

这里写图片描述

部分添加

-- 给表里的部分字段添加值
insert into article(title,content_file_path) values("Java学习","/article/details/80244167");
insert into article(content_file_path,title) values("Android学习","/article/details/80244167");

这里写图片描述

一次性添加多条数据

insert into article(title,content_file_path) 
values("Python学习1","/article/details/80244167"),
("Python学习2","/article/details/80244167"),
("Python学习3","/article/details/80244167"),
("Python学习4","/article/details/80244167");

这里写图片描述

修改数据

修改一个字段的值

update article  set content_num=520 where id=5;  --修改id 为5 的记录的content_num 的值为520
update article  set content_num=1314 where id=7;  --修改id 为7 的记录的content_num 的值为1314

这里写图片描述

修改多个字段的值

--修改id 为5 的记录的content_num 的值为520,title为大话西游
update article  set title="大话西游",content_num=1206 where id=5; 
update article  set title="爱你一万年",content_num=820 where id=7;

这里写图片描述

修改某个字段的全部值

update article  set title="YanglingWang",content_num=5201314 ;

这里写图片描述

删除数据

删除数据

delete from article where id=9;

这里写图片描述

删除两张表的记录1

delete from article1,article2 USING article1, article2 where article1.id=1 and article2.id=1;

这里写图片描述

伪删除(通过逻辑控制,不显示数据)

update article  set is_delect=1

这里写图片描述


到此结 DragonFangQy 2018.5.10

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值