search
HomeDatabaseMysql Tutorial SQL SERVER 2005/2008 中关于架构的理解(一)

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点有点难理解,我们来看一张图,通过这张图,大家应该能很显示的理解这一点:

(上图来自网络)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment