当前位置:文档之家› SQL常用增删改查语句

SQL常用增删改查语句

SQLSQL常用增删改查语句作者:hiker一.Insert 插入语句1.Insert into 表名(列名)values (对应列名值) //插入一行.2.Insert into 新表名(列名)Select (列名) 旧表名3.Select 旧表名.字段…Into 新表名from 旧表名4.Select identity ( 数据类型,标识种子,标识增长量) as 列名Into新表名From 旧表名5.Insert 表名(列名)Select (对应列名值) unionSelect (对应列名值) unionSelect (对应列名值)二.Update 更新语句1.Update 表名set 列名=’更新值’ where 更新条件三.delete 删除语句1.delete from 表名where 删除条件2.truncate table 表名//删除表中所有行四.select 基本查询语句1.select 列名from 表名where 查询条件order by 排序的列名asc或desc升/降2.select 列名as 别名from 表名where 查询条件3.select 列名from 表名where 列名is null //查询空值4.select 列名, ‘常量值’ as 别名from 表名//查询时定义输出一列常量值5.select top 5 列名from 表名//查询前5行6.select top 5 percent 列名from 表名//查询前百分之5的数据行五.select 函数查询语句1.select LEN(Class_Name)from Class //查询class_Name字符串长度2.select upper(Class_Name)from Class //查询class_Name并转换为大写3.ltrim和rtrim //清除字符串左右空格4.select REPLACE(card_No,'0','9')from CardRecord//修改列中字符串中的字符列名字符串中0修改为95.select STUFF(Card_No,2,3,'8888')from CardRecord列名字符串中第2个开始删除3个字符,再从第二个开始插入8888字符串6.select GETDATE()//显示系统日期六.select 高级查询语句1.select * from 表名where列名like ‘ %s%’//模糊查询2.select * from 表名where 列名between 60 and 80 //范围查询3.select * from 表名where 列名in (‘列举’,’’,’’) //在列举范围内查询4.select SUM(Score_Num)from scores //查询分数总和5.avg max min count //查询平均分/最大数/最小数/行数select course_Id,SUM(Score_Num)from scoresgroupby Course_Id //分组查询having Course_Id='jsj001'//分组子句筛选七.S elect 多表连接查询语句1.select s.stu_Name as'姓名',c.Course_name as'科目',sc.Score_Numfrom Students as sinnerjoin Scores as sc on(sc.Stu_Id=s.Stu_ID)innerjoin Courses as c on(sc.Course_Id=c.Course_Id)orderby s.Stu_Name desc //三表内联查询2.select s.stu_Name as'姓名',c.Course_name as'科目',sc.Score_Numfrom Students as sleftouterjoin Scores as sc on(sc.Stu_Id=s.Stu_ID)leftouterjoin Courses as c on(sc.Course_Id=c.Course_Id)//三表左外联查询,以stu表为主,其它表为从。

3.select s.stu_Name as'姓名',c.Course_name as'科目',sc.Score_Numfrom Courses as crightouterjoin Scores as sc on(sc.Course_Id=c.Course_Id)rightouterjoin Students as s on(sc.Stu_Id=s.Stu_ID)//三表右外联查询,以stu右表为主,其它表为从。

八.C reate 创建数据库语句1.create database 数据库名on[primary](<数据文件参数>[,…n] [<文件参数>])[log on]({<日志文件参数> […n]})文件参数:Name=逻辑文件名,filename=物理文件名,size=大小,maxsize=最大容量,Filegrowth=增长文件组参数:Filegroup 文件组名<文件参数>例:usemastergoif exists(select*from sysdatabases where name='abc') dropdatabase abccreatedatabase abconprimary(name='abc',filename='d:\abc.mdf',size=5,maxsize=50,filegrowth=10%)log on(name='abc_log',filename='d:\abc_log.ldf',size=2,maxsize=20,filegrowth=1)e 数据库名gocreate table 表名(字段数据类型列的特征)Go例:use db_myschoolgoif exists(select*from sysobjects where name='test1') droptable test1createtable test1(Id int notnull,SName nvarchar(50)notnull,Tel int notnull)go3.使用SQL语句创建和删除约束alter table表名Add constraint 约束名约束类型描述说明altertable dbo.test addconstraint PK_ID primarykey (ID)主键:primary key PK_唯一:unique UQ_检查:check CK_ 默认:default DF_ 外键:foreign key FK_ 九.登录验证语句1.exec sp_addlogin'abc','abc'//添加SQL用户名use db_myqqgoexec sp_grantdbaccess'abc'//添加用户名到数据库中3.授权语句Grant 权限 on 表名 to 数据库用户名十.S QL编程语句局部变量/全局变量1.以@标记符作前缀Declare @name varchar(8) //声明Set @name = valueSelect @name=value //赋值2.以@@标记符作前缀@@error //最后一个T-SQL错误的错误号@@identity //最后一次插入的标识值@@language //当前使用的语言的名称@@max_connections //可以创建的同时连接的最大数目@@rowcount //受上一个SQL语句影响的行数@@servername //本地服务器的名称@@servicename //该计算机上的SQL服务的名称@@timeticks //当前计算机上每刻度的微秒数@@transcount //当前连接打开的事务数@@version //SQL Server的版本信息4.输出print'SQL服务名:'+@@servicenameselect@@SERVICENAME as'SQL服务名'5.逻辑控件语句declare@avg floatselect@avg=avg(Score_Num)from Scores where Stu_Id='sc0002'print'平均分为'+convert(varchar(8),@avg)+'分'if(@avg>90)beginprint'最高分'select MAX(Score_Num)from Scoresendelsebeginprint'最低分'select MIN(Score_Num)from Scores6.while 循环语句declare@n intwhile(1=1)beginselect@n=COUNT(*)from Scores where Score_Num<60if(@n>0)update Scores set Score_Num+=2 where Score_Num<60 elsebreakendprint'加分后的成绩'select*from Scores7.Case多分支语句select Stu_id,score=casewhen Score_Num>90 then'A'when Score_Num between 80 and 89 then'B'when Score_Num between 60 and 79 then'C'else'D'endfrom Scores十一.高级查询1.where子查询2.in 和 not in 子查询3.if exists (子查询)语句十二. 事务begintransaction //显式事务开始declare@errorsum intset@errorsum=0update bank set CustomerMoney-=1000 where CustomerName='王小东' set@errorsum+=@@ERRORupdate bank set CustomerMoney+=1000 where CustomerName='葛力'set@errorsum+=@@ERRORselect*from bankif(@errorsum<>0)beginprint'交易失败'rollbacktransaction //回滚事务endbeginprint'交易成功'committransaction //提交事务endgoselect*from bank3.索引Create [unique] [clustered] [notclustered] index index_nameOn table_name (column)With[fillfactor]4.视图Create view view_nameAs<select 语句>十三.创建存储过程1.create procedure 存储过程名{@参数1 数据类型} =默认值 ouput…AsSql语句不带参数例:use db_myschoolgoif exists(select*from sysobjects where name='proc_Stu')dropprocedure proc_Stugocreateprocedure proc_Stuasdeclare@avg floatselect@avg=AVG(Score_Num)from Scores where Course_Id='jsj001'print'科目的平均分:'+convert(varchar(5),@avg)+'分'if(@avg>80)print'本科目成绩优秀'elsebeginprint'本科目成绩较差'print'=============================='print'70分以下的学员'select Stu_Name,Scores.Score_Num,Courses.Course_Name from Students innerjoin Scores on (Students.Stu_ID=Scores.Stu_Id)innerjoin Courses on (Scores.Course_Id=Courses.Course_Id)where Score_Num<70 and courses.Course_Id='jsj001'go带输出参数例:略此文档整理了平时用到的SQL语句,在运用中忘记语句时,希望此文档能帮助到每一位需要的您!。

相关主题