Sql Server常用的Sql语句

1、创建数据库

  

Create DataBase students
on
(
  Name='students_data',            --数据文件的逻辑名
  FileName='D:\students_data.mdf',    --数据文件的实际名
  Size=10Mb,                --数据文件初始大小
  FileGrowth=15%            --数据文件的增长率
)
Log on
(
  Name='students_log',            --日志文件的逻辑名
  FileName='D:\students_log.ldf',    --日志文件的物理名
  Size=5Mb,                --日志文件的初始化大小
  FileGrowth=2Mb            --日志文件的增长率
)

2 创建表

Create Table studentInfo    /*按列名、数据类型、列描述创建学生信息表*/
(
  sName        Varchar(20)    Not Null,    --姓名,非空
  sNo        Char(6)        Primary Key,    --学号,主键
  sAge        Int        Null,        --年龄,可空
  sTime        SmallDateTime   Not Null,    --入学时间、非空、必须添
  sAddress    Varchar(50)    Default('深圳') --住址,使用默认值"深圳"
)
Create Table studentScore    /*创建学生分数表*/
(
  sId        Int Identity(1,1) Primary Key,    --编号(标识种子为1,增量为1)
  sNo        Char(6)              Not Null,    --学号
  sPhysics    Int          Not Null,    --物理成绩
  sMaths    Int          Not Null,    --数学成绩
  sChinese      Int           Not Null,    --语文成绩  
)

3、增加记录

Insert into studentInfo(sName,sNo,sAge,sTime,sAddress)
    values('帕瓦罗蒂','ST1001',16,'2008-07-07','深圳福田')

Insert into studentInfo(sName,sNo,sAge,sTime)
    values('神雕大侠','ST1003',16,'2012-2-6')

Insert into studentInfo
    values('仅次于狼','ST1002',16,'2012-2-6','神州大地')

一次增加多个记录:

CREATE TABLE [学生表] (Sno INT,Sname VARCHAR(4),Ssex VARCHAR(2),Sage INT,sdept VARCHAR(2))
INSERT INTO [学生表]
SELECT 95001,'李勇','',20,'CS' UNION ALL
SELECT 95002,'刘晨','',19,'IS' UNION ALL
SELECT 95003,'王敏','',18,'IS' UNION ALL
SELECT 95004,'张立','',19,'MA' UNION ALL
SELECT 96001,'徐一','',20,'IS' UNION ALL
SELECT 96002,'张三','',21,'CS' UNION ALL
SELECT 96003,'李四','',18,'IS'

 

4、更新记录

Update studentInfo
set sAddress = '日本广岛',sTime='2008-4-6'
where sName = '仅次于狼'

5 、删除记录

Delete studentInfo where sNo='ST1001'
Delete studentInfo where sTime<'2004-9-8' or sAge > 30

6、查询记录

select * from studentInfo where sTime>'2004-5-5' or sAge>=16

select sName as 姓名,地址=sAddress from studentInfo

select sName as 姓名,sAge from studentInfo order by sAge Desc
select * from studentInfo where sNo In('ST1002','ST1003')

select * from studentInfo where sAddress Like '%深圳%'

select * from studentInfo where sTime is not null

  /* Like'深圳%'  所有以'深圳'开头的字符串*/

  /* Like 'SB_'    所有以'SB’开头的三个字符串*/

  /* Like '0[0-9][0-9]'  所有三位电话区号如021 */

select top 2 sName from studentInfo where sAge>15    --查询年龄大于15的前两个人

select count(*) from studentInfo where sAge > 15    --查询年龄大于15的人数

select 平均年龄=Avg(sAge) from studentInfo    --查询平均年龄

select sum(sAge) from studentInfo
                                                
select 姓名=A.sName,物理=B.sPhysics,数学=B.sMaths,语文=B.sChinese,
    总分 = B.sPhysics + B.sMaths + B.sChinese
from studentInfo as A  --A是别名
Join studentScore as B --join on 将两个表连接起来
on A.sNo = B.sNo  
下面是存储过程的定义:  
Create Procedure mp_selectScoreByID
    @psNo  char(6),          --输入参数,学号
    @name varchar(20) OutPut, --输出参数,名字
    @score int       OutPut --输出参数,总分
As
    Declare @count int       --定义变量
    set     @count = 0      --给变量赋值
/*查询该学号姓名赋值给@name*/
select @name = sName from dbo.studentInfo where sNo=@psNo

/*查询该学号考试次数赋值给@count  */
select @count = count(*) from dbo.studentInfo where sNo=@psNo

/*如果考试次数不是一次返回-1,否则返回总分 */
if @count != 1
    return -1
else
    select @score = sPhysics + sMaths + sChinese
        from dbo.studentScore where sNo = @psNo
    return 0

Go
下面是执行存储过程:
declare @a Varchar(20)
declare @b Int
exec  dbo.mp_selectScoreById 'ST1002' , @a OutPut,@b OutPut
select 姓名 = @a,总分 = @b

转载于:https://www.cnblogs.com/wang7/archive/2012/06/05/2536247.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值