SQL Server 数据库操作.docx

上传人:b****6 文档编号:7922531 上传时间:2023-01-27 格式:DOCX 页数:27 大小:28.26KB
下载 相关 举报
SQL Server 数据库操作.docx_第1页
第1页 / 共27页
SQL Server 数据库操作.docx_第2页
第2页 / 共27页
SQL Server 数据库操作.docx_第3页
第3页 / 共27页
SQL Server 数据库操作.docx_第4页
第4页 / 共27页
SQL Server 数据库操作.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

SQL Server 数据库操作.docx

《SQL Server 数据库操作.docx》由会员分享,可在线阅读,更多相关《SQL Server 数据库操作.docx(27页珍藏版)》请在冰豆网上搜索。

SQL Server 数据库操作.docx

SQLServer数据库操作

SQLServer数据库操作

--SQL Server 数据库操作 

--================================================ 

go 

--使用T-SQL语句创建数据库 

CREATE  DATABASE  new_db  --数据库名称 

 ON PRIMARY 

( 

    NAME= 'new.mdf', 

    FILENAME= 'D:

\My Documents\SQL_Server\new.mdf', 

    SIZE=3mb, 

    MAXSIZE=30mb, 

    FILEGROWTH=10% 

) 

go 

--修改数据库容量 

ALTER DATABASE new_db 

MODIFY  FILE 

( 

    NAME='new.mdf',  --逻辑名称 

    SIZE=5MB 

) 

go 

--缩减数据库容量 

DBCC SHRINKDATABASE(new_db,1)  

go 

--使用数据库 

use new_db 

go 

--切换使用的数据库 

use master 

go 

--更改数据库名称 

EXEC sp_renamedb new_db , new_db123 

EXEC SP_RENAMEDB new_db123 , new_db 

go 

--删除数据库 

DROP DATABASE new_db 

go 

--分离数据库 

 

--附加数据库 

 

----------------------------------------------------- 

--创建数据库 

create database st_db 

on primary 

( 

    name='st.mdf', 

    filename='D:

\My Documents\SQL_Server\st_db.mdf', 

    size=5mb, 

    maxsize=50mb, 

    filegrowth=10% 

) 

 

go 

--使用数据库 

use st_db 

 

go 

--创建表st_table 

create table st_table 

        ( 

            学号 int primary key identity , 

            名称 char(6) not null, 

            专业方向 varchar(10) not null, 

            系部代码 char

(2) not null, 

            备注 varchar(50) 

        ) 

 

go 

--重命名表:

 

exec sp_rename 'st_table','st_table123' 

exec sp_rename 'st_table123','st_table' 

 

go 

--重命名列:

 

exec sp_rename 'st_table.学号','学号123','column' 

exec sp_rename 'st_table.学号123','学号','column' 

go 

 

--添加新列:

 

alter table st_table add 成绩 smallint 

go 

--更改列的数据类型:

 

alter table st_table  alter column  成绩  char(4)  

go 

--删除列:

 

 alter table st_table drop column 成绩 

go 

--使用系统存储过程sp_help查看表信息 

EXEC  sp_help st_table 

go 

--删除表:

 

drop table st_table 

 

go 

--创建表sc_table 

create table sc_table 

( 

    学号 int primary key identity 

) 

go 

alter table sc_table 

    add 姓名 char(6) not null 

go 

--删除表sc_table 

drop table sc_table     

/* 

alter table sc_table 

    add 专业 varchar(10) not null 

alter table sc_table 

    add 系部代码 char

(2) not null 

*/ 

--创建另一个表 

create table t1 

( 

    学号 int not null, 

    us varchar(30) 

) 

 

--删除表的所有数据:

 

Truncate table t1 

go 

--创建主键约束 

alter table t1 

    add constraint pk 

    primary key clustered (学号) 

 

go 

--创建t2表 

create table t2 

( 

    学号 int not null, 

    us varchar(30) 

) 

go     

--创建外建约束 

alter table t2 

    add constraint wz 

    foreign key (学号) 

    references t1(学号) 

go 

--删除t2表 

drop table t2 

go 

--删除t1表 

drop table t1 

--============================================================= 

 

go 

--创建表 

create table st_table2 

        ( 

            学号 int not null identity , 

            名称 char(10) not null, 

            专业方向 varchar(10) not null, 

            系部代码 char

(2) not null, 

            备注 varchar(50) 

        ) 

go 

alter table st_table2 

    add 高考分数 int 

go 

 

 

 

--数据的添加 

insert st_table2 values ('李四','计算机系','01',null,null) 

insert st_table2 values ('张三','中文系','02','',null) 

insert st_table2 values ('张龙','计算机系','01','HELLO !

','492') 

insert st_table2 values ('王五','外语系','03','外语系学习各国的语言',null) 

insert st_table2 values ('赵六','中文系','02',null,'532') 

insert st_table2 values ('赵七八','计算机系','01','学习C语言等有关课程','581') 

go 

--省略values 的insert 语句 

insert st_table2(名称,专业方向,系部代码,备注) 

    select 名称,专业方向,系部代码,备注 from st_table2 

insert st_table2(名称,专业方向,系部代码,备注) 

    select 名称,专业方向,系部代码,备注 from st_table2 

insert st_table2(名称,专业方向,系部代码,备注) 

    select 名称,专业方向,系部代码,备注 from st_table2 

insert st_table2(名称,专业方向,系部代码,备注) 

    select 名称,专业方向,系部代码,备注 from st_table2 

go 

--数据的修改     

update st_table2 set 系部代码='01'  

update st_table2 set 系部代码='02'where 专业方向='中文系' 

update st_table2 set 系部代码='03'where 专业方向='外语系' 

 

 

 

 

go 

--输出表中的所有列 

select * from st_table2 

select 学号,名称,专业方向,系部代码,备注 from st_table2 

go 

--输出表中部分列 

select 学号,名称 from st_table2 

select 系部代码,名称 from st_table2 

go 

--选择表中的若干记录 

select distinct 系部代码 from st_table2 

select distinct 系部代码,专业方向 from st_table2 

select distinct 系部代码,名称 from st_table2 

go 

--限制返回行数 

select top 10 * from st_table2 

select top 5 系部代码,名称 from st_table2 

select top 5 学号,名称 from st_table2 

go 

 

--常用的查询条件 

select * from st_table2 where 学号>=65 

select * from st_table2 where 学号<>65 and 学号<>66 and 学号<>67 and 学号<>68 

select * from st_table2 where 学号 between 60 and 80 or 系部代码=01 

select * from st_table2 where 学号 in(2,23,24,3,4,5,6,7,8) 

select * from st_table2 where 学号 not in(1,2,23,24,3,4,5,6,7,8) 

select * from st_table2 where 学号 not between 60 and 80 

go 

select distinct * from st_table2  

    where (专业方向 like '外语系'  or 名称 like '张三') and 备注 is not null 

go 

select * from st_table2  

    where 专业方向 not like '外语系' and 专业方向 not like'计算机系' 

go 

select * from st_table2 where 名称 like '张_' 

select * from st_table2 where 名称 like '赵__' 

select * from st_table2 where 名称 like '赵%' 

select * from st_table2 where 备注 like '学习%' or 备注 like '外语系%' 

go 

--涉及空值的查询 

select * from st_table2 where  备注 is null 

select * from st_table2 where  not 备注 is null 

select * from st_table2 where  备注 is not null 

go 

--多重条件查询 

select * from st_table2 where not 名称 like '张_' 

select * from st_table2 where not 备注 like '学习%' or 备注 like '外语系%' 

go 

--用查询结果生成新表 

select * into new_st_table2 from st_table2 where  not 备注 is null 

go 

--删除该表 

drop table new_st_table2 

go 

select * into new2_st_table2 from st_table2 

    where 专业方向 not like '外语系' and 专业方向 not like'计算机系' 

go 

--删除该表 

drop table new2_st_table2 

go 

--新表前加"#"是创建临时表 

select 学号,名称,专业方向,高考分数  

    into #new3_st_table2  

    from st_table2  

    where  not 备注 is null 

select * from #new3_st_table2 

go 

--删除new3_st_table2表 

drop table #new3_st_table2 

/* 

完整的select 语句的基本语法格式 

 

虽然select 语句的完整语法较复杂,但是其主要的语法格式可归纳如下:

 

 

SELECT select_list 

[into new_table_name] 

 FROM table_list 

[WHERE search_conditions] 

[GROUP BY group_by_expression] 

[HAVING search_condition] 

[ORDER BY  order_expression [asc|desc] ] 

 

SELECT select_list 描述结果集的列,它是一个逗号分隔的表达式, 

在选择列表中使用 “*”表达式指定返回源表中所有的列 

 

[INTO new_table_name] 用于指定使用结果集来创建一个新表,new_table_name是新表的名称 

 

FROM table_list 结果集数据来源于哪些表或视图。

 

 

[WHERE search_conditions]查询条件 

 

GROUP BY group_by_expression    根据列中的值将结果分组。

 

 

HAVING search_conditions 结果集附加筛选,通常与GROUP BY 一起使用。

 

 

ORDER BY  order_expression [ASC|DESC] 对结果进行分组ASC 和DESC 关键字用于指定行是 

                                      按升序还是降序排序。

 

*/ 

 

 

 

--对结果进行分组 

select 专业方向,名称,高考分数 from st_table2 

    group by 专业方向,名称,高考分数 

go     

--HAVING 筛选条件表达式对结果集分组  

select 专业方向,名称,高考分数 from st_table2 

    group by 专业方向,名称,高考分数 

    having 专业方向 not like '动漫' 

go 

select 专业方向,名称,高考分数 from st_table2 

    group by 专业方向,名称,高考分数 

    having 专业方向<>'动漫' 

go     

--对查询的结果排序 

select * from st_table2 order by 高考分数 asc  --升序排列 

select * from st_table2 order by 高考分数 desc  --升序排列 

go 

--对数据进行统计 

select top 3 * from st_table2 order by 高考分数 desc  --升序排列 

select COUNT(*) from st_table2 

select COUNT(学号) as 高考总人数 from st_table2 

select sum(高考分数) as 高考总分数 from st_table2 

select AVG(高考分数) as 高考平均分 from st_table2 

select max(高考分数) as 高考最高分 from st_table2 

select min(高考分数) as 高考最低分 from st_table2 

go 

 

--使用COMPUTE  

---对查询结果集中的所有记录进行汇总统计 

select *from st_table2 

    order by 高考分数 desc 

    compute avg(高考分数) 

go 

select 专业方向,COUNT(*) 系部总人数,AVG(高考分数) 系部平均成绩 

    from st_table2 

    group by 专业方向 

    compute count(count(*))  

    compute sum(count(*))  

    compute avg(avg(高考分数))  

    /*这个与 select AVG(高考分数) as 高考平均分 from st_table2 

    算出来的结果为什么不同  ?

 ?

 ?

 */ 

go     

select 专业方向,名称,高考分数 from st_table2 where 专业方向='外语系' 

select 专业方向,名称,高考分数 from st_table2 where 专业方向 like '外语系' 

go 

select  distinct 专业方向,高考分数  

    from st_table2  

    where 专业方向='外语系'     

    compute count(专业方向) 

 

go 

--清空数据 

truncate table st_table2 

go 

--插入数据 

insert st_table2 values('张学友','网络','01','没有','411') 

insert st_table2 values('刘德华','计算机','02','没有','412') 

insert st_table2 values('舒淇','计算机','01','没有','413') 

insert st_table2 values('梁咏琪','动漫','02','没有','431') 

insert st_table2 values('杨千嬅','计算机','01','没有','465') 

insert st_table2 values('李宇春','动漫','02','没有','485') 

insert st_table2 values('蔡依林','网络','01','没有','468') 

insert st_table2 values('郑源','计算机','02','没有','510') 

insert st_table2 values('陈楚生','动漫','01','没有','550') 

insert st_table2 values('张韶涵','计算机','02','没有','421') 

insert st_table2 values('猛非','动漫','01','没有','423') 

insert st_table2 values('郑秀文','网络','02','没有','411') 

insert st_table2 values('林俊杰','计算机','01','没有','511') 

insert st_table2 values('羽泉','计算机','01','没有','500') 

insert st_table2 values('郭富城','网络','02','没有','400') 

insert st_table2 values('黄品源','动漫','02','没有','589') 

insert st_table2 values('梁朝伟','计算机','02','没有','530') 

insert st_table2 values('李克勤','网络','01','没有','520') 

insert st_table2 values('陈小春','国际金融','02','没有','512') 

insert st_table2 values('刘若英','证券期货','02','没有','421') 

insert st_table2 values('刘嘉玲','房地产金融','01','没有','428') 

insert st_table2 values('谭咏麟','房地产金融','02','没有','498') 

insert st_table2 values('张学友','证券期货','01','没有','454') 

insert st_table2 values('张卫健','证券期货','02','没有','515') 

insert st_table2 values('周传雄','房地产金融','01','没有','532') 

insert st_table2 values('周星驰','国际金融','02','没有','423') 

insert st_table2 values('游鸿明','房地产金融','02','没有','477') 

insert st_table2 values('言承旭','国际金融','02','没有','488') 

insert st_table2 values('许志安','国际金融','01','没有','582') 

insert st_table2 values('叶倩文','房地产金融','01','没有','495') 

insert st_table2 values('叶世荣','房地产金融','02','没有','499') 

insert st_table2 values('张雨生','证券期货','02','没有','531') 

insert st_table2 values('周润发','国际金融','01','没有','531') 

insert st_table2 values('张信哲','证券期货','01','没有','424') 

insert st_table2 values('周渝民','证券期货','02','没有','412') 

insert st_table2 values('太极乐队','证券期货','02','没有','423') 

go 

select * from st_table2 

go 

--数据的删除 

delete st_table2 where 系部代码='03' 

delete st_table2  --删除表中的所有数据 

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 职业技术培训

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1