SQL Server 数据库基本操作语句总结

代码如下:

--sql基本操作

--创建数据库

create database Studets

--创建表

create table student ( sno char(5), sname char(20), ssex char(2), sage smallint, sdept char(15) )

create table course ( cno char(3), cname char(30), cpno char(3), ccredit smallint )

create table sc ( sno char(5), cno char(3), grade int )

--查看表信息

select * from student select sno as 学号 from student select * from course select * from sc

--修改表

--插入列

alter table student add  scome  datetime

--修改列的字段类型 alter table student alter column scome  char(50)

--删除 --删除列

alter table student drop column scome

--删除表 drop table student drop table course drop table sc

--完整性约束实现

--sno 非空唯一,ssex检查约束, sage默认大小

create table student ( sno char(5) not null unique, sname char(20), sex char(2), sage smallint default 20, sdept char(15), constraint sex check(sex in('男','女')), )

--删除表的约束 alter table student drop  constraint ssex

--添加字段约束 alter table student add constraint ssex check(sex in('男','女'))

--添加主键约束 alter table student add constraint PK_SNO primary key(sno) create table course ( cno char(3) not null unique, cname char(30), cpno char(3), ccredit smallint )

--关联表主键已经存在,可以如下操作添加主键和外键约束

alter table course add constraint PK_CNO primary key(cno), constraint FK_CPNO foreign key(cpno) REFERENCES sc(cno)

create table sc

(

sno char(5) foreign key references student(sno),

cno char(3) foreign key references course(cno),

grade int,

constraint PK_SC primary key(sno,cno)

)

ALTER TABLE [dbo].[sc] DROP CONSTRAINT [FK__sc__sno__0F975522]

ALTER TABLE [dbo].[sc] DROP CONSTRAINT [PK_SC]

ALTER TABLE [dbo].[sc] DROP CONSTRAINT [PK_SC]

--创建sc后,通过如下修改主外键

alter table sc add constraint PK_SC primary key(sno,cno),

constraint FK_SNO foreign key(sno) references student(sno),

constraint FK_CNO foreign key(cno) references course(cno)

--创建索引。

分为聚簇索引(clustered物理顺序)和非聚簇索引(nonclustered逻辑顺序,可多个)

代码如下:

--not null约束字段时候。会创建一个系统内置的约束键值,并且这种非空判断,通过索引查询实现 --的,索引默认创建一个系统索引

create unique index STUsno

on student(sno)

create unique index COUcno

on course(cno)

create unique index SCno

on sc(sno asc,cno desc)

drop index SCno on sc

--显示表的数据和索引的碎块信息 DBCC SHOWCONTIG

--插入数据 select * from student

alter table student alter column sno char(10)

insert into student values('10021','张三','男',20,'计科系')

insert into student values('10022','王朝','女',18,'软件')

insert into student values('10023','朱元璋','男',20,'管理')

insert into student values('10024','刘彻','男',18,'军事')

insert into student values('10025','刘表','男',20,'商学系')

insert into student values('10026','白居易','男',19,'文法')

insert into student values('10027','李清照','女',24,'文法')

select * from course insert into course  values('001','数据库','005',4)

insert into course  values('002','高等数学','',2)

insert into course  values('003','信息系统','001',4)

insert into course  values('004','操作系统','006',2)

insert into course  values('005','数据结构','007',3)

insert into course  values('006','数据处理','',2)

insert into course  values('007','C语言','006',5)

select * from sc insert into sc values('10021','002',100)

insert into sc values('10021','001',88)

insert into sc values('10021','006',100)

insert into sc values('10021','007',68)

insert into sc values('10022','002',100)

insert into sc values('10023','005',30)

insert into sc values('10024','002',100)

insert into sc values('10024','006',56)

select * from student --查询操作

--查询 select * from student select * from course select * from sc

--去掉重复行 select distinct sno from sc

--格式化查询

select sname as '姓名',2013-sage as '出生日期' from student

select sname,'出生日期',2013-sage   from student

select 姓名=sname,出生日期=2013-sage  from student

--条件查询

select * from course where ccredit>3

select * from course where ccredit between 2 and 5

select * from course where ccredit> 2 and ccredit<5

select * from course where ccredit in(2)

select * from course where ccredit  not in(2)

--匹配查询

select * from student  where sname like '刘__'

select * from student  where sname like '_表__'

select * from student  where sname like '%表%'

--算术元算查询

select grade*(1+0.2) as 总成绩,grade/(10) as 绩点 from sc

--分组函数查询

select COUNT(*) as 总人数 from student

select COUNT(distinct sno) as '选修的总人数' from sc

select AVG(grade) as '平均成绩' from sc where sno='10021'

select MAX(grade) as 'MAX成绩' from sc where sno='10021'

select MIN(grade) as 'MIN成绩' from sc where sno='10021'

select SUM(grade) as '总成绩' from sc where sno='10021'

select SUM(grade)/COUNT(grade) as '平均成绩' from sc where sno='10021'

select SUM(grade) as '总成绩' from sc group by sno  having sum(grade)>100 -

-连接查询、

--等值连接

select distinct student.*,sc.* from student,sc where student.sno=sc.sno

--自身连接

select distinct A.*,B.* from student A,sc B where A.sno=B.sno

select B.sname as '同一个系' from student A,student B where A.sname='白居易' and A.sdept=B.sdept

--外连接

select A.*,B.* from student  A left join sc B on A.sno=B.sno

select A.*,B.* from student  A right join sc B on A.sno=B.sno

select A.*,B.* from student  A FULL join sc B on A.sno=B.sno

--复合条件连接

select * from sc select * from course

select distinct  A.*,B.* from student A,sc B where A.sno=B.sno and B.grade>99 and B.cno='002'

select distinct  A.*,B.*,C.* from student A,sc B,course C where A.sno=B.sno and B.cno=C.cno and B.grade>99 and B.cno='002'

--字符串连接查询

select sname+sno from student

select distinct sname from student ,sc where student.sno=sc.sno

select  sname from student ,sc where student.sno=sc.sno and student.sno not in (select sno from sc where grade<60) group by sname

--子查询

select * from student where sage>(select AVG(sage) from student)

--是否存在的查询

select * from student where exists(select * from sc where sno=student.sno)

select * from student where not exists(select * from sc where sno=student.sno)

--sql创建用户 sys.sp_addlogin bnc,bnc,Studets sp_adduser bnc,bnc

--权限分配和收回

grant select on student to bnc

select * from student

revoke select on student from bnc

--视图的创建

create view VIEW_STUGrade(学号,姓名,课程,成绩)

as

select student.sno,student.sname,course.cname,sc.grade from student,course,sc

where student.sno=sc.sno and course.cno=sc.cno and student.sdept='软件'

--查看视图

select * from VIEW_STUGrade

--视图修改

alter view VIEW_STUGrade(学号,姓名,课程,成绩)

as

select student.sno,student.sname,course.cname,sc.grade from student,course,sc

where student.sno=sc.sno and course.cno=sc.cno and student.sdept='软件'

with check option

--更新失败后不影响视图查看

--视图更新

update VIEW_STUGrade set 姓名='王超' where 学号='10022' select * from student where  sno='10022'

/* 1,可更新视图:   a,单个基本表导出的 2,不可更新视图   a 两个以上基本表导出的   b 视图字段来自表达式或者函数   c 嵌套查询的表   d 分组子句使用distinct */

--删除视图 drop view VIEW_STUGrade

--高级sql编程

--数据类型1,int 2,smallint 3,tinyint (0--255) 4,bigint 5char固定长度<800.如:学号,姓名 6,varchar可变长度小于800 7,text 2GB 8,nvarchar1--4000 */

--运算符和通配符

select  GETDATE()-1 昨天,GETDATE() 今天,GETDATE()+1 明天

select 59&12

select 59|12

select 59^12

--模糊查询

select * from student where sname like '%刘%'

select * from student where sno like '1002[5-9]'

--控制流程语句

declare @name char(10) set @name='司马相如'

print @name

--输出一个表达式,不能进行查询 select @name

--输出多个表达式

declare @a nvarchar(50),@b nvarchar(50)

set @a=33 set @b=34             ---简写select@a=33,@b=34

if @a>@b

print '最小值是:'+@a

else

print '最大值是:'+@b

--waitfor间隔一段时间执行

waitfor delay '00:00:04' print '推迟4秒执行'

waitfor time '17:45:50' print '等待这一时刻执行'

--创建函数

CREATE FUNCTION GetTime (    @date1 datetime,   @date2 datetime )

RETURNS TABLE

AS RETURN (

select datediff(dd,@date1,@date2) 日差,datediff(mm,@date1,@date2) 月差,  datediff(yy,@date1,@date2) 年差

)

--创建存储过程,

--查看

GO create proc [dbo].[sel] (

@sno char(10)

)

as

select * from student where sno=@sno

exec sel @sno='10021'

--查看

GO create proc sel2

as

select * from student

exec sel2

--修改

GO create proc updat @sno char(10), @sex char(2)

as

update student set sex=@sex where sno=@sno

select * from student  exec updat @sno='10021', @sex='女'

--删除

GO create proc dele @sno char(10)

as

delete student where sno=@sno

select * from student

exec dele @sno='10029'

--插入

GO create proc inser @sno char(10), @sname char(20), @sex char(2), @sage smallint, @sdept char(15)

as

insert into student values(@sno,@sname,@sex,@sage,@sdept)

exec inser @sno='10029', @sname='tom', @sex='男', @sage=100, @sdept='sc' select * from student

--查询操作

--查询

select * from student select * from course select * from sc

--去掉重复行 select distinct sno from sc

--格式化查询

select sname as '姓名',2013-sage as '出生日期' from student

select sname,'出生日期',2013-sage   from student

select 姓名=sname,出生日期=2013-sage  from student

--条件查询

select * from course where ccredit>3

select * from course where ccredit between 2 and 5

select * from course where ccredit> 2 and ccredit<5

select * from course where ccredit in(2)

select * from course where ccredit  not in(2)

--匹配查询

select * from student  where sname like '刘__'

select * from student  where sname like '_表__'

select * from student  where sname like '%表%'

--算术元算查询

select grade*(1+0.2) as 总成绩,grade/(10) as 绩点 from sc

--分组函数查询

select COUNT(*) as 总人数 from student

select COUNT(distinct sno) as '选修的总人数' from sc select AVG(grade) as '平均成绩' from sc where sno='10021'

select MAX(grade) as 'MAX成绩' from sc where sno='10021'

select MIN(grade) as 'MIN成绩' from sc where sno='10021'

select SUM(grade) as '总成绩' from sc where sno='10021'

select SUM(grade)/COUNT(grade) as '平均成绩' from sc where sno='10021'

select SUM(grade) as '总成绩' from sc group by sno  having sum(grade)>100

--连接查询、 --等值连接

select distinct student.*,sc.* from student,sc where student.sno=sc.sno

--自身连接

select distinct A.*,B.* from student A,sc B where A.sno=B.sno select B.sname as '同一个系' from student A,student B where A.sname='白居易' and A.sdept=B.sdept

--外连接

select A.*,B.* from student  A left join sc B on A.sno=B.sno select A.*,B.* from student  A right join sc B on A.sno=B.sno

select A.*,B.* from student  A FULL join sc B on A.sno=B.sno

-复合条件连接

select distinct  A.*,B.* from student A,sc B where A.sno=B.sno and B.grade>99 and B.cno='002'

select distinct  A.*,B.*,C.* from student A,sc B,course C where A.sno=B.sno and B.cno=C.cno and B.grade>99 and B.cno='002'

--字符串连接查询

select sname+sno from student

select distinct sname from student ,sc where student.sno=sc.sno

select  sname from student ,sc where student.sno=sc.sno and student.sno not in (select sno from sc where grade<60) group by sname

--子查询

select * from student where sage>(select AVG(sage) from student)

--是否存在的查询

select * from student where exists(select * from sc where sno=student.sno)

select * from student where not exists(select * from sc where sno=student.sno)

--sql创建用户

sys.sp_addlogin bnc,bnc,Studets sp_adduser bnc,bnc

--权限分配和收回

grant select on student to bnc

select * from student

revoke select on student from bnc

--视图的创建

create view VIEW_STUGrade(学号,姓名,课程,成绩)

as

select student.sno,student.sname,course.cname,sc.grade from student,course,sc

where student.sno=sc.sno and course.cno=sc.cno and student.sdept='软件'

--查看视图

select * from VIEW_STUGrade

--视图修改

alter view VIEW_STUGrade(学号,姓名,课程,成绩) as select student.sno,student.sname,course.cname,sc.grade from student,course,sc

where student.sno=sc.sno and course.cno=sc.cno and student.sdept='软件'

with check option

--更新失败后不影响视图查看 --视图更新

update VIEW_STUGrade set 姓名='王超' where 学号='10022' select * from student where  sno='10022'

/* 1,可更新视图:   a,单个基本表导出的 2,不可更新视图   a 两个以上基本表导出的   b 视图字段来自表达式或者函数   c 嵌套查询的表   d 分组子句使用distinct */

--删除视图 drop view VIEW_STUGrade

--触发器

use Studets

GO create trigger insert_Tri

ON student  after

insert as print '有新数据插入!'

GO create trigger update_Tri

on student after

update as print '有数据更新!'

GO create trigger delete_Tri

on student after

delete as print '有数据删除!'

--修改触发器

GO alter trigger delete_Tri

on student after delete

as

if '王帅' in (select sname from deleted)

print '该信息不许删除!'

rollback transaction

--执行存储过程查看触发器使用情况

exec sel @sno='10021'

exec inser @sno='10029', @sname='王帅', @sex='男', @sage=25, @sdept='国贸'

exec updat @sno='10029', @sex='女'

exec dele @sno='10029'

--查看,修改,删除触发器

/*   sp_*+触发器名称

sp_helptext:触发器正文信息   sp_help:查看一般信息,触发器名称,属性,创建时间,类型   sp_depends:引用或指定表的所有触发器   sp_helptrigger:指定信息 */  sp_help delete_Tri

sp_helptext delete_Tri

sp_depends delete_Tri

sp_helptrigger student

--删除触发器

drop trigger delete_Tri

(0)

相关推荐

  • 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等

    用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP COLUMN column_NAME 3.修改字段类型 ALTER TABLE table_name ALTER COLUMN column_name new_data_type 4.sp_rename 改名 更改当前数据库中用户创建对象(如表.列或用户定义数据类型)的名称. 语法 sp_rename

  • SQLSERVER 2005中使用sql语句对xml文件和其数据的进行操作(很全面)

    --用SQL多条可以将多条数据组成一棵XML树L一次插入 --将XML树作为varchar参数传入用 --insert xx select xxx from openxml() 的语法插入数据 -----------------------------------导入,导出xml-------------------------- --1导入实例 --单个表 create table Xmltable(Name nvarchar(20),Nowtime nvarchar(20)) declare

  • 六条比较有用的MySQL数据库操作的SQL语句小结

    在MySQL数据库操作中,我们常常编写一些SQL语句来实现自己想要的功能.但是对于初学MySQL数据库的人来说这似乎又有一定的难度.本文我们总结了六条比较有用的SQL语句,初学者可以套用下面的格式,接下来就让我们一起来了解一下这部分内容. 1.计算年数 如果您想通过生日来计算这个人的年龄,可以用以下的语句来实现: SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(@dateofbirth)), '%Y') + 0; 2.两个时间的差 取得

  • sql语句之数据操作介绍

    一.insert语句 insert用于向表中输入数据,其具体的语法结构如下. INSERT INTO 表名称 VALUES (值1, 值2,....) 我们也可以指定所要插入数据的列:   INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) 示例: 复制代码 代码如下: insert into country values('美国') 二.update语句 update语句用于更新表中的数据,其具体的语法结构如下. UPDATE 表名

  • 浅析SQL数据操作语句

    SQL 中的运算符  1算术运算符: +:加运算,求两个数或表达式想加的和 -:减运算,求两个数或表达式相减的差 *,乘运算,求两个数或表达式相乘的积 /:除运算,求两个数或表达式相除的商 %:取模运算,求两个数或表达式相除的余数 2.赋值运算 =:把一个数或表达式赋值给另一个标量. 3.比较运算符 =:等于   >大于   < 小于    <>不等于   >= 大于等于   <=小于等于    !=不等于 4.逻辑运算符 AND :当且仅当两个布尔表达式为true时,

  • Sql Server 和 Access 操作数据库结构Sql语句小结

    下面是Sql Server 和 Access 操作数据库结构的常用Sql,希望对你有所帮助.内容由海娃整理,不正确与不完整之处还请提出,谢谢. 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default '默认值' null ,[字段2] ntext null ,[字段3] datetime,[字段4] money null ,[字段5] int default 0,[字段6]

  • SQL Server 数据库基本操作语句总结

    复制代码 代码如下: --sql基本操作 --创建数据库 create database Studets --创建表 create table student ( sno char(5), sname char(20), ssex char(2), sage smallint, sdept char(15) ) create table course ( cno char(3), cname char(30), cpno char(3), ccredit smallint ) create ta

  • T-sql语句修改SQL Server数据库逻辑名、数据库名、物理名的方法

    本文实例讲述了T-sql语句修改SQL Server数据库逻辑名.数据库名.物理名的方法.分享给大家供大家参考,具体如下: 更改MSSQL数据库物理文件名Sql语句的写法 注意:要在活动监视器里面确保没有进程连接你要改名的数据库!!!!!!!!!!!!!!!!!!!! Sql语句如下 USE master --改逻辑名 ALTER DATABASE YQBlog MODIFY FILE(NAME='YQBlogAA',NEWNAME='YQBlog') -- GO ALTER DATABASE

  • SQL SERVER 数据库备份的三种策略及语句

    1.全量数据备份 备份整个数据库,恢复时恢复所有.优点是简单,缺点是数据量太大,非常耗时 全数据库备份因为容易实施,被许多系统优先采用.在一天或一周中预定的时间进行全数据库备份使你不用动什么脑筋.使用这种类型的备份带来的问题是非常缺乏灵活性,而且当数据库被冲掉后,你面临丢失大量数据的潜在威胁.例如,假设你每天在午夜备份数据库. 如果服务器在晚上11点崩溃了,你将丢失前面23个小时对数据所做的全部修改.对大多数系统来说,这是无法接受的.对此规则,为数不多的例外如下: 1.系统中所存的数据可以很容易

  • 详解SQL Server数据库链接查询的方式

    SQL Server数据库链接查询的方式的相关知识是本文我们主要要介绍的内容,我们知道,通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志.多表连接查询是使用Sql的基本操作,但连接的方式却有多种,熟练使用这些连接方式能够简化Sql语句,提高数据库运行效率. 在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在一个表中.当检索数据时,通过连接操作查询出存放在多个表中的不同实体的信息.连接操作给用户带来很

  • VS连接SQL server数据库及实现基本CRUD操作

    目录 连接数据库 使用dataGridView控件显示表中的数据. 实现基本CRUD操作 总结 连接数据库 打开vs,点击 视图,打开sql资源管理器,添加SQL Server 输入服务器名称,用户名,密码,进行连接. 如图,就可以看到vs已经连接到了自己的数据库,class和song两个数据库 .可以看到class下面有五个表. 查看其中一个SC表,数据显示正常,证明已连接. 使用dataGridView控件显示表中的数据. 在工具箱中找到dataGridView控件拖入Form1中,如图:

  • SQL Server数据库入门学习总结

    一图胜"十"言:SQL Server 数据库总结 一个大概的总结 经过一段时间的学习,也对数据库有了一些认识. 数据库基本是由表,关系,操作组成:对于初学者首先要学的: 1.数据库是如何存储数据的 表,约束,触发器 2.数据库是如何操作数据的 insert,update,delete T-sql 函数 存储过程 触发器 3.数据库是如何显示数据的 select SQLServer数据库学习总结 1.SQL基础 SQL Server2000安装.配置,服务器启动.停止,企业管理器.查询分

  • JavaScript使用ActiveXObject访问Access和SQL Server数据库

    JS操作 Access 数据库 复制代码 代码如下: <SCRIPT LANGUAGE="JavaScript">  <!--  var filePath = location.href.substring(0, location.href.indexOf("实例197.连接Access数据库.html"));    //以当前页面文件为基础,找到文件所在的绝对路径  var path = filePath + "197.mdb"

  • Python3操作SQL Server数据库(实例讲解)

    1.前言 前面学完了SQL Server的基本语法,接下来学习如何在程序中使用sql,毕竟不能在程序中使用的话,实用性就不那么大了. 2.最基本的SQL查询语句 python是使用pymssql这个模块来操作SQL Server数据库的,所有需要先安装pymssql. 这个直接在命令行里输入pip install pymssql安装就行了 然后还要配置好自己本地的SQL Server数据库,进入Microsoft SQL Server Management Studio中可以进行设置.如果你选择

  • ASP语言实现对SQL SERVER数据库的操作

    目前管理信息系统已从传统的客户机/服务器(C/S)模式转向了浏览器/服务器(B/S)模式,特别是微软公司推出它的新产品ASP语言之后,这种转变更加迅猛.管理信息系统的核心是对数据库进行包括添加.修改和查询等等操作,ASP提供的ADO数据库接口控件,使得程序员再也勿需编写复杂的CGI程序了,而只要用几句简单的语句即可实现以上操作.1.系统环境 PII 350,Ram 64M,WINNT Server 4.0, Service Pack4, IIS 4.0, SQL Server7.0. 2.系统功

  • SQL Server数据库中批量导入数据的四种方法总结

    在软件项目实施的时候,数据导入一直是项目人员比较头疼的问题.其实,在SQL Server中集成了很多成批导入数据的方法.有些项目实施顾问头疼的问题,在我们数据库管理员眼中,是小菜一碟.现在的重点就是,如何让用户了解这些方法,让数据导入变得轻松一些. 第一:使用Select Into语句 若企业数据库都采用的是SQL Server数据库的话,则可以利用Select Into语句来实现数据的导入.Select Into语句,他的作用就是把数据从另外一个数据库中查询出来,然后加入到某个用户指定的表中.

随机推荐