sql server语法介绍(一)

最近在sql server 的时候,发现网上有很多标题为sql server语法的教程,不过有部分语句不能用,所以我就自己又在网上查了查,并把所有的敲了一遍。


--创建数据库
create database ZhouDatabess


--删除数据库
drop database ZhouDatabess


--删除一张表
drop table student


--创建一张表
create table student(stuID integer not null primary key,stuName varchar(5),stuAge integer) 


--从已有表创建新表
select * into newstudent from student


--备份一张表
exec sp_addumpdevice 'disk' ,'testBack' ,'d:\360Downloads'
backup database ZhouDatabess to testBack


--新加一列,列增加之后不能删除
alter table student add  size varchar(10)


----------------------------------增删选查-------------------------------------------
--添加数据(1)
insert into student(stuID,stuName) values(123,'张三')

--添加数据(2) ps:据说比第一种要快
insert into student(stuID,stuName)
select 135,'zxcz'
union all
select 133,'xcvxv'
union all
select 134,'zzz';


--添加数据(3)
insert into student(stuID,stuName)
values(136,'呵呵'),(137,'嘻嘻'),(138,'胜多')


--删除数据(1)
delete from student where stuID=123


--删除数据(2)
delete from student where stuID in(123,124,125)


--查找数据(1) ps:desc为倒序,asc为正序
select Top 5 stuID,stuName from student where stuID<140
order by stuID asc;


--查找数据(2) 
select * from student where stuName like '%z%'


--更新数据
update  student set stuName='zxc'  where stuID<133




---------------------主键部分------------------------------------------------
--添加一个主键

alter table student add primary key(stuID)


--删除一个主键,其实是删除一个主键约束
alter table newstudent drop constraint 主键名

【注,这里的主键名并不是上面设置的stuID,你可以用下面那个语句查找,也可以在sql server主键里面找到】


--查找主键约束
select name from sys.indexes where object_id=object_id('student')
and is_primary_key=1


---------------------索引及视图----------------------------------------------------


--创建一个索引,索引不可改,想要修改,必须删掉重建
create index studentindex on student(stuID)


--删除索引
drop index studentindex on student


--创建视图
create view studentview As select * from student


--删除视图
drop view studentview


---------------------几种函数----------------------------------------------------
--排序
select * from student order by stuID desc
--求和
select SUM(stuID) from student 
--求平均数
select AVG(stuID) from student
--求不为空的总数
select COUNT(stuAge) from student
--其他函数 max min 等等



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值