数据库day04

本文详细介绍了SQL的基本操作,包括如何查询、修改和删除数据。从简单的SELECT语句到复杂的UPDATE和DELETE命令,每个操作都有清晰的示例。通过创建数据库、表,插入数据,再到更新特定记录和删除数据,最后展示了如何进行综合性的数据库管理练习。内容覆盖了数据查询的多种方式,如按条件筛选、获取特定字段等,以及如何批量修改和删除数据。
摘要由CSDN通过智能技术生成

查询数据

格式:select 字段信息 from 表 where 条件;
select id,name,chinese,enlish,math,from student;
select*from student;查询Student表中所有的字段信息
select name from emp;查询所有的名字
select name,age from emp where age<20;查询年龄小于20的名字和年龄
select*from student where name='猪八戒';

修改数据

格式:update 表名 set xxx=xxx,xxx=xxx where 条件;
update student set math=25 where id=3;
把猪八戒的id修改为9,年龄改为666
update emp set id=9,age=666 where name='张飞';
如果条件为NULL 不能用= 要用is
update emp set name='唐僧'where name is null

删除数据

格式 delete from 表名 where 条件;
delete from emp where age<20;
delete from emp where name='张飞';
delete from emp;

综合练习:1.创建mybd3 数据库字符集为utf8 并使用
show database mybd3 character utf8;
2.创建hero表 字段 :id,name,type
create table hero(id int,name varchar(10),type varchar(10));
3.插入数据:1.李白 刺客 2.诸葛亮 法师 3.刘备 战士 4.孙尚香 射手5.关羽 战士 6.刘禅 法师
insert into hero valuse(1,'李白','刺客'),(2,'诸葛亮','法师'),(3,'刘备','战士'),(4,'孙尚香','射手'),(5,'关羽','战士'),(6,'刘禅','法师');
4.修改李白为type为诗人
update hero set type='诗人' where id=1;
5.给表添加money字段在name后面
alter table hero add money int after name;
6.修改id<3的money为6888
update hero set money=6888 where id<3;
7.修改诗人为打野
update hero set type='打野'where id=1;
8.修改刘婵名字为阿斗价格为50
update hero set name='阿斗'where id=6;
9.删除id大于5的数据
delete from hero  where id>5;
10.删除money字段 11.修改表名为heros
alter table hero drop money;
rename table hero to heros;
12.修改所有英雄价格为13888
update heros set money=13888; 
13.删除所有数据
delete from heros;
14.删除表
drop table heros;
15.删除库
drop database mybd3; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值