--添加數據
--添加單條
insert into 表名(字段名1,字段名2) values (值1,值2) --添加多條
insert into 表名(字段名1,字段名2) values (值1,值2), (值1,值2), (值1,值2) --使用update更新語句,修改數據
update 表名 set 字段名=新值,字段名2=新值2 update student set ccredit=2 where cno='co1'
--刪除數據 --刪除表中所有數據
delete from 表名 delete from 表名 where 條件 --刪除前20%的數據
delete top(20) present from sc --數據查詢
--查詢全部數據
select * from 表名 select 字段1,字段2 from 表名 --給列取別名
select sname as 姓名 from student --限制結果集的行數,指定取出多少行
select top 5 sname,ssex from student --取出前5行數據
--消除重復的列 ,多個相同的課程號列只取一個
select distinct sno from sc --條件查詢
-- > < = != not
--查找年齡小於20學生的學號
select sno from student where sage<20
--范圍查詢 between and 和 not between and
--查找年齡在20到50之間學生
select * from student where sage between 20 and 50
--in 確定集合,屬於集合的元組
select * from student where sdept in ('信息系','計算機系','數學系') --模糊查詢 like
-- _ 匹配任意一個字符,
-- %匹配0個多個字符,
-- []匹配【】中的任意一個字符,
-- [^]不匹配他中的任意一個字符
--查詢姓張的學生
select * from student where sname like '張%'
--查詢第二個字為大或小的人
select * from student where sname like '_[大小]%'
--查詢最后一位不是 1 ,2的學生
select * from student where sno like '%[^12]'
--數據排序 order by 默認是升序(上面小,下面打)
select *from student order by gkfs --降序排列
select *from student order by gkfs desc
--聚合函數,進行計算 --1. 查詢各個系男女學生人數,高考分數平均分,高考分數最高分,高考分數最低分, -- 顯示系,性別,人數,高考分數平均分,高考分數最高分,高考分數最低分。
select sdept,ssex,count(ssex),AVG(gkfs)as 平均分,MAX(gkfs)as 最高分,MIN(gkfs)as 最低分 from 錄取表 --分組計算 group by
--統計每門課的選課人數,顯示出課程號和人數
select cno as 課程號,COUNT(sno) as 選課人數 from sc group by cno --2. 查詢每個學生的所選課程的課程數,所選課程的平均分,顯示學號,所選課程的課程數,所選課程的平均分。
select * from sc select sno,COUNT(cno) as 課程數,AVG(grade) as 平均分 from sc GROUP by sno --3. 查詢每個學生的所選課程各科都及格的課程數,所選課程的平均分, -- 顯示學號,所選課程的課程數,所選課程的平均分。 --having是對分組進行篩選
select * from sc select sno,COUNT(cno) as 課程數,AVG(grade)as 平均分 from sc group by sno having MIN(grade)>=60
--4. 查詢每個學生的所選課程中分數不低於80分的課程平均分,顯示學號,所選課程的平均分。
select sno,AVG(grade) from sc where grade>=80 group by sno --5. 查詢每門課程的選修人數 ,所選課程的平均分
select cno,COUNT(*),AVG(grade) from sc group by cno --查詢選修了三門以上課程的學生--Query students who have taken more than three courses
select sno from sc group by sno having COUNT(*)>3
--LEFT對字符串進行操作,從左邊進行截取
select LEFT('170508010430',4) --1705
--right對字符串進行操作,從右邊進行截取
select right('170508010430',2) --30
--substaring 從指定位置取出 從第幾位開始,取出幾個數
select substring('1700508010430',2,5) --70050
--轉化為大寫
select UPPER('YAng123') --轉化為小寫
select lower('YAng123') --子查詢
--單值嵌套查詢
delect sno,grade from sc where cno=(select cno from course where cname='數據庫基礎') --多值嵌套查詢 in
--查詢和劉晨在同一個系的學生
select sno,sname,sdept from student where sdept in (select sdept from student where sname='劉晨') and sname != '劉晨'
--any 其中之一,有一個滿足就為true
select * from course where ccredit >any(select ccredit from course) --all 所有的,全部滿足才為true
select * from course where ccredit > all(select ccredit from course) 你 --exists 存在性檢測
--查詢了選秀了c01課程的學生姓名
select sname from student where exists(select * from sc where sno=student.sno and cno='c01') --多表連接查詢 join
--自連接 先join on 再where 最后group by
--查詢和劉晨在同一個系的學生的姓名和所在系
select s2,sname,s2.sdept from student s1 join student s2 on s1.sdept=s2.sdept where s1.sname ='劉晨'
and s2.sname !='劉晨'
--並運算 union 會自動剔除重復的數據行
--列出課程編號為c01 c02的課程名和學分
select cname,ccredit from xourse where cno='c01'
union
select cname,ccredit from course where cno='c03'
--交運算
--既修了01又修了02
select cname,ccredit from xourse where cno='c01'
intersect
select cname,ccredit from course where cno='c03'
--差運算 except同not in 。在一個集合有另一個集合沒有
--case函數
-- 將一個測試表達式和一組簡單的表達式進行比較,返回相應的結果
--查詢c07的課程,根據分數返回成績結果並顯示
select sno, case
when grade>=90 then '優秀'
when grade between 80 and 89 then '良好'
when grade between 70 and 79 then '中等'
when grade between 60 and 69 then '及格'
when grade <60 then '不及格'
end as 成績 from scwhere cno='c07'
--1.在teacher中使用case語句為 --教授的老師基本工資設定為5000元, --副教授老師基本工資設定為4000元, --講師老師基本工資設定為3000元, --助教老師基本工資設定為2000元。
update teacher set 基本工資=
case 職稱 when '教授' then 5000
when '副教授' then 4000
when '講師' then 3000
when '助教' then 2000
end
--3.在teacher中使用case語句為 --教授的老師基本工資上浮50% --副教授老師基本工資上浮40%, --講師的老師基本工資上浮30% --助教老師基本工資上浮20%, --其他人員基本工資上浮10%。
update teacher set 基本工資=基本工資*
case 職稱 when '教授' then 1.5 --只能用小數不能使用百分數
when '副教授' then 1.4
when '講師' then 1.3
when '助教' then 1.2
else 1.1
end