SQL数据库中表的增删改除四种基本操作与逻辑表达式


敲打

--增删改查CRUD

select *from table_class --查询table_class所有数据
select *from table_student
--插入数据(C)
--单条数据的插入
 -- 列的数目=值得数目 数据需要满足约束  数据类型保持一致(存在默认转换)
 -- 自增列不能手动赋值
insert into table_class(class_name,create_time) values('计110班',default)
insert into table_student(stu_number,stu_name,stu_age,birthday,class_id) 
values('1000000001','james',20,'1996-06-06',null)
--批量插入数据
insert into table_class(class_name) 
select '光101班'union
select '光102班'union
select '光103班'union
select '光104班' 


insert into table_class(class_name)
values('光105班'),('光106班'),('光107班'),('光108班')


--数据迁移(批量操作) 既实现了复制表结构,同时实现了数据的迁移
--利用table_student表复制一张新表,复制部分列
--select 指定列 into 新表 from 旧表(该语句不能进行两次操作,因为表不能再建立第二遍)
select stu_number,stu_name,stu_age
  into table_student1 from table_student
--insert into 新表 select 指定列 from 旧表 (没有创建表,表是预先准备好的,仅做数据迁移)
insert into table_student1 select stu_number,stu_name,stu_age from table_student


select *from table_student1
select distinct *from table_student1 --(distinct 过滤重复的行 数据还是存在)
select distinct stu_number from table_student1


--------------------------(例题)
create table table_test
(
id int,
name varchar(20)
)
--Q:去除表table_test中重复的行
--1.先查询不重复的数据,将数据保存到一张新表
select distinct * into table_new from table_test 
--2.删除table_test的数据
delete from table_test
--3.将新表中的数据迁移回table_test
insert into table_test(id,name) select *from table_new
--4.删除那张新表
drop table table_new


select *from table_test






--D 删除操作 delete from 表名 where 删除哪些数据
delete from table_student --删除table_student表里的所有的数据
delete from table_student where id=3 --删除id=3的数据
delete from table_student where id!=3 --删除id!=3的数据
delete from table_student where id<>3 --删除id!=3的数据
delete from table_student where stu_name='张三' --删除stu_name为张三'的数据
delete from table_student where class_id is null --删除class_id是null值的数据
delete from table_student where class_id is not null --删除class_id不是null值的数据


--区别
--drop:删除的是结构,毁灭性的
--delete:删除的都是数据,可以加where条件,不会将自增列的计数器清零,所有的delete会记录日志,速度比truncate慢
--truncate:删除的也都是数据,可是不能带where条件 truncate table 表名
truncate table table_student --会将自增列的计数器清零,重新开始递增,不会记录日志,删除的速度快



--U 修改操作 update 表明 set 列=值,列=值 where 条件
update table_student set stu_name='詹姆斯',stu_age=23 where id=1


--R 查询操作 数据检索
--   *代表查询所有列
-- select 列名 from 表
select @@version
select getdate() --精简版
select stu_number,stu_name from table_student
--列的重命名
select stu_number as '学号',stu_name as '姓名' from table_student --as可以省略


--逻辑表达式
--and 逻辑与 
--or  逻辑或
--not 逻辑非
     --范围关系
select * from table_student where stu_age>=23 and stu_age<=29
select * from table_student where stu_age between 23 and 29

select * from table_student where birthday between '1996-01-01' and '1999-12-31'






--------携着一股什么也不服的劲在活着

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值