create database step2; go use step2; go -- 學生表 create table StudentInfo ( stuId char(10) primary key, -- 主鍵 stuName varchar(20), -- 姓名 ClassId int, ...
create database step2; go use step2; go -- 學生表 create table StudentInfo ( stuId char(10) primary key, -- 主鍵 stuName varchar(20), -- 姓名 ClassId int, -- 班級編號,邏輯外鍵,並不是真正的外鍵約束 stuPhone char(11), -- 電話號碼 stuSex char(4), -- 性別 stuBirthday datetime -- 生日 ); go -- 班級表 create table ClassInfo ( Id int primary key identity, -- 班級的主鍵 Name varchar(30), -- 班級名稱 College varchar(20) -- 學院 ); go -- 成績表 create table StudentScore ( Id int primary key identity, -- 成績的主鍵 stuId char(10), -- 學生外鍵 CourseName varchar(20), -- 課程 theoryScore int, -- 理論成績 skillScore int -- 技能成績 ); INSERT INTO dbo.StudentInfo(stuId,stuName,ClassId,stuPhone,stuSex,stuBirthday)VALUES ('180325011','任我行',5,'13823204456','男', '1999-09-09'), ('180325012','張三',4,'13823204452','女', '1998-08-08'), ('180325013','李四',2,'18899251152','男', '1997-07-07'), ('180325014','王五',1,'13597445645','女', '1998-08-08'), ('180325015','帥天行',5,'13814204456','男', '1998-06-06'), ('180325016','葉星辰',5,'17623204936','男', '1998-05-05'), ('180325017','趙日天',0,'13922044932','男', '1997-07-15'); go INSERT INTO dbo.ClassInfo(Name,College)VALUES ('軟體技術1班', '電腦系' ), ('會計1班', '經濟管理系' ), ('會計2班', '經濟管理系' ), ('歐美軟體外包班', '電腦系' ), ('會計3班', '經濟管理系' ); go INSERT INTO dbo.StudentScore(stuId,CourseName,theoryScore,skillScore)VALUES ( '180325011', '會計從業', 80, 90 ), ( '180325011', 'C# 入門編程', 99, 100 ), ( '180325012', 'SQLServer編程', 70, 75 ), ( '180325013', '會計從業', 93, 80 ), ( '180325014', 'C# 高級編程', 99, 99 ), ( '180325015', '會計從業', 74, 40 ), ( '180325015', 'C# 入門編程', 80, 90 ); --1.如何查看表中所有數據? -- 查看學生表 -- select: 查詢 -- *:代表表中所有的列 select * from StudentInfo -- * 號,在資料庫優化的章節中,不建議使用*號,因為系統要解析這個*號,需要一點點時間 -- 實際開發中,如果欄位過多,我們查詢時,只查出業務中所需要的欄位 select stuName,stuId from StudentInfo -- 查詢班級表,執行 select id,Name,College from ClassInfo --2.如何查詢指定幾個欄位的值? -- 查詢學生的姓名,性別,生日,班級 select stuName,stuSex,stuBirthday,ClassId from StudentInfo --3.如何給欄位取別名?(可以省略as) -- 把學生表中所有的欄位都取別名 select stuId as 學生主鍵,stuName as 姓名,ClassId 班級編號,stuPhone 電話號碼, stuSex 性別,stuBirthday 生日 from StudentInfo --4.distinct的用法?多個欄位的用法? -- distinct:去除重覆項 select distinct ClassId from StudentInfo select distinct stuSex from StudentInfo -- 指的是兩個欄位組合在一起,不會重覆 select distinct stuSex,ClassId from StudentInfo -- 這兩個結果集為什麼會一樣? select stuId,CourseName from StudentScore -- disctinct 後面跟著幾個欄位,表示 去除這幾個欄位(組合在一起)重覆的意思 select distinct stuId,CourseName from StudentScore -- 這樣寫會去除重覆嗎? select distinct * from StudentInfo -- 這樣寫沒有意義,反而增加系統的開銷 --5.top 的用法? -- 取前3條數據 -- top :前。。。條 select top 3 * from StudentInfo --6.top ... percent(百分比)? -- 查詢前30%的學生數據 -- 從這個故事告訴我們,數據沒有半條,向上取整 select top 30 percent * from StudentInfo --7.查詢年齡大於20歲的? -- year():獲取年份 -- 年齡 = 當前年份 - 生日所在年份 select * from StudentInfo where (year(getdate())-year(stuBirthday))>20 -- 查詢學生的,姓名,性別,年齡 所有欄位取別名 -- 年齡 = 當前年份 - 生日所在年份 select stuName as 姓名,stuSex as 性別,(year(getdate())-year(stuBirthday)) as 年齡 from StudentInfo -- 查詢80 後的女生 -- 80後:1980-1989 select * from StudentInfo where year(stuBirthday)>=1980 and year(stuBirthday)<=1989 and stuSex='女' --9.查詢姓李的學生信息 -- like:模糊查詢,中文意思是:像。。。 select * from StudentInfo where stuName like '李%' --10.列出技能成績大於90分的成績單 select * from StudentScore where skillScore>=90 --11.查詢課程包含”SqlServer”的成績信息 select * from StudentScore where CourseName like '%SqlServer%' --12.查詢每個學生不同的成績列表 select distinct stuId,skillScore from StudentScore --15.查詢年齡大於20歲前3條學生的姓名,年齡,所有欄位取別名 select top 3 stuName as 姓名,stuSex as 性別 from StudentInfo where (year(getdate())-year(stuBirthday))>20
配套視頻鏈接:SQLServer 入門基礎 - 網易雲課堂 (163.com)
海闊平魚躍,天高任我行,給我一片藍天,讓我自由翱翔。