常见的SQL语法汇总(笔记)

SQL语句
382
0
0
2022-05-17
标签   SQL语句

语法:

学习网站建设的站长避免不了的就是对于数据库的操作,今天给大家分享的是sql server相关的操作技巧,希望能帮助到大家

常见的SQL语法汇总(笔记)

sql SERVER操作技巧

一、修改表

(1)修改表的名称

exec sp_rename '旧表名','新表名'

(2)修改列的名称

exec sp_rename '表名.旧表名','新表名'

(3)创建自动编号

alter table 表名 add 列名 int identity (1,1) primary key (列名)

(4)修改某列为主键

alter table 表名 add constraint pk_表名_列名 primary key (列名)

(5)查看表的约束

exec sp_helpconstraint 表名

(6)删除表的约束

alter table 表名 drop constraint 约束名

二、删除表

drop table 表名

三、删除表中的所有行

truncate table 表名

四、删除列

alter table 表名 drop column 列名

五、建立索引

create unique(唯一索引)/clustered(物理索引) index 列名(索引名) on 表名 (列名 次序,列名 次序)

六、删除索引

drop index 索引名

七、插入数据

insert into 表名 (列名,列名)

values (值,值)

八、修改数据

update 表名 set 列名=值 where 条件

九、删除数据

delete from 表名 where 条件

十、查询

select 匹配串 from 表名 where 条件

十一、更名

selete old_name as new_name from 表名 where 条件

十二、清除取值重复的行

selete distinct 列名 from 表名

十三、字符匹配(like)

selete 匹配串 from 表名 where 条件

例:查找姓刘的学生的姓名、年龄

selete sname,sage from student where sname like '刘%';

十四、转义符的使用

selete 匹配串 from 表名 where 条件

例:查询课程名称以“db_”开头,且倒数第3个字符为I的课程和详细情况

selete * from course where cname like 'db\_%I__'escape '\'

十五、使用集函数

select count(统计) from 表名 where 条件

sum(总和)

avg(平均值)

max(最大值)

min(最小值)

十六、对查询结果分组

select 匹配串 from 表名 group by 列名 having 条件

十七、集合查询

select 匹配串 from 表名 where 条件 union

select 匹配串 from 表名 where 条件

十八、建立视图

creat view 视图名 as selete 匹配串 from 表名 where 条件

十九、删除视图

drop view 视图名

二十、查询视图

select 匹配串 from 视图名 where 条件

二十一、更新视图

update 视图名 set 列名=值 where 条件

二十二、授权语句

grant 权限 on 表名 to 用户 with grant option(允许用户把授予的权限再授予给其他用户

二十三、收权语句

revoke 权限 on 表名 from 用户