第二节 外键约束、重新标识列、别名、排序、top关键字、percent百分比

View Code
--向子表中插入外键列数据时,在主表中必须存在

--否则将违反外键约束

--如果在子表中存在关联主表中的数据,那么删除主表

--会违反外键约束,无法删除,如果一定要删除主表中的

--数据,应先将子表中的数据删除后再删除主表中的数据


--delete语句删除表标识列不会重新编号

deletefrom StuBak

--truncate语句删除表会重新编号标识列

--注:不能用于有外键约束的表

truncatetable StuBak


--指定列名查询表的信息

select SName,Age,Address from Students

--取别名查询表的信息

select SName as姓名,Age as年龄,

Address as地址from Students

where ID%2=0


select姓名=SName,年龄=Age from Students

select'姓名'=SName,'年龄'=Age from Students


--排序数据(倒序)

select*from Students orderby Age desc

--正序

select*from Students orderby Sex asc

--如果是正序asc关键字可以省略

select*from Students orderby Sex


--查询:将多列合并成一列

select ID,LastName+''+FirstName as EmpName

from Employees

--查询:指定列为空的数据

select*from Students where Address isnull

--查询:指定列不为空的数据

select*from Students where

Address isnotnull

--查询:指定列为空字符的数据

select*from Students where

Address=''

--查询:在查询中加入常量列

select*,'金华NIIT'as SchoolName

from Students

--查询top关键字的使用

selecttop 2 *from Students orderby Age desc


--以百分比查询表中的数据

selecttop 20 percent*from Students

--使用union来合并多表中的行为一个结果集

select LastName+''+FirstName as Emp

from Employees union

select A_LastName+''+A_FirstName as Emp

from Authors orderby Emp desc


--多列排序:先对指定第一列进行排序

--如第一列值相等,再用第二列进行排序

select*from StuMark orderby CourseID,Score

 

posted @ 2012-06-24 10:11  ComBat  阅读(169)  评论(0编辑  收藏  举报