常用几个SQL语句(增删改查)

本文详细介绍如何使用SQL创建学生测试表,进行数据的增删改查等基本操作,并演示了使用case when语句来更新数据的方法。

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

--创建一个学生测试表
create table teststu(
 no char(2),
 name char(4),
 age  number(2)
);
--insert 插入数据 insert into 表名(列名1,列名2...) values(数据1,数据2...);
 insert into teststu values('10','黎明',19);
 insert into teststu values('11','李明',18);
 insert into teststu values('12','张明',21);
 insert into teststu values('13','张三',22);
 insert into teststu values('13','周三',20);
 insert into teststu values('13','李思',17);
 insert into teststu values('15','李思',17);
 --改数据 update 表名 set 列1名字=更新值,列2名字=更新值...
  -- where 更新条件
  update teststu set no='14'
	   where name='周三';
 -- 删除数据 delete from 表名 where 删除条件
  delete  from teststu 
	   where no='15';
 -- distinct (在查询的结果集中去掉重复行,不改变表内容)
   select distinct no from teststu
	   order by no asc;	 --升序排序
 -- 查询 select 列名 from 表名;*表示所有列
  select * from teststu;
	select no,name from teststu;
 --查询排序(升序) order by 列名 asc;下面列子表示如果no相同,则比较age
     select * from teststu t
		  -- where t.age>=18
			  order by t.no asc,t.age asc;
 --select case when 语句
 --第一种
  select t.no,t.name,t.age,(
	 case t.age
		 when 17 then 23
		 when 18 then 24
			 else t.age
				 end
	)from teststu t;		 
 --update case when 语句
  update teststu t set t.age=(
	 case t.age
		  when 17 then 23
	    when 18 then 24
			 else t.age
				 end
	);
 
   update teststu t set t.no=
	 case when t.age='23'then t.no='15'
			 else t.no
				 end;
	

  

转载于:https://www.cnblogs.com/ysg520/p/9665739.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值