SELECT name FROM customer ORDER BY name COLLATE Latin1_General_CS_AI;
这一条查询语句表示按照Latin1_General_CS_AI的排序规则来进行排序。Expression-level collations的一个好处就是非常灵活。
create table student1
(
stuid int not null,
stuname nvarchar(20) not null,
);
create table student2
(
stuid int not null,
stuname nvarchar(20) COLLATE Latin1_General_CS_AI not null,
);
--求表连接Step2:
select s1.*,s2.* from student1 s1,student2 s2 where s1.stuname=s2.stuname
执行上述查询报错如下所示: Cannot resolve the collation conflict between "Latin1_General_CS_AI" and "Chinese_PRC_CI_AS" in the equal to operation.
然后在expression-level使用Collate Database_Default
select s1.*,s2.* from student1 s1,student2 s2 where s1.stuname=s2.stuname Collate Database_Default
上述查询执行成功。
需要注意的是collation只能用在字符串类型的列上面,如果在int列上使用collate会报错。
--实验1:测试nvarchar和varchar的存储长度
--创建一个默认collation为Chinese_PRC_CI_AS的数据库TESTDB3
USE TESTDB1
CREATE TABLE test
(
lastname NVARCHAR(8) NOT NULL,--nvarchar类型,双字节存储
title VARCHAR(8) NOT NULL, --varchar类型,单字节存储
);
insert into test values('姓名1','标题1');
select * from test;
insert into test values('123456789','1');--String or binary data would be truncated.
insert into test values('12345678','1');
insert into test values('1','12345678');
insert into test values('一二三四五六七八','一二三四');
select * from test;
--总结:
/*
1.nvarchar(n),按字符来存储,不论是英文字符还是中文字符。最多能够存储n个中文或者是英文,但是所占用的存储空间是2n+2个字节。1