--使用數據庫 use date go --創建表班級表 create table classInfo ( classNo int primary key identity(1,1),--主鍵約束使用primary key identity className varchar(10) not null--非空約束 ) go --創建學員表 create table stuInfo --標識列約束使用identity ( stuNo int primary key identity(1,1),--主鍵約束要求主鍵數據唯一並且不允許為空 stuname varchar(10)not null unique,--唯一約束使用unique關鍵字允許為空,但只能出現一個空值 age int check(age>10 and age<100),--檢查約束使用check設置年齡 sex nchar(1)default'男',--默認約束 class int foreign key references classInfo(classNo)--外鍵約束用於兩表之間建立關系,需要指定引用主表的那一列 ) go --插入數據 insert into classInfo values('S2S147'),('S2S148') insert into stuInfo values ('田七',18,'女',null), ('張三',38,'女',null), ('王五',20,'女',null), ('趙六',30,'男',null) --修改表中的數據 update stuInfo set class=1 --刪除表中的數據 delete stuInfo where stuNo=1 --修改朱茂深的年齡和性別 update stuInfo set age=18,sex='男'where stuname='張三' --查詢表中的數據 select * from stuInfo select * from classInfo --查詢性別為女的數據 select * from stuInfo where sex='女' --模糊查詢使用like --查詢'朱'字開頭的名字 select * from stuInfo where stuname like '張%' --查詢年齡18到20之間的數據 select * from stuInfo where age=18 or age=20 --使用in的效果一樣 select * from stuInfo where age in(18,20) select * from stuInfo where age between 18 and 20 --查詢姓名年齡 select '姓名'=stuname,'年齡'=age from stuInfo where age between 18 and 20 --查詢男生的人數(使用聚合函數) select count(*) as 人數 from stuInfo where sex='男' --sum(求和)、avg(平均值)、max(最大值)、min(最小值)、count(計算總數) --查詢男生女生分別多少人(分組)(group by 統計函數聯合使用) select 性別=sex, 人數=count(*) from stuInfo group by sex --查詢年齡18到20之間的個數大於等於2的數 select 性別=sex, 人數=count(*) from stuInfo where age in (18,20) group by sex having COUNT(*)>=2 --order by(作用是升降序 (ASC升從小到大排列)(Desc降)從大到小排列) select * from stuInfo order by age asc select * from stuInfo order by age desc --使用top查詢前三條數據 --查詢降序后的三條數據 select top 3 * from stuInfo order by age desc --查詢出男生的 姓名 年齡 性別 select 姓名=stuname,年齡=age,性別=sex from stuInfo,classInfo where stuInfo.class=classInfo.classNo and sex='男' --內鏈接查詢 --查詢 姓名 年齡 性別 班級 select 姓名=stuname,年齡=age,性別=sex,班級=className from stuInfo inner join classInfo on stuInfo.class=classInfo.classNo where sex='男' --左外連接查詢使用(left join) select stuname,age,sex,className from stuInfo left join classInfo on stuInfo.class=classInfo.classNo --右外連接查詢 使用(right join)如果不滿足條件的則返回null(空值) select stuname,age,sex,classname from stuInfo right join classInfo on stuInfo.class=classInfo.classNo --全外連接查詢使用(full outer join)outer可以不寫 select stuname,age,sex,classname from stuInfo full join classInfo on stuInfo.class=classInfo.classNo --交叉鏈接查詢(笛卡爾積)就是查詢所指定要查詢的值 select stuname,age,sex,classname from stuInfo cross join classInfo --自連接查詢查詢張三所在班級的所有學員信息 select 姓名=s1.stuname , 年齡=s1.age, 性別=s1.sex from stuInfo s1,stuInfo s2 where s1.class=s2.class and s2.stuname='張三' select 姓名=s1.stuname, 年齡=s1.age, 性別=s1.sex, 班級=c3 .className from stuInfo s1, stuInfo s2 ,classInfo c3 where s1.class=s2.class and s2.stuname='張三' and s1.class=c3.classNo --子查詢 select 姓名=stuname, 年齡=age, 性別=sex, 班級=class from stuInfo where class=(select class from stuInfo where stuname='張三') select 姓名=stuname, 年齡=age, 性別=sex, 班級=(select classname from classInfo where classNo=class) from stuInfo where class= (select class from stuInfo where stuname='張三')