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 ,如需转载请自行联系原作者
相关文章
|
3月前
|
人工智能 运维 关系型数据库
数据库运维:mysql 数据库迁移方法-mysqldump
本文介绍了MySQL数据库迁移的方法与技巧,重点探讨了数据量大小对迁移方式的影响。对于10GB以下的小型数据库,推荐使用mysqldump进行逻辑导出和source导入;10GB以上可考虑mydumper与myloader工具;100GB以上则建议物理迁移。文中还提供了统计数据库及表空间大小的SQL语句,并讲解了如何使用mysqldump导出存储过程、函数和数据结构。通过结合实际应用场景选择合适的工具与方法,可实现高效的数据迁移。
628 1
|
20天前
|
存储 关系型数据库 MySQL
MySQL数据库中进行日期比较的多种方法介绍。
以上方法提供了灵活多样地处理和对比MySQL数据库中存储地不同格式地日子信息方式。根据实际需求选择适当方式能够有效执行所需操作并保证性能优化。
144 10
|
6月前
|
数据库
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
【YashanDB知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
|
2月前
|
SQL Oracle 关系型数据库
比较MySQL和Oracle数据库系统,特别是在进行分页查询的方法上的不同
两者的性能差异将取决于数据量大小、索引优化、查询设计以及具体版本的数据库服务器。考虑硬件资源、数据库设计和具体需求对于实现优化的分页查询至关重要。开发者和数据库管理员需要根据自身使用的具体数据库系统版本和环境,选择最合适的分页机制,并进行必要的性能调优来满足应用需求。
93 11
|
4月前
|
存储 算法 Java
实现不同数据库的表间的 JOIN 运算的极简方法
跨库计算是数据分析中的常见难题,尤其涉及多数据库系统时,表间 JOIN 操作复杂度显著提升。esProc 提供了一种高效解决方案,能够简化跨库 JOIN 的实现。例如,在车辆管理、交管和公民信息系统中,通过 esProc 可轻松完成如下任务:按城市统计有车公民事件数量、找出近一年获表彰的车主信息,以及按年份和品牌统计车辆违章次数。esProc 支持不同关联场景(如维表关联与主子表关联)的优化算法,如内存索引、游标处理和有序归并,从而大幅提升编码和运算效率。无论是同构还是异构数据源,esProc 均能灵活应对,为复杂数据分析提供强大支持。
|
5月前
|
Oracle 安全 关系型数据库
【Oracle】使用Navicat Premium连接Oracle数据库两种方法
以上就是两种使用Navicat Premium连接Oracle数据库的方法介绍,希望对你有所帮助!
1071 28
|
5月前
|
SQL 关系型数据库 MySQL
大数据新视界--大数据大厂之MySQL数据库课程设计:MySQL 数据库 SQL 语句调优方法详解(2-1)
本文深入介绍 MySQL 数据库 SQL 语句调优方法。涵盖分析查询执行计划,如使用 EXPLAIN 命令及理解关键指标;优化查询语句结构,包括避免子查询、减少函数使用、合理用索引列及避免 “OR”。还介绍了索引类型知识,如 B 树索引、哈希索引等。结合与 MySQL 数据库课程设计相关文章,强调 SQL 语句调优重要性。为提升数据库性能提供实用方法,适合数据库管理员和开发人员。
|
10月前
|
存储 监控 安全
数据库多实例的部署与配置方法
【10月更文挑战第23天】数据库多实例的部署和配置需要综合考虑多个因素,包括硬件资源、软件设置、性能优化、安全保障等。通过合理的部署和配置,可以充分发挥多实例的优势,提高数据库系统的运行效率和可靠性。在实际操作中,要不断总结经验,根据实际情况进行调整和优化,以适应不断变化的业务需求。
|
6月前
|
SQL 数据库连接 Linux
数据库编程:在PHP环境下使用SQL Server的方法。
看看你吧,就像一个调皮的小丑鱼在一片广阔的数据库海洋中游弋,一路上吞下大小数据如同海中的珍珠。不管有多少难关,只要记住这个流程,剩下的就只是探索未知的乐趣,沉浸在这个充满挑战的数据库海洋中。
135 16
|
8月前
|
数据采集 数据库 Python
有哪些方法可以验证用户输入数据的格式是否符合数据库的要求?
有哪些方法可以验证用户输入数据的格式是否符合数据库的要求?
360 75

热门文章

最新文章