SQL练习之不破坏应用程序现有查询的修改模式
当我还是一个菜鸟的时候,当然现在也是,当我的软件需求发生变化时,并且数据库设计同样要求发生变化,我通常会放弃原有的代码(或者对原有的代码进行大改),先在我知道了两个不破坏应用程序现有查询的修改模式,下面就来一个个介绍!
假设你的公司有一张记录在数据库中的设备登记表:
资产标识 描述 收到日期
50430 桌面PC 2016-5-6
50431 19寸监视器 2016-5-7
现在公司再融资之后,进行了扩张,在另一个地方又建立了新的办公室,这个时候如果简单的复制并修改原先的应用和数据库,并且在两个位置独立运行的实例是一种诱人的做法(说简单点就是再建一个用于维护新办公室信息的页面),然后这种做法虽然快速地解决了当前的问题,但是从长远的角度看会引起跟多的问题,这样我们有两个应用程序需要维护(并且这两个应用程序在功能上基本接近),所以这种方法不具有伸缩性,并且随着办公室的增加会使问题变得更加糟糕!
下面是分析列子所需的sql文件:
CREATE TABLE Equipment(
AssetTag varchar(30) not null,
Description varchar(255) null,
RequiredDate datetime null
)
INSERT INTO Equipment VALUES('','桌面PC',GETDATE())
INSERT INTO Equipment VALUES('','19寸监视器',GETDATE())
代码如下:
SELECT *
FROM Equipment
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAS8AAAA1CAIAAACfhvrGAAAK90lEQVR4nO1cz27bOBOfFymaJ/DJMFi/QQ49NLkmh08OfO0hSE69NKeN7AAukGfIsf5zyDPkYCCoE+cSYHe72+1mT1Wzie04DvY7SJT4Z0hJlmlJtgYDQ6TI4QzJn4eUxIH/TNL9/b1R+SYojzrHpXWwMY8ERqXncdTzqHNcWgcb80hm0fjz50+j8k1QHnWOS+tgYx6pQKNIedQ5Lq2DjXmklNB4Xgeotm6Tyj+vg0j184QyEZ1vW9VFtpA+hdq4PDNvW1V3IqxcJ89BZtHoOA6af16Her0+Jxz98ePlLWr4EJ25Fm9b1cSNISbw+aoCC6IwG5MpEKsubrK6kw33TLpkFo0/fvzAsm9b1fr5f+dzwtEwGhGdFzhT9bSsqRZu40K7VEeqP6DQP6xVJHh+no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F/VtX7OskkThUDWuiFWj2HoqIBqnvGxO9b5wPjX0DbziKSBwrHokjCTBS4SV8ixMXjaHl9XKWYGPBC2HjkThStzAuL1Bn1FNBGLEl+wp/KIuSk8uxseAF8v8BXSk+zWGIVuwAAAAASUVORK5CYII=" alt="" />
1、通过修改表的方式完成不破坏应用程序现有查询的修改模式 代码如下:
ALTER TABLE Equipment ADD Office varchar(30) null UPDATE Equipment
SET Office='Headquarter' SELECT *
FROM Equipment
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX8AAABACAIAAAB9feD8AAANqElEQVR4nO1czU4byxLuF4mSJ/DKsjp+gyyyCGzD4trI2ywQWWUTVhfbSI7EM7CJEgyReAYWllAMZoN0z19ODmfFzwEMxujcxUz/V/e0Pe757VIJzbS7q6u6qj9q2uNCn7988+zZs+fkGX3+8u1fl3R9fe1UvgvKo86zUhlszCOVwS/X19eD86PB+ZFHH4DyqPOsVAYb80hl8Ety6HN1deVUvgvKo86zUhlszCOVwS9XV1cefbSUR51npTLYmEcqg18ygD4HLYTqvbO48g9aSKbWQUyZgM5nvfoiZ0ifIm1MzsyzXj0IhMIt8hwE7xcW5WzLBG2tA3aRF0oOfS4vL8H2gxZqtVpzwg+NV1HeotYf0FmY8axXjz0ZYILYruuwIIqyMZ4CM42FTdYvsuOVSZcAvxy0GLac9eohANGAJxf5WZbLy8uE0Ofi4gJqPuvVWwf/HswJP47RB9B5gTvTTEnFULSNC11SE+kANxKgi0iKXxRrA7ck9V/KBV1cXDD0eXycju8nLng6ffrr/G9A/mgLN/fH96cd/LIzmozvJyfdl0Fmibun6u14tIXDzPNlZ3TaCW7w1gmTud9Eq31y22+yBL65NxElSAPtdB5tYXA6QTHAkPH9fpPXZLSF8WqT6s/fotX+PWcanZFNQWbEW50mJ3NRfpFsBBQIbZQXkx8YXLO/gXWr/Ugh+kUWvSl6XxWbZwb8MtrCXGDL68Zotc9WTwy5jK1SYCNDn8nj9G784IKn06c/f/6lyj/u4EY/vMCd07v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UnrwMBTc5Nuc+BtQAQSoSfQx9rG088pVVhTDzlVUTr6z6cNzBCHeP49U2hFdTqa4CxQcQOgO4QmXq5z5ggkZW2fp3p+qtIfcxpVgcqM1lo6+s6iurinISrqwaH3pM6KMEqBoftAVKuaXwSg19+BCkSpoxxZJ56AHRR53CfiKzjb6yKmFfWVWRk0Bl1WEbI9wdxitsGNQ2jNwJmvMFeqZIjDP5NXX0kaMKGZ+DIhMWpKQ5aiMvSnc7h42+sqpJjq+s6ryyar+JYtdUNaMPUART/7+IT4I034CkduqsK7gZiT78hXRrEGIJNJEYZHoi9pVVfWXVFCurfm0I/41r7ZM4lVV1ehiqTxrODjQVKlM/94n1vs986DNw8I27r6zqK6sWqrKqeRtkkBN41xnNiD6R/c1yErDR80I4ab9kobah08qqqXs0zgLFZPrPA2zUEd9zoMl3VFHqbTI2el4glxF9nFZWTd2jcRaoqFwGG/PIhfcLgD5OK6umbnCcBSoql8HGPHLh/QK+6+ywsmrqBsdZoKJyGWzMIxfeLwD6ePLkyVMCxCMR+vzlW+pw6Nmz5xKyRx/Pnj2nwx59PHv2nA579PHs2XM67NHHs2fP6bBHH8+ePafD/weXiD43xhOj/wAAAABJRU5ErkJggg==" alt="" />
这个时候,在表中添加一个新列Office,并且将现在设备表中的每一行数据都被赋值给了现在的办公室。
但是这一点都不符合大多数的实际情况,大所属情况可能是,我们将老办公室的一部分设备(用不到了的)搬到新办公室,然后老办公室被提升为总部(负责管理),这个时候只能手动去修改哪些已经被转移的设备到新的办公室!
现在我们可以插入新办公室的数据行了。然而这个时候问题又来了,原先依赖与这个表的所有查询都需要被重新检查。没有指定列的INSERT操作都会执行失败。因此,如果原来的INSERT语句为:
INSERT INTO Equipment VALUES('','19寸监视器',GETDATE())
DBMS会报错:列名或所提供值的数目与表定义不匹配。
但是,如果语句是:
INSERT INTO Equipment(AssetTag,Description,RequiredDate) VALUES('','桌面PC',GETDATE())
那么这句INSERT将会执行成功,NULL值将会被输入Office列。
可以看到,即使表以及被修改,还能是原先的查询正常的工作,但是他们会返回来自两个办公数的数据,即使我们只希望返回一个办公室的数据!
2、第二种模式通过代替表的视图来完成不破坏应用程序现有查询的修改模式
另一种方式是把现有的设备数据复制到新设计的设备表中,然后将新表中的每一行数据都归为老办公室的,如果设备有移动,那就做相应数据的更改,代码如下:
CREATE TABLE EquipmentMultiSite(
AssetTag varchar(30) not null,
Description varchar(255) null,
RequiredDate datetime null,
Office varchar(30) null
)
INSERT
INTO EquipmentMultiSite
SELECT Equipment.*,'Headquarter'
FROM Equipment SELECT *
FROM EquipmentMultiSite
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYUAAAA6CAIAAADQqtILAAANj0lEQVR4nO1czU4byxLuF4mSJ/DKsjp+gyyyCGzD4o6Rt1kgssomrC62kRyJZ2BzdWNDJJ6BBRKKwWyQ7vnLyeGsMBywwRhxFzPd0z/VPW3P9Py5SyU0M+6uruqq/qjpmSn0/Pz8n/9+e7ZJNzc3VuXboCLqPC8tg41FpGXwy83NzfHliczo2eERREXUeV5aBhuLSMvglyzx6Pr62qp8G1REneelZbCxiLQMfrm+vnZ4NAcVUed5aRlsLCItg19yiUeHTYTq3Yu48g+bSKTmYUyZgM4X3XqSI2RPkTamZ+ZFt+4HQukmeQGC10sY5eGS8a81D8ODolCWeDQajcDrh03UbDYXBCQawby8pDwC6MyNeNGtxx4MMIG/rmqQEEXZGE+BufrCJqsn2fLMZEuAXw6bIdpcdOsBJNGAJwfFmZbRaJQZHl1dXUGXL7r15uHz4YKAZBmPAJ0TXKt6Siuqom1MdEp1pILgSMguI0l+kaz13ZLW/y0bdHV1pcOjx8fZ5H5qg2ezp78u/wbkD3dw42Byf97Gr9vD6eR+etZ57eejuHMun06GOzjIV1+3h+dt/wTvnIUyDxpovU9O+40w7W/sT3kJQkcznYc7GByOUwwwZHJ/0GA1Ge5gvN6g+rOnaL1/z5hGRwyHICPinXaDkZmUXwQbAQUCG8XJZDv6x+Ff37r1fqQQ9STz3uS9L4stMgN+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xcFGFaiApf3WPC6b50dyrVIURcHUK/IawElMA31jWXgMGxnrXL1aV682OSFGeKSoVzud3J+3MWrsx6pXq7SKZrRbB7Jfw4SIL9PFdGcBKKP7NU0ZU3aitfmRBkdA2IrEI00bYxtPXL1aLsxcvdoc1Kt9OG1jhDun8epDwsNL1WegiAGC6Riu+5n5/hGYxJGJNv7CVj7V5Ee6NIyBuYVsdPVqXb1aXk7m9Wrjg5EOj6SQlSOGXoESdSHgMsMjNiipknqUMWQWjEA8kocwH0hvo6tXS9jVq5XkZFKvdtDCCHcG8YpD+vUhI9eGYp+C7lYSc3WezhyPxDhD2runyKQGSamQfJEVpTpdwEZXr1Ynx9WrzaBebb+BYleq1eMRUFpU/f+KTZQUT1sy289WlTGNxCP2QDjVCDGEnkhU0t1Hu3q1rl5trurVfvW4/9i11lmcerWq4TU1PTV7EIq6n5nvH8V6/2gxPDq28Lzf1at19WpLXq9WvzByyCm8n43mxKPI9no5KdjoOBFO2y/5rA9ptV5t5j5OcMrmZfoPBryoIrblsSInkkXJp+nY6DhBdnhkvV5t5j5OcMpKw8tgYxG59H4xwiOr9Wozn4IEp6w0vAw2FpFL7xfD97Mt1qvNfAoSnLLS8DLYWEQuvV+M8MiRI0eOUiANVAV4lDlkOnbs2PH/AX3n7MTcMwuqAAAAAElFTkSuQmCC" alt="" />
这里如果如果设备有移动,那就做相应数据的更改!修改之后的数据就是老办公室的设备数据!
现在就可以删除Equipment表,并用一个同名的Equipment视图来替换!
DROP TABLE Equipment
go
CREATE VIEW Equipment AS
SELECT AssetTag,Description,RequiredDate
FROM EquipmentMultiSite
WHERE Office='Headquarter'
go
这个时候,你会发现应用程序现有的查询,依然能够正常的工作,原先的办公室使用Equipment,不在乎他使用的是表还是视图,老办公室的经理现在应该能够继续利用该数据库的应用程序,但是他仅能操作(增删查该)老办公室的有关的设备。
但是对于新办公室的经理,你还需要做一些工作,你要确保Equipment关联每一个用户,这样的话,你就能为每一个用户以硬编码的方式指定正确的办公室!
SQL练习之不破坏应用程序现有查询的修改模式的更多相关文章
- SQL Server2005中使用XML-数据类型、查询与修改
SQL 2005引进了XML数据类型,可以直接将XML当作字符串直接存入该列. 这样可以不需要对它进行XML解析. USE AdventureWorks -- 创建一个送货排程表 CREATE TAB ...
- [强烈推荐]ORACLE PL/SQL编程详解之七:程序包的创建与应用(聪明在于学习,天才在于积累!)
原文:[强烈推荐]ORACLE PL/SQL编程详解之七:程序包的创建与应用(聪明在于学习,天才在于积累!) [强烈推荐]ORACLE PL/SQL编程详解之七: 程序包的创建与应用(聪明在于学习,天 ...
- SQL点滴15—在SQL Server 2008中调用C#程序
原文:SQL点滴15-在SQL Server 2008中调用C#程序 T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可 ...
- 什么是Pro*C/C++,嵌入式SQL,第一个pro*c程序,pro*c++,Makefile,Proc增删改查
1 什么是Pro*C/C++ 1.通过在过程编程语言C/C++中嵌入SQL语句而开发出的应用程序 2.什么是嵌入式SQL 1.在通用编程语言中使用的SQL称为嵌入式SQL 2.在SQL标准中定义 ...
- SQL使用数据库引擎存储过程,系统视图查询,DBA,BI开发人员必备基础知识
在开发过程中会遇到需要弄清楚这个数据库什么时候建的,这个数据库中有多少表,这个存储过程长的什么样子等等信息,今天把自己工作过程中经常用到的一些数据库引擎存储过程,系统视图等等总结一下以备不时之用.下面 ...
- 警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的性能陷阱
警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的性能陷阱 以下文章来源:https://blog.csdn.net/defonds/article/details/4 ...
- sql处理百万级以上的数据提高查询速度的方法
原文:http://blog.csdn.net/zhengyiluan/article/details/51671599 处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中 ...
- 《转》sql处理百万级以上的数据提高查询速度的方法
处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考 ...
- [SQL] SQL 基础知识梳理(五) - 复杂查询
SQL 基础知识梳理(五) - 复杂查询 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5939796.html 序 这是<SQL 基础知识梳理( ...
随机推荐
- setTimeout()与setInterval() 问题
提示:setTimeout() 只执行 code 一次.如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout(). 1. setInterval(c ...
- 普林斯顿大学算法课 Algorithm Part I Week 3 排序算法复杂度 Sorting Complexity
计算复杂度(Computational complexity):用于研究解决特定问题X的算法效率的框架 计算模型(Model of computation):可允许的操作(Allowable oper ...
- hdu 5590 ZYB's Biology
Problem Description After getting scores ) begins to work with biological questions.Now he give you ...
- [置顶] java web 动态服务器
写了一个java web 动态服务器,主要通过内部类来实现,动态类使用了外部类,采用了 classforname 实例化,动态类的构造方法不能带参数, 效果都出来了,分享给有需要的 朋友.判断做的不够 ...
- org.hibernate.QueryException: could not resolve property: address of:
Hibernate: select count(*) as y0_ from test.course this_ org.hibernate.QueryException: could not res ...
- Qt QtableView使用
ui->setupUi(this); ui->mainToolBar->hide(); tableView = new QTableView(this); // 设置表头 QStan ...
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 管理 sendmail 的邮件队列
邮件队列是存储 sendmail 命令传送的邮件消息数据和控制文件的目录.缺省情况下,邮件队列是 /var/spool/mqueue. 邮件消息可能由于很多原因而排入队列. 例如: sendmail ...
- The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, SettingFragment, String)
The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the ...
- Cortex-M3 动态加载一(地址无关代码实现)
这篇文章是自己疑惑究竟地址无关性是如何实现,然后查看汇编和CPU指令手册,最后分析解除自己疑惑的,高手不要鄙视,哈哈. 编译C代码时候需要制定--acps/ropi选项,如下例子: void Syst ...