自学内容网 自学内容网

MySQL(python开发)——(3)表数据的基本操作,增删改查

MySQL(python开发)——(1)数据库概述及其MySQL介绍
MySQL(python开发)——(2)数据库基本操作及数据类型

MySQL—— 表数据基本操作

一、表中插入(insert)数据——增

insert into 表名 values (值1,值2...),(值1,值2...),...;
insert into 表名 (字段1,...) values (值1,值2...),...;

* values 也可以写 value
insert into class values
(1,"Lily",18,'f',89),
(2,"Lucy",18,'f',76),
(3,"Tom",17,'m',83);

insert into class
(name,age,sex,score)
values
("Levi",18,'m',86),
("Sunny",17,'m',91),
("Eva",17,'f',71);

insert into hobby
(name,hobby,level,price,remark)
values
("Joy","sing,dance","A",56800,"骨骼惊奇"),
("Abby","sing","B",14800.888,"天籁之音"),
("Barom","draw","B",26800.00,"当代达芬奇");

insert into hobby
(name,hobby,level,price)
values
("Jame","dance","C",8800),
("Emma","draw,sing","B",44800),
("Chen","sing","C",16800);

随堂练习:
创建一个数据库  exercise

创建一个数据表  books  类型和约束自己设计 ,字段 : id  书名   作者   出版社  价格   备注

向其中插入数据若干  >= 6条
参考 : 老舍  沈从文  鲁迅  冰心  ....
出版社 : 中国文学  人民教育   机械工业
价格 :  30-120


创建一个数据库  books
create database books charset=utf8;
use books;

创建一个数据表  books  类型和约束自己设计 ,字段 : id  书名   作者   出版社  价格   备注
create table books(
id int primary key auto_increment,
bname varchar(50) not null,
author  varchar(30) default "佚名",
press varchar(128),
price float unsigned,
comment text
);


向其中插入数据若干  >= 6条
参考 : 老舍  沈从文  鲁迅  冰心  ....
出版社 : 中国文学  人民教育   机械工业
价格 :  30-120

insert into books
(bname,author,press,price,comment)
values
("边城","沈从文","机械工业出版社",36,"小城故事多"),
("骆驼祥子","老舍","机械工业出版社",43,"你是祥子么?"),
("茶馆","老舍","中国文学出版社",55,"老北京"),
("呐喊","鲁迅","人民教育出版社",71,"最后的声音"),
("朝花夕拾","鲁迅","中国文学出版社",53,"好时光"),
("围城","钱钟书","中国文学出版社",44,"你心中的围城是什么");

insert into books
(bname,author,press,price)
values
("林家铺子","茅盾","机械工业出版社",51),
("子夜","茅盾","人民教育出版社",47);

二、表数据 查询(select)——查

select * from 表名 [where 条件];
select 字段1,字段2 from 表名 [where 条件];

e.g. 
select * from class;
select name,age from class;

where子句

where子句在sql语句中扮演了重要角色,主要通过一定的运算条件进行数据的筛选,在查询,删除,修改中都有使用。

  • 算数运算符
    在这里插入图片描述
e.g.
select * from class_1 where age % 2 = 0;

  • 比较运算符

在这里插入图片描述

e.g.
select * from class where age > 8;
select * from class where age between 8 and 10;
select * from class where age in (8,9);
select * from class where sex is null;
  • 逻辑运算符

在这里插入图片描述

e.g.
select * from class where sex='m' and age>9;

在这里插入图片描述

查询练习

1. 查找30多元的图书
2.查找人民教育出版社出版的图书 
3.查找老舍写的,中国文学出版社出版的图书 
4.查找备注不为空的图书
5.查找价格超过60元的图书,只看书名和价格
6.查找鲁迅写的或者茅盾写的图书


1. 查找30多元的图书
select * from books
where price between 30 and 39.99;

2.查找人民教育出版社出版的图书 
select * from books where press="人民教育出版社";

3.查找老舍写的,中国文学出版社出版的图书 
select * from books
where author="老舍" and press="中国文学出版社";

4.查找备注不为空的图书
select * from books where comment is not null;

5.查找价格超过60元的图书,只看书名和价格
select bname,price from books where price>60;

6.查找鲁迅写的或者茅盾写的图书
select * from books where author in ("鲁迅","茅盾");

三、 更新表记录(update)——改

update 表名 set 字段1=值1,字段2=值2,... where 条件;

注意:update语句后如果不加where条件,所有记录全部更新

e.g.
update class set age=18,score=91 where name="Abby";
update class set sex='m' where sex is null;
update class set age=age+1;

四、 删除表记录(delete)——删

delete from 表名 where 条件;

注意:delete语句后如果不加where条件,所有记录全部清空

e.g.
delete from class where score=0 and sex='m';

五、表字段的操作(alter)

语法 :alter table 表名 执行动作;

* 添加字段(add)
    alter table 表名 add 字段名 数据类型;
    alter table 表名 add 字段名 数据类型 first;
    alter table 表名 add 字段名 数据类型 after 字段名;
* 删除字段(drop)
    alter table 表名 drop 字段名;
* 修改数据类型(modify)
    alter table 表名 modify 字段名 新数据类型;
* 替换字段(change)
    alter table 表名 change 旧字段名 新字段名 新数据类型;

e.g. 
--增加字段
alter table hobby add phone char(10) after price;

--删除字段
alter table hobby drop level;

--修改字段数据类型
alter table hobby modify phone char(16);

--替换字段
alter table hobby change phone tel char(16);

时间类型数据

  • 日期 : DATE
  • 日期时间: DATETIME,TIMESTAMP
  • 时间: TIME
  • 年份 :YEAR

在这里插入图片描述

  • 时间格式

    date"YYYY-MM-DD"
    time"HH:MM:SS"
    datetime"YYYY-MM-DD HH:MM:SS"
    timestamp"YYYY-MM-DD HH:MM:SS"
    
    
e.g.
create table marathon (
id int primary key auto_increment,
athlete varchar(32),
birthday date,
r_time datetime comment "报名时间",
performance time
);

insert into marathon values
(1,"曹操","1998-2-16","2021/5/6 10:10:27","2:38:49"),
(2,"关羽","2000-7-19","2021/4/30 16:22:09","2:27:18"),
(3,"孙策","1995-10-23","2021/5/2 20:1:2","2:44:00");

  • 日期时间函数

    • now() 返回服务器当前日期时间,格式对应datetime类型
  • 时间操作

    时间类型数据可以进行比较和排序等操作,在写时间字符串时尽量按照标准格式书写。

  select * from marathon where birthday>='2000-01-01';
  select * from marathon where birthday>="2000-07-01" and performance<="2:30:00";

练习 使用book表
1. 将呐喊的价格修改为452. 增加一个字段出版时间 类型为 date 放在价格后面
3. 修改所有老舍的作品出版时间为 2018-10-1
4. 修改所有中国文学出版社出版的但是不是老舍的作品出版时间为 2020-1-1
5. 修改所有出版时间为Null的图书 出版时间为 2019-10-1
6. 所有鲁迅的图书价格增加57. 删除所有价格超过70元或者不到40元的图书


1.将呐喊的价格修改为45update books set price=45 where bname="呐喊";

2.增加一个字段出版时间 类型为 date 放在价格后面
alter table books
add p_time date comment "出版时间" after price;

3.修改所有老舍的作品出版时间为 2018-10-1
update books set p_time="2018-10-1"
where author="老舍";

4.修改所有中国文学出版社出版的但是不是老舍的作品
出版时间为 2020-1-1
update books set p_time="2020-1-1"
where press="中国文学出版社" and author!="老舍";

5.修改所有出版时间为Null的图书出版时间为2019-10-1
update books set p_time="2019-10-1"
where p_time is null;

6.所有鲁迅的图书价格增加5update books set price=price+5
where author="鲁迅";

7.删除所有价格超过70元或者不到40元的图书
delete from books where price not between 40 and 70;

原文地址:https://blog.csdn.net/qxl_799915/article/details/142926842

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!