sql server常用语句
创建数据库
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 --日志文件的增长率 )
创建数据表
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 studentInfo /*按列名、数据类型、列描述创建学生信息表*/ ( sName Varchar(20) Not Null, --姓名,非空 sNo Char(6) Primary Key, --学号,主键 sAge Int Null, --年龄,可空 sTime SmallDateTime Not Null, --入学时间、非空、必须添 sAddress Varchar(50) Default('深圳') --住址,使用默认值"深圳" )
增加记录
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'
更新记录
Update studentInfo set sAddress = '日本广岛',sTime='2008-4-6' where sName = '仅次于狼'
删除记录
Delete studentInfo where sNo='ST1001' Delete studentInfo where sTime<'2004-9-8' or sAge > 30
查询记录
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