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: 数据库操作方法基础的更多相关文章

  1. iOS中数据库应用基础

    iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...

  2. .NET基础拾遗(6)ADO.NET与数据库开发基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...

  3. 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用

    在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...

  4. C#-数据库访问技术 ado.net——创建 数据库连接类 与 数据库操作方法 以及简单的数据的添加、删除、修改、查看

    数据库访问技术 ado.net 将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层的数据库访问技术 1.创建数据库,并设置主外键 ...

  5. 常用的PHP数据库操作方法(MYSQL版)

    常用的PHP数据库操作方法(MYSQL版) 作者: 字体:[增加 减小] 类型:转载 时间:2011-06-08   最近一直在折腾自己的网站首页,写的大部分PHP脚本都要用到和MYSQL数据库相关的 ...

  6. 黄聪:Discuz!X/数据库操作方法、DB::table、C::t

    函数 功能 DB::table($tablename) 获取正确带前缀的表名,转换数据库句柄, DB::delete($tablename, 条件,条数限制) 删除表中的数据 DB::insert($ ...

  7. [转载]NoSQL数据库的基础知识

    关系型数据库和NoSQL数据库 什么是NoSQL 大家有没有听说过“NoSQL”呢?近年,这个词极受关注.看到“NoSQL”这个词,大家可能会误以为是“No!SQL”的缩写,并深感愤怒:“SQL怎么会 ...

  8. 《C#语言和数据库技术基础》单词必备

    <C#语言和数据库技术基础> 第一章1..NET Framework   框架2.sharp            尖锐,强烈的3.application      应用程序4.devel ...

  9. MySQL(一) -- MySQL学习路线、数据库的基础、关系型数据库、关键字说明、SQL、MySQL数据库、MySQL服务器对象、SQL的基本操作、库操作、表操作、数据操作、中文数据问题、 校对集问题、web乱码问题

    1 MySQL学习路线 基础阶段:MySQL数据库的基本操作(增删改查),以及一些高级操作(视图.触发器.函数.存储过程等). 优化阶段:如何提高数据库的效率,如索引,分表等. 部署阶段:如何搭建真实 ...

随机推荐

  1. VMware ESXI 5.5 注册码

    VMware ESXI 5.5 注册码 ESXI 注册码0A42V-8M182-3ZZ88-R21N6-32K5H ESXi Server许可证类型产品: Mware vSphere 5 Enterp ...

  2. 详解Django-auth-ldap 配置方法

    使用场景 公司内部使用Django作为后端服务框架的Web服务,当需要使用公司内部搭建的Ldap 或者 Windows 的AD服务器作为Web登录认证系统时,就需要这个Django-auth-ldap ...

  3. POJ 1149 PIGS | 最大流问题

    参考了这个PDF 第一道网络流啊!感动 #include<cstdio> #include<algorithm> #include<cstring> #includ ...

  4. mySql 查询当天、本周、最近7天、本月、最近30天的语句

    mySql 查询当天.本周.最近7天.本月.最近30天的语句 原创 2017年04月13日 16:40:38 标签: 962 编辑 删除 -- 当天 SELECT * FROM  表名 WHERE w ...

  5. 自定义Windows服务并实施安装

    1.新建项目DemoService,并添加windows服务,命名DemoService 2.添加代码 using System; using System.Collections.Generic; ...

  6. 俄罗斯方块(NOIP模拟赛)(水·模拟)

    真是一道神奇的题目233~ 原题传送门 迫不得已贴了个题解的链接.. 好吧,这道题就是分情况讨论,纯模拟,, 没有什么难的.. 脑洞要大,四面都要考虑,不能漏! #include<iostrea ...

  7. How to Use Instruments in Xcode

    http://blog.csdn.net/woaifen3344/article/details/40748075 This is a blog post by iOS Tutorial Team m ...

  8. 28.Implement strStr()---kmp

    题目链接:https://leetcode.com/problems/implement-strstr/description/ 题目大意:字符串匹配,从字符串中,找到给定字符串第一次出现的位置下标, ...

  9. python接口自动化4-绕过验证码登录(cookie)【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E ...

  10. ORA-01157:无法标识/锁定数据文件,ORA-01110:表空间丢失错误

    https://blog.csdn.net/u014432433/article/details/51051854