当前位置:文档之家› MSSQL数据库各种语句学习资料

MSSQL数据库各种语句学习资料

数据库(catalog)表(table)列(column)或叫字段(field)数据类型(datatype)记录(record)或叫行(row)主键(PrimaryKey)索引(index)表关联:这种将两张表通过字段关联起来的方式就被称为“表关联”,关联到其他表主键的字段被称为“外键”例子:select * from employees where age<18delete from employees where position=‘名誉总裁’create table T_person (FName Varchar(20),FAge int,FRemark Varchar(20),primarykey(FName));create table T_Debt(FNumber Varchar(20),FAmount Numeric(10,2) NOT NULL,FPerson varchar(20),PrimaryKey(FNumber),foreignkey(FPerson) references T_Person(FName));insert into T_person(FName,FAge,FRemark)values('tom',18,'USA') 注:在插入数据的时候某些字段没有值,我们可以忽略这些字段,例子:insert into T_Person(FAge,FName) values(22,'lxf')说明:Numeric(10,2) 指定字段是数字型,长度为10位,小数为两位foreignkey(FPerson)外部约束主键为FPerson说明:增加一个列Alter table tabname add columnname type例子:alter table dbo.T_Person add Fcity varchar(20)*非空约束对数据插入或更新的影响如果对一个字段添加了非空约束,那么我们是不能向这个字段中插入或更新为NULL值的。

*主键对数据插入或更新的影响主键是在同一张表中必须是唯一的,如果在进行数据插入或更新的时候指定的主键与表中已有的数据重复的话则会导致违反主键约束的异常。

*外键对数据插入或更新的影响外键是指向另一个表中已有的数据的约束,因此外键值必须是在目标表中存在的。

如果插入或更新的数据在目标表中不存在的话则会导致违反外键约束异常。

**UPDATEupdate T_Personset FRemark='sonin'update T_Personset FAge=12where FName='tom'update T_Personset FAge=22where FName='jim' or FName='LXF'**DELETEdelete from T_Person;删除T_Person表中的所有数据drop table T_Person;删除表中的所有数据,及把表结构全部删除。

delete from T_Person where FAge>20 or FRemark='Mars'********数据检索select * from T_Employeeselect FNumber,FName,FAge,FSalary from T_Employeeselect FNumber as 编号,FName as 姓名,FAge as 年龄from T_Employee (其中的‘as’不是必须的,是可以省略的)select * from T_Employeewhere FSalary<5000 or FAge>25;几种聚合函数:MAX 计算字段最大值MIN 计算字段最小值A VG 计算字段平均值SUM 计算字段合计值COUNT 统计数据条数select MAX(FSalary) from T_Employeewhere FAge>25 注:查询年龄大于25岁的员工的最高工资。

select MAX(FSalary) as MAX_SALARY from T_Employeewhere FAge>25select A VG(FAge) from T_Employeewhere FSalary>3800 注:统计工资大于3800元的员工的平均年龄。

select SUM(FSalary) from T_Employee; 注:统计应支出工资的总额。

select MIN(FSalary),MAX(FSalary) from T_Employee; 注:多次使用聚合函数,统计公司的最低工资和最高工资。

select COUNT(*),COUNT(FNumber) from T_Employee; 注:COUNT(*)统计的是结果集的总条数,而COUNT(FNumber)统计的则是除了结果集中FNumber字段不为空值(也就是不等于NULL)的记录的总条数。

*****排序select * from T_Employeeorder by FAge ASC 注:按升序排列,ASC是可以省略的select * from T_Employeeorder by FAge DESC 注:按降序排列,select * from T_Employeeorder by FAge DESC, FSalary DESC; 注:order by 允许指定多个排序列,首先按第一个排序,分不出的按第二个排序。

**** select * from T_Employeewhere FAge>23order by FAge DESC,FSalary DESC;注:ORDER BY 子句要放到where子句后,不能颠倒它们的顺序。

*******通配符过滤SQL中的通配符过滤使用LIKE关键字。

注:使用通配符时,数据库要对全表进行扫描,所以速度非常慢,不要过分使用通配符。

1.单字符匹配select * from T_Employeewhere FName LIKE '_erry'; 注:以任意字符开头,剩余部分为“erry”。

select * from T_Employeewhere FName LIKE '__n_' ; 注:检索长度为4,第三个字符为“n”,其他字符为任意字符的姓名。

2.多字符匹配select * from T_Employeewhere FName LIKE 'T%' ; 注:检索以“T”开头,长度任意,select * from T_Employeewhere FName LIKE '%n%' ; 注:检索姓名中包含字母“n”的员工信息select * from T_Employeewhere FName LIKE '%n_' ; 注:检索最后一个字符为任意字符,倒数第二个字符为“n”长度任意的字符串。

select * from T_Employeewhere FName LIKE '[SJ]%' ;注:检索的是以“S”或者“J”开头,长度任意的数据select * from T_Employeewhere FName LIKE '[^SJ]%' ;注:否定符“^”是来对集合取反,即检索的是不以“S”或者“J”开头,长度任意的数据******空值检测select * from T_Employeewhere FName IS NULL ; 注:不能使用普通的等于运算符进行判断,而要使用IS NULL 关键字。

select * from T_Employeewhere FName IS NOT NULL ; 注:检索FName字段不为空的数据。

select * from T_Employeewhere FName IS NOT NULL AND FSalary<5000; 注:查询所有姓名已知且工资小于5000的员工的信息。

*****反义运算符select * from T_Employeewhere FAge!=22 AND FSalary!<2000 ;注:检索所有年龄不等于22岁并且工资不小于2000员的信息。

<> 不等于<= 不大于>= 不小于NOT 运算符用来将一个表达式的值取反select * from T_Employeewhere NOT(FAge=22) AND NOT(FSalary<2000) ;注:检索所有年龄不等于22岁并且工资不小于2000元的信息。

“!”运算符只能运行MSSQL和DB2两种数据库上,统一运算符可以使用在所有数据库中,建议采用NOT运算符,能比较容易的表达要实现的需求。

*****多值检测select FAge,FNumber,FName from T_Employeewhere FAge IN(23,25,28) ; 注:为了解决进行多个离散值的匹配问题,SQL提供了IN语句。

检索年龄为23,25,28的数据。

select * from T_Employeewhere FAge between 23 and 60 ;注:检索年龄在23到60岁之间的数据,包括23和60。

select * from T_Employeewhere (FSalary between 2000 and 3000)OR (FSalary between 5000 and 8000) ; 注:检索所有工资介于2000元到3000元之间以及5000元到8000元的员工信息。

*******数据分组ALTER TABLE T_Employee ADD FSubCompany V ARCHAR(20);ALTER TABLE T_Employee ADD FDepartment V ARCHAR (20); 注:ALTER ADD 通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。

**GROUP BY 子句进行分组select FAge from T_Employeewhere FSubCompany='Beijing'group by FAge ; 注:采用分组以后的查询结果是以分组形式提供的。

select FSubCompany,FDepartment from T_Employeegroup by FSubCompany,FDepartment ; 注:先根据FSubCompany,再在每个小组内根据FDepartment进行二次分组,查询数据select FAge,COUNT(*) AS CountOfThisAge from T_EmployeeGROUP BY FAge ; 注:检索每个年龄段的员工的人数select FSubCompany,FAge,COUNT(*) AS CountOfThisSubCompAge from T_Employee group by FSubCompany,FAgeorder by FSubCompany ; 注:统计每个公司的年龄段的人数。

相关主题