※与数据库有关的数据编码
查看系统中的环境变量:
SHOW VARIABLES;
查看跟编码有关的环境变量:
SHOW VARIABLES WHERE variable_name LIKE '%character%';
解决中文乱码,如下三个编码保持一致就可以:
character_set_client、character_set_connection、character_set_results
另外,如果我们的数据库中想存放中文信息,那么建立数据库时最好自己指定能够支持中文的编码如GBK,UTF8
设置某个编码:
set character_set_results = gbk; 或
set character_set_results = 'gbk';
※※表与表之间的关系※※
一、一对一
1)表结构设计(建物理表)
☆法1:分开两个独立的表
create table wife(
id int primary key,
name varchar(10),
sex char(1)
);
create table husband(
id int primary key,
name varchar(10),
sex char(1),
wid int unique,
constraint husband_fk foreign key(wid) references wife(id)
); //通过constraint约束体现一对一
☆法2:合并在一个独立表
create table person(
id int primary key,
name varchar(10),
sex char(1),
wife int,
husband int
);
insert into person values(1,'小花','0', 0,3);
insert into person values(2,'玉芬','0', 0,4);
insert into person values(3,'张三','1', 1,0);
insert into person values(4,'李四','1', 2,0);
insert into person values(5,'王五','0', 0,0);
insert into person values(6,'张五','2', 0,0);
语法:case when then end
select name, (case sex when '0' then '女'
when '1' then '男'
else '未知'
end) as 性别, id from person;
通过分别建立视图来体现一对一的关系
create view women as select * from person where sex='0';
create view men as select * from person where sex='1';
//查询夫妻信息
1)旧版本
select women.name as 妻子, men.name as 丈夫 from women,men
where women.husband = men.id
2)新版本(96):关联(效率更高)
select women.name as 妻子, men.name as 丈夫 from women
INNER JOIN men ON women.husband = men.id