新建数据库

create database stu on
(name='s2',
filename='C:\temp\stu.mdf',
size=3MB,
filegrowth=5%
)
log on(
name='s2_log',
filename='C:\temp\stu.ldf',
size=3MB,
filegrowth=5%
)

删除数据库

创建学生表

create table stu_info(
    sid int primary key not null identity(1,1),
    name nvarchar(10) not null,
    sex char(2) check(sex in('男','女')) not null,
    age as datediff(year,birth,getdate()),
    birth date not null,
    addr nvarchar(50) default ('北京'),
    email nvarchar(50) check(email like '%@%'),
    tel nvarchar(30),
)

删除学生表

创建课程表和成绩表

create table course(
    cid int primary key not null identity(1,1),
    cname nvarchar(20) not null, 
)
-- 创建成绩表
create table score(
    sid int foreign key(sid) references stu_info(sid),
    cid int foreign key(cid) references course(cid),
    cj int not null check(cj between 0 and 100),
    primary key(sid,cid),    
)

处理数据

-- 修改表结构
-- 增加字段
alter table stu_info add mr bit
select * from stu_info
alter table stu_info add beizhu nvarchar(200);

-- 删除字段
alter table stu_info drop column beizhu;

-- 修改字段
alter table stu_info alter column beizhu nvarchar(50);

-- 增加数据
-- 全部字段插入
insert into stu_info values('w1','男','1988-8-8','上海','w1@onetest.com','13825689516',0);

-- 插入部分记录
insert into stu_info(name,sex,birth,email) values('w2','女','1999-1-1','w2@abc.com');

-- 插入空字段
insert into stu_info values('w3','女','2002-1-1',DEFAULT,null,null,null);
-- 一次插入多行数据
insert into stu_info 
select'w4','女','2012-1-1','武汉',null,null,1 union
select'w5','男','1992-1-1','武汉',null,'18510000001',1 union
select'w6','男','1997-1-1','上海','w6@def.com','13411125566',0 union
select'w7','男','2007-7-21','上海','w7@wdt.com','18266578888',0

-- 插入课程表纪录
insert into course
select '渗透测试' union
select '安全运维' union
select '软件开发' union
select '安全开发' union
select '逆向工程' 
select * from course;

-- 插入成绩表记录
insert into score
select 1,1,60 union
select 1,2,90 union
select 1,3,30 union
select 1,4,75 union
select 1,5,100 union
select 2,2,40 union
select 2,5,100 union
select 3,2,60 union
select 3,3,90 union
select 7,2,65
select * from score;

-- 修改记录
select * from stu_info;
-- 将姓名为w7的学生的性别改为女
update stu_info set sex='女' where name='w7'
-- 将所有北京人的邮箱为空的改为'bj@126.com'
update stu_info set email='bj@126.com' where addr='北京' and email is null;

-- 删除记录name为w6的学生
delete from stu_info where name='w6';

insert into stu_info values('w8','男','2007-7-21','上海','w7@wdt.com','18266578888',0)
delete from stu_info where name='w8';

-- 获得元数据

-- 获得所有数据库名
select name from master..Sysdatabases order by name
-- 获得所有的表名
select name from SysObjects where xtype='U'
-- 获得所有字段名
select name from SysColumns where id=OBJECT_ID('stu_info')
-- 获得数据库版本
select @@version
-- 查询服务名
select @@SERVERNAME
-- 查询主机名
select HOST_NAME();
-- 查询当前数据库名
select DB_NAME();
-- 查询第一个数据库名
select DB_NAME(1)
-- 查询当前数据库的拥有者
select USER

-- 聚合函数

-- 总共有多少人
select COUNT(*) from stu_info;
-- 总分
select SUM(cj) from score;
-- 第一名信息
select top 1 * from score order by cj desc;
-- 最值聚合函数
select MAX(cj) as '成绩' from score;
select MIN(cj) as '成绩' from score;
select AVG(cj) as '成绩' from score;
-- 男生多少人,女生多少人
select sex,COUNT(*) from stu_info
group by sex;
-- 男生的人数
select sex,COUNT(*) from stu_info
group by sex
having sex='男';

select sex,COUNT(*) from stu_info 
where sex='男'
group by sex;

-- 每个学生的平均成绩
select * from score
select sid,AVG(cj) from score
group by sid;

-- 每个学生的平均成绩低于70的显示
select sid,AVG(cj) from score
group by sid
having AVG(cj)<70;

-- 邮箱不为空的人数
select COUNT(*) from stu_info
where email is not null;

-- 每门课程的最高分
select cid,MAX(cj) from score
group by cid;

-- 至少有一门课程及格的学生学号
select sid from score
where cj>=60
group by sid;

select distinct sid from score
where cj>=60

-- 至少有两门课程及格的学生学号
select sid from score
where cj>=60
group by sid
having count(sid)>=2

-- 只要有及格的就找到
select distinct sid from score
where cj>=60;

-- 每个人的平均分
select sid,AVG(cj) from score
group by sid;

-- 每门课程的平均分
select cid,AVG(cj) 课程平均分 from score
group by cid;

-- 每人总分
select sid,SUM(cj) 学生总分 from score 
group by sid;

-- 平均分大于70分的学生
select sid,AVG(cj) 学生平均分 from score
group by sid
having AVG(cj)>70;

-- 60分以上的成绩中,每个人平均分
select sid,AVG(cj) from score
where cj>=60
group by sid;

-- 每个人平均分,从大到小排:按照学号分组,求每个人的平均分
select sid,AVG(cj) from score
group by sid 
order by avg(cj) desc;

-- 时间函数

select GETDATE();
select DATEDIFF(MM,'04/01/1999','07/01/1999');
select DATEPART(WEEKDAY,GETDATE());
select YEAR(GETDATE());
select MONTH(GETDATE());
select DAY(GETDATE());
select CONVERT(varchar(5),12345)+'是数字';
select CONVERT(varchar (20),getdate(),120);

-- 找出出生在8月的学生信息
select * from stu_info
where month(birth)=8

-- 找出所有的90后
select * from stu_info
where YEAR(birth) between 1990 and 1999;
-- 延时
waitfor delay '0:0:3';
-- 字符串函数
select LEN('sql课程')
select RIGHT('买卖提,吐尔松',3)
select left('买卖提,吐尔松',3)
-- 将所有姓w的改称姓y的 
select COUNT(*) from stu_info where name like 'w%' 
update stu_info set name='y'+RIGHT(name,LEN(name)-1) 
where name like 'w%' 
select * from stu_info
-- unionn查询
select 1,2,3 union select 4,5,6;

-- 存储过程

-- 局部变量
declare @a nvarchar(10);
set @a='zhang';
select @a;
print @a;

-- 无参存储过程
-- 求平均分大于70的学生
create proc p1
as
    declare @a int;
    select @a=AVG(cj) from score
    if @a>70
        print '合格'
    else 
        print '不合格'
go
-- 删除存储过程(在可编程性》存储过程显示)
drop proc p1
-- 执行存储过程
exec p1

-- 带参数的存储过程 使用execute语句调用存储过程查询信息基础的平均分,最高分和最低分数
create proc p2 @ccid int 
as 
    select AVG(cj) as 平均分,MAX(cj) as 最高分,MIN(cj) as 最低分数 
    from score
    where cid=@ccid;
go

-- 执行存储过程
exec p2 2

-- 系统存储过程
-- 显示服务器上所有数据库
exec sp_databases;
-- 报告所有数据库或指定数据库信息
exec sp_helpdb;
-- 返回当前环境下可查询的对象的列表、
exec sp_tables;
-- 修改数据库名称
exec sp_renamedb stubak,stu;
-- 返回某个列表的信息 ,如学生表(stu_info)
exec sp_columns stu_info;
-- 查看某个表的所有信息,如stu表
exec sp_help stu;
-- 列出当前环境中的所有储存过程
exec sp_stored_procedures;
-- 显示默认值,未加密的储存过程,用户定义的储存过程,触发器或视图的实际文本
exec sp_helptext p1;
-- 修改用户密码
exec sp_password NulL,'123456','sa';

-- 些高风险的存储过程
-- 获得MS SQL的版本号
execute master..sp_msgetversion
-- 得到硬盘文件信息
-- 参数说明:目录名,目录深度,是否显示文件
execute master..xp_dirtree 'c:' 
execute master..xp_dirtree 'c:',1,1
-- 列出服务器上所有windows本地组 
execute master..xp_enumgroups
-- 列出sql server错误日志列表,最后更新时间 
execute master..xp_enumerrorlogs
-- 列出服务器上固定驱动器,以及每个驱动器的可用空间
execute master..xp_fixeddrives;
-- 得到当前sql server服务器的计算机名称
execute master..xp_getnetname;
-- 列出当前错误日志的具体内容 
EXEC [master].[dbo].[xp_readerrorlog]
-- 列出指定目录的所有下一级子目录 
EXEC [master].[dbo].[xp_subdirs] 'c:\WINDOWS'
---列出驱动器的名称 --以字节为单位的空闲空间(low free) --以驱动器类型:软驱(1),硬盘(2),cd-rom(8) 
EXEC [master].[dbo].[xp_availablemedia]
-- 查看所有用户 
exec sp_helpuser;

server2003安装sqlserver以及基本操作相关推荐

  1. Gradle安装使用以及基本操作

    转自:https://www.cnblogs.com/linkstar/p/7899191.html Gradle安装使用以及基本操作 阅读目录 简单介绍 安装 使用idea创建一个web的Gradl ...

  2. 安装完mysql后sqlserver_您还在用下一步下一步的方式安装SQLSERVER和SQLSERVER补丁吗?...

    注意下面的参数必须要指定的: /q:完全没有界面 /qs:有界面,界面只是作为显示进度的用途 /IACCEPTSQLSERVERLICENSETERMS:接受许可条款 /PID:产品密钥 指定 SQL ...

  3. centos7 安装sqlserver驱动以及扩展

    安装sqlserver驱动 sudo su curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repo ...

  4. 完整安装sqlserver always on集群

    准备工作 1.  四台已安装windows server 2008 r2 系统的虚拟机,配置如下: CPU : 1核 MEMORY : 2GB DISK : 40GB(未分区) NetAdapter ...

  5. ubuntu php7 pdo,记录捣鼓ubuntu下PHP7.1下安装sqlserver拓展

    记录捣鼓ubuntu下PHP7.1下安装sqlserver拓展 由于工作需要,得使得PHP支持sqlserver拓展来操作sqlserver数据库. 但是进行到Step2.1时,通过phpinfo() ...

  6. linux安装sqlserver

    1.下载 Microsoft SQL Server 2017 Red Hat 存储库配置文件: sudo curl -o /etc/yum.repos.d/mssql-server.repo http ...

  7. windows 2008 r2或win7安装SP1补丁,安装sqlserver 2012

    说明:安装sql server 2012时,win7和win2008r2系统都需要打sp1补丁. 1.SP1补丁下载地址(建议用迅雷下载): http://download.microsoft.com ...

  8. @Redis(redis简介,下载与安装配置,基本操作)

    title: Redis author: Xoni tags: Redis categories: java学习 Redis abbrlink: bae4ff13 Redis基础 1. Redis 简 ...

  9. 护卫神怎么安装mysql,护卫神·主机大师增加一键安装SQLServer功能_护卫神

    好消息,护卫神·主机大师 增加一键安装SQLServer数据库的功能! 自从主机大师推出市场以来,都将安全作为重中之重,深受广大用户的好评.我们也在不断地对其进行升级完善,让其更加好用.实用和安全. ...

  10. Server2003安装SP2补丁提示密钥无效的解决方法

    Server2003安装SP2补丁提示密钥无效的解决方法 机器一直没有打SP2的补丁.几天要安装一个程序,还非要SP2不可.没办法.居然打补丁的时候出现了这样的错误信息: -------------- ...

最新文章

  1. java字节流6_JavaIO流之字节流
  2. 【控制】二阶含时滞多智能体系统一致性仿真程序 离散化处理
  3. freemarker 教程
  4. linux搭建ca服务器搭建,linux下安装EJBCA 搭建私有CA服务器
  5. 【数据竞赛】盘点Kaggle中常见的AutoEDA工具库
  6. MyBatis 源码分析-技术分享
  7. Angular 开发里的 polyfills.js、runtime.js、styles.js 和 vendor.js 是用来做什么的
  8. 2018第二届河北省大学生程序设计竞赛题解
  9. Linux minilogd占用内存过高及开机启动项修改
  10. android udt协议,接口和软件包  |  Android 开源项目  |  Android Open Source Project
  11. 敏捷开发绩效管理之六:敏捷开发生产率(中)(功能点分析,FPA,简化的功能点)...
  12. 数据结构计算机专业教学计划编制,[实验报告]教学计划编制问题
  13. Elasticsearch数据迁移工具elasticdump工具
  14. Vue项目上传github并预览
  15. active mq topic消费后删除_天天在用消息队列,却不知道为啥要用 MQ ,这就尴尬了...
  16. 【LaTex使用总结】LaTex,pdflatex,xelatex,xetex等的区别和关系
  17. 服务器驱动用什么工具_服务器是做什么用的,具体有什么作用,为什么机房要用服务器?...
  18. gitbook转PDF或epub
  19. VSCode 配置 python环境 相对齐全 有遇到问题欢迎投稿哈
  20. 线性方法求欧拉数-POJ2478

热门文章

  1. 华为手机root的最简单方法教程!附专用root工具!详细
  2. 【数学】徐小湛第七高等数学新版
  3. 143个相见恨晚的排行榜网站,总有一个用得着!
  4. 安全合规/法案--30--《网络安全审查办法》原文及解读
  5. c++报错:表达式必须含有常量值
  6. Vue中this的指向问题
  7. opencv-python 图像基础处理(三)
  8. “老三论”与“新三论”
  9. 关闭jupyter notebook报错:python.exe-应用程序错误
  10. 著名的php项目,PHP著名开源项目汇总