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
![]() |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
ylb: 数据库操作方法基础的更多相关文章
- iOS中数据库应用基础
iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...
- .NET基础拾遗(6)ADO.NET与数据库开发基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...
- 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用
在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...
- C#-数据库访问技术 ado.net——创建 数据库连接类 与 数据库操作方法 以及简单的数据的添加、删除、修改、查看
数据库访问技术 ado.net 将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层的数据库访问技术 1.创建数据库,并设置主外键 ...
- 常用的PHP数据库操作方法(MYSQL版)
常用的PHP数据库操作方法(MYSQL版) 作者: 字体:[增加 减小] 类型:转载 时间:2011-06-08 最近一直在折腾自己的网站首页,写的大部分PHP脚本都要用到和MYSQL数据库相关的 ...
- 黄聪:Discuz!X/数据库操作方法、DB::table、C::t
函数 功能 DB::table($tablename) 获取正确带前缀的表名,转换数据库句柄, DB::delete($tablename, 条件,条数限制) 删除表中的数据 DB::insert($ ...
- [转载]NoSQL数据库的基础知识
关系型数据库和NoSQL数据库 什么是NoSQL 大家有没有听说过“NoSQL”呢?近年,这个词极受关注.看到“NoSQL”这个词,大家可能会误以为是“No!SQL”的缩写,并深感愤怒:“SQL怎么会 ...
- 《C#语言和数据库技术基础》单词必备
<C#语言和数据库技术基础> 第一章1..NET Framework 框架2.sharp 尖锐,强烈的3.application 应用程序4.devel ...
- MySQL(一) -- MySQL学习路线、数据库的基础、关系型数据库、关键字说明、SQL、MySQL数据库、MySQL服务器对象、SQL的基本操作、库操作、表操作、数据操作、中文数据问题、 校对集问题、web乱码问题
1 MySQL学习路线 基础阶段:MySQL数据库的基本操作(增删改查),以及一些高级操作(视图.触发器.函数.存储过程等). 优化阶段:如何提高数据库的效率,如索引,分表等. 部署阶段:如何搭建真实 ...
随机推荐
- CentOS7 设置开机直接进入命令行界面
上网查询centsos设置开机直接进入命令行界面的方法都说修改/etc/inittab文件,将文件中的“ :id:5:initdefault:”改为“ :id:3:initdefault:”,即将默认 ...
- PHPEXCEL 设置被导出的表格样式代码
1.设置表格高度.宽度 代码: $objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);//行高 ...
- Bsd内核选项总结
Bsd内核选项总结 一: 下面这个选项在每个内核中都要有: machine i386 它指明了机器的硬件体系结构.它必须是i386, pc98, sparc64, alpha, ia64, amd64 ...
- Lights inside 3D Grid LightOJ - 1284 (概率dp + 推导)
Lights inside 3D Grid LightOJ - 1284 题意: 在一个三维的空间,每个点都有一盏灯,开始全是关的, 现在每次随机选两个点,把两个点之间的全部点,开关都按一遍:问k次过 ...
- HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题
首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...
- Android自复制传播APP原理学习(翻译)
Android自复制传播APP原理学习(翻译) 1 背景介绍 论文链接:http://arxiv.org/abs/1511.00444 项目地址:https://github.com/Tribler ...
- Welcome to Workrave
Welcome to Workrave Workrave is a free program that assists in the recovery and prevention of Repeti ...
- dom内容区域的滚动overflow,scroll
去掉手机上点击点中的默认高亮效果 -webkit-tap-highlight-color: rgba(0,0,0,0); ios手动启动一下监听touch事件以响应css伪类: document.ad ...
- Html5学习进阶三 Input 类型
HTML5 新的 Input 类型 HTML5 拥有多个新的表单输入类型.这些新特性提供了更好的输入控制和验证. 本章全面介绍这些新的输入类型: email url number range Date ...
- Kotlin来了
kotlin优点(主要是代码简洁度) 1.避免空指针异常,其次在一个文件中java方法必须写在class里面,而kotlin则没要求 2.在同一个文件中,有class a则不允许在class外的方法( ...
