2024年3月20日发(作者:)

本文格式为Word版下载后可任意编辑和复制

数据库增删改查语句

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 表名//查询时定义输出一列常

第 1 页 共 8 页

本文格式为Word版下载后可任意编辑和复制

量值

5. select top 5 列名 from 表名 //查询前5行

6. select top 5 percent 列名 from 表名 //查询前百分之5的数据行

五.

1.

2.

3.

4. select 函数查询语句 selectLEN(Class_Name)fromClass //查询

class_Name字符串长度 selectupper(Class_Name)fromClass //查询class_Name

并转换为大写 ltrim和rtrim //清除字符串左右空格

selectREPLACE(card_No,0,9)fromCardRecord//修改列中字符串中的字符 列

名字符串中0修改为9

5. selectSTUFF(Card_No,2,3,8888)fromCardRecord

列名字符串中第2个开头删除3个字符,再从第二个开头插入8888字符串

6. selectGETDATE()//显示系统日期

六.

1.

2.

3.

4.

5. select 高级查询语句 select * from 表名 where列名 like ‘ %s%’ //

模糊查询 select * from 表名 where 列名 between 60 and 80 //范围查询

select * from 表名 where 列名 in (‘列举’,’’,’’) //在列举范围内

查询 selectSUM(Score_Num)fromscores //查询分数总和 avg max min

count //查询平均分/最大数/最小数/行数

selectcourse_Id,SUM(Score_Num)fromscores

groupbyCourse_Id//分组查询

havingCourse_Id=jsj001//分组子句筛选

第 2 页 共 8 页