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

SQL常用增删改查语句

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 (对应列名值) union

Select (对应列名值) union

Select (对应列名值)

二.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修改为9

5.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 scores

groupby Course_Id //分组查询

having Course_Id='jsj001'//分组子句筛选

七.S elect 多表连接查询语句

1.select s.stu_Name as'姓名',c.Course_name as'科目',sc.Score_Num

from Students as s

innerjoin 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_Num

from Students as s

leftouterjoin 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_Num

from Courses as c

rightouterjoin 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 文件组名<文件参数>

例:

usemaster

go

if exists(select*from sysdatabases where name='abc') dropdatabase abc

createdatabase abc

onprimary

(

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

)

https://www.doczj.com/doc/296524013.html,e 数据库名

go

create table 表名

(

字段数据类型列的特征

)

Go

例:

use db_myschool

go

if exists(select*from sysobjects where name='test1') droptable test1

createtable test1

(

Id int notnull,

SName nvarchar(50)notnull,

Tel int notnull

)

go

3.使用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_myqq

go

exec sp_grantdbaccess'abc'//添加用户名到数据库中

3.授权语句

Grant 权限 on 表名 to 数据库用户名

十.S QL编程语句

局部变量/全局变量

1.以@标记符作前缀

Declare @name varchar(8) //声明

Set @name = value

Select @name=value //赋值

2.以@@标记符作前缀

@@error //最后一个T-SQL错误的错误号

@@identity //最后一次插入的标识值

@@language //当前使用的语言的名称

@@max_connections //可以创建的同时连接的最大数目

@@rowcount //受上一个SQL语句影响的行数

@@servername //本地服务器的名称

@@servicename //该计算机上的SQL服务的名称

@@timeticks //当前计算机上每刻度的微秒数

@@transcount //当前连接打开的事务数

@@version //SQL Server的版本信息

4.输出

print'SQL服务名:'+@@servicename

select@@SERVICENAME as'SQL服务名'

5.逻辑控件语句

declare@avg float

select@avg=avg(Score_Num)from Scores where Stu_Id='sc0002'

print'平均分为'+convert(varchar(8),@avg)+'分'

if(@avg>90)

begin

print'最高分'

select MAX(Score_Num)from Scores

end

else

begin

print'最低分'

select MIN(Score_Num)from Scores

6.while 循环语句

declare@n int

while(1=1)

begin

select@n=COUNT(*)from Scores where Score_Num<60

if(@n>0)

update Scores set Score_Num+=2 where Score_Num<60 else

break

end

print'加分后的成绩'

select*from Scores

7.Case多分支语句

select Stu_id,score=case

when 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'

end

from Scores

十一.高级查询

1.where子查询

2.in 和 not in 子查询

3.if exists (子查询)

语句

十二. 事务

begintransaction //显式事务开始

declare@errorsum int

set@errorsum=0

update bank set CustomerMoney-=1000 where CustomerName='王小东' set@errorsum+=@@ERROR

update bank set CustomerMoney+=1000 where CustomerName='葛力'

set@errorsum+=@@ERROR

select*from bank

if(@errorsum<>0)

begin

print'交易失败'

rollbacktransaction //回滚事务

end

begin

print'交易成功'

committransaction //提交事务

end

go

select*from bank

3.索引

Create [unique] [clustered] [notclustered] index index_name

On table_name (column)

With

[fillfactor]

4.视图

Create view view_name

As