Sql sever的一些基础语句

Sql的基本操作有增删改查等等
作为初学者,我记录了一些基本语句
使用现在的数据库软件,无论关键字是大写还是小写都能正常运行,为了书写方便,我就都小写了

查询所有数据库
select * from sys.databases;		--查询所有数据库
创建以及启用数据库
create database lab;		--创建数据库(简单形式)
create database TEMP		--创建数据库
on(
name='TEMP',
filename='E:\TEMP.mdf',	--Primary data files
size=10MB,
maxsize=100MB,
filegrowth=5MB
)
log on(
name='TEMP_log',
filename='E:\TEMP.ldf',	--Log data files
size=10MB,
maxsize=100MB,
filegrowth=5MB
)
use lab;		--启用某数据库
创建表
create table students(		--创建students表
	name varchar(10),		--列名name,数据类型为长度为10的varchar
	id int					--列名id,数据类型为int
)							
insert into students(name,id) values('张三',202000);		--增加数据
insert into students(name,id,grade)values('王二',202002,90),('麻子',202003,80);		--插入多组数据
alter table students add grade tinyint;						--增加列
delete from students where name = '张三';		--用where来定向删除数据
alter table students drop column column_name;	--删除列,column_name为列名
drop table students;							--删除表
drop database lab;								--删除数据库
update students set grade = 0;						--将所有grade设置为0
update students set grade = 60 where name = '李四';	--用where来定向更新数据
exec sp_rename students,school;						--将students表改名为school
exec sp_rename '表名.列名','新列名';					--修改列名
ALTER TABLE students MODIFY COLUMN s_name VARCHAR(50); --修改列的属性,这里是将其改为varchar(50);
select * from sys.tables;		--查询当前数据库中所有表
select * from students;			--查询students表的所有数据
select * from students where name = '李四';		--用where定向查询name为李四的所有信息
select o_quantity from orders;	--仅查询o_quantity列
select o_quantity alias from orders;	--给o_quantity列取个别名alias
模糊查询

使用LIKE和正则表达式进行模糊查询
使用通配符_和%
_可以匹配1个任意字符,%可以匹配0-多个字符
若要查询的字符串中含有这两个字符则用_和%进行转义

select * from students where name like '__';	两个下划线,意为查询所有名字是两个字的人
排序

ORDER BY

select * from students order by(id) desc;	意为以id为基准进行排序,方式为desc(降序) 
select * from students order by(id) asc;	意为以id为基准进行排序,方式为asc(升序)
select * from students order by(id);	意为以id为基准进行排序,方式为默认(asc)
多个重复值仅返回一个

DISTINCT

select distinct name FROM students;	 若有重复的学生姓名则仅会返回一个 
分组

GROUP BY

select sex,figure,name from students group by sex,figure,name;
--依照sex,figure,name的顺序分组,以前面的顺序为准
聚合函数

count(*) 返回项目的数量

--count
select count(*) from students;	返回学生信息的行数
select count(distinct id) from students;   仅返回id不重复的学生信息
--max
select max(o_quantity) from orders;	--返回o_quantity的最大值
--min
select min(o_quantity) from orders;	--返回o_quantity的最小值
--avg
select avg(o_quantity) from orders;	--返回o_quantity的平均值
--sum
select sum(o_quantity) from orders;	--返回全体o_quantity的和
select sum(o_quantity)alias from orders group by p_no;	--按p_on分组后返回各组o_quantity的和
having语句
select sum(o_quantity)alias from orders group by p_no having (sum(o_quantity)>5);	--按p_on分组后返回各组o_quantity的和,用having进行筛选,选出其中>5的
exists的使用
select m_account from members
where exists (select * from orders where orders.p_no=101 
and orders.m_account = members.m_account)
--exist()中的东西存在时,exist()前的查询才会返回结果,否则什么都不返回
in的使用
select p_no,m_account from orders
where p_no in 
(select p_no from orders where orders.m_account = 'lfz')
--括号内select的结果为多个,p_no为其中一个即可
获取当前年份
select year(getdate());
BETWEEN AND
update students set figure = '胖' where id between 202001 and 202003;	将学号属于[202001,202003]的人的身材修改为胖
创建视图
create view ori
as
select products.p_on,products.p_price,o_quantity
from products,orders
--从products和orders表中组合出几项作为视图
grant和revoke
grant select,update(o_quantity)
on orders 
to U1;
--将查询表orders和更新o_quantity列的权限赋予用户U1
grant select
on orders
to U1
with grant option;
--将查询表orders和更新o_quantity列的权限赋予用户U1,U1可以赋给其他用户
create view ori
as
select products.p_on,products.p_price,o_quantity
from products,orders
grant select
on ori
to U1;
---------------------------------------------------------------------
revoke select,update(o_quantity)
on orders 
from U1;
回收U1的查询表orders和更新o_quantity列的权限

另外,不同的SQL的语法存在一定的差异,
例如Mysql的show databases和show tables,SQL sever里面就没有,而是采用了select * from sys.databases和select * from sys.tables。所以在网上查找Sql相关语句时直接复制回来可能会因报错而不能使用。

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值