sql完成对数据库表数据的CRUD操作

这篇博客详细介绍了如何使用SQL进行数据库表的数据操作,包括插入数据、查看数据、删除记录、更新记录和查询记录,涉及单条、批量、条件、排序和聚合函数等操作。还探讨了delete和truncate的区别以及查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

插入数据:

insert into 表名(列名1,列名2,列名3) values (值1,值2,值3);

例子:insert into student(sid,sname,sex,age) values (1,'zhangsan',0,21);[写列名]

 简单写法: insert into student values(2,'lisi',1,32);[不加列名]

注:简单写法,如果插入式全列名的数据,表名后面的列名可以省略.

id不要重复,否则会报以下错误:

插入其中几列:

insert into 数据库表名(列名1,列名2) values (值1,值2);[对的]

例子:insert into student(sid,sname) values (3,'wangwu');[对的]

insert into 数据库表名 (sid,sname) values (值1,值2)[错的]

注:如果是插入部分列的话,列名不能省略.

批量插入:

insert into 数据库表名 (列名1,列名2,列名3) values (值1,值2,值3),(值1,值2,值3),(值1,值2,值3);

例子:insert into student(sid,sname,sex,age) values (1,'zhangsan',0,21),(2,'zhangsan',0,21),(3,'zhangsan',0,21);

注:单条插入与批量插入的效率:
批量插入的效率更高.假设需插入10条数据,单条插入:插入10次即可.批量插入:一次性插入10条即可,但,如果,中间有数据错了,接下来未插入的数据就不会执行了.

查看表中数据:

select * from 表名;

例子:select * from student;

删除记录:

delete from 表名 [where 条件];

例子:delete from student where id = 2;

delete from 表名;

如果没有指定条件,会将表中数据一条一条全部删除掉.

例子:delete from student;

面试问题:请说一下,delete 删除数据和truncate 删除数据有什么差别?

delete:是一个关键字,属于DML,一条一条删除表中的数据.

truncate:属于DDL,先删除表在重建表.

关于哪条执行效率高:具体要看表中的数据量

   如果数据比较少,delete比较高效.

   如果数据比较多,truncate比较高效.

更新表记录:

update 表名 set 列名=列的值,列名2=列的值2 [where 条件];

例子:将sid为5的名字改为李四.

如果参数是字符串,日期,要加上单引号,如果是数字,直接写即可.

update student set sname='李四' where id=5;[可行]

update student set sname='李四',sex=0;[可行,不加where条件会将表中sex列的值都改为0]

查询记录:

select [distinct] [ *  ] [列名1,列名2] from 表名 [where条件];

distinct:去处重复数据

select:选择显示哪些列的内容

商品分类:手机数码,鞋靴箱包

1.分类ID

2.分类名称

3.分类描述

create table cayegory(

cid int primary key auto_increment,

cname vatchar(30),

cdesc varchar(50)

);

insert into category values (null,'手机数码','全都是数据数码,手机,电脑');

insert into category values (null,'鞋靴箱包','全都是鞋靴箱包,男鞋,女鞋');

insert into category values (null,'美酒美食','全都是美酒美食,葡萄酒,火锅');

insert into category values (null,'零食','全都都是零食,瓜子,花生');

select * from category;

select cname,cdesc from category;

所有商品:

1.商品id

2.商品名称

3.商品价格

4.生产日期

5.商品分类id

商品和商品分类关系:所属关系

create table product(

pid int primary key auto_increment,

pname varchar(20),

price double,

pdate timestamp,

cno int

);

insert into product values(null,'小米',998,null,1);

insert into product values(null,'行李箱',200,null,2);

insert into product values(null,'葡萄酒',666,null,3);

insert into product values(null,'瓜子',10,null,4);

简单查询:

查询所有商品:

select * from product;

查询商品名称和商品价格:

select pname,price from product;

别名查询 as关键字 as关键字可省略.

表列名:select p.pname,p.price from product p;(主要是用在多表查询)[不使用as关键字]

select p.pname,p.price from product as p;[使用as关键字]

列别名:select pname as 商品名称,price as 商品价格 from product;

select pname,price from product;

select pname as 商品名称,price as 商品价格 from product;[使用as关键字]

select pname  商品名称,price  商品价格 from product;[不使用as关键字]

去掉重复的值:

   查询商品所有的价格:

   select price from product;

  select distinct price from product;[使用distinct关键字去重]

select运算查询:[仅仅在查询结果上做了运算 + - * / ]

    select *,price*1.5 from product;

   select *,price*1.5 as  折后价 from product;

   select *,price*0.8 from product;//打八折

条件查询[where关键字]:

where关键字:指定条件,确定要操作的记录.

查询商品价格>60元的所有商品信息:

select * from product where price > 60;

where后的条件写法:

关系运算符:> >= < <= = != <>

<>:不等于 标准sql语法

!=:不等于  非标准sql语法[sqlserver]

查询商品价格不等于88的所有商品价格

select * from product where price <> 88;

select * from product where price != 88;

查询商品价格在10到100之间:

select * from product where price > 10 and price < 100;

between ...and...关键字:

select * from product where price between 10 and 100;

逻辑运算 :and ,or ,not

查询商品价格小于35或者商品价格大于900:

select * from product where price < 35 or price >900;

like:模糊查询:

-:代表一个字符

%:代表的是多个字符

查询出名字中带有饼的所有商品:

使用%   '%饼%'

select * from product where pname like '%饼%';

查询第二个名字是熊的所有商品:

使用_   '_熊%'

select * from product where pname like '_熊%';

in在某个范围内获得值:

查询出商品分类ID在1,4,5里面的所有商品:

select * from product where cno in(1,4,5);

排序查询:order by 关键字

asc:ascend 升序 [默认的排序方式]

desc:descend 降序

查询所有商品,按照价格进行排序

select * from product order by price;[结果默认是升序]

查询所有的商品,按照价格降序排序(asc-升序,desc-降序)

select * from product order by price desc;

查询名称有  小  的商品,按照价格降序排序:

   1.查询出名称有 小 的商品

select * from product where pname like '%小%';

   2.进行排序,得出结果

select * from product where pname like '%小%' order by asc;

聚合函数:

sum():求和

avg():求平均值

count():统计数量

max():最大值

min():最小值

1.获得所有商品价格的总和

 select sum(price) from product;

2.获得所有商品的平均价格

select avg(price) from product;

3.获得所有商品的个数

select count(*) from product;

注:where条件后面不能接聚合函数,否则会报错

查询商品价格大于平均价格的所有商品

1.查出所有商品

select * from product;

2.条件  大于平均价格

select avg(price) from product;

3.两个结果合在一块(子查询)

select * from product > (select avg(price) from product);[对的]

select * from product where price > avg(price);[错的]

分组:group by

1.根据cno字段分组,分组后统计商品的个数

select cno,count(*) from product group by cno;

2.根据cno分组,分组统计每组商品的平均价格 并且商品平均价格 >60

1.根据cno分组,分组统计每组商品的平均价格.

select cno,avg(price) from product group by cno;

2.加条件 商品平均价格 >60

select cno,avg(price) from product group by cno having avg(price) > 60;

having 关键字 可以接聚合函数 出现在分组之后

where关键字 不可接聚合函数  出现在分组之前

sql的编写顺序:

----S...F....W...G...H...O

select ...from...where...group by...having...order by

执行顺序:

F ... W...G...H...S...O

from ... where ...group by ... having ... select ... order by

sql练习:

-------------------------------------------------------------------------
数据库操作sql练习
一、数据库的创建:
	1、创建一个名称为mydb1的数据库
	    create database mydb1;
	2、创建一个使用utf8字符集的mydb2数据库。
	      create database mydb2 character set utf8;
	3、创建一个使用utf8字符集,并带比较规则的mydb3数据库。
	    create database mydb3 character set utf8 collate utf8_general_ci;
二、数据库的修改:
	修改mydb2字符集为gbk;
	alter database mydb2 character set gbk;
三、数据库的删除:
	删除数据库mydb3。
	drop database mydb3;
四、数据库查看:
	查看所有数据库。
	show databases;
	查看数据库mydb1的字符集
	show create database mydb1;
-----------------------------------------------
数据库中表操作的sql练习
一、创建表
	1、创建一张员工表employee
			字段		类型
			id			整形
			name		字符型
			gender		字符型
			birthday    日期型
			entry_date  日期型
			job			字符型
			salary		小数型
			resume		文本
	2、创建一张员工表employee2
			字段		类型
			id			整形
			name		字符型
			gender		字符型
			birthday    日期型
			entry_date  日期型
			job			字符型
			salary		小数型
			resume		文本
	要求:把id 设置成主键,并且自动增长。name不允许为空。

 create table employee (id int primary key auto_increment,
 name varchar(20) not null,
 gender varchar(10),
 birthday date,
 entry_date date,
 job varchar(30),
 salary double,
 resume text
 );

二、删除表
	1、删除employee2表
	drop table employee;
三、数据表的结构的修改:
	1、在上面员工表的基本上增加一个image列。
  	  alter table employee add image varchr(20);
	2、修改job列,使其长度为60。
	  alter table employee modify job varchar(60);
	3、删除gender列。
	   alter table employee drop gender;
	4、表名改为user。
	   rename table employee to user;
	5、修改表的字符集为utf8
	   alter table user character set utf8;
	6、列名name修改为username
	    alter table user change name username varchar(20) not null;
四、查看表结构
	1、查看数据库内的所有表
	   show tables;
	2、查看employee的建表语句
	   show create table employee;
	3、查看employee的表结构
	   desc employee;
----------------------------------------------------
表记录的操作
一、插入语句 ---insert
	1、向employee中插入三个员工信息,要求员工姓名分别是zs,ls,wangwu
二、更新语句 ---update
	1、将所有员工薪水修改为5000元。
	    update employee set salary=5000 ;
	2、将姓名为’zs’的员工薪水修改为3000元。
	     update employee set salary=3000 where name='zs';
	3、将姓名为’ls’的员工薪水修改为4000元,job改为ccc。
	     update employee set salary=3000,job='ccc' where name='ls';
	4、将wangwu的薪水在原有基础上增加1000元。
	    update employee set salary=salary+1000 where name='wangwu';
	   
三、删除语句 ---delete
	1、删除表中名称为’zs’的记录。
	   delete from employee where name='ls';
	2、删除表中所有记录。
	    delete from employee;
四、查询语句 ---select
	create table exam(
		id int primary key auto_increment,
		name varchar(20) not null,
		chinese double,
		math double,
		english double
	);
	insert into exam values(null,'关羽',85,76,70);
	insert into exam values(null,'张飞',70,75,70);
	insert into exam values(null,'赵云',90,65,95);
	insert into exam values(null,'刘备',97,50,50);
	insert into exam values(null,'曹操',90,89,80);
	insert into exam values(null,'司马懿',90,67,65);
	练习:
	1、查询表中所有学生的信息。
 	   select * from exam;
	2、查询表中所有学生的姓名和对应的英语成绩。
	   select name,english from exam;
	3、过滤表中重复数据。
	   
	4、在所有学生分数上加10分特长分。

	5、统计每个学生的总分。
            select *,chinese+math+english from exam;
	6、使用别名表示学生分数。
	   select *,chinese+math+english as 总分 from exam;
	-----使用where子句
	7、查询姓名为刘备的学生成绩
	    select * from exam where name='刘备';
	8、查询英语成绩大于90分的同学
	   select * from exam where english>90;
	9、查询总分大于200分的所有同学
	    select * from exam where chinese+math+english>200;
	10、查询英语分数在 80-90之间的同学。
             select * from exam where english between 80 and 90;
	      select * from exam where english>=80 and english<=90;
	11、查询数学分数为89,75,91的同学。
	    select * from exam where math=89 or math=75 or math=91;
   	    select * from exam where math in(89,75,91);
	12、查询所有姓刘的学生成绩。
	    select * from exam where name like '刘%';
	13、查询所有姓刘两个字的学生成绩。
	     select * from exam where name like '刘_';
	14、查询数学分>80并且语文分>80的同学。
	    select * from exam where math>80 and chinese>80;
	15、查询数学分>80 或者 语文分>80的同学。
	    select * from exam where math>80 or chinese>80;
	------使用order by  排序
	16、对数学成绩排序后输出。
	    select * from exam order by math;
	17、对总分排序按从高到低的顺序输出
	     select *,chinese+math+english as 总分 from exam order by 总分 desc;
	     select *,chinese+math+english as 总分 from exam order by chinese+math+
	     english desc;
	18、对姓刘的学生成绩排序输出
	    select * from exam where name like '刘%' order by math desc;
	------使用count(函数)
	19、统计一个班级共有多少学生?
	    select  count(id) from exam;
	20、统计数学成绩大于或等于90的学生有多少个?
	    select  count(math) from exam where math>=90;
	21、统计总分大于250的人数有多少?
	    select count(id) from exam where chinese+math+english>250;
	-------使用sum函数
	22、统计一个班级数学总成绩?
	    selcet * from exam;
	23、统计一个班级语文、英语、数学各科的总成绩
	    select sum(chinese),sum(math),sum(english) from exam;
	24、统计一个班级语文、英语、数学的成绩总和 
	     select sum(chinese)+sum(math)+sum(english) from exam;
	25、统计一个班级语文成绩平均分
	     select sum(chinese)/count(id) from exam;
	--------使用avg函数
	26、求一个班级数学平均分?
	     select avg(math) from exam;
	27、求一个班级总分平均分
	    select avg(ifnull(chinese,0))+avg(ifnull(math,0))+avg(ifnull(english,0))
	    from exam;
	-------使用max,min函数
	28、求班级最高分和最低分(数值范围在统计中特别有用)
	    select max(ifnull(chinese,0)+ifnull(math,0)+ifnull(english,0)) from exam;
	    select min(ifnull(chinese,0)+ifnull(math,0)+ifnull(english,0)) from exam;
	
	create table orders(
	id int,
	product varchar(20),
	price float
	);

	insert into orders(id,product,price) values(1,'电视',900);
	insert into orders(id,product,price) values(2,'洗衣机',100);
	insert into orders(id,product,price) values(3,'洗衣粉',90);
	insert into orders(id,product,price) values(4,'桔子',9);
	insert into orders(id,product,price) values(5,'洗衣粉',90);

	29、查询购买了几类商品,并且每类总价大于100的商品

	select product,sum(price) from orders where price>100 group by product;
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值