ylb: 数据库操作方法基础

简介:
ylbtech-SQL Server:SQL Server-数据库操作方法基础

 数据库操作方法基础

ylb: 数据库操作方法基础返回顶部
复制代码
----------试图操作(view)---------------------
--创建视图
create view titles_view
as
select title,type from titles
--调用视图
select * from titles_view
--删除视图
drop view titles_view
--修改视图
alter view titles_view
as
select title,type,price from titles
go
--
------对表(Table)的操作------------------ create table teacher ( number int primary key, name varchar(20) not null, sex char(2) check(sex='' or sex=''), birthday datetime, job_title varchar(20), salary money, memo ntext, nicheng varchar(20) unique, height numeric(7,2) ) select * from teacher drop table student create table Student ( number int primary key, name varchar(20) not null, sex char(2) check(sex='' or sex=''), teachernumber int foreign key references teacher(number) ) --在 Student 表 添加一个新列 alter table Student add birthday datetime,salary money --在 Student 表 删除一个已有的列 alter table Student drop column salary --在 Sutdent 表 修改一个列的约束 alter table Student alter column name varchar(20) insert Student(number,name,sex,teachernumber) values(0003,'小小黑2','',1) insert Student(number,name,sex,teachernumber) values(0004,'小小黑4','',1) --外键必须产生于主键 --在删除的时候,如果这表上的列在其他表有外键的话 --(如果插入的数据产生关联)必须先删外键数据之后,才可以删除这表的数据 ------ ------查询技术 use pubs go --查询书名表的所有列 select * from titles --查询书名表的书名编号、书名名称、单价、类型 select * from titles select title_id,title,price,type from titles --as 用法 取别名 select title_id as '书名编号',title as '书名名称',price as '单价',type as'类型' from titles --oder by 排序 asc,desc --查询书名表的所有列 按价格排序(从大到小) asc select title,price from titles order by price select title,price from titles order by price asc --查询书名表的所有列 按价格排序(从小到大)desc select title,price from titles order by price desc ---where 条件 --查看书名编号为:BU1111的记录信息 select * from titles select * from titles where title_id='BU1111' --查看书的类型是"business"的所有信息 select * from titles where type='business' -- in 包含 -- not in 不包含 -- or 或者 -- and 且 --查看书的类型是"business,mod_cook"的所有信息 select title,type from titles where type='business' ortype='mod_cook' select title,type from titles where typein('business','mod_cook') --查看书的类型不是"business,mod_cook"的所有信息 select title,type from titles where type!='business' andtype!='mod_cook' select title,[type] from titles where type notin('busines','mod_cook') --一些函数应用min,max,sum,avg,count,count(*) select * from titles --不算price 等于null ----min 最小值 select min(price) from titles select price from titles where type='business' select min(price) from titles where type='business' -----max 最大值 select max(price) from titles ----- sum 总和 select sum(price) from titles -----avg 平均值 select avg(price) from titles -----count(*),count(列明) select count(*) as '总计' from titles select count(title_id) '总计' from titles -- like 像 select * from titles --查一下 title_id 中有'BU'的所有行数 -----'%' 代表所有字符 select * from titles where title_id like '%BU%' -----‘_’ 代表一个字符 select * from titles where title_id like '__1%' --group by 分组 select type,count(*) '记录总数',min(price) '最小价格',max(price)'最大价格',sum(price) '总价格'
,avg(price) '平均价格' from titles group bytype --比较运算符=,>,<,>=,<=,!= ----!= 不等于 select title,price from titles select title,price from titles where price>10 --any 任何一个,all 都 select title,price from titles where price >any(select price from titles wheretype='business') select price from titles where type='business' select min(price) from titles where type='business' select title,price from titles where price >all(select price from titles wheretype='business') select max(price) from titles --exists 存在 use master go -------对数据库(Database)的操作--------------- if exists(select * from sys.databases where name='db2') begin drop database db2 end go create database db2 go use db2 2011/2/17 ylb pm17:20
复制代码
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:  SQL Server
本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/p/3509338.html ,如需转载请自行联系原作者
相关文章
|
1月前
|
SQL 缓存 PHP
PHP技术探究:优化数据库查询效率的实用方法
本文将深入探讨PHP中优化数据库查询效率的实用方法,包括索引优化、SQL语句优化以及缓存机制的应用。通过合理的优化策略和技巧,可以显著提升系统性能,提高用户体验,是PHP开发者不容忽视的重要议题。
|
1月前
|
存储 缓存 NoSQL
利用Redis List实现数据库分页快速查询的有效方法
利用Redis List实现数据库分页快速查询的有效方法
|
3月前
|
设计模式 NoSQL Java
常用的设计模式以及操作Redis、MySQL数据库、各种MQ、数据类型转换的方法
常用的设计模式以及操作Redis、MySQL数据库、各种MQ、数据类型转换的方法
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
101 1
|
4月前
|
Oracle 关系型数据库 MySQL
不同数据库注释方法的随记
oracle、mysql、postgresql、Microsoft的注释方法
53 2
|
6月前
|
弹性计算 Linux 网络安全
数据库出现了网络连通性类相关问题的抓包方法
如下列举了数据库遇到连通性问题时,在三种不同的操作系统上抓包的方式。
106 2
|
6月前
|
存储 SQL 关系型数据库
linux系统中使用QT来实现数据库的调用方法
linux系统中使用QT来实现数据库的调用方法
52 0
|
6月前
|
缓存 关系型数据库 MySQL
MySQL索引原理与实践:优化数据库性能的有效方法3.0
全文索引,主键索引,唯一索引,覆盖索引,组合索引,普通索引,外键索引,空间索引,前缀索引,哈希索引等 在接下来MySQL索引原理与实践3.0中我会重点介绍mysql索引优化等一些方面相关的理论与实践,有小伙伴是从3.0开始看的,可以优先看一下1.0/2.0 http://t.csdnimg.cn/hHn9A
114 0
|
4月前
|
SQL 监控 druid
p6spy【SpringBoot集成】使用p6spy-spring-boot-starter集成p6spy监控数据库(配置方法举例)
p6spy【SpringBoot集成】使用p6spy-spring-boot-starter集成p6spy监控数据库(配置方法举例)
201 0
|
1月前
|
Oracle Java 关系型数据库
java实现遍历树形菜单方法——数据库表的创建
java实现遍历树形菜单方法——数据库表的创建
11 0

热门文章

最新文章