mysql sql归类

SQL Server collation refers to a set of character and character encoding rules, and influences how information is stored according to the order in the data page, how data is matched by comparing two columns, and how information is arranged in the T-SQL query statement. Collate SQL follows rules applied on a table when Select, Insert, Update or Delete operations are performed against contained data. Data always follows collation constraint rules, which are configured when creating an object.

SQL Server排序规则是指一组字符和字符编码规则,它会影响如何按照数据页中的顺序存储信息,如何通过比较两列来匹配数据以及如何在T-SQL查询语句中安排信息。 对包含的数据执行“选择”,“插入”,“更新”或“删除”操作时,Collat​​e SQL遵循应用于表的规则。 数据始终遵循归类约束规则,该规则在创建对象时进行配置。

When retrieving data using a T-SQL query, collation plays a fundamental role in the execution. It matters which collation is associated with a column when ordering clause is applied to that column. For example, information in the column can be case sensitive, and lowercase letters will not be treated the same as uppercase letters. This distinction is important for data sorting in the query result set.

使用T-SQL查询检索数据时,排序规则在执行中起着基本作用。 当将ordering子句应用于该列时,与该列关联的排序规则很重要。 例如,列中的信息可能区分大小写,并且小写字母将不会与大写字母一样。 这种区别对于查询结果集中的数据排序很重要。

SQL Server installation configuration sets the default collation for the created instance (Latin1_General_CI_AI). New databases will be configured with the default collation of the SQL Server. Users can change the collation settings at the database level but not at the SQL Server level. With the assistance of the SQL Server Installation setup, collation can be changed, however, the effect of that on the current database and its tables can be unpredictable. This is not recommended since it can influence existing query execution and query result sets.

SQL Server安装配置为创建的实例(Latin1_General_CI_AI)设置默认排序规则。 新数据库将使用SQL Server的默认排序规则进行配置。 用户可以在数据库级别更改排序规则设置,但不能在SQL Server级别更改排序规则设置。 借助于SQL Server安装设置,可以更改排序规则,但是,排序规则对当前数据库及其表的影响是不可预测的。 不建议这样做,因为它会影响现有的查询执行和查询结果集。

A list of collations supported by the installed version of SQL Server can be generated using sys.fn_helpcollations() (a system DMV). SQL Server allows collations to be configured at different levels of the database engine, but, by default, every level will inherit the collation settings from the parent level. In descending order, these are:

可以使用sys.fn_helpcollat​​ions()(系统DMV)生成SQL Server已安装版本支持的排序规则列表。 SQL Server允许在数据库引擎的不同级别上配置排序规则,但是默认情况下,每个级别都将从父级别继承排序规则设置。 按降序排列,它们是:

  • SQL Server instance SQL Server实例
  • Database 数据库
  • Column 柱
  • Query 询问

SQL Server实例级别排序规则 (SQL Server instance level collation)

A SQL Server instance collation is the default collation for the system databases. It includes master, tempdb and model. If a user’s database collation is configured differently than one of the SQL Server instance collation, then a comparison between tables in that database and in tempdb, when it comes to textual data comparison, will return an error. Tempdb will always be configured with the instance level default collation. If one of the user databases is restored with other than tempdb’s collation and the temp table is used in any T-SQL statement or procedure inside a user database that compares text-based columns using WHERE clause or JOIN conditions, the query will return an error because both of the tables do not have the same collation.

SQL Server实例排序规则是系统数据库的默认排序规则。 它包括master,tempdb和model。 如果用户的数据库排序规则的配置与SQL Server实例排序规则之一的配置不同,则涉及文本数据比较时,该数据库与tempdb中的表之间的比较将返回错误。 Tempdb将始终使用实例级别的默认排序规则进行配置。 如果使用除tempdb的排序规则以外的其他方式还原了一个用户数据库,并且在使用WHERE子句或JOIN条件比较基于文本的列的用户数据库中的任何T-SQL语句或过程中使用了temp表,则查询将返回错误因为两个表没有相同的排序规则。

To find the SQL Server instance level collation use, the T-SQL shown below:

要查找SQL Server实例级别的排序规则用法,请如下所示的T-SQL:

SELECT SERVERPROPERTY('collation') AS ServerCollation;

To handle a collation violation, a developer can make a small change in the T-SQL by defining collation on a column. This will be explained in the “Query level collation” section.

为了处理违反排序规则的问题,开发人员可以通过在列上定义排序规则来对T-SQL进行一些小的更改。 这将在“查询级别归类”部分中说明。

数据库整理 (Database collation)

Default database collation is inherited from SQL Server instance collation and used for database restoration. Collation is copied in backup when the backup is created. To find the current database collation, use the T-SQL shown below:

默认数据库排序规则继承自SQL Server实例排序规则,并用于数据库还原。 创建备份后,将在备份中复制排序规则。 要查找当前的数据库排序规则,请使用如下所示的T-SQL:

SELECT name, collation_name DbCollation
FROM sys.databases

If the default collation of a database is changed by a user, then the collation of existing, user-defined table-columns won’t be changed. However, new tables will be characterized by the changed database collation. But, if tables with various collations exist in the database, there can be errors while creating a join between them. To apply new collation to existing tables of a database, the user can set new collation on TEXT columns of tables. This activity will place a lock on the tables because the column will be altered with the new collation. Therefore, the existing column index will be dropped and re-created automatically.

如果用户更改了数据库的默认排序规则,则不会更改现有的用户定义表列的排序规则。 但是,新表将以更改的数据库排序规则为特征。 但是,如果数据库中存在具有各种排序规则的表,则在它们之间创建联接时可能会出错。 要将新的排序规则应用于数据库的现有表,用户可以在表的TEXT列上设置新的排序规则。 此活动将在表上放置一个锁,因为该列将使用新的排序规则进行更改。 因此,现有的列索引将被删除并自动重新创建。

列级排序规则 (Column level collation)

The default collation for SQL Server user-defined column is inherited from the database configured collation. If the database collation is changed, then the existing columns will retain the original collation but new columns will be created with current database collation.

SQL Server用户定义列的默认排序规则是从数据库配置的排序规则继承的。 如果更改了数据库排序规则,则现有列将保留原始排序规则,但将使用当前数据库排序规则创建新列。

This T-SQL can help find the collation for a particular column:

此T-SQL可以帮助查找特定列的排序规则:

SELECT OBJECT_NAME(OBJECT_ID), name ColumnName, collation_name AS ColumnCollation
FROM sys.columns
WHERE collation_name IS NOT NULL
AND OBJECT_NAME(OBJECT_ID) = '?'
AND name = '?'

The image below shows collation type as a column property when navigated through Object explorer and displayed from the Property option of the context menu:

下图通过对象资源管理器浏览并从上下文菜单的“ 属性”选项中显示时,归类类型作为列属性显示:

查询级别整理 (Query level collation)

SQL Server doesn’t support automatic collation casting when executing a query with a comparison of two columns with different collations. As mentioned above, collation is consistent with parent levels. In these situations with various UNICODE characters, COLLATE casting should be utilized.

当执行具有不同归类的两列的比较的查询时,SQL Server不支持自动归类转换。 如上所述,排序规则与父级一致。 在这些情况下,使用各种UNICODE字符,应使用COLLATE强制转换。

Query level collation is useful when a user wants to sidestep the collation of the column in a specific T-SQL query statement. It is possible to collate SQL by adding a collation name with the COLLATE keyword in the expression. The COLLATE is a column collation casting keyword which is operation generally known as collate SQL. COLLATE can also be used with the column name and input character string.

当用户想回避特定T-SQL查询语句中列的排序规则时,查询级别排序规则很有用。 通过在表达式中添加带有COLLATE关键字的排序规则名称,可以对SQL进行排序。 COLLATE是列归类强制转换关键字,通常称为操作归类SQL。 COLLATE也可以与列名和输入字符串一起使用。

WHERE [Column X] = [Column Y] COLLATE Latin1_General_CI_AS
or
ON [Column X] = [Column X] COLLATE Latin1_General_CS_AS

A common use of the query level collation (collate SQL) is to compare case-sensitive strings. For example, imagine two tables with similar collation and compare their columns using join or subquery. Data with the lower-case string exists in the chosen columns of those tables. The requirement is to compare both columns in case-sensitive terms, but both columns don’t have the same collation. Collation at the query level (collate SQL) will allow overriding the database or column-level collation when running a query.

查询级别排序规则(collat​​e SQL)的常见用法是比较区分大小写的字符串。 例如,假设两个表具有相似的排序规则,然后使用join或subquery比较它们的列。 这些表的所选列中存在带有小写字符串的数据。 要求以区分大小写的方式比较两列,但两列没有相同的排序规则。 查询级别(排序SQL)的排序规则将允许在运行查询时覆盖数据库或列级别的排序规则。

整理冲突 (Collation conflicts)

SQL Server supports multiple collations but cannot properly deal with collation mismatches. If a user tries to compare fields with different collations, SQL Server will return an error with collation names as shown below:

SQL Server支持多个排序规则,但不能正确处理排序规则不匹配。 如果用户尝试比较具有不同归类的字段,SQL Server将返回一个错误,其中包含归类名称,如下所示:

Msg 468, Level 16, State 9, Line 9 Cannot resolve the collation conflict between “Latin1_General_CI_AI” and “SQL_Latin1_General_CP1_CI_AS” in the like operation.

消息468,级别16,状态9,行9无法通过类似操作解决“ Latin1_General_CI_AI”和“ SQL_Latin1_General_CP1_CI_AS”之间的排序规则冲突。

To avoid conflicts, users can add a default collation setting in the T-SQL query statement:

为了避免冲突,用户可以在T-SQL查询语句中添加默认排序规则设置:

COLLATE DATABASE_DEFAULT
WHERE Column1 COLLATE DATABASE_DEFAULT = Column2

This may prompt an issue when programming for SQL Server. Users can resolve this issue by utilizing a SQL Server column compare clause with COLLATE as described above. In a situation where SQL Server is recently installed and an inappropriate collation setting is identified, it can cause an issue with the utilization of tempdb. As previously mentioned, tempdb is made with default server-level collation when restarting the SQL Server service.

为SQL Server编程时,这可能会提示问题。 用户可以通过如上所述使用带有COLLATESQL Server列compare子句来解决此问题。 在最近安装SQL Server并标识了不正确的排序规则设置的情况下,它可能导致tempdb的使用出现问题。 如前所述,tempdb是在重新启动SQL Server服务时使用默认服务器级别的排序规则创建的。

在T-SQL JOIN中使用整理SQL (Use collate SQL in T-SQL JOIN)

COLLATE SQL is a very effective column casting function in the SQL Server but it can cause query performance issues. COLLATE SQL can help a developer with a quick fix for a collation error on a Remote Query execution. Imagine a table with a column collation of Latin1_General_CS_AS and another table with a column that has a different collation. When joining the two tables based on columns with the differing collations:

COLLATE SQL是SQL Server中非常有效的列转换功能,但它可能导致查询性能问题。 COLLATE SQL可以帮助开发人员快速解决远程查询执行中的排序规则错误。 想象一下一个表的列排序规则为Latin1_General_CS_AS,而另一个表的列排序规则为不同。 在基于具有不同归类的列的两个表联接时:

SELECT *
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.Col1 COLLATE Latin1_General_CS_AS = TABLE2.Col1 COLLATE Latin1_General_CS_AS

Here, we have utilized COLLATE with both the columns in a JOIN articulation to make a common collation to avoid an error. In summary, the COLLATE keyword with its collation value as argument is used next to a column name when ad hoc collation resolution is required.

在这里,我们利用JOATE关节中的两个列使用COLLATE进行通用排序规则以避免错误。 总之,当需要临时排序规则解析时,将在其列名旁边使用以排序规则值为参数的COLLATE关键字。

SQL Server collation is a major aspect of the database engine. A mistake in collation configuration during SQL Server installation can cause huge issues during query execution. Although there is a workaround to resolve issues with collate SQL casting, it is always recommended that for a new SQL Server set up, server migration or database migration, collation should be at the top of the activity checklist. The choice of collation should be carefully thought through and consider future integration and implementation needs. It’s especially important in the event that you’ll need to join tables with other databases.

SQL Server排序规则是数据库引擎的主要方面。 SQL Server安装过程中排序规则配置中的错误可能会在查询执行过程中引起巨大的问题。 尽管有解决方法来解决归类SQL强制转换问题,但始终建议对于新SQL Server设置(服务器迁移或数据库迁移),应将归类放在活动清单的顶部。 应仔细考虑排序规则的选择,并考虑将来的集成和实施需求。 在需要将表与其他数据库连接的情况下,这一点尤其重要。

翻译自: https://www.sqlshack.com/sql-server-collation-introduction-with-collate-sql-casting/

mysql sql归类

mysql sql归类_带有归类SQL强制转换SQL Server归类介绍相关推荐

  1. 数据库逻辑删除的sql语句_通过数据库的眼睛查询sql的逻辑流程

    数据库逻辑删除的sql语句 Structured Query Language (SQL) is famously known as the romance language of data. Eve ...

  2. java执行sql文件_面试官问你MyBatis SQL是如何执行的?把这篇文章甩给他

    初识 MyBatis MyBatis 是第一个支持自定义 SQL.存储过程和高级映射的类持久框架.MyBatis 消除了大部分 JDBC 的样板代码.手动设置参数以及检索结果.MyBatis 能够支持 ...

  3. 两个sql交集_如何使用性能分析工具定位SQL执行慢的原因?

    在前面的文章中就讲过了查询优化器,知道在查询优化器中会经历逻辑查询优化和物理查询优化.需要注意的是,查询优化器只能在已经确定的情况下(SQL 语句.索引设计.缓冲池大小.查询优化器参数等)决定最优的查 ...

  4. mysql sqlite 语法_[Sqlite] Sqlite的基本日常SQL操作语句汇总

    序言: 嵌入式数据库Sqlite的基本sql使用汇总,使用测试起来,与关系型数据库mysql在语法上有很多的相似之处,先准备测试数据: CREATE TABLE COMPANY(ID INT NOT ...

  5. mysql 索引未命中_联合索引命中率问题导致SQL查询效率慢的问题

    执行 MySQL DumpSlow 结果是:Count: 1358  Time=0.33s (448s)  Lock=0.00s (0s)  Rows=2.5 (3343) Count:出现次数 Ti ...

  6. mysql在线模拟器_力荐一款在线SQL模拟器

    我在旧文里给大家推荐了几款在线SQL模拟器,不知道大家都安排上没?最近又发现了一款模拟器,试用了一段时间感觉还不错,今天就把它介绍给大家. 今天要给大家介绍的主角叫做 SQL Online.在正式介绍 ...

  7. mysql excel 数据处理_将excel的数据进行sql处理

    使用mysql进行处理. 后面如果知道excel的哪个函数适合处理,这里会进行补充.暂时使用自己数据的sql进行处理. 一:概论 1.需求 查找员工连续三个月内的工资是0的员工 二:将数据添加到mys ...

  8. mysql 集合 思想_面向集合的思维编写SQL(第九篇 --- “我说的是:苹果柿子李子栗子梨的那个橘子啊”)...

    max:屏幕前的同学,这个时候还在天善智能看博文,肯定是单身狗吧... 猿同学:520是什么鬼,我的世界只有1024... 大家好,距离上篇(面向集合的思想编写SQL的第八篇)已经过去N多个月了... ...

  9. 软件测试mysql常用语句_测试中常用到的SQL语句(Oracle)

    在测试过程中,可能会进入到数据库中检查数据. 下面列举一些经常用到的SQL语句. (1)删除记录(delete与truncate) 语法:delete from  表名 where 条件 trunca ...

最新文章

  1. SparCC的微生物网络构建示例
  2. Direct2D (25) : 将画笔线条所占用的区域转换为路径 - ID2D1Geometry.Widen()
  3. Linux安装vsftpd组件
  4. 推荐系统XDeepFM模型--DeepFM和DCN升级版
  5. 【4.0】jdbcTemplate
  6. SpringBoot-异常处理
  7. [Vue-cli3] is a Vue CLI 3 only command and you are using Vue CLI 2.9.6. You may...
  8. 在线Excel转CSV工具
  9. mysql配置参数优化提示
  10. 动态截屏软件jpg格式
  11. java版spring cloud+spring boot+redis社交电子商务平台-docker-feign配置(五)
  12. java代码混淆工具
  13. 移动硬盘插入提示需要格式化RAW_分区变成RAW格式怎么办?
  14. PO系列之 PO接口在ECC中的开发
  15. 2022 年的前端行业,咋样啦?
  16. CGAL例程:地理信息系统----点云数据生成DSM、DTM、等高线和数据分类
  17. python 三维度显示,Python实现高斯函数的三维显示方法
  18. 前端开发 SSR 是什么技术?
  19. MATLAB快速入门(五):符号微积分
  20. ubuntu AndroiStudio 中 外设(手机、平板) 无法使用

热门文章

  1. 配置 MySQL 服务器容器
  2. myEclipse怎样将程序部署到tomcat(附录MyEclipse调试快捷键)
  3. Ubuntu的多文件编译以及c语言的数组、函数
  4. Motan:目录结构
  5. Kendo UI grid 表格数据更新
  6. gcc malloc/free的质疑
  7. SQL Server 2005导入导出存储过程
  8. 零基础带你学习MySQL—创建数据库(一)
  9. JavaScript学习(三十六)—移动的小球
  10. 5G是什么?5G能做什么?5G在未来将带来什么?