SQLSERVER2005/2008 中关于架构的理解(一) 在一次的实际工作中碰到以下情况,在SQLSERVER2008中,新建了一个新用户去访问几张由其他用户创建的表,但是无法进行查询,提示对象名'CustomEntry'无效。。当带上了架构名称之后(如cus.CustomEntry),却又能查询到
SQL SERVER 2005/2008 中关于架构的理解(一)
在一次的实际工作中碰到以下情况,在 SQL SERVER 2008中,新建了一个新用户去访问几张由其他用户创建的表,但是无法进行查询,提示“对象名'CustomEntry' 无效。”。当带上了架构名称之后(如“cus.CustomEntry”),却又能查询到表中的数据了,但是查询语句是已经写死在了应用程序中的,如果要进行更改,就有很大的工作量, 这是一件很郁闷的事情。于是想从数据库层面上解决此问题,在查询了大量的资料之后,对于SQL SERVER中的架构有所了解,并解决以上问题。
下面来说说,自己对SQL SERVER 中架构的理解,并在此记录,以备查。
在SQL SERVER 2000中不存在上面所说的问题,那为什么在2008中会出现这样的事情,这样的设置可以带来哪些好处?导致这一问题的原因主要在于SQL SERVER 2005/2008中多了一个新的概念——架构。
首先,我们来看一下微软对架构的官方定义:架构(Schema)是形成单个命名空间的数据库实体的集合。命名空间是一个集合,其中每个元素的名称都是唯一的。在这里,我们可以将架构看成一个存放数据库中对象的一个容器。
架构实际上在SQL SERVER 2000中就已经存在,在SQL SERVER 2000中数据库用户和架构是隐式连接在一起的, 每个数据库用户都是与该用户同名的架构的所有者。当我们使用查询分析器去查询一个表的时候,一个完整的表的名称应该包括服务器名.数据库名.用户名.对象名,而在SQL SERVER 2005/2008中一个表的完全限定名称应该为服务器名.数据库名.架构名.对象名
在SQL SERVER 2000中的完全限定名称中的“用户名”也是数据库中的用户,也是“架构名”。假如有一个账户df在test数据库中创建了一张表tb1的时候,在查询分析器中应该输入的查询语句为select * from test.df.tb1,也就是说,在SQL SERVER 2000中一张表所属的架构默认就是表的创建者的登录名称,用户可以修改他所创建的所有数据库对象。但在2008中已经将用户和其创建对象所属关联取消了,而加入了一个全新的架构体系。
用户架构分离的好处
那么将架构与数据库用户分离对管理员和开发人员而言有什么好处呢?
1. 架构管理与用户管理分开。多个用户可以通过角色(role)或组(Windows groups)成员关系拥有同一个架构。在SQL SERVER 2005/2008 中,每个数据库中的固定数据库角色都有一个属于自己的架构,如果我们创建一个表,给它指定的架构名称为 db_ddladmin,那么任何一个属于db_ddladmin中的用户都是可以去查询、修改和删除属于这个架构中的表,但是不属于这个组的用户是没有对这个架构中的表进行操作的权限,有一点必须注意,db_dbdatareader组的成员可以查看所有数据库中的表,db_dbdatawriter组成员可以修改所有数据库中 的表,db_owner组成员可以对数据库所有表进行所有操作,这几个组的成员可以通过角色获取到在数据库中的特殊权限。
2. 在创建数据库用户时,可以指定该用户账号所属的默认架构。 ( 建议大家指定)
3. 删除数据库用户变得极为简单。在 SQL Server 2000 中,用户(User)和架构是隐含关联的,即每个用户拥有与其同名的架构。因此要删除一个用户,必须先删除或修改这个用户所拥有的所有数据库对象,就比如 一个员工要离职要删除他的账户的时候,还得将他所创建的表和视图等都删除,影响过大。SQL SERVER 2005/2008将架构和对象者分离后就不在存在这样的问题,删除用户的时候不需要重命名该用户架构所包含的对象,在删除创建架构所含对象的用户后,不再需要修改和测试显式引用这些对象的应用程序。
4. 共享缺省架构使得开发人员可以为特定的应用程序创建特定的架构来存放对象,这比仅使用管理员架构(DBO schema)要好。
5. 在架构和架构所包含的对象上设置权限(permissions)比以前的版本拥有更高的可管理性。
6. 区分不同业务处理需要的对象,例如,我们可以把公共的表设置成pub的架构,把销售相关的设置为sales,这样管理和访问起来更容易。
大多数用户在创建对象的时候习惯直接输入对象名而将对象的架构名称省略,在2005/2008 中,,会给用户创建的这样的表加上一个缺省的架构,用户如果没有对自己的默 认架构做设置,那缺省架构就是dbo,也就是说,如果一个db_ddladmin的成员在数据库中创建一个没有加上架构名称的表,这个表在数据库中的完整 名称应该是dbo.表名,创建者在数据库中如果不是属于其它特殊组的成员,是不能对自己创建的表进行任何修改和查询的,那就相当于把自己赚的钱存进了别人的银行卡,自己却取不出来。
7 若不指定默认架构,则为DBO,为了向前兼容,早期版本中的对象迁移到新版本中,早期版本中没有架构的概念的。所以就该对象的架构名就是dbo.在SQL Server 2008中,DBO就是一个架构
8 当查找对象时,先找与用户默认架构相同的架构下的对象,找不到再找DBO的对象
第8点有点难理解,我们来看一张图,通过这张图,大家应该能很显示的理解这一点:
(上图来自网络)

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor
