一、连接查询
1)进入自己创建的zz数据库
2)创建学生表:
create table students ( id int unsigned not null auto_increment primary key, name varchar(20) default ‘‘, age tinyint unsigned default 0, high decimal(5,2), gender enum(‘男‘, ‘女‘, ‘中性‘, ‘保密‘) default ‘保密‘, cls_id int unsigned default 0, is_delete bit default 0 );
往学生表写入数据
insert into students values (0,‘小明‘,18,180.00,2,1,0), (0,‘小月月‘,19,180.00,2,2,0), (0,‘彭于晏‘,28,185.00,1,1,0), (0,‘刘德华‘,58,175.00,1,2,0), (0,‘黄蓉‘,108,160.00,2,1,0), (0,‘凤姐‘,44,150.00,4,2,1), (0,‘王祖贤‘,52,170.00,2,1,1), (0,‘周杰伦儿‘,34,null,1,1,0), (0,‘程坤‘,44,181.00,1,2,0), (0,‘和珅‘,55,166.00,2,2,0), (0,‘刘亦菲‘,29,162.00,3,3,0), (0,‘金星‘,45,180.00,2,4,0), (0,‘静香‘,18,170.00,1,4,0), (0,‘郭静‘,22,167.00,2,5,0), (0,‘周杰‘,33,178.00,1,1,0), (0,‘钱小豪‘,56,178.00,1,1,0), (0,‘谢霆锋‘,38,175.00,1,1,0);
3)创建班级表:
create table classes( id int unsigned auto_increment primary key not null, name varchar(20) not null );
往班级表里添加数据:
insert into classes values (0, ‘云唯_01期‘),(0, ‘云唯_02期‘);
内关联: --inner join(在zz数据库下)
1.查询学生对应的班级信息
select * from students as s inner join classes as c on s.cls_id=c.id order by s.cld_id ;
#将students与classes关联显示并且取别名为s和c。第三个蓝色块则是将具体的字符关联起来,并以学生表的id字段排序,如果查看相反信息则转换students和classes的位置。
2.左关联:
select * from students as s left join classes as c on c.id=s.cls_id order by s.cls_id;
#左关联既是以左边的students为基准关联classes。效果如下:
+----+--------------+------+--------+--------+--------+-----------+------+--------------+ | id | name | age | high | gender | cls_id | is_delete | id | name | +----+--------------+------+--------+--------+--------+-----------+------+--------------+ | 1 | 小明 | 18 | 180.00 | 女 | 1 | | 1 | 云唯_01期 | | 16 | 钱小豪 | 56 | 178.00 | 男 | 1 | | 1 | 云唯_01期 | | 15 | 周杰 | 33 | 178.00 | 男 | 1 | | 1 | 云唯_01期 | | 17 | 谢霆锋 | 38 | 175.00 | 男 | 1 | | 1 | 云唯_01期 | | 8 | 周杰伦儿 | 34 | NULL | 男 | 1 | | 1 | 云唯_01期 | | 7 | 王祖贤 | 52 | 170.00 | 女 | 1 | | 1 | 云唯_01期 | | 5 | 黄蓉 | 108 | 160.00 | 女 | 1 | | 1 | 云唯_01期 | | 3 | 彭于晏 | 28 | 185.00 | 男 | 1 | | 1 | 云唯_01期 | | 6 | 凤姐 | 44 | 150.00 | 保密 | 2 | | 2 | 云唯_02期 | | 2 | 小月月 | 19 | 180.00 | 女 | 2 | | 2 | 云唯_02期 | | 9 | 程坤 | 44 | 181.00 | 男 | 2 | | 2 | 云唯_02期 | | 10 | 和珅 | 55 | 166.00 | 女 | 2 | | 2 | 云唯_02期 | | 4 | 刘德华 | 58 | 175.00 | 男 | 2 | | 2 | 云唯_02期 | | 11 | 刘亦菲 | 29 | 162.00 | 中性 | 3 | | NULL | NULL | | 13 | 静香 | 18 | 170.00 | 男 | 4 | | NULL | NULL | | 12 | 金星 | 45 | 180.00 | 女 | 4 | | NULL | NULL | | 14 | 郭静 | 22 | 167.00 | 女 | 5 | | NULL | NULL | +----+--------------+------+--------+--------+--------+-----------+------+--------------+
因为classes的id没有3,4,5,所以在关联的时候默认为空值显示。
3.右关联
与左关联规则一致,上述命令的left 改为right后id指挥显示1和2,而不是显示3,4,5,因为以右边的classes为基准.
4.自关联
source test.sql #将终端下的文件复制到数据库下
以下是关联表:
MariaDB [zz]> select * from areas; +-----+-----------+------+ | aid | name | pid | +-----+-----------+------+ | 1 | 北京市 | NULL | | 2 | 天津市 | NULL | | 3 | 河北省 | NULL | | 4 | 山西省 | NULL | | 5 | 海淀区 | 1 | | 6 | 滨海区 | 2 | | 7 | 沧州市 | 3 | | 8 | 大同市 | 4 | | 9 | 朝阳区 | 1 | | 10 | 武清区 | 2 | | 11 | 石家庄 | 3 | | 12 | 太原市 | 4 | | 13 | 西二旗 | 5 | | 14 | 大港 | 6 | | 15 | 任丘市 | 7 | | 16 | 清徐 | 8 | | 17 | 中关村 | 5 | | 18 | 汉沽 | 6 | | 19 | 河间市 | 7 | | 20 | 阳曲 | 8 |
1)查找北京市下的区
MariaDB [zz]> select * from areas as p inner join areas as c on p.aid=c.pid where p.name=‘北京市‘ ;
| aid | name | pid | aid | name | pid | #自关联并列显示 +-----+-----------+------+-----+-----------+------+ | 1 | 北京市 | NULL | 5 | 海淀区 | 1 | | 1 | 北京市 | NULL | 9 | 朝阳区 | 1 |
2)查找所有省市关联的区域,并且制作视图
create view v_v as select p.*,c.aid as id ,c.name as n,c.pid as i from areas as p inner join areas as c where p.aid = c.pid
##蓝色块区域为查看p表下的所有字段以及将c表的name、aid、pid都改成别名显示(因为c表和b表的字段冲突,p、c表也是areas表的别名)
视图**
create view 视图名 as select语句; #创建视图
show tables; #查看视图
drop view 视图名; #删除视图
# 视图与表同级,所以最好用带‘v’来区分。
事务***
为什么要有事务
事务具有ACID特性:原子性(A,atomicity)、一致性(C,consistency)、隔离性(I,isolation)、持久性(D,durabulity)。
事务命令
show create table students;
修改数据的命令会触发事务,包括insert、update、delete
开启事务,命令如下:
rollback
索引**
1)查看索引:
方式一:建表时创建索引
create table create_index( id int primary key, name varchar(10) unique, age int, key (age) #在字段上设置索引 );
方式二:对于已经存在的表,添加索引
如果指定字段是字符串,需要指定长度,建议长度与定义字段时的长度一致
字段类型如果不是字符串,可以不填写长度部分
create index 索引名称 on 表名(字段名称(长度))
例:
create index age_index on create_index(age) #在create_index表上的age字段创建索引
show create table create_index #查看此表是否添加上索引。
drop index 索引名称 on 表名; #删除成功则KEY `age_index` (`age`)消失
原文:https://www.cnblogs.com/zzzynx/p/10858903.html