use test; create table T_Person ( FName varchar(20), FAge int ) create table T_Studnet ( FNumber varchar(20) not null, FName varchar(20) not null, FAge int not null, FFavorite varchar(20), FPhoneNumber varchar(20) ) create table T_Teacher ( FNumber varchar(20), FName varchar(20), FAge int, FISMaster varchar(5) default 'NO' ) create table T_Bus ( FNumber varchar(20), FDriverName varchar(20), FUsedYears int, primary key(FNumber) ) create table T_PersonalMember ( FCompanyName varchar(20), FInternalNumber varchar(20), FName varchar(20), primary key(FCompanyName, FInternalNumber) ) create table T_Department ( FId varchar(20), FName varchar(20), FLevel int, primary key(FId) ); create table T_Employee ( FNumber varchar(20), FName varchar(20), FDepartmentId varchar(20), foreign key(FDepartmentId) references T_Department(FId) ) alter table T_Person add FFavorite varchar(20) alter table T_Person drop column FAge drop table T_Person drop table T_Department
第三章 数据的增删改
create table T_Person ( FName varchar(20), FAge int, FRemark varchar(20), primary key(FName) ); create table T_Debt ( FNumber varchar(20), FAmount numeric(10,2) not null, FPerson varchar(20), primary key(FNumber), foreign key(FPerson) references T_Person(FName) ) insert into T_Person(FName, FAge, FRemark) values('Tom',18,'USA') select * from T_Person insert into T_Person(FName, FAge, FRemark) values('Jim',20,'USA'); insert into T_Person(FName, FAge, FRemark) values('Lili',22,'China'); insert into T_Person(FName, FAge, FRemark) values('XiaoWang',17,'China'); insert into T_Person(FAge, FName, FRemark) values(21, 'Kimisushi', 'Korea') insert into T_Person(FAge, FName) values(22, 'LXF') insert into T_Person values('lurenl', 23, 'China') --非空约束,插入失败 insert into T_Debt(FNumber, FPerson) values('1', 'Jim') insert into T_Debt(FNumber, FAmount, FPerson) values('1',200, 'Jim') select * from T_Debt --主键约束 insert into T_Debt(FNumber, FAmount, FPerson) values('1', 300, 'Jim') insert into T_Debt(FNumber, FAmount, FPerson) values('2', 300, 'Jim') --外键约束 insert into T_Debt(FNumber, FAmount, FPerson) values('3', 100, 'Jerry') insert into T_Debt(FNumber, FAmount, FPerson) values('3', 100, 'Tom') update T_Person set FRemark = 'SuperMan' select * from T_Person update T_Person set FRemark = 'Sonic', FAge = 25 update T_Person set FAge = 12 where FName = 'Tom' update T_Person set FRemark = 'BlaBla' where FAge = 25 update T_Person set FAge = 22 where FName = 'jim' or FName = 'LXF' update T_Debt set FAmount = null where FPerson='Tom' update T_Debt set FAmount = 123 where FPerson = 'Tom' select * from T_Debt update T_Debt set FNumber = '2' where FPerson = 'Tom' update T_Debt set FNumber = '8' where FPerson = 'Tom' update T_Debt set FPerson = 'Merry' where FNumber = '1' update T_Debt set FPerson = 'Lili' where FNumber = '1' delete from T_Debt; delete from T_Person insert into T_Person (FName, FAge, FRemark) values ('Jim', 20, 'USA'), ('Lili', 22, 'China'), ('XiaoWang', 17, 'China') insert into T_Person (FName, FAge, FRemark) values ('Sam', 16, 'China'), ('BlueFin', 12, 'Mars') select * from T_Person delete from T_Person where FAge > 20 or FRemark = 'Mars' drop table T_Debt; drop table T_Person
第三章 数据的检索
create table T_Employee ( FNumber varchar(20), FName varchar(20), FAge int, FSalary numeric(10,2), primary key(FNumber) ); insert into T_Employee(FNumber, FName, FAge, FSalary) values ('DEV001', 'Tom', 25, 8300), ('DEV002', 'Jerry', 38, 2300.80), ('SALES001', 'John', 23, 5000), ('SALES002', 'Kerry', 28, 6200), ('SALES003', 'Stone', 22, 1200), ('HR001', 'Jane', 23, 2200.88), ('HR002', 'Tina', 25, 5200.36), ('IT001', 'Smith', 28, 3900); select * from T_Employee select FNumber from T_Employee select FName, FAge from T_Employee select FNumber, FName, FAge, FSalary from T_Employee select FNumber as Number1, FName as Name, FAge as Age, FSalary as Salary from T_Employee select FNumber 工号, FName 姓名, FAge 年龄, FSalary 工资 from T_Employee select FName from T_Employee where FSalary < 5000 select * from T_Employee where FSalary < 5000 and FAge > 25 select max(FSalary) as MAX_SALARY from T_Employee where FAge > 25 select AVG(FAge) from T_Employee where FSalary > 3800 select sum(FSalary) from T_Employee select min(FSalary), max(FSalary) from T_Employee select count(*), count(FNumber) from T_Employee insert into T_Employee(FNumber, FAge, FSalary) values('IT002', 27, 2800) select count(*), count(FNumber), count(FName) from T_Employee select * from T_Employee order by FAge asc select * from T_Employee order by FAge desc, FSalary desc select * from T_Employee where FName like '_erry' select * from T_Employee where FName like '__n_' select * from T_Employee where FName like 'T%' select * from T_Employee where FName like '%n%' select * from T_Employee where FName like '%n_' --集合匹配,微软特有的 select * from T_Employee where FName like '[SJ]%' --^可以对集合取反 select * from T_Employee where FName like '[^SJ]%' --其它数据库也可以变通实现 select * from T_Employee where FName like 'S%' or FName like 'J%' select * from T_Employee where not(FName like 'S%') and not(FName like 'J%') --错误 select * from T_Employee where FName = null select * from T_Employee where FName is null select * from T_Employee where FName is not null and FSalary < 5000 --反义运算符 select * from T_Employee where FAge !=22 and FSalary !< 2000 select * from T_Employee where FAge <> 22 and FSalary >= 2000 select * from T_Employee where not(FAge =22) and not(FSalary < 2000) --除了不等于<>,推荐使用NOT运算符来表示“非”的语义(或者<=,>=) select FAge, FNumber, FName from T_Employee where FAge = 23 or FAge = 25 or FAge = 28 --多值检测:离散值和范围值 select FAge, FNumber, FName from T_Employee where FAge in(23, 25, 28) select FAge,FNumber, FName from T_Employee where FAge >=23 and FAge <=27 --优先使用between...and...数据库系统对其进行了查询优化 select FAge, FNumber, FName from T_Employee where FAge between 23 and 27 select * from T_Employee where (FSalary between 2000 and 3000) or (FSalary between 5000 and 8000) alter table T_Employee add FSubCompany varchar(20), FDepartment varchar(20) update T_Employee set FSubCompany='Beijing',FDepartment='Development' where FNumber='DEV001'; update T_Employee set FSubCompany='ShenZhen',FDepartment='Development' where FNumber='DEV002'; update T_Employee set FSubCompany='Beijing',FDepartment='HumanResource' where FNumber='HR001'; update T_Employee set FSubCompany='Beijing',FDepartment='HumanResource' where FNumber='HR002'; update T_Employee set FSubCompany='Beijing',FDepartment='InfoTech' where FNumber='IT001'; update T_Employee set FSubCompany='ShenZhen',FDepartment='InfoTech' where FNumber='IT002'; update T_Employee set FSubCompany='Beijing',FDepartment='Sales' where FNumber='SALES001'; update T_Employee set FSubCompany='Beijing',FDepartment='Sales' where FNumber='SALES002'; update T_Employee set FSubCompany='ShenZhen',FDepartment='Sales' where FNumber='SALES003'; select FAge from T_Employee group by FAge select FSubCompany, FDepartment from T_Employee Group by FSubCompany, FDepartment select FAge, count(*) as CountOfThisAge from T_Employee group by FAge select FSubCompany, FAge, count(*) as CountOfThisSubCompAge from T_Employee group by FSubCompany, FAge order by FSubCompany select FSubCompany, sum(FSalary) as FSalarySUM from T_Employee group by FSubCompany select FDepartment, sum(FSalary) as FSalarySUM from T_Employee group by FDepartment select * from T_Employee select FDepartment, min(FAge) as FAgeMIN, max(FAge) as FAgeMAX from T_Employee group by FDepartment select FAge, count(*) as CountOfThisAge from T_Employee group by FAge having count(*) > 1 update T_Employee set FAge = 28 where FName = 'Jerry' select FAge, count(*) as CountOfThisAge from T_Employee group by FAge having count(*) = 1 or count(*) = 3 select FAge, count(*) as CountOfThisAge from T_Employee group by FAge having count(*) in (1,3) --以下错误 select FAge, count(*) as CountOfThisAge from T_Employee group by FAge having FName is not null select FAge, count(*) as CountOFThisAge from T_Employee where FName is not null group by FAge --sql server2000时的数据分页 select top 3 * from T_Employee where FNumber not in( select top 5 FNumber from T_Employee order by FSalary desc) order by FSalary desc select row_number() over(order by FSalary desc), FNumber, FName, FSalary, FAge from T_Employee --利用窗口函数分页 select * from( select row_number() over(order by FSalary desc) as rownum, FNumber, FName, FSalary, FAge from T_Employee) as a where a.rownum >= 3 and a.rownum <=5 select distinct FDepartment , FSubCompany from T_Employee select 'CowNew集团' as CompanyName, 918000000 as RegAmount, FName, FAge, FSubCompany from T_Employee select FNumber, FName, FAge * FSalary as FSalaryIndex from T_Employee select 125+521, FNumber, FName, FSalary/(FAge-21) as FHappyIndex from T_Employee select * from T_Employee where FSalary/(FAge-21)>1000 select FName, Len(Fname) as nameLength from T_Employee where FName is not null select FName, substring(FName,2,3) from T_Employee where FName is not null select FName, FAge, sin(FAge), abs(sin(FAge)) from T_Employee select '工号为'+FNumber+'的员工姓名为'+FName from T_Employee where FName is not null select * from T_Employee where FSalary between FAge*1.5+2000 and FAge*1.8+5000 select max(FSalary/FAge) as maxValue, min(FSalary/FAge) as minValue from T_Employee update T_Employee set FAge = FAge+1 select len('abc'),1,2 create table T_TempEmployee ( FidCardNumber varchar(20), FName varchar(20), FAge numeric(10), primary key(FidCardNumber) ) insert into T_TempEmployee(FidCardNumber, FName, FAge) values ('1234567890121', 'Sarani', 33), ('1234567890122', 'Tom', 26), ('1234567890123', 'Yalaha', 38), ('1234567890124', 'Tina', 26), ('1234567890125', 'Konkaya', 29), ('1234567890126', 'Fotifa', 46) select * from T_TempEmployee select FNumber, FName, FAge,FDepartment from T_Employee union all select FidCardNumber, FName, FAge,'临时工' from T_TempEmployee select FName, FAge from T_Employee union select FName, FAge from T_TempEmployee select '正式员工最高年龄',max(FAge) from T_Employee union all select '正式员工最低年龄',min(FAge) from T_Employee union all select '临时员工最高年龄',max(FAge) from T_TempEmployee union all select '临时员工最低年龄',min(FAge) from T_TempEmployee select FNumber, FSalary from T_Employee union all select '工资合计', sum(FSalary) from T_Employee select 1, 1*1 union all select 2, 2*2 union all select 3, 3*3 select '正式员工姓名' union all select FName from T_Employee union all select '临时工的姓名' union all select FName from T_TempEmployee drop table T_Employee drop table T_TempEmployee
第5章 函数
create table T_Person ( FIdNumber varchar(20), FName varchar(20), FBirthday datetime, FRegDay datetime, FWeight numeric(10,2) ) insert into T_Person(FIdNumber, FName, FBirthday, FRegDay, FWeight) values ('123456789120', 'Tom', '1981-03-22', '1998-05-01', 56.67), ('123456789121', 'Jim', '1987-01-18', '1999-08-21', 36.17), ('123456789122', 'Lily', '1987-11-08', '2001-09-18', 40.33), ('123456789123', 'Kelly', '1982-07-12', '2000-03-01', 46.23), ('123456789124', 'Sam', '1983-02-16', '1998-05-01', 48.68), ('123456789125', 'Kerry', '1984-08-07', '1999-03-01', 66.67), ('123456789126', 'Smith', '1980-01-09', '2002-09-23', 51.28), ('123456789127', 'BillGetes', '1972-07-18', '1995-06-19', 60.32) select * from T_Person --绝对值 select FWeight - 50, abs(FWeight - 50), abs(-5.38) from T_Person --指数 select FWeight, power(FWeight, 1), power(FWeight,2), power(FWeight,3), power(FWeight,4) from T_Person --平方根 select FWeight, sqrt(FWeight) from T_Person --随机数,参数种子可选,如果指定,则返回确定的随机值 select rand(1); --舍入到最大整数 select FName, FWeight, ceiling(FWeight), ceiling(FWeight*-1) from T_Person --舍入到最小整数 select FName, FWeight, floor(FWeight), floor(FWeight*-1) from T_Person --四舍五入,第2个参数是精度,正数舍入,负数截断 select FName, FWeight, round(FWeight,1), round(FWeight*-1,0), round(FWeight,-1) from T_Person --第3个参数默认是0,代表舍入,非0代表截断 select round(150.75,0,1) --圆周率 select pi() --求符号 select FName, FWeight-48.68, sign(FWeight-48.68) from T_Person --求整除余数 select FName, FWeight, FWeight%5 from T_Person --求幂 select FName, FWeight, power(1.18,FWeight) from T_Person --字符串函数 select FName, len(FName) from T_Person select FName, lower(FName), upper(FName) from T_Person select FName, ltrim(' abc'), rtrim(FName) from T_Person select substring('abcdef111',2,3) select substring(FName, 2,3) from T_Person --查子串的位置 select FName, charindex('m',FName), charindex('ly',FName) from T_Person select FName, left(FName,3), left(FName,2) from T_Person select FName, right(FName,3), right(FName,2) from T_Person select FName, replace(FName,'i','e'),FIDNumber, replace(FIDNumber,'2345','abcd') from T_Person select FName, replace(FName,'m',''), FIDNumber, replace(FIDNumber,'123','') from T_Person select replace(' abc 123 wpf',' ',''),replace(' ccw enet wcff',' ','') select ascii('a'), ascii('abc') select FName, left(FName,1), ascii(left(FName,1)), ascii(FName) from T_Person select char(56), char(90), 'a', char(ascii('a')) select FWeight,ceiling(FWeight), char(ceiling(FWeight)) from T_Person select soundex('jack'), soundex('jeck'),soundex('joke'),soundex('juke'),soundex('look'),soundex('jobe') select difference('smith','smythe') select getdate() as 当前日期时间 select convert(varchar(50), getdate(), 101) as 当前日期 select convert(varchar(50), getdate(), 108) as 当前时间 select FBirthday, dateadd(year, 3, FBirthday) as threeYearsLater, dateadd(quarter, 20, FBirthday) as [20QuartersLater], dateadd(month, 68, FBirthday) as [68MonthsLater], dateadd(week, -1000, FBirthday) as [1000WeeksBefor] from T_Person SELECT DATEADD(month, 1, '2006-08-30'); SELECT DATEADD(month, 1, '2006-08-31'); select FRegDay, FBirthday, datediff(week, FBirthday, FRegDay) from T_Person select FRegDay, FBirthday, datediff(year, FBirthday, FRegDay) from T_Person select FBirthday, datename(weekday, FBirthday) as 出生日期是周几, FRegDay, datename(weekday,FRegDay) as 注册日期是周几 from T_Person select FBirthday, datename(year,FBirthday) as 年, datename(dayofyear, FBirthday) as 日, datename(week, FBirthday) as 周 from T_Person select FBirthday, datepart(dayofyear, FBirthday), FRegDay, datepart(year, FRegDay) from T_Person select cast('-30' as int) as i, convert(decimal, '3.1415926') as d, convert(datetime,'2008-08-08 08:09:10') as dt select FIdNumber, right(FIdNumber,3) as 后3位, cast(right(FIdNumber,3) as integer) as 后3位的整数形式, cast(right(FIdNumber, 3) as int)+1 as 后3位加1, convert(int,right(FIdNumber,3))/2 as 后3位除以2 from T_Person update T_Person set FBirthday=null where FName = 'Kerry'; update T_Person set FBirthday=null,FRegDay=null where FName ='Smith'; select * from T_Person select FName, FBirthday, FRegDay, coalesce(FBirthday,FRegDay,'2008-08-08') as ImportDay from T_Person select FBirthday, FRegDay, isnull(FBirthday, FRegDay) as ImportDay from T_Person select FBirthday, FRegDay, nullif(FBirthday, FRegDay) from T_Person select FName, (case FName when 'Tom' then 'GoodBoy' when 'Lily' then 'GoodGirl' when 'Sam' then 'BadBoy' when 'Kerry' then 'BadGirl' else 'Normal' end) as isGood from T_Person select FName, FWeight, (case when FWeight<40 then 'thin' when FWeight > 50 then 'fat' else 'ok' end) as isNormal from T_Person --独有函数pattern index 意思模式,图案,样品 select FName, patindex('%_m%',FName) from T_Person insert into T_Person (FName) values('whatm') select FName, FWeight, cast(FWeight/20 as int), replicate(FName, cast(FWeight/20 as int)) from T_Person select FName, reverse(FName) from T_Person select isdate(null) as d1, isdate('13/43/3425') as d2, isdate('1995-10-1a') as d3, isdate(19920808) as d4, isdate('1/23/95') as d5, isdate('1995-10-1') as d6, isdate('19920808') as d7, isdate(' Abc') as d8 select isnumeric(null) as d1, isnumeric('13/43/3425') as d2, isnumeric('30a.8') as d3, isnumeric(19920808) as d4, isnumeric('1/23/95') as d5, isnumeric('3E-3') as d6, isnumeric('19920808') as d7, isnumeric('-30.3') as d8 select app_name() as appName, current_user as cu, host_name() as hostName select newid() as id1, newid() as id2
第6章 索引
--索引 create table T_Person(FNumber varchar(20), FName varchar(20), FAge int) create index idx_person_name on T_Person(FName) select * from T_Person create index idx_person_nameAge on T_Person(FName, FAge) drop index T_Person.idx_person_name; drop index T_Person.idx_person_nameAge drop table T_Person --非空约束 create table T_Person (FNumber varchar(20) not null, FName varchar(20), FAge int) insert into T_Person(FNumber, FName, FAge) values(null, 'kingchou',20) insert into T_Person (FNumber, FName, FAge) values('1', 'kingchou', 20) update T_Person set FNumber = null drop table T_Person --唯一约束 create table T_Person(FNumber varchar(20) unique, FName varchar(20), FAge int) insert into T_Person(FNumber,FName,FAge) values('2','stef',22); insert into T_Person(FNumber,FName,FAge) values('2','kitty',20) drop table T_Person create table T_Person (FNumber varchar(20), FDepartmentNumber varchar(20), FName varchar(20), FAge int, constraint uni_dep_num unique(FNumber,FDepartmentNumber)) insert into T_Person (FNumber, FDepartmentNumber, FName, FAge) values('1','dev001','kingchou',20), ('2','dev001','afka',20), ('1','sales001','slfd',20), ('2','sales001','slfkd',20) insert into T_Person(FNumber, FDepartmentNumber, FName, FAge) values('2','sales001','dialdskf',30) drop table T_Person create table T_Person(FNumber varchar(20), FDepartmentNumber varchar(20), FName varchar(20), FAge int, constraint unic_1 unique(FNumber, FDepartmentNumber), constraint unic_2 unique(FNumber, FDepartmentNumber)) alter table T_Person add constraint unic_3 unique(FName, FAge) alter table T_Person drop constraint unic_1,unic_2,unic_3 drop table T_Person --check约束 create table T_Person (FNumber varchar(20), FName varchar(20), Fage int check(FAge>0), FWorkYear int check(FWorkYear>0)) insert into T_Person(FNumber, FName, FAge,FWorkYear) values('001','john',25,-3) drop table T_Person create table T_Person( FNumber varchar(20) check(len(FNumber)>12), FName varchar(20), FAge int check(FAge>0), FWorkYear int check(FWorkYear>0)) insert into T_Person(FNumber, FName, FAge, FWorkYear) values('001', 'John', 25,3) insert into T_Person(FNumber, FName, FAge,FWorkYear) values('1234567890123','John',25,3) drop table T_Person --错误用法 create table T_Person( FNumber varchar(20), FName varchar(20), FAge int, FWorkYear int check(FWorkYear<FAge)) --正确用法 create table T_Person( FNumber varchar(20), FName varchar(20), FAge int, FWorkYear int, constraint ck_1 check(FWorkYear<FAge)) insert into T_Person (FNumber, FName, FAge, FWorkYear) values('001', 'John', 25, 30) alter table T_Person add constraint ck_2 check(Fage>14) alter table T_Person drop constraint ck_2; drop table T_Person create table T_Person (FNumber varchar(20) primary key, FName varchar(20), FAge int) insert into T_Person(FNumber, FName, FAge) values('1', 'kingchou', 20), ('2', 'stef', 22), ('3', 'long', 26), ('4', 'yangzk', 27) insert into T_Person(FNumber, FName, FAge) values('3', 'sunny', 22) drop table T_Person create table T_Person(FNumber varchar(20), FName varchar(20), FAge int, constraint pk_1 primary key(FNumber, FName)) create table T_Person(FNumber varchar(20) not null, FName varchar(20) not null, FAge int) alter table T_Person add constraint pk_1 primary key(FNumber, FName) alter table T_Person drop constraint pk_1 drop table T_Person create table T_Author (FId varchar(20) primary key, FName varchar(100), FAge int, FE_mail varchar(20) ) create table T_Book ( FId varchar(20) primary key, FName varchar(100), FPageCount int, FAuthorId varchar(20) ) insert into T_Author(FId, FName, FAge, FE_mail) values('1', 'lily', 20, 'lily@cownew.com'); insert into T_Book(FId, FName, FPageCount, FAuthorId) values('1', 'About Jave', 300, '1'); insert into T_Book(FId, FName, FPageCount, FAuthorId) values('9', 'About WinCE', 320, '9') select * from T_Book drop table T_Author, T_Book create table T_Author (FId varchar(20) primary key, FName varchar(100), FAge int, FE_mail varchar(20) ); create table T_Book (FId varchar(20) primary key, FName varchar(100), FPageCount int, FAuthorId varchar(20), foreign key(FAuthorId) references T_Author(FId) ) insert into T_Book(FId,FName, FPageCount, FAuthorId) values('9', 'About WinCE', 320, '9') insert into T_Book(FId, FName, FPageCount, FAuthorId) values('9', 'About Java', 300, '1') select * from T_Author, T_Book delete from T_Author where FName = 'lily' delete from T_Book where FAuthorId = '1' alter table T_Book add constraint fk_book_author foreign key(FAuthorId) references T_Author(FId) alter table T_Book drop constraint FK__T_Book__FAuthorI__5535A963 drop table T_Book, T_Author
第7章 表连接
create table T_Customer( FId int not null, FName varchar(20) not null, FAge int, primary key(FId)); create table T_OrderType( FId int not null, FName varchar(20) not null, primary key(FId)); create table T_Order( FId int not null, FNumber varchar(20) not null, FPrice numeric(10,2), FCustomerId int, FTypeId int, primary key(FId)); insert into T_Customer(FId, FName, FAge) values(1,'Tom',21), (2,'Mike',24), (3,'Jack',30), (4,'Tom',25), (5,'Linda',null); insert into T_OrderType(FId, FName) values(1,'MarketOrder'), (2,'LimitOrder'), (3,'Stop Order'), (4,'StopLimit Order'); insert into T_Order(FId, FNumber, Fprice, FCustomerId, FTypeId) values(1,'K001',100,1,1), (2,'K002',200,1,1), (3,'T003',300,1,2), (4,'N002',100,2,2), (5,'N003',500,3,4), (6,'T001',300,4,3), (7,'T002',100,null,1) select * from T_Customer select * from T_OrderType select * from T_Order select FNumber, FPrice from T_Order inner join T_Customer on FCustomerId = T_Customer.FId where T_Customer.FName = 'Tom' --列名不明确,俩表都有该字段 select FNumber, FPrice from T_Order inner join T_Customer on FCustomerId = FId where T_Customer.FName = 'Tom' select FId, FNumber, FPrice from T_Order inner join T_Customer on FCustomerId = T_Customer.FId where T_Customer.FName = 'Tom' select T_Order.FId, FNumber, FPrice from T_Order inner join T_Customer on FCustomerId = T_Customer.FId where T_Customer.FName = 'Tom' select o.FNumber, o.FPrice,c.FName,ot.FName from T_Order o join T_Customer c on o.FCustomerId = c.FId join T_OrderType ot on o.FTypeId = ot.FId select o.FNumber, o.FPrice, c.FName, c.FAge from T_Order o join T_Customer c on o.FPrice < c.FAge * 5 and o.FCustomerId = c.FId select c.FId, c.FName, c.FAge, o.FId, o.FNumber, o.FPrice from T_Customer c, T_Order o select c.FId, c.FName, c.FAge, o.FId, o.FNumber, o.FPrice from T_Customer c cross join T_Order o select * from T_Order select FNumber,FPrice,FTypeId from T_Order where FTypeId = FTypeId select o1.FNumber, o1.FPrice, o1.FtypeId, o2.FNumber, o2.FPrice, o2.FTypeId from T_Order o1 inner join T_Order o2 on o1.FTypeId = o2.FTypeId and o1.FId <>o2.FId --正确的自连接 select o1.FNumber, o1.FPrice, o1.FtypeId, o2.FNumber, o2.FPrice, o2.FTypeId from T_Order o1 inner join T_Order o2 on o1.FTypeId = o2.FTypeId and o1.FId < o2.FId select o.FNumber,o.FPrice,o.FCustomerId,c.FName,c.FAge from T_Order o left outer join T_Customer c on o.FCustomerId = c.FId select o.FNumber, o.FPrice, o.FCustomerId, c.FName, c.FAge from T_Order o left outer join T_Customer c on o.FCustomerId = c.FId where o.FPrice >= 150 select o.FNumber,o.FPrice, o.FCustomerId,c.FName,c.FAge from T_Order o right outer join T_Customer c on o.FCustomerId = c.FId select o.FNumber, o.FPrice, o.FCustomerId, c.FName, c.FAge from T_Order o full outer join T_Customer c on o.FCustomerId = c.FId drop table T_Order drop table T_OrderType drop table T_Customer
第8章 子查询
select * from T_Reader select * from T_Book select * from T_Category select * from T_ReaderFavorite --单值子查询 select 1 as f1, 2, (select min(FYearPublished) from T_Book), (select max(FYearPublished) from T_Book) as f4 select 1 as f1, 2,(select FYearPublished from T_Book) select 1 as f1, 2, (select max(FYearPublished), min(FYearPublished) from T_Book) select 1 as f1, 2, (select FYearPublished from T_Book where FYearPublished < 2000) select 1 as f1, 2, (select FYearPublished from T_Book where FYearPublished < 1750) --列值子查询 select T_Reader.FName, t2.FYearPublished, t2.FName from T_Reader, (select * from T_Book where FYearPublished < 1800) t2 select T_Reader.FName, t2.FYear, t2.FName, t2.F3 from T_Reader,(select FYearPublished as FYear,FName,1+2 as F3 from T_Book where FYearPublished < 1800) t2 --select列表中的标量子查询 select FId, FName, (select max(FYearPublished) from T_Book where T_Book.FcategoryId = T_Category.FId) from T_Category select FId, FName,(select max(FYearPublished) from T_Book) from T_Category --where子句中的标量子查询 select FReaderId from T_ReaderFavorite where FCategoryId =( select FId from T_Category where FName = 'Story') select c.FName, b.FName, b.FYearPublished from T_Category c join T_Book b on C.FId = b.FcategoryId where b.FYearPublished = ( select min(FYearPublished) from T_Book where T_Book.FCategoryId= c.FId) --集合运算符与子查询 select * from T_Book where FYearPublished in(2001,2003,2005) select FYearPublished from T_Book select * from T_Reader where FYearOfJoin in( select FYearPublished from T_Book) select * from T_Reader where FYearOfJoin = any( select FYearPublished from T_Book) select * from T_Reader where FYearOfJoin in( select FYearPublished from T_Book) select * from T_Book where FYearPublished < any( select FYearOfBirth from T_Reader) select * from T_Book where FYearPublished < all( select FYearOfJoin from T_Reader) select * from T_Book where FYearPublished < all( select FYearOfJoin from T_Reader where FProvince = 'JiangSu') select * from T_Book where exists( select * from T_Reader where FProvince = 'ShanDong') select * from T_Book where exists( select * from T_Reader where FProvince = 'YunNan') select * from T_Category where exists( select * from T_Book where T_Book.FCategoryId = T_Category .FId and T_Book.FYearPublished < 1950) --在其它类型SQL语句中的子查询应用 create table T_ReaderFavorite2(FCategoryId int, FReaderId int) insert into T_ReaderFavorite2(FCategoryId, FReaderId) select FCategoryId, FReaderId from T_ReaderFavorite delete from T_ReaderFavorite2 insert into T_ReaderFavorite2(FCategoryId, FReaderId) select FCategoryId,( case when FReaderId <=10 then FReaderId else FReaderId - FCategoryId end) from T_ReaderFavorite drop table T_ReaderFavorite2 insert into T_ReaderFavorite(FCategoryId, FReaderId) select 1,FId from T_Reader where not exists( select * from T_ReaderFavorite where FCategoryId = 1 and T_Reader.FId = T_ReaderFavorite.FReaderId) select * from T_ReaderFavorite update T_Book set FYearPublished = ( select max(FYearPublished) from T_Book) update T_Book set FYearPublished = 2005 where( select count(*) from T_Book b2 where T_Book.FCategoryId = b2.FCategoryId) > 3 select * from T_Book delete from T_Book where( select count(*) from T_Book b2 where T_Book.FCategoryId = b2.FCategoryId) > 3 drop table T_Book drop table T_Reader drop table T_Category drop table T_ReaderFavorite